Tests: probe_read_kernel into a scalar, shared ctx names, map value compares

This commit is contained in:
2026-09-25 18:25:54 +05:30
parent 1dfb22d3dc
commit 35d176692f
4 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,33 @@
# A comparison with a map value compares what the map holds, on either side:
# `head == body.lookup(1)` with both sides map lookups used to compare the two
# entries' addresses.
from ctypes import c_int64, c_uint64, c_void_p
from pythonbpf import bpf, map, section, bpfglobal, compile
from pythonbpf.maps import HashMap
@bpf
@map
def body() -> HashMap:
return HashMap(key=c_uint64, value=c_uint64, max_entries=10)
@bpf
@section("tracepoint/syscalls/sys_enter_getppid")
def prog(ctx: c_void_p) -> c_int64:
head = body.lookup(0)
if head == body.lookup(1):
return c_int64(1)
k = 3
if body.lookup(2) == body.lookup(k):
return c_int64(2)
return c_int64(0)
@bpf
@bpfglobal
def LICENSE() -> str:
return "GPL"
compile()

View File

@ -0,0 +1,34 @@
# probe_read_kernel into a scalar reads the scalar's width, and the source may
# be an address held in a register field (C: (void *)ctx->si). bpfsnake reads
# one typed byte this way: `@key = *((int8*)arg1)`.
from ctypes import c_int8, c_int64
from pythonbpf import bpf, section, bpfglobal, compile
from pythonbpf.helper import probe_read_kernel
from vmlinux import struct_pt_regs
@bpf
@bpfglobal
def last() -> c_int64:
return c_int64(0)
# pty_write(struct tty_struct *tty, const u8 *buf, size_t c)
@bpf
@section("kprobe/pty_write")
def prog(ctx: struct_pt_regs) -> c_int64:
global last
if ctx.dx == 1:
byte = c_int8(0)
probe_read_kernel(byte, ctx.si)
last = byte
return c_int64(0)
@bpf
@bpfglobal
def LICENSE() -> str:
return "GPL"
compile()

View File

@ -0,0 +1,27 @@
# Two programs whose context parameters share a name and a vmlinux type. Their
# debug info used to share one cached DILocalVariable, scoped to both
# functions, and llvmlite recursed forever hashing the resulting cycle.
from ctypes import c_int64
from pythonbpf import bpf, section, bpfglobal, compile
from vmlinux import struct_pt_regs
@bpf
@section("kprobe/do_unlinkat")
def first(ctx: struct_pt_regs) -> c_int64:
return c_int64(0)
@bpf
@section("kprobe/do_rmdir")
def second(ctx: struct_pt_regs) -> c_int64:
return c_int64(0)
@bpf
@bpfglobal
def LICENSE() -> str:
return "GPL"
compile()

View File

@ -93,6 +93,18 @@ CASES = {
[r"deref_0_not_null", r"ret i64 %"],
[r"ret i64\*"],
),
# One byte, sized from the c_int8 destination, read from the address in
# a register field.
"vmlinux/probe_read_kernel_scalar.py": (
[r'%"byte" = alloca i8', r"inttoptr i64 .* to i8\*", r"i32 1, i8\*"],
[],
),
# Both sides of each comparison are loaded before the compare; the
# pointers into the map are never compared themselves.
"assign/map_value_compare.py": (
[r"icmp eq i64 %"],
[r"icmp (eq|ne) i64\* %\S+, %"],
),
}