mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 18:08:36 +00:00
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:
@ -115,7 +115,10 @@ def handle_variable_assignment(
|
|||||||
|
|
||||||
# A name declared with `global` writes the @bpfglobal symbol directly:
|
# A name declared with `global` writes the @bpfglobal symbol directly:
|
||||||
# the plain `store i64 %v, ptr @counter` form of the C reference.
|
# 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]
|
sym = compilation_context.bpf_globals[var_name]
|
||||||
val_result = eval_expr(func, compilation_context, builder, rval, local_sym_tab)
|
val_result = eval_expr(func, compilation_context, builder, rval, local_sym_tab)
|
||||||
if val_result is None:
|
if val_result is None:
|
||||||
|
|||||||
@ -205,7 +205,18 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
|
|||||||
"""
|
"""
|
||||||
if isinstance(stmt.target, ast.Name):
|
if isinstance(stmt.target, ast.Name):
|
||||||
name = stmt.target.id
|
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]
|
sym = compilation_context.bpf_globals[name]
|
||||||
slot, slot_type = sym.var, sym.ir_type
|
slot, slot_type = sym.var, sym.ir_type
|
||||||
elif name in compilation_context.bpf_globals:
|
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"augmented assignment to '{name}' shadows the BPF global of "
|
||||||
f"the same name — add 'global {name}' to write to it"
|
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:
|
else:
|
||||||
raise SyntaxError(f"augmented assignment to undefined variable '{name}'")
|
raise SyntaxError(f"augmented assignment to undefined variable '{name}'")
|
||||||
elif isinstance(stmt.target, ast.Attribute) and isinstance(
|
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
|
# @bpfglobal, never a local. Undeclared writes to a global name are
|
||||||
# rejected in the allocation pass rather than silently shadowing.
|
# rejected in the allocation pass rather than silently shadowing.
|
||||||
declared_globals: set[str] = set()
|
declared_globals: set[str] = set()
|
||||||
|
param_names = {arg.arg for arg in func_node.args.args}
|
||||||
for node in ast.walk(func_node):
|
for node in ast.walk(func_node):
|
||||||
if isinstance(node, ast.Global):
|
if isinstance(node, ast.Global):
|
||||||
for gname in node.names:
|
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:
|
if gname not in compilation_context.bpf_globals:
|
||||||
raise SyntaxError(
|
raise SyntaxError(
|
||||||
f"'global {gname}' in '{func_node.name}': no @bpfglobal "
|
f"'global {gname}' in '{func_node.name}': no @bpfglobal "
|
||||||
|
|||||||
29
tests/failing_tests/globals_parameter_and_global.py
Normal file
29
tests/failing_tests/globals_parameter_and_global.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# Declaring a parameter `global` is a SyntaxError in Python itself
|
||||||
|
# ("name 'ctx' is parameter and global"), and the compiler must say the same:
|
||||||
|
# otherwise reads (local first) and writes would resolve `ctx` to different
|
||||||
|
# storage.
|
||||||
|
from pythonbpf import bpf, section, bpfglobal, compile
|
||||||
|
from ctypes import c_void_p, c_int64, c_uint64
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def ctx() -> c_uint64:
|
||||||
|
return c_uint64(0)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/raw_syscalls/sys_enter")
|
||||||
|
def prog(ctx: c_void_p) -> c_int64: # noqa: F811 -- the collision is the test
|
||||||
|
global ctx # noqa: F811
|
||||||
|
ctx += 1
|
||||||
|
return c_int64(0)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile()
|
||||||
@ -40,3 +40,5 @@
|
|||||||
"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_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"}
|
"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"}
|
||||||
|
|
||||||
|
"failing_tests/globals_parameter_and_global.py" = {reason = "A parameter may not be declared global (Python: name is parameter and global)", level = "ir"}
|
||||||
|
|||||||
Reference in New Issue
Block a user