mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
Core: Support writes to @bpfglobal variables via the global statement
Python's own scoping marks the write: 'global cg_id' declares that assignments
to cg_id in this function mean the BPF global, and the assignment then emits
the plain 'store i64 %v, ptr @cg_id' of the C reference, with the same implicit
widening/truncation rules as local assignments.
Without the declaration, an assignment to a global's name is refused:
SyntaxError: assignment to 'cg_id' shadows the BPF global of the same
name -- add 'global cg_id' to write to it
That is deliberate. In real Python such an assignment creates a shadowing
local; silently compiling it as either a local or a global store would be wrong
in one direction or the other, so it is a loud error instead. 'global' naming
something that is not a @bpfglobal is also a compile error.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
This commit is contained in:
@ -49,6 +49,20 @@ def handle_assign_allocation(compilation_context, builder, stmt, local_sym_tab):
|
||||
continue
|
||||
|
||||
var_name = target.id
|
||||
|
||||
# Writes to @bpfglobal variables use the global symbol, not a stack
|
||||
# slot. Requires Python's own `global` declaration; without it an
|
||||
# assignment to a global's name would silently create a local that
|
||||
# shadows it, which is exactly the bug class we refuse to compile.
|
||||
if var_name in compilation_context.current_func_globals:
|
||||
logger.debug(f"'{var_name}' is a declared global, no allocation needed")
|
||||
continue
|
||||
if var_name in compilation_context.bpf_globals:
|
||||
raise SyntaxError(
|
||||
f"assignment to '{var_name}' shadows the BPF global of the same "
|
||||
f"name — add 'global {var_name}' to write to it"
|
||||
)
|
||||
|
||||
# Skip if already allocated
|
||||
if var_name in local_sym_tab:
|
||||
logger.debug(f"Variable {var_name} already allocated, skipping")
|
||||
|
||||
Reference in New Issue
Block a user