mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
This commit is contained in:
22
tests/passing_tests/return/xdp_name_shadowed.py
Normal file
22
tests/passing_tests/return/xdp_name_shadowed.py
Normal file
@ -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()
|
||||
41
tests/passing_tests/vmlinux/enum_shadowing.py
Normal file
41
tests/passing_tests/vmlinux/enum_shadowing.py
Normal file
@ -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()
|
||||
Reference in New Issue
Block a user