diff --git a/pythonbpf/assign_pass.py b/pythonbpf/assign_pass.py index c71bd28..d00f433 100644 --- a/pythonbpf/assign_pass.py +++ b/pythonbpf/assign_pass.py @@ -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: diff --git a/pythonbpf/functions/functions_pass.py b/pythonbpf/functions/functions_pass.py index 2bebd09..3519eb4 100644 --- a/pythonbpf/functions/functions_pass.py +++ b/pythonbpf/functions/functions_pass.py @@ -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 " diff --git a/tests/failing_tests/globals_parameter_and_global.py b/tests/failing_tests/globals_parameter_and_global.py new file mode 100644 index 0000000..735e485 --- /dev/null +++ b/tests/failing_tests/globals_parameter_and_global.py @@ -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() diff --git a/tests/test_config.toml b/tests/test_config.toml index 6a1853e..73d68b3 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -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_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"}