Core: Pass helpers the pointer a local holds, not its slot's address

A local bound to a map lookup holds a pointer, and get_or_create_ptr_from_arg
handed a helper the address of that local's stack slot instead. So
count.update(k, prev) stored the kernel address of the map value, not the
value, and after prev = prev + 1 it stored the address of the temporary.
The verifier accepts both when loading as root, so nothing failed: the
failing_tests/vmlinux/named_arg.py example has been passing all along while
storing the wrong thing. The slot is now loaded when it holds a pointer.

named_arg.py moves to passing_tests, and both it and a new
map_update_from_lookup.py are pinned by IR assertions.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pragyansh Chaturvedi
2026-10-09 03:13:47 +05:30
parent de259c8e86
commit dde720f46e
4 changed files with 52 additions and 6 deletions

View File

@ -67,6 +67,11 @@ def get_or_create_ptr_from_arg(
else: else:
# Stack space is already allocated # Stack space is already allocated
ptr = get_var_ptr_from_name(arg.id, local_sym_tab) 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): elif isinstance(arg, ast.Constant) and isinstance(arg.value, int):
int_width = 64 # Default to i64 int_width = 64 # Default to i64
if expected_type and isinstance(expected_type, ir.IntType): if expected_type and isinstance(expected_type, ir.IntType):

View File

@ -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()

View File

@ -4,12 +4,11 @@ from pythonbpf.maps import HashMap
from ctypes import c_void_p, c_int64 from ctypes import c_void_p, c_int64
# NOTE: This example exposes the problems with our typing system. # `prev` starts as a map lookup result (a pointer) and is rebound to
# We can't do steps on line 25 and 27. # `prev + 1`, which lives in a temporary that `prev` then points at. The update
# prev is of type i64**. For prev + 1, we deref it down to i64 # must store the incremented value: the helper gets the pointer the local
# To assign it back to prev, we need to go back to i64**. # holds, not the address of the local's own slot (that used to store a kernel
# We cannot allocate space for the intermediate type now. # stack address into the map).
# We probably need to track the ref/deref chain for each variable.
@bpf @bpf

View File

@ -112,6 +112,17 @@ CASES = {
[r"deref_0_not_null", r"ret i64 %"], [r"deref_0_not_null", r"ret i64 %"],
[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\)'],
),
} }