Skip to content

Commit 638cc86

Browse files
r41k0uclaude
andcommitted
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>
1 parent 1a2a7f1 commit 638cc86

6 files changed

Lines changed: 193 additions & 0 deletions

File tree

‎tests/passing_tests/deref/alias.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Control: a pointer local assigned a pointer keeps the pointer (aliasing).
2+
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
3+
from pythonbpf.maps import HashMap
4+
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
5+
6+
7+
@bpf
8+
@map
9+
def m() -> HashMap:
10+
return HashMap(key=c_uint32, value=c_uint64, max_entries=4)
11+
12+
13+
@bpf
14+
@struct
15+
class rec:
16+
val: c_uint64
17+
18+
19+
@bpf
20+
@section("tracepoint/raw_syscalls/sys_enter")
21+
def prog(ctx: c_void_p) -> c_int64:
22+
k = c_uint32(1)
23+
p = m.lookup(k)
24+
q = m.lookup(k)
25+
if p:
26+
q = p # noqa: F841
27+
return c_int64(0)
28+
29+
30+
@bpf
31+
@bpfglobal
32+
def LICENSE() -> str:
33+
return "GPL"
34+
35+
36+
compile()

‎tests/passing_tests/deref/cast.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# A ctypes cast of a map-lookup pointer is a cast of its value, as p + 0 is.
2+
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
3+
from pythonbpf.maps import HashMap
4+
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
5+
6+
7+
@bpf
8+
@map
9+
def m() -> HashMap:
10+
return HashMap(key=c_uint32, value=c_uint64, max_entries=4)
11+
12+
13+
@bpf
14+
@struct
15+
class rec:
16+
val: c_uint64
17+
18+
19+
@bpf
20+
@section("tracepoint/raw_syscalls/sys_enter")
21+
def prog(ctx: c_void_p) -> c_int64:
22+
k = c_uint32(1)
23+
p = m.lookup(k)
24+
q = m.lookup(k) # noqa: F841
25+
if p:
26+
y = c_int64(p) # noqa: F841
27+
return c_int64(0)
28+
29+
30+
@bpf
31+
@bpfglobal
32+
def LICENSE() -> str:
33+
return "GPL"
34+
35+
36+
compile()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# An integer local takes the value a map-lookup pointer points at.
2+
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
3+
from pythonbpf.maps import HashMap
4+
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
5+
6+
7+
@bpf
8+
@map
9+
def m() -> HashMap:
10+
return HashMap(key=c_uint32, value=c_uint64, max_entries=4)
11+
12+
13+
@bpf
14+
@struct
15+
class rec:
16+
val: c_uint64
17+
18+
19+
@bpf
20+
@section("tracepoint/raw_syscalls/sys_enter")
21+
def prog(ctx: c_void_p) -> c_int64:
22+
k = c_uint32(1)
23+
p = m.lookup(k)
24+
q = m.lookup(k) # noqa: F841
25+
if p:
26+
x = c_uint64(0)
27+
x = p # noqa: F841
28+
return c_int64(0)
29+
30+
31+
@bpf
32+
@bpfglobal
33+
def LICENSE() -> str:
34+
return "GPL"
35+
36+
37+
compile()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# The same cast on a return, the natural spelling.
2+
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
3+
from pythonbpf.maps import HashMap
4+
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
5+
6+
7+
@bpf
8+
@map
9+
def m() -> HashMap:
10+
return HashMap(key=c_uint32, value=c_uint64, max_entries=4)
11+
12+
13+
@bpf
14+
@struct
15+
class rec:
16+
val: c_uint64
17+
18+
19+
@bpf
20+
@section("tracepoint/raw_syscalls/sys_enter")
21+
def prog(ctx: c_void_p) -> c_int64:
22+
k = c_uint32(1)
23+
p = m.lookup(k)
24+
q = m.lookup(k) # noqa: F841
25+
if p:
26+
return c_int64(p)
27+
return c_int64(0)
28+
29+
30+
@bpf
31+
@bpfglobal
32+
def LICENSE() -> str:
33+
return "GPL"
34+
35+
36+
compile()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# An integer struct field takes the value a map-lookup pointer points at.
2+
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
3+
from pythonbpf.maps import HashMap
4+
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
5+
6+
7+
@bpf
8+
@map
9+
def m() -> HashMap:
10+
return HashMap(key=c_uint32, value=c_uint64, max_entries=4)
11+
12+
13+
@bpf
14+
@struct
15+
class rec:
16+
val: c_uint64
17+
18+
19+
@bpf
20+
@section("tracepoint/raw_syscalls/sys_enter")
21+
def prog(ctx: c_void_p) -> c_int64:
22+
k = c_uint32(1)
23+
p = m.lookup(k)
24+
q = m.lookup(k) # noqa: F841
25+
if p:
26+
r = rec()
27+
r.val = p
28+
return c_int64(0)
29+
30+
31+
@bpf
32+
@bpfglobal
33+
def LICENSE() -> str:
34+
return "GPL"
35+
36+
37+
compile()

‎tests/test_signedness_ir.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@
6868
[r"\bsub i64", r"trunc i64 .* to i32", r"zext i32 .* to i64"],
6969
[],
7070
),
71+
# A pointer consumed as a number is dereferenced (null-checked) first; no
72+
# pointer is ever stored into an integer slot.
73+
"deref/struct_field.py": ([r"deref_\d_not_null"], [r"store i64\* [^,]*, i64\* "]),
74+
"deref/int_local.py": (
75+
[r"deref_\d_not_null"],
76+
[r'store i64\* [^,]*, i64\*\* %"x"'],
77+
),
78+
"deref/cast.py": ([r"deref_\d_not_null"], [r'store i64\* [^,]*, i64\*\* %"y"']),
79+
"deref/return_cast.py": ([r"deref_\d_not_null"], [r"ret i64\*"]),
80+
# Control: pointer into a pointer slot stays a pointer, no dereference.
81+
"deref/alias.py": ([r'store i64\* [^,]*, i64\*\* %"q"'], [r"deref_\d_not_null"]),
7182
# A bool widens with zext and an integer narrows to it by != 0, never by
7283
# trunc; sext of an i1 would return -1 for True.
7384
"signedness/bool_int.py": (

0 commit comments

Comments
 (0)