From f5eb809f4b7053f7041b57c67c7ff86a191c5f6a Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 9 Oct 2026 03:13:51 +0530 Subject: [PATCH] Tests: Move tests that pass out of failing_tests retype, helper_cond, direct_assign and globals had no xfail entry and have been passing at every level; their NOTE headers describe limits that have since been lifted, so they are rewritten. xdp_pass was an xfail only because it used the old count().lookup() spelling of a map call; in the current syntax it compiles. Co-Authored-By: Claude Opus 5.5 (1M context) --- tests/failing_tests/vmlinux/direct_assign.py | 45 ---------------- tests/failing_tests/vmlinux/xdp_pass.py | 54 ------------------- .../assign/retype.py | 10 ++-- .../conditionals/helper_cond.py | 6 +-- .../globals/several_globals.py} | 0 tests/passing_tests/vmlinux/direct_assign.py | 37 +++++++++++++ tests/passing_tests/vmlinux/xdp_pass.py | 41 ++++++++++++++ tests/test_config.toml | 1 - 8 files changed, 83 insertions(+), 111 deletions(-) delete mode 100644 tests/failing_tests/vmlinux/direct_assign.py delete mode 100644 tests/failing_tests/vmlinux/xdp_pass.py rename tests/{failing_tests => passing_tests}/assign/retype.py (53%) rename tests/{failing_tests => passing_tests}/conditionals/helper_cond.py (75%) rename tests/{failing_tests/globals.py => passing_tests/globals/several_globals.py} (100%) create mode 100644 tests/passing_tests/vmlinux/direct_assign.py create mode 100644 tests/passing_tests/vmlinux/xdp_pass.py diff --git a/tests/failing_tests/vmlinux/direct_assign.py b/tests/failing_tests/vmlinux/direct_assign.py deleted file mode 100644 index f3362b4..0000000 --- a/tests/failing_tests/vmlinux/direct_assign.py +++ /dev/null @@ -1,45 +0,0 @@ -from pythonbpf import bpf, map, section, bpfglobal, compile -from vmlinux import XDP_PASS -from pythonbpf.maps import HashMap - -from ctypes import c_void_p, c_int64 - -# NOTE: I have decided to not fix this example for now. -# The issue is in line 31, where we are passing an expression. -# The update helper expects a pointer type. But the problem is -# that we must allocate the space for said pointer in the first -# basic block. As that usage is in a different basic block, we -# are unable to cast the expression to a pointer type. (as we never -# allocated space for it). -# Shall we change our space allocation logic? That allows users to -# spam the same helper with the same args, and still run out of -# stack space. So we consider this usage invalid for now. -# Might fix it later. - - -@bpf -@map -def count() -> HashMap: - return HashMap(key=c_int64, value=c_int64, max_entries=1) - - -@bpf -@section("xdp") -def hello_world(ctx: c_void_p) -> c_int64: - prev = count.lookup(0) - if prev: - count.update(0, prev + 1) - return XDP_PASS - else: - count.update(0, 1) - - return XDP_PASS - - -@bpf -@bpfglobal -def LICENSE() -> str: - return "GPL" - - -compile() diff --git a/tests/failing_tests/vmlinux/xdp_pass.py b/tests/failing_tests/vmlinux/xdp_pass.py deleted file mode 100644 index b37a9b8..0000000 --- a/tests/failing_tests/vmlinux/xdp_pass.py +++ /dev/null @@ -1,54 +0,0 @@ -from pythonbpf import bpf, map, section, bpfglobal, compile_to_ir -from pythonbpf.maps import HashMap -from vmlinux import XDP_PASS -from vmlinux import TASK_COMM_LEN # noqa: F401 - -from vmlinux import struct_qspinlock # noqa: F401 - -# from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401 -# from vmlinux import struct_posix_cputimers # noqa: F401 -from vmlinux import struct_xdp_md - -# from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401 -# from vmlinux import struct_ring_buffer_per_cpu # noqa: F401 -# from vmlinux import struct_request # noqa: F401 -from ctypes import c_int64 - -# Instructions to how to run this program -# 1. Install PythonBPF: pip install pythonbpf -# 2. Run the program: python examples/xdp_pass.py -# 3. Run the program with sudo: sudo tools/check.sh run examples/xdp_pass.o -# 4. Attach object file to any network device with something like ./check.sh xdp examples/xdp_pass.o tailscale0 -# 5. send traffic through the device and observe effects - - -@bpf -@map -def count() -> HashMap: - return HashMap(key=c_int64, value=c_int64, max_entries=1) - - -@bpf -@section("xdp") -def hello_world(ctx: struct_xdp_md) -> c_int64: - key = 0 - one = 1 - prev = count().lookup(key) - if prev: - prevval = prev + 1 - print(f"count: {prevval}") - count().update(key, prevval) - return XDP_PASS - else: - count().update(key, one) - - return XDP_PASS - - -@bpf -@bpfglobal -def LICENSE() -> str: - return "GPL" - - -compile_to_ir("xdp_pass.py", "xdp_pass.ll") diff --git a/tests/failing_tests/assign/retype.py b/tests/passing_tests/assign/retype.py similarity index 53% rename from tests/failing_tests/assign/retype.py rename to tests/passing_tests/assign/retype.py index 0cd99fb..f446619 100644 --- a/tests/failing_tests/assign/retype.py +++ b/tests/passing_tests/assign/retype.py @@ -3,13 +3,9 @@ from ctypes import c_void_p, c_int64, c_uint64 from pythonbpf.maps import HashMap -# NOTE: This example tries to reinterpret the variable `x` to a different type. -# We do not allow this for now, as stack allocations are typed and have to be -# done in the first basic block. Allowing re-interpretation would require -# re-allocation of stack space (possibly in a new basic block), which is not -# supported in eBPF yet. -# We can allow bitcasts in cases where the width of the types is the same in -# the future. But for now, we do not allow any re-interpretation of variables. +# `x` is first bound to a map lookup (a pointer) and then rebound to an integer, +# as Python allows; the comparison sees the integer. This was once refused +# because stack slots are typed, and now works. @bpf diff --git a/tests/failing_tests/conditionals/helper_cond.py b/tests/passing_tests/conditionals/helper_cond.py similarity index 75% rename from tests/failing_tests/conditionals/helper_cond.py rename to tests/passing_tests/conditionals/helper_cond.py index 8cf5bdb..5d3aa4b 100644 --- a/tests/failing_tests/conditionals/helper_cond.py +++ b/tests/passing_tests/conditionals/helper_cond.py @@ -2,10 +2,8 @@ from pythonbpf import bpf, map, section, bpfglobal, compile from ctypes import c_void_p, c_int64, c_uint64 from pythonbpf.maps import HashMap -# NOTE: Decided against fixing this -# as a workaround is assigning the result of lookup to a variable -# and then using that variable in the if statement. -# Might fix in future. +# A map lookup used directly in a comparison: the value is null-checked and +# dereferenced, and a missing key takes the else branch. @bpf diff --git a/tests/failing_tests/globals.py b/tests/passing_tests/globals/several_globals.py similarity index 100% rename from tests/failing_tests/globals.py rename to tests/passing_tests/globals/several_globals.py diff --git a/tests/passing_tests/vmlinux/direct_assign.py b/tests/passing_tests/vmlinux/direct_assign.py new file mode 100644 index 0000000..5c7fc9b --- /dev/null +++ b/tests/passing_tests/vmlinux/direct_assign.py @@ -0,0 +1,37 @@ +from pythonbpf import bpf, map, section, bpfglobal, compile +from vmlinux import XDP_PASS +from pythonbpf.maps import HashMap + +from ctypes import c_void_p, c_int64 + +# An expression (`prev + 1`) passed straight to map.update(): it is evaluated +# into a scratch slot and the helper gets that slot's address. This was once +# refused for want of a slot allocated up front, and now works. + + +@bpf +@map +def count() -> HashMap: + return HashMap(key=c_int64, value=c_int64, max_entries=1) + + +@bpf +@section("xdp") +def hello_world(ctx: c_void_p) -> c_int64: + prev = count.lookup(0) + if prev: + count.update(0, prev + 1) + return XDP_PASS + else: + count.update(0, 1) + + return XDP_PASS + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/vmlinux/xdp_pass.py b/tests/passing_tests/vmlinux/xdp_pass.py new file mode 100644 index 0000000..ccd4dcd --- /dev/null +++ b/tests/passing_tests/vmlinux/xdp_pass.py @@ -0,0 +1,41 @@ +# Counts packets in a one-entry map and lets every packet through. It used to +# be an xfail, but the only thing failing was the old `count().lookup(...)` +# spelling of a map call; `count.lookup(...)` is the current one. + +from pythonbpf import bpf, map, section, bpfglobal, compile +from pythonbpf.maps import HashMap +from vmlinux import XDP_PASS +from vmlinux import struct_xdp_md +from ctypes import c_int64 + + +@bpf +@map +def count() -> HashMap: + return HashMap(key=c_int64, value=c_int64, max_entries=1) + + +@bpf +@section("xdp") +def hello_world(ctx: struct_xdp_md) -> c_int64: + key = 0 + one = 1 + prev = count.lookup(key) + if prev: + prevval = prev + 1 + print(f"count: {prevval}") + count.update(key, prevval) + return XDP_PASS + else: + count.update(key, one) + + return XDP_PASS + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/test_config.toml b/tests/test_config.toml index c935312..cc24a3a 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -28,7 +28,6 @@ "failing_tests/vmlinux/args_test.py" = {reason = "struct_trace_event_raw_sys_enter args field access not supported", level = "ir"} -"failing_tests/vmlinux/xdp_pass.py" = {reason = "XDP program using vmlinux structs (struct_xdp_md) and complex map/struct interaction not yet supported", level = "ir"} "failing_tests/globals_read_before_shadow.py" = {reason = "Reading a name above the assignment that makes it a local shadowing a global is UnboundLocalError in Python, and a compile error here", level = "ir"}