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:

View File

@ -205,7 +205,18 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
"""
if isinstance(stmt.target, ast.Name):
name = stmt.target.id
if name in compilation_context.current_func_globals:
# Same resolution order as reads: local, then declared global. The two
# cannot both hold a name (a parameter may not be declared global, and
# an undeclared write to a global's name is refused), so this order is
# about giving the same answer as _handle_name_expr, not precedence.
if name in local_sym_tab:
slot = local_sym_tab[name].var
slot_type = local_sym_tab[name].ir_type
if slot is None:
raise SyntaxError(
f"cannot assign to '{name}': it is the context parameter"
)
elif name in compilation_context.current_func_globals:
sym = compilation_context.bpf_globals[name]
slot, slot_type = sym.var, sym.ir_type
elif name in compilation_context.bpf_globals:
@ -213,9 +224,6 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
f"augmented assignment to '{name}' shadows the BPF global of "
f"the same name — add 'global {name}' to write to it"
)
elif name in local_sym_tab:
slot = local_sym_tab[name].var
slot_type = local_sym_tab[name].ir_type
else:
raise SyntaxError(f"augmented assignment to undefined variable '{name}'")
elif isinstance(stmt.target, ast.Attribute) and isinstance(
@ -411,9 +419,15 @@ def process_func_body(
# @bpfglobal, never a local. Undeclared writes to a global name are
# rejected in the allocation pass rather than silently shadowing.
declared_globals: set[str] = set()
param_names = {arg.arg for arg in func_node.args.args}
for node in ast.walk(func_node):
if isinstance(node, ast.Global):
for gname in node.names:
if gname in param_names:
# Python's own rule and wording. Without it the read path
# (local first) and the write path would resolve the same
# name to different storage.
raise SyntaxError(f"name '{gname}' is parameter and global")
if gname not in compilation_context.bpf_globals:
raise SyntaxError(
f"'global {gname}' in '{func_node.name}': no @bpfglobal "