Restrict rewrite of unary operators to dereference operator
authorPaul Chaignon <paul.chaignon@gmail.com>
Thu, 23 Feb 2017 09:03:58 +0000 (10:03 +0100)
committerPaul Chaignon <paul.chaignon@gmail.com>
Mon, 6 Mar 2017 20:48:14 +0000 (21:48 +0100)
Since the whole expression, unary operator included, is replaced by a
call to bpf_probe_read, the dereference operator is currently the
only unary operator properly rewritten. When rewriting an increment
expression (++val) for instance, the increment operator is lost in
translation.

Trying to rewrite all unary operators sometimes confuses bcc and
results in improper code, for instance when trying to rewrite a
logical negation.

src/cc/frontends/clang/b_frontend_action.cc
tests/python/test_clang.py

index 94bab5c..206ec00 100644 (file)
@@ -200,7 +200,7 @@ bool ProbeVisitor::VisitBinaryOperator(BinaryOperator *E) {
   return true;
 }
 bool ProbeVisitor::VisitUnaryOperator(UnaryOperator *E) {
-  if (E->getOpcode() == UO_AddrOf)
+  if (E->getOpcode() != UO_Deref)
     return true;
   if (memb_visited_.find(E) != memb_visited_.end())
     return true;
index 93d8742..691083d 100755 (executable)
@@ -408,6 +408,17 @@ int dns_test(struct __sk_buff *skb) {
         """
         b = BPF(text=text)
 
+    def test_unary_operator(self):
+        text = """
+#include <linux/fs.h>
+#include <uapi/linux/ptrace.h>
+int trace_read_entry(struct pt_regs *ctx, struct file *file) {
+    return !file->f_op->read_iter;
+}
+        """
+        b = BPF(text=text)
+        b.attach_kprobe(event="__vfs_read", fn_name="trace_read_entry")
+
 if __name__ == "__main__":
     main()