From 274bac9313bcf957d3059309fea3a44ebf0f80a0 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 3 Sep 2026 06:41:46 +0530 Subject: [PATCH] Tests: Pin name-resolution order under vmlinux collisions Two tests locking the shadowing semantics: a local named XDP_PASS returned from a program (the exact case the return fast path used to hijack), and a vmlinux-importing program where a local shadows one enum constant while a @bpfglobal shadows another, read in expressions and f-strings. The vmlinux test needs noqa on its import and both collision sites: ruff rightly flags unused imports and redefinitions in normal Python, and this file is deliberately made of them. These pin compilation; the value-level proof (ret of 55, not 2) was verified against the emitted IR and the clang reference for the same collision shapes. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF --- .../passing_tests/return/xdp_name_shadowed.py | 22 ++++++++++ tests/passing_tests/vmlinux/enum_shadowing.py | 41 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/passing_tests/return/xdp_name_shadowed.py create mode 100644 tests/passing_tests/vmlinux/enum_shadowing.py diff --git a/tests/passing_tests/return/xdp_name_shadowed.py b/tests/passing_tests/return/xdp_name_shadowed.py new file mode 100644 index 0000000..0caacd8 --- /dev/null +++ b/tests/passing_tests/return/xdp_name_shadowed.py @@ -0,0 +1,22 @@ +# A local named after an XDP action must shadow the helper constant table, +# in return position too. clang agrees: a local legally shadows an enum +# constant, and the local's value is what returns (tests/c-form reference). +# Before the fix this returned the hardcoded 2 while XDP_PASS held 55. +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("tracepoint/raw_syscalls/sys_enter") +def prog(ctx: c_void_p) -> c_int64: + XDP_PASS = 55 + return XDP_PASS + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/vmlinux/enum_shadowing.py b/tests/passing_tests/vmlinux/enum_shadowing.py new file mode 100644 index 0000000..2d7baf6 --- /dev/null +++ b/tests/passing_tests/vmlinux/enum_shadowing.py @@ -0,0 +1,41 @@ +# Name-resolution order under collision with vmlinux enum constants: +# local wins over global wins over vmlinux, consistently in expressions, +# f-strings, and returns. +# +# clang's take on the same collisions (tests/c-form): a local shadowing an +# enum constant is legal and wins; a file-scope variable colliding with one is +# a hard error. PythonBPF follows Python's rebinding semantics instead for the +# global case -- the @bpfglobal wins -- and logs a compile-time warning so the +# shadowing is never silent. +# XDP_ABORTED's import is load-bearing despite being "unused": it is what +# registers the enum with the compiler, so the local below has something to +# shadow. Python-level unused is the point. +from vmlinux import XDP_ABORTED, XDP_TX # noqa: F401 +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64, c_uint64 + + +# Shadows the vmlinux enum constant XDP_TX (warns at compile time; reads of +# XDP_TX below resolve to this global, value 77, not the enum value 3). +@bpf +@bpfglobal +def XDP_TX() -> c_uint64: # noqa: F811 -- the collision is the test + return c_uint64(77) + + +@bpf +@section("tracepoint/raw_syscalls/sys_enter") +def prog(ctx: c_void_p) -> c_int64: + XDP_ABORTED = 55 # noqa: F811 -- local shadows the enum (value 0) + x = XDP_ABORTED + XDP_TX + print(f"local {XDP_ABORTED} global {XDP_TX}") + return c_int64(x) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile()