Core: Resolve assignment targets local-first, and reject global on a parameter

Reads resolve a name local-first (_handle_name_expr, get_operand_value, the
printk formatter), but both write paths checked the declared-global set
before local_sym_tab. Normally harmless because the two tables are disjoint
by construction -- except for a parameter whose name matches a @bpfglobal.
With 'global ctx' in a function taking ctx, the write path stored to the
global while a read of the same name picked the parameter and crashed on its
None slot: the same name resolving to different storage depending on which
side of an assignment it sat.

Python forbids the construct outright, and now so does the compiler, with
Python's own wording: SyntaxError: name 'ctx' is parameter and global. Both
write paths now resolve local-first like the reads, so all name lookups read
the same way; and an augmented assignment to the context parameter itself
gets a clear error instead of a store to None.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
This commit is contained in:
Pragyansh Chaturvedi
2026-09-03 10:32:10 +05:30
parent f5c4dece3e
commit f4ec12df60
4 changed files with 53 additions and 5 deletions

View File

@ -115,7 +115,10 @@ def handle_variable_assignment(
# A name declared with `global` writes the @bpfglobal symbol directly:
# the plain `store i64 %v, ptr @counter` form of the C reference.
if var_name in compilation_context.current_func_globals:
if (
var_name not in local_sym_tab
and var_name in compilation_context.current_func_globals
):
sym = compilation_context.bpf_globals[var_name]
val_result = eval_expr(func, compilation_context, builder, rval, local_sym_tab)
if val_result is None: