From 7f5773e5d8df604e4138112de71c7b0d6f80da2a Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 3 Sep 2026 06:40:11 +0530 Subject: [PATCH] Core: Make XDP return names respect local and global shadowing handle_return consulted the hardcoded XDP action table before any symbol table, so with a local XDP_PASS = 55 in scope, "return XDP_PASS" silently returned 2. clang treats the equivalent C (a local shadowing an enum constant) as the local winning, and every other resolution site in the compiler already resolves local, then global, then vmlinux -- the return fast path was the one place with the order inverted. It now applies only when the name is bound in neither table. Also warn when a @bpfglobal shadows a vmlinux enum constant. C rejects that outright as a redefinition; PythonBPF follows the rebinding semantics the Python file itself has (the global wins, consistently), but the collision is worth a compile-time warning rather than silence. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF --- pythonbpf/functions/functions_pass.py | 14 +++++++++++++- pythonbpf/globals_pass.py | 12 ++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pythonbpf/functions/functions_pass.py b/pythonbpf/functions/functions_pass.py index 494de6a..6ccf2f2 100644 --- a/pythonbpf/functions/functions_pass.py +++ b/pythonbpf/functions/functions_pass.py @@ -265,7 +265,19 @@ def handle_return(builder, stmt, local_sym_tab, ret_type, compilation_context=No logger.info(f"Handling return statement: {ast.dump(stmt)}") if stmt.value is None: return handle_none_return(builder) - elif isinstance(stmt.value, ast.Name) and is_xdp_name(stmt.value.id): + elif ( + isinstance(stmt.value, ast.Name) + and is_xdp_name(stmt.value.id) + and stmt.value.id not in local_sym_tab + and ( + compilation_context is None + or stmt.value.id not in compilation_context.bpf_globals + ) + ): + # The XDP fast path resolves names like XDP_PASS from the helper + # constant table, but only as a fallback: a local or @bpfglobal of the + # same name shadows it, mirroring C (a local shadows an enum constant) + # and the resolution order everywhere else in the compiler. return handle_xdp_return(stmt, builder, ret_type) else: # Fallback for now if ctx not passed, but caller should pass it diff --git a/pythonbpf/globals_pass.py b/pythonbpf/globals_pass.py index 6afcb9c..a1cb251 100644 --- a/pythonbpf/globals_pass.py +++ b/pythonbpf/globals_pass.py @@ -6,6 +6,7 @@ from logging import Logger import logging from .type_deducer import ctypes_to_ir from .debuginfo import DebugInfoGenerator +from .expr import VmlinuxHandlerRegistry from .debuginfo import dwarf_constants as dc logger: Logger = logging.getLogger(__name__) @@ -169,6 +170,17 @@ def globals_processing(tree, compilation_context): ) ): gvar = _emit_global(compilation_context.module, node, name) + if VmlinuxHandlerRegistry.handle_name(name) is not None: + # C rejects this outright ("redefinition as different + # kind of symbol"); Python's rebinding semantics let + # the global win, and resolution order (local, then + # global, then vmlinux) applies it consistently. Warn + # so the shadowing is at least never silent. + logger.warning( + f"@bpfglobal '{name}' shadows a vmlinux enum " + f"constant of the same name; reads of '{name}' " + f"will use the global" + ) if isinstance(gvar.value_type, ir.IntType): compilation_context.bpf_globals[name] = BpfGlobalSymbol( var=gvar,