From 52e80c8dddb95a9d9fe7648613dc5bb3dafd47af Mon Sep 17 00:00:00 2001 From: Varun R Mallya Date: Wed, 23 Sep 2026 18:33:16 +0530 Subject: [PATCH] expr_pass: add bpf_passthrough attributes to make tests pass --- pythonbpf/context.py | 4 +++ pythonbpf/expr/expr_pass.py | 26 +++++++++++++++++++ .../xdp/xdp_test_1.py | 0 tests/test_config.toml | 8 ------ 4 files changed, 30 insertions(+), 8 deletions(-) rename tests/{failing_tests => passing_tests}/xdp/xdp_test_1.py (100%) diff --git a/pythonbpf/context.py b/pythonbpf/context.py index c9a2f80..9675d7c 100644 --- a/pythonbpf/context.py +++ b/pythonbpf/context.py @@ -72,6 +72,10 @@ class CompilationContext: # Helper management self.scratch_pool = ScratchPoolManager() + # Sequence numbers for llvm.bpf.passthrough barriers; each call needs a + # distinct one so the optimizer cannot CSE two barriers together. + self.passthrough_seq = 0 + # Vmlinux handling (optional, specialized) self.vmlinux_handler = None # Can be VmlinuxHandler instance diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index 1441034..8d90574 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -407,6 +407,28 @@ def _handle_unary_op( # ============================================================================ +def _bpf_passthrough(builder, compilation_context, val): + """Wrap an i1 in llvm.bpf.passthrough, an optimization barrier the BPF + backend strips before instruction selection. + + Without it, opt's SimplifyCFG speculates a short-circuit operand's compare + into the preceding block, and SelectionDAG then folds the two null checks + of `p1 or p2` into `(p1 | p2) != 0`. The verifier rejects that with + "pointer |= pointer prohibited". BPFAdjustOpt uses the same intrinsic to + keep compares serialized for the verifier. + """ + module = compilation_context.module + name = "llvm.bpf.passthrough.i1.i1" + fn = module.globals.get(name) + if fn is None: + fn_type = ir.FunctionType(ir.IntType(1), [ir.IntType(32), ir.IntType(1)]) + fn = ir.Function(module, fn_type, name=name) + + seq = ir.Constant(ir.IntType(32), compilation_context.passthrough_seq) + compilation_context.passthrough_seq += 1 + return builder.call(fn, [seq, val]) + + def _handle_and_op(func, builder, expr, local_sym_tab, compilation_context): """Handle `and` boolean operations.""" @@ -432,6 +454,8 @@ def _handle_and_op(func, builder, expr, local_sym_tab, compilation_context): # Convert to boolean if needed operand_bool = convert_to_bool(builder, operand_val) + if i > 0: + operand_bool = _bpf_passthrough(builder, compilation_context, operand_bool) current_block = builder.block if is_last: @@ -485,6 +509,8 @@ def _handle_or_op(func, builder, expr, local_sym_tab, compilation_context): # Convert to boolean if needed operand_bool = convert_to_bool(builder, operand_val) + if i > 0: + operand_bool = _bpf_passthrough(builder, compilation_context, operand_bool) current_block = builder.block if is_last: diff --git a/tests/failing_tests/xdp/xdp_test_1.py b/tests/passing_tests/xdp/xdp_test_1.py similarity index 100% rename from tests/failing_tests/xdp/xdp_test_1.py rename to tests/passing_tests/xdp/xdp_test_1.py diff --git a/tests/test_config.toml b/tests/test_config.toml index cab8db6..31feb09 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -15,14 +15,6 @@ "failing_tests/conditionals/struct_ptr.py" = {reason = "Struct pointer used directly as boolean condition not supported", level = "ir"} -# Compiles cleanly; rejected by the kernel. The `data + 34 < data_end` guard is -# emitted as a signed compare over values round-tripped through the stack, so the -# verifier never narrows the packet range (it stays r=0) and the later -# `iph.saddr` read fails with "invalid access to packet, off=26 size=4" / -# "R1 offset is outside of the packet". Direct packet access needs bounds checks -# in a form the verifier can follow. -"failing_tests/xdp/xdp_test_1.py" = {reason = "XDP direct packet access: the data/data_end guard does not establish a packet range the verifier can use", level = "verifier"} - "failing_tests/license.py" = {reason = "Missing LICENSE global produces IR that llc rejects — should be caught earlier with a clear error message", level = "llc"} "failing_tests/undeclared_values.py" = {reason = "Undeclared variable used in f-string — should raise SyntaxError (correct behaviour, test documents it)", level = "ir"}