Commit d45a6ee8 authored by 4ast's avatar 4ast

Merge pull request #259 from iovisor/bblanco_dev

Don't treat fundamentally typed args as needing probe_read
parents 19a48d57 c48ab4b0
...@@ -97,11 +97,15 @@ bool BMapDeclVisitor::VisitBuiltinType(const BuiltinType *T) { ...@@ -97,11 +97,15 @@ bool BMapDeclVisitor::VisitBuiltinType(const BuiltinType *T) {
class ProbeChecker : public RecursiveASTVisitor<ProbeChecker> { class ProbeChecker : public RecursiveASTVisitor<ProbeChecker> {
public: public:
explicit ProbeChecker(Expr *arg, const set<Decl *> &ptregs) explicit ProbeChecker(Expr *arg, const set<Decl *> &ptregs)
: needs_probe_(false), ptregs_(ptregs) { : needs_probe_(false), is_transitive_(false), ptregs_(ptregs) {
if (arg) if (arg) {
TraverseStmt(arg); TraverseStmt(arg);
if (arg->getType()->isPointerType())
is_transitive_ = needs_probe_;
}
} }
bool VisitCallExpr(CallExpr *E) { bool VisitCallExpr(CallExpr *E) {
needs_probe_ = false;
return false; return false;
} }
bool VisitDeclRefExpr(DeclRefExpr *E) { bool VisitDeclRefExpr(DeclRefExpr *E) {
...@@ -110,8 +114,10 @@ class ProbeChecker : public RecursiveASTVisitor<ProbeChecker> { ...@@ -110,8 +114,10 @@ class ProbeChecker : public RecursiveASTVisitor<ProbeChecker> {
return true; return true;
} }
bool needs_probe() const { return needs_probe_; } bool needs_probe() const { return needs_probe_; }
bool is_transitive() const { return is_transitive_; }
private: private:
bool needs_probe_; bool needs_probe_;
bool is_transitive_;
const set<Decl *> &ptregs_; const set<Decl *> &ptregs_;
}; };
...@@ -131,7 +137,7 @@ ProbeVisitor::ProbeVisitor(Rewriter &rewriter) : rewriter_(rewriter) {} ...@@ -131,7 +137,7 @@ ProbeVisitor::ProbeVisitor(Rewriter &rewriter) : rewriter_(rewriter) {}
bool ProbeVisitor::VisitVarDecl(VarDecl *Decl) { bool ProbeVisitor::VisitVarDecl(VarDecl *Decl) {
if (Expr *E = Decl->getInit()) { if (Expr *E = Decl->getInit()) {
if (ProbeChecker(E, ptregs_).needs_probe()) if (ProbeChecker(E, ptregs_).is_transitive())
set_ptreg(Decl); set_ptreg(Decl);
} }
return true; return true;
...@@ -157,7 +163,7 @@ bool ProbeVisitor::VisitBinaryOperator(BinaryOperator *E) { ...@@ -157,7 +163,7 @@ bool ProbeVisitor::VisitBinaryOperator(BinaryOperator *E) {
if (!E->isAssignmentOp()) if (!E->isAssignmentOp())
return true; return true;
// copy probe attribute from RHS to LHS if present // copy probe attribute from RHS to LHS if present
if (ProbeChecker(E->getRHS(), ptregs_).needs_probe()) { if (ProbeChecker(E->getRHS(), ptregs_).is_transitive()) {
ProbeSetter setter(&ptregs_); ProbeSetter setter(&ptregs_);
setter.TraverseStmt(E->getLHS()); setter.TraverseStmt(E->getLHS());
} }
...@@ -570,7 +576,7 @@ bool ProbeConsumer::HandleTopLevelDecl(DeclGroupRef Group) { ...@@ -570,7 +576,7 @@ bool ProbeConsumer::HandleTopLevelDecl(DeclGroupRef Group) {
if (FunctionDecl *F = dyn_cast<FunctionDecl>(D)) { if (FunctionDecl *F = dyn_cast<FunctionDecl>(D)) {
if (F->isExternallyVisible() && F->hasBody()) { if (F->isExternallyVisible() && F->hasBody()) {
for (auto arg : F->parameters()) { for (auto arg : F->parameters()) {
if (arg != F->getParamDecl(0)) if (arg != F->getParamDecl(0) && !arg->getType()->isFundamentalType())
visitor_.set_ptreg(arg); visitor_.set_ptreg(arg);
} }
visitor_.TraverseDecl(D); visitor_.TraverseDecl(D);
......
...@@ -235,6 +235,19 @@ int kprobe__finish_task_switch(struct pt_regs *ctx, struct task_struct *prev) { ...@@ -235,6 +235,19 @@ int kprobe__finish_task_switch(struct pt_regs *ctx, struct task_struct *prev) {
} }
""") """)
def test_probe_simple_assign(self):
b = BPF(text="""
#include <uapi/linux/ptrace.h>
#include <linux/gfp.h>
struct leaf { size_t size; };
BPF_HASH(simple_map, u32, struct leaf);
int kprobe____kmalloc(struct pt_regs *ctx, size_t size) {
u32 pid = bpf_get_current_pid_tgid();
struct leaf* leaf = simple_map.lookup(&pid);
if (leaf)
leaf->size += size;
return 0;
}""", debug=4)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment