mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
Tests: Cover global variable reads, writes, and the error cases
Five passing tests: read in condition and print; the get_cgroup_id_kern.c read-one-write-one shape; the counter += 1 idiom; two programs sharing global state (the test_autoattach.c shape); and mixed .bss/.data placement with mixed widths and signedness. Three strict expected failures documenting deliberate errors: assignment without the global statement (shadowing), a global statement naming a non-global, and a non-integer global type. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
This commit is contained in:
25
tests/failing_tests/globals_bad_type.py
Normal file
25
tests/failing_tests/globals_bad_type.py
Normal file
@ -0,0 +1,25 @@
|
||||
# Only integer scalar globals are supported in milestone 1; anything else must
|
||||
# be a clear NotImplementedError, not a half-emitted symbol.
|
||||
from pythonbpf import bpf, section, bpfglobal, compile
|
||||
from ctypes import c_void_p, c_int64, c_double
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def ratio() -> c_double:
|
||||
return c_double(0.5)
|
||||
|
||||
|
||||
@bpf
|
||||
@section("tracepoint/raw_syscalls/sys_enter")
|
||||
def prog(ctx: c_void_p) -> c_int64:
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
compile()
|
||||
26
tests/failing_tests/globals_shadowing.py
Normal file
26
tests/failing_tests/globals_shadowing.py
Normal file
@ -0,0 +1,26 @@
|
||||
# Writing a global's name without `global` must be a loud compile error, not a
|
||||
# silently-created shadowing local (real Python) or a silent global store.
|
||||
from pythonbpf import bpf, section, bpfglobal, compile
|
||||
from ctypes import c_void_p, c_int64, c_uint64
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def counter() -> c_uint64:
|
||||
return c_uint64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@section("tracepoint/raw_syscalls/sys_enter")
|
||||
def prog(ctx: c_void_p) -> c_int64:
|
||||
counter = 1 # noqa: F841 -- missing `global counter` on purpose
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
compile()
|
||||
19
tests/failing_tests/globals_undeclared_name.py
Normal file
19
tests/failing_tests/globals_undeclared_name.py
Normal file
@ -0,0 +1,19 @@
|
||||
# `global x` naming something that is not a @bpfglobal is a compile error.
|
||||
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:
|
||||
global nosuch
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
compile()
|
||||
31
tests/passing_tests/globals/augassign_counter.py
Normal file
31
tests/passing_tests/globals/augassign_counter.py
Normal file
@ -0,0 +1,31 @@
|
||||
# The canonical selftest idiom: a per-object event counter. `counter += 1`
|
||||
# desugars to load/add/store on the global; the local augassign checks the
|
||||
# desugaring path for stack variables too.
|
||||
from pythonbpf import bpf, section, bpfglobal, compile
|
||||
from ctypes import c_void_p, c_int64, c_uint64
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def counter() -> c_uint64:
|
||||
return c_uint64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@section("tracepoint/raw_syscalls/sys_enter")
|
||||
def tick(ctx: c_void_p) -> c_int64:
|
||||
global counter
|
||||
counter += 1
|
||||
x = 5
|
||||
x += 2
|
||||
print(f"count {counter} x {x}")
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
compile()
|
||||
34
tests/passing_tests/globals/bss_and_data.py
Normal file
34
tests/passing_tests/globals/bss_and_data.py
Normal file
@ -0,0 +1,34 @@
|
||||
# Section placement needs no marker: a zero initializer lands the global in
|
||||
# .bss, a non-zero one in .data, decided by LLVM exactly as for C. Mixed
|
||||
# widths and signedness also exercise the BTF basic-type emission.
|
||||
from pythonbpf import bpf, section, bpfglobal, compile
|
||||
from ctypes import c_void_p, c_int64, c_uint64, c_int32
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def zeroed() -> c_uint64:
|
||||
return c_uint64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def preset() -> c_int32:
|
||||
return c_int32(42)
|
||||
|
||||
|
||||
@bpf
|
||||
@section("tracepoint/raw_syscalls/sys_enter")
|
||||
def prog(ctx: c_void_p) -> c_int64:
|
||||
global zeroed
|
||||
zeroed = preset + 1
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
compile()
|
||||
28
tests/passing_tests/globals/read_scalar.py
Normal file
28
tests/passing_tests/globals/read_scalar.py
Normal file
@ -0,0 +1,28 @@
|
||||
# A @bpfglobal read in a condition and in a print. Reads need no marker;
|
||||
# they compile to a plain `load i64, ptr @expected_pid`.
|
||||
from pythonbpf import bpf, section, bpfglobal, compile
|
||||
from pythonbpf.helper import pid
|
||||
from ctypes import c_void_p, c_int64, c_uint64
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def expected_pid() -> c_uint64:
|
||||
return c_uint64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@section("tracepoint/syscalls/sys_enter_nanosleep")
|
||||
def trace(ctx: c_void_p) -> c_int64:
|
||||
if expected_pid == pid():
|
||||
print(f"matched {expected_pid}")
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
compile()
|
||||
41
tests/passing_tests/globals/shared_between_programs.py
Normal file
41
tests/passing_tests/globals/shared_between_programs.py
Normal file
@ -0,0 +1,41 @@
|
||||
# The test_autoattach.c shape: two programs on different attach points writing
|
||||
# flags into shared global state. Both write the same section's globals.
|
||||
from pythonbpf import bpf, section, bpfglobal, compile
|
||||
from ctypes import c_void_p, c_int64, c_uint64
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def prog1_called() -> c_uint64:
|
||||
return c_uint64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def prog2_called() -> c_uint64:
|
||||
return c_uint64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@section("raw_tp/sys_enter")
|
||||
def prog1(ctx: c_void_p) -> c_int64:
|
||||
global prog1_called
|
||||
prog1_called = 1
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@section("raw_tp/sys_exit")
|
||||
def prog2(ctx: c_void_p) -> c_int64:
|
||||
global prog2_called
|
||||
prog2_called = 1
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
compile()
|
||||
36
tests/passing_tests/globals/write_scalar.py
Normal file
36
tests/passing_tests/globals/write_scalar.py
Normal file
@ -0,0 +1,36 @@
|
||||
# The get_cgroup_id_kern.c shape from the kernel selftests: read one global,
|
||||
# write another, gated on a pid check. Writes require Python's own `global`
|
||||
# statement and compile to `store i64 %v, ptr @cg_id`.
|
||||
from pythonbpf import bpf, section, bpfglobal, compile
|
||||
from pythonbpf.helper import pid, get_current_cgroup_id
|
||||
from ctypes import c_void_p, c_int64, c_uint64
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def expected_pid() -> c_uint64:
|
||||
return c_uint64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def cg_id() -> c_uint64:
|
||||
return c_uint64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@section("tracepoint/syscalls/sys_enter_nanosleep")
|
||||
def trace(ctx: c_void_p) -> c_int64:
|
||||
global cg_id
|
||||
if expected_pid == pid():
|
||||
cg_id = get_current_cgroup_id()
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
compile()
|
||||
@ -32,3 +32,9 @@
|
||||
"failing_tests/vmlinux/assignment_handling.py" = {reason = "Assigning vmlinux enum value (XDP_PASS) to a local variable not yet supported", level = "ir"}
|
||||
|
||||
"failing_tests/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_shadowing.py" = {reason = "Assignment to a global name without a `global` statement is a deliberate compile error (would shadow the BPF global)", level = "ir"}
|
||||
|
||||
"failing_tests/globals_undeclared_name.py" = {reason = "`global x` naming something that is not a @bpfglobal is a compile error", level = "ir"}
|
||||
|
||||
"failing_tests/globals_bad_type.py" = {reason = "Non-integer-scalar globals are not supported in milestone 1 (NotImplementedError by design)", level = "ir"}
|
||||
|
||||
Reference in New Issue
Block a user