diff --git a/pythonbpf/helper/helper_utils.py b/pythonbpf/helper/helper_utils.py index 28b60b3..a621fef 100644 --- a/pythonbpf/helper/helper_utils.py +++ b/pythonbpf/helper/helper_utils.py @@ -67,6 +67,11 @@ def get_or_create_ptr_from_arg( else: # Stack space is already allocated ptr = get_var_ptr_from_name(arg.id, local_sym_tab) + if symbol is not None and isinstance(symbol.ir_type, ir.PointerType): + # The slot holds a pointer (a map lookup result, or a local + # rebound to point at a temporary): the helper wants that + # pointer, not the address of the slot it is kept in. + ptr = builder.load(ptr) elif isinstance(arg, ast.Constant) and isinstance(arg.value, int): int_width = 64 # Default to i64 if expected_type and isinstance(expected_type, ir.IntType): diff --git a/tests/passing_tests/helpers/map_update_from_lookup.py b/tests/passing_tests/helpers/map_update_from_lookup.py new file mode 100644 index 0000000..010c6d6 --- /dev/null +++ b/tests/passing_tests/helpers/map_update_from_lookup.py @@ -0,0 +1,31 @@ +# A map lookup result passed straight to map.update(): the helper gets the +# pointer the local holds (the map value), as bpf_map_update_elem(&m, &k, prev, 0) +# would in C, not the address of the local's slot. + +from pythonbpf import bpf, map, section, bpfglobal, compile +from pythonbpf.maps import HashMap +from ctypes import c_void_p, c_int64 + + +@bpf +@map +def count() -> HashMap: + return HashMap(key=c_int64, value=c_int64, max_entries=2) + + +@bpf +@section("tracepoint/syscalls/sys_enter_getpid") +def copy(ctx: c_void_p) -> c_int64: + prev = count.lookup(0) + if prev: + count.update(1, prev) + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/failing_tests/vmlinux/named_arg.py b/tests/passing_tests/vmlinux/named_arg.py similarity index 61% rename from tests/failing_tests/vmlinux/named_arg.py rename to tests/passing_tests/vmlinux/named_arg.py index bea5e22..05a553a 100644 --- a/tests/failing_tests/vmlinux/named_arg.py +++ b/tests/passing_tests/vmlinux/named_arg.py @@ -4,12 +4,11 @@ from pythonbpf.maps import HashMap from ctypes import c_void_p, c_int64 -# NOTE: This example exposes the problems with our typing system. -# We can't do steps on line 25 and 27. -# prev is of type i64**. For prev + 1, we deref it down to i64 -# To assign it back to prev, we need to go back to i64**. -# We cannot allocate space for the intermediate type now. -# We probably need to track the ref/deref chain for each variable. +# `prev` starts as a map lookup result (a pointer) and is rebound to +# `prev + 1`, which lives in a temporary that `prev` then points at. The update +# must store the incremented value: the helper gets the pointer the local +# holds, not the address of the local's own slot (that used to store a kernel +# stack address into the map). @bpf diff --git a/tests/test_signedness_ir.py b/tests/test_signedness_ir.py index 96b003e..ce1067e 100644 --- a/tests/test_signedness_ir.py +++ b/tests/test_signedness_ir.py @@ -112,6 +112,17 @@ CASES = { [r"deref_0_not_null", r"ret i64 %"], [r"ret i64\*"], ), + # A local holding a pointer is passed to a helper as that pointer, loaded + # from its slot; the slot's own address (an i64**) would make the map store + # a kernel address instead of the value. + "helpers/map_update_from_lookup.py": ( + [r'load i64\*, i64\*\* %"prev"'], + [r'i64\*\* %"prev", i64 0\)'], + ), + "vmlinux/named_arg.py": ( + [r'load i64\*, i64\*\* %"prev"'], + [r'i64\*\* %"prev", i64 0\)'], + ), }