Tests: Map-lookup pointers into a struct field, an integer local, a cast, a return

The four shapes from the shelved remind-deref list, plus the aliasing
control, with IR-shape assertions: a null-checked dereference, and no
pointer stored into an integer slot.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pragyansh Chaturvedi
2026-09-25 13:16:15 +05:30
parent 1a2a7f144a
commit 638cc86e08
6 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,36 @@
# Control: a pointer local assigned a pointer keeps the pointer (aliasing).
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
from pythonbpf.maps import HashMap
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
@bpf
@map
def m() -> HashMap:
return HashMap(key=c_uint32, value=c_uint64, max_entries=4)
@bpf
@struct
class rec:
val: c_uint64
@bpf
@section("tracepoint/raw_syscalls/sys_enter")
def prog(ctx: c_void_p) -> c_int64:
k = c_uint32(1)
p = m.lookup(k)
q = m.lookup(k)
if p:
q = p # noqa: F841
return c_int64(0)
@bpf
@bpfglobal
def LICENSE() -> str:
return "GPL"
compile()

View File

@ -0,0 +1,36 @@
# A ctypes cast of a map-lookup pointer is a cast of its value, as p + 0 is.
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
from pythonbpf.maps import HashMap
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
@bpf
@map
def m() -> HashMap:
return HashMap(key=c_uint32, value=c_uint64, max_entries=4)
@bpf
@struct
class rec:
val: c_uint64
@bpf
@section("tracepoint/raw_syscalls/sys_enter")
def prog(ctx: c_void_p) -> c_int64:
k = c_uint32(1)
p = m.lookup(k)
q = m.lookup(k) # noqa: F841
if p:
y = c_int64(p) # noqa: F841
return c_int64(0)
@bpf
@bpfglobal
def LICENSE() -> str:
return "GPL"
compile()

View File

@ -0,0 +1,37 @@
# An integer local takes the value a map-lookup pointer points at.
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
from pythonbpf.maps import HashMap
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
@bpf
@map
def m() -> HashMap:
return HashMap(key=c_uint32, value=c_uint64, max_entries=4)
@bpf
@struct
class rec:
val: c_uint64
@bpf
@section("tracepoint/raw_syscalls/sys_enter")
def prog(ctx: c_void_p) -> c_int64:
k = c_uint32(1)
p = m.lookup(k)
q = m.lookup(k) # noqa: F841
if p:
x = c_uint64(0)
x = p # noqa: F841
return c_int64(0)
@bpf
@bpfglobal
def LICENSE() -> str:
return "GPL"
compile()

View File

@ -0,0 +1,36 @@
# The same cast on a return, the natural spelling.
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
from pythonbpf.maps import HashMap
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
@bpf
@map
def m() -> HashMap:
return HashMap(key=c_uint32, value=c_uint64, max_entries=4)
@bpf
@struct
class rec:
val: c_uint64
@bpf
@section("tracepoint/raw_syscalls/sys_enter")
def prog(ctx: c_void_p) -> c_int64:
k = c_uint32(1)
p = m.lookup(k)
q = m.lookup(k) # noqa: F841
if p:
return c_int64(p)
return c_int64(0)
@bpf
@bpfglobal
def LICENSE() -> str:
return "GPL"
compile()

View File

@ -0,0 +1,37 @@
# An integer struct field takes the value a map-lookup pointer points at.
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
from pythonbpf.maps import HashMap
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
@bpf
@map
def m() -> HashMap:
return HashMap(key=c_uint32, value=c_uint64, max_entries=4)
@bpf
@struct
class rec:
val: c_uint64
@bpf
@section("tracepoint/raw_syscalls/sys_enter")
def prog(ctx: c_void_p) -> c_int64:
k = c_uint32(1)
p = m.lookup(k)
q = m.lookup(k) # noqa: F841
if p:
r = rec()
r.val = p
return c_int64(0)
@bpf
@bpfglobal
def LICENSE() -> str:
return "GPL"
compile()

View File

@ -68,6 +68,17 @@ CASES = {
[r"\bsub i64", r"trunc i64 .* to i32", r"zext i32 .* to i64"],
[],
),
# A pointer consumed as a number is dereferenced (null-checked) first; no
# pointer is ever stored into an integer slot.
"deref/struct_field.py": ([r"deref_\d_not_null"], [r"store i64\* [^,]*, i64\* "]),
"deref/int_local.py": (
[r"deref_\d_not_null"],
[r'store i64\* [^,]*, i64\*\* %"x"'],
),
"deref/cast.py": ([r"deref_\d_not_null"], [r'store i64\* [^,]*, i64\*\* %"y"']),
"deref/return_cast.py": ([r"deref_\d_not_null"], [r"ret i64\*"]),
# Control: pointer into a pointer slot stays a pointer, no dereference.
"deref/alias.py": ([r'store i64\* [^,]*, i64\*\* %"q"'], [r"deref_\d_not_null"]),
# A bool widens with zext and an integer narrows to it by != 0, never by
# trunc; sext of an i1 would return -1 for True.
"signedness/bool_int.py": (