Skip to content

Commit

Permalink
Check for NULL result from bpf_attach_kprobe
Browse files Browse the repository at this point in the history
The check for NULL in the return value from C functions was comparing to
None incorrectly, causing an error check to pass improperly. Simply
check the truthiness of the return instead seems to be more resilient.

Add a test as well.

Fixes: iovisor#568
Signed-off-by: Brenden Blanco <[email protected]>
  • Loading branch information
Brenden Blanco committed Jun 15, 2016
1 parent cfac8da commit 9964bf2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/python/bcc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def __init__(self, src_file="", hdr_file="", text=None, cb=None, debug=0, cflags
self.module = lib.bpf_module_create_c(src_file.encode("ascii"),
self.debug, cflags_array, len(cflags_array))

if self.module == None:
if not self.module:
raise Exception("Failed to compile BPF module %s" % src_file)

# If any "kprobe__" prefixed functions were defined, they will be
Expand All @@ -195,7 +195,7 @@ def load_func(self, func_name, prog_type):
if func_name in self.funcs:
return self.funcs[func_name]

if lib.bpf_function_start(self.module, func_name.encode("ascii")) == None:
if not lib.bpf_function_start(self.module, func_name.encode("ascii")):
raise Exception("Unknown program %s" % func_name)

log_buf = ct.create_string_buffer(65536) if self.debug else None
Expand Down Expand Up @@ -227,7 +227,7 @@ def dump_func(self, func_name):
"""
Return the eBPF bytecodes for the specified function as a string
"""
if lib.bpf_function_start(self.module, func_name.encode("ascii")) == None:
if not lib.bpf_function_start(self.module, func_name.encode("ascii")):
raise Exception("Unknown program %s" % func_name)

start, = lib.bpf_function_start(self.module, func_name.encode("ascii")),
Expand Down Expand Up @@ -373,7 +373,7 @@ def attach_kprobe(self, event="", fn_name="", event_re="",
desc.encode("ascii"), pid, cpu, group_fd,
self._reader_cb_impl, ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p)
if res == None:
if not res:
raise Exception("Failed to attach BPF to kprobe")
open_kprobes[ev_name] = res
return self
Expand Down Expand Up @@ -421,7 +421,7 @@ def attach_kretprobe(self, event="", fn_name="", event_re="",
desc.encode("ascii"), pid, cpu, group_fd,
self._reader_cb_impl, ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p)
if res == None:
if not res:
raise Exception("Failed to attach BPF to kprobe")
open_kprobes[ev_name] = res
return self
Expand Down Expand Up @@ -481,7 +481,7 @@ def attach_uprobe(self, name="", sym="", addr=None,
desc.encode("ascii"), pid, cpu, group_fd,
self._reader_cb_impl, ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p)
if res == None:
if not res:
raise Exception("Failed to attach BPF to uprobe")
open_uprobes[ev_name] = res
return self
Expand Down Expand Up @@ -525,7 +525,7 @@ def attach_uretprobe(self, name="", sym="", addr=None,
desc.encode("ascii"), pid, cpu, group_fd,
self._reader_cb_impl, ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p)
if res == None:
if not res:
raise Exception("Failed to attach BPF to uprobe")
open_uprobes[ev_name] = res
return self
Expand Down
8 changes: 8 additions & 0 deletions tests/python/test_probe_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def test_probe_quota(self):
with self.assertRaises(Exception):
self.b.attach_kprobe(event_re=".*", fn_name="count")

class TestProbeNotExist(TestCase):
def setUp(self):
self.b = BPF(text="""int count(void *ctx) { return 0; }""")

def test_not_exist(self):
with self.assertRaises(Exception):
b.attach_kprobe(event="___doesnotexist", fn_name="count")


if __name__ == "__main__":
main()

0 comments on commit 9964bf2

Please sign in to comment.