From c0fda58d2789198d3314abe4d46200e26807b3e1 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 1 Sep 2026 17:10:15 +0530 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF --- tests/failing_tests/globals_bad_type.py | 25 +++++++++++ tests/failing_tests/globals_shadowing.py | 26 ++++++++++++ .../failing_tests/globals_undeclared_name.py | 19 +++++++++ .../globals/augassign_counter.py | 31 ++++++++++++++ tests/passing_tests/globals/bss_and_data.py | 34 +++++++++++++++ tests/passing_tests/globals/read_scalar.py | 28 +++++++++++++ .../globals/shared_between_programs.py | 41 +++++++++++++++++++ tests/passing_tests/globals/write_scalar.py | 36 ++++++++++++++++ tests/test_config.toml | 6 +++ 9 files changed, 246 insertions(+) create mode 100644 tests/failing_tests/globals_bad_type.py create mode 100644 tests/failing_tests/globals_shadowing.py create mode 100644 tests/failing_tests/globals_undeclared_name.py create mode 100644 tests/passing_tests/globals/augassign_counter.py create mode 100644 tests/passing_tests/globals/bss_and_data.py create mode 100644 tests/passing_tests/globals/read_scalar.py create mode 100644 tests/passing_tests/globals/shared_between_programs.py create mode 100644 tests/passing_tests/globals/write_scalar.py diff --git a/tests/failing_tests/globals_bad_type.py b/tests/failing_tests/globals_bad_type.py new file mode 100644 index 0000000..4c0f749 --- /dev/null +++ b/tests/failing_tests/globals_bad_type.py @@ -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() diff --git a/tests/failing_tests/globals_shadowing.py b/tests/failing_tests/globals_shadowing.py new file mode 100644 index 0000000..87e5827 --- /dev/null +++ b/tests/failing_tests/globals_shadowing.py @@ -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() diff --git a/tests/failing_tests/globals_undeclared_name.py b/tests/failing_tests/globals_undeclared_name.py new file mode 100644 index 0000000..e55a06b --- /dev/null +++ b/tests/failing_tests/globals_undeclared_name.py @@ -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() diff --git a/tests/passing_tests/globals/augassign_counter.py b/tests/passing_tests/globals/augassign_counter.py new file mode 100644 index 0000000..5a95f08 --- /dev/null +++ b/tests/passing_tests/globals/augassign_counter.py @@ -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() diff --git a/tests/passing_tests/globals/bss_and_data.py b/tests/passing_tests/globals/bss_and_data.py new file mode 100644 index 0000000..b78661f --- /dev/null +++ b/tests/passing_tests/globals/bss_and_data.py @@ -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() diff --git a/tests/passing_tests/globals/read_scalar.py b/tests/passing_tests/globals/read_scalar.py new file mode 100644 index 0000000..8fde2ae --- /dev/null +++ b/tests/passing_tests/globals/read_scalar.py @@ -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() diff --git a/tests/passing_tests/globals/shared_between_programs.py b/tests/passing_tests/globals/shared_between_programs.py new file mode 100644 index 0000000..915bc34 --- /dev/null +++ b/tests/passing_tests/globals/shared_between_programs.py @@ -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() diff --git a/tests/passing_tests/globals/write_scalar.py b/tests/passing_tests/globals/write_scalar.py new file mode 100644 index 0000000..98d68cb --- /dev/null +++ b/tests/passing_tests/globals/write_scalar.py @@ -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() diff --git a/tests/test_config.toml b/tests/test_config.toml index 8a6255a..5a97fda 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -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"}