From ce4908a1c1ff0c5e1898b0b7e8479e534e0c844f Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sat, 19 Sep 2026 15:28:07 +0530 Subject: [PATCH] Tests: A map lookup that appears only in if and elif conditions Co-Authored-By: Claude Fable 5.1 --- .../conditionals/lookup_in_condition.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/passing_tests/conditionals/lookup_in_condition.py diff --git a/tests/passing_tests/conditionals/lookup_in_condition.py b/tests/passing_tests/conditionals/lookup_in_condition.py new file mode 100644 index 0000000..d99ab12 --- /dev/null +++ b/tests/passing_tests/conditionals/lookup_in_condition.py @@ -0,0 +1,32 @@ +# A helper call that needs a scratch temp (the literal key has to be spilled +# to the stack for bpf_map_lookup_elem) appearing only in an `if` condition. +# The temp count used to skip conditions, so this failed with "Scratch pool +# exhausted" while the same lookup on a line of its own compiled. +from pythonbpf import bpf, map, section, bpfglobal, compile +from pythonbpf.maps import HashMap +from ctypes import c_void_p, c_int64, c_uint32, c_uint64 + + +@bpf +@map +def m() -> HashMap: + return HashMap(key=c_uint32, value=c_uint64, max_entries=4) + + +@bpf +@section("tracepoint/raw_syscalls/sys_enter") +def prog(ctx: c_void_p) -> c_int64: + if m.lookup(5): + return c_int64(1) + elif m.lookup(6): + return c_int64(2) + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile()