From 1b9adc7134887349469d88f94b8b5fcf8ad5df7e Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 3 Sep 2026 07:22:48 +0530 Subject: [PATCH] Tests: Pin augmented assignment on struct fields and the shadowing error A struct-field test with a non-add operator and a sub-64-bit field (the narrow-on-store path), and a strict expected failure for counter += 1 without a global statement, which now fails as loudly as plain assignment does. Ruff flags that line as F823 (referenced before assignment) -- the same UnboundLocalError Python itself would raise, which is exactly the semantics the global statement requirement mirrors. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF --- .../globals_augassign_shadowing.py | 28 +++++++++++++++ .../assign/augassign_struct_field.py | 35 +++++++++++++++++++ tests/test_config.toml | 2 ++ 3 files changed, 65 insertions(+) create mode 100644 tests/failing_tests/globals_augassign_shadowing.py create mode 100644 tests/passing_tests/assign/augassign_struct_field.py diff --git a/tests/failing_tests/globals_augassign_shadowing.py b/tests/failing_tests/globals_augassign_shadowing.py new file mode 100644 index 0000000..ecfb5a9 --- /dev/null +++ b/tests/failing_tests/globals_augassign_shadowing.py @@ -0,0 +1,28 @@ +# `counter += 1` without `global counter` must be the same loud error as plain +# assignment. Before direct lowering this slipped past the allocation pass +# (which only walks Assign) and failed later with a misleading message. +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: F823, F841 -- missing `global counter` on purpose; this is + # the UnboundLocalError shape, and the compiler must reject it just as loudly + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/assign/augassign_struct_field.py b/tests/passing_tests/assign/augassign_struct_field.py new file mode 100644 index 0000000..6c56370 --- /dev/null +++ b/tests/passing_tests/assign/augassign_struct_field.py @@ -0,0 +1,35 @@ +# Augmented assignment to a struct field: one GEP, load, op, store. Also a +# non-add operator and a sub-64-bit field, which exercises the narrow-on-store +# path of the direct lowering. +from pythonbpf import bpf, section, bpfglobal, compile, struct +from ctypes import c_void_p, c_int64, c_uint64, c_uint32 + + +@bpf +@struct +class data_t: + pid: c_uint64 + hits: c_uint32 + + +@bpf +@section("tracepoint/raw_syscalls/sys_enter") +def prog(ctx: c_void_p) -> c_int64: + x = 5 + x -= 1 + dat = data_t() + dat.pid = 10 + dat.pid += x + dat.hits = 1 + dat.hits <<= 2 + print(f"pid {dat.pid} hits {dat.hits} x {x}") + 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 5a97fda..6a1853e 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -38,3 +38,5 @@ "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"} + +"failing_tests/globals_augassign_shadowing.py" = {reason = "Augmented assignment to a global name without a `global` statement is a deliberate compile error (would shadow the BPF global)", level = "ir"}