diff --git a/tests/passing_tests/assign/map_value_compare.py b/tests/passing_tests/assign/map_value_compare.py new file mode 100644 index 0000000..5fdcb0e --- /dev/null +++ b/tests/passing_tests/assign/map_value_compare.py @@ -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() diff --git a/tests/passing_tests/vmlinux/probe_read_kernel_scalar.py b/tests/passing_tests/vmlinux/probe_read_kernel_scalar.py new file mode 100644 index 0000000..c1bf40d --- /dev/null +++ b/tests/passing_tests/vmlinux/probe_read_kernel_scalar.py @@ -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() diff --git a/tests/passing_tests/vmlinux/shared_ctx_name.py b/tests/passing_tests/vmlinux/shared_ctx_name.py new file mode 100644 index 0000000..ab93fc1 --- /dev/null +++ b/tests/passing_tests/vmlinux/shared_ctx_name.py @@ -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() diff --git a/tests/test_signedness_ir.py b/tests/test_signedness_ir.py index 5a4b31d..13341bd 100644 --- a/tests/test_signedness_ir.py +++ b/tests/test_signedness_ir.py @@ -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+, %"], + ), }