Skip to content

Commit ce4908a

Browse files
r41k0uclaude
andcommitted
Tests: A map lookup that appears only in if and elif conditions
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent b3d79af commit ce4908a

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# A helper call that needs a scratch temp (the literal key has to be spilled
2+
# to the stack for bpf_map_lookup_elem) appearing only in an `if` condition.
3+
# The temp count used to skip conditions, so this failed with "Scratch pool
4+
# exhausted" while the same lookup on a line of its own compiled.
5+
from pythonbpf import bpf, map, section, bpfglobal, compile
6+
from pythonbpf.maps import HashMap
7+
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
8+
9+
10+
@bpf
11+
@map
12+
def m() -> HashMap:
13+
return HashMap(key=c_uint32, value=c_uint64, max_entries=4)
14+
15+
16+
@bpf
17+
@section("tracepoint/raw_syscalls/sys_enter")
18+
def prog(ctx: c_void_p) -> c_int64:
19+
if m.lookup(5):
20+
return c_int64(1)
21+
elif m.lookup(6):
22+
return c_int64(2)
23+
return c_int64(0)
24+
25+
26+
@bpf
27+
@bpfglobal
28+
def LICENSE() -> str:
29+
return "GPL"
30+
31+
32+
compile()

0 commit comments

Comments
 (0)