From 638cc86e081d91f4f254cbf1b682d7e038671d82 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 25 Sep 2026 13:16:15 +0530 Subject: [PATCH] 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) --- tests/passing_tests/deref/alias.py | 36 ++++++++++++++++++++++ tests/passing_tests/deref/cast.py | 36 ++++++++++++++++++++++ tests/passing_tests/deref/int_local.py | 37 +++++++++++++++++++++++ tests/passing_tests/deref/return_cast.py | 36 ++++++++++++++++++++++ tests/passing_tests/deref/struct_field.py | 37 +++++++++++++++++++++++ tests/test_signedness_ir.py | 11 +++++++ 6 files changed, 193 insertions(+) create mode 100644 tests/passing_tests/deref/alias.py create mode 100644 tests/passing_tests/deref/cast.py create mode 100644 tests/passing_tests/deref/int_local.py create mode 100644 tests/passing_tests/deref/return_cast.py create mode 100644 tests/passing_tests/deref/struct_field.py diff --git a/tests/passing_tests/deref/alias.py b/tests/passing_tests/deref/alias.py new file mode 100644 index 0000000..cb0fceb --- /dev/null +++ b/tests/passing_tests/deref/alias.py @@ -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() diff --git a/tests/passing_tests/deref/cast.py b/tests/passing_tests/deref/cast.py new file mode 100644 index 0000000..653b237 --- /dev/null +++ b/tests/passing_tests/deref/cast.py @@ -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() diff --git a/tests/passing_tests/deref/int_local.py b/tests/passing_tests/deref/int_local.py new file mode 100644 index 0000000..97d384a --- /dev/null +++ b/tests/passing_tests/deref/int_local.py @@ -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() diff --git a/tests/passing_tests/deref/return_cast.py b/tests/passing_tests/deref/return_cast.py new file mode 100644 index 0000000..b0fa45f --- /dev/null +++ b/tests/passing_tests/deref/return_cast.py @@ -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() diff --git a/tests/passing_tests/deref/struct_field.py b/tests/passing_tests/deref/struct_field.py new file mode 100644 index 0000000..59446aa --- /dev/null +++ b/tests/passing_tests/deref/struct_field.py @@ -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() diff --git a/tests/test_signedness_ir.py b/tests/test_signedness_ir.py index 2166ed8..47eb2b8 100644 --- a/tests/test_signedness_ir.py +++ b/tests/test_signedness_ir.py @@ -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": (