diff --git a/pythonbpf/allocation_pass.py b/pythonbpf/allocation_pass.py index 466800b..9cae135 100644 --- a/pythonbpf/allocation_pass.py +++ b/pythonbpf/allocation_pass.py @@ -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") diff --git a/pythonbpf/assign_pass.py b/pythonbpf/assign_pass.py index 91133db..9939357 100644 --- a/pythonbpf/assign_pass.py +++ b/pythonbpf/assign_pass.py @@ -105,6 +105,30 @@ def handle_variable_assignment( ): """Handle single named 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: + sym = compilation_context.bpf_globals[var_name] + val_result = eval_expr(func, compilation_context, builder, rval, local_sym_tab) + if val_result is None: + logger.error(f"Failed to evaluate value for global {var_name}") + return False + val, val_type = val_result + if isinstance(val_type, ir.IntType) and isinstance(sym.ir_type, ir.IntType): + # Same implicit widening/truncation rules as local assignments + if val_type.width < sym.ir_type.width: + val = builder.sext(val, sym.ir_type) + elif val_type.width > sym.ir_type.width: + val = builder.trunc(val, sym.ir_type) + elif val_type != sym.ir_type: + logger.error( + f"Type mismatch for global {var_name}: {val_type} vs {sym.ir_type}" + ) + return False + builder.store(val, sym.var) + logger.info(f"Stored to BPF global {var_name}") + return True + if var_name not in local_sym_tab: logger.error(f"Variable {var_name} not declared.") return False diff --git a/pythonbpf/functions/functions_pass.py b/pythonbpf/functions/functions_pass.py index 69be323..124cdb1 100644 --- a/pythonbpf/functions/functions_pass.py +++ b/pythonbpf/functions/functions_pass.py @@ -284,6 +284,9 @@ def process_stmt( handle_assign(func, compilation_context, builder, stmt, local_sym_tab) elif isinstance(stmt, ast.AugAssign): raise SyntaxError("Augmented assignment not supported") + elif isinstance(stmt, ast.Global): + # Declarations were collected by process_func_body; nothing to emit. + pass elif isinstance(stmt, ast.If): handle_if(func, compilation_context, builder, stmt, local_sym_tab) elif isinstance(stmt, ast.Return): @@ -311,6 +314,22 @@ def process_func_body( local_sym_tab = {} + # Collect `global x` declarations. Python scoping rules apply: a declared + # name may be written anywhere in this function and always means the + # @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() + for node in ast.walk(func_node): + if isinstance(node, ast.Global): + for gname in node.names: + if gname not in compilation_context.bpf_globals: + raise SyntaxError( + f"'global {gname}' in '{func_node.name}': no @bpfglobal " + f"named '{gname}' is declared" + ) + declared_globals.add(gname) + compilation_context.current_func_globals = declared_globals + # Add the context parameter (first function argument) to the local symbol table if func_node.args.args and len(func_node.args.args) > 0: context_arg = func_node.args.args[0] @@ -375,6 +394,8 @@ def process_func_body( if not did_return: builder.ret(ir.Constant(ir.IntType(64), 0)) + compilation_context.current_func_globals = set() + def process_bpf_chunk(func_node, compilation_context, return_type): """Process a single BPF chunk (function) and emit corresponding LLVM IR."""