From a5eeaafdf3415ac14e88e3956d85fb4d4cbd2fcd Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 25 Sep 2026 22:20:09 +0530 Subject: [PATCH] Tests: Context fields into slots of every width, and c_bool ctx_field_into_slots.py stores 8-, 32- and 64-bit sk_buff fields into locals declared at every width and into struct fields of every width, with IR assertions on the slot widths and the truncation before each store. It is the compact form of a 52-case matrix (four field widths, nine local and four struct-field destinations) compiled on master and here: every case compiles, nothing sign-extends a field on the way in, and where master compiled the IR is identical except where master put a narrow local in a 64-bit slot. c_bool.py covers the local, struct field, map value and global. Co-Authored-By: Claude Opus 5.5 (1M context) --- tests/passing_tests/signedness/c_bool.py | 49 +++++++++++++++++++ .../vmlinux/ctx_field_into_slots.py | 47 ++++++++++++++++++ tests/test_signedness_ir.py | 19 +++++++ 3 files changed, 115 insertions(+) create mode 100644 tests/passing_tests/signedness/c_bool.py create mode 100644 tests/passing_tests/vmlinux/ctx_field_into_slots.py diff --git a/tests/passing_tests/signedness/c_bool.py b/tests/passing_tests/signedness/c_bool.py new file mode 100644 index 0000000..e808904 --- /dev/null +++ b/tests/passing_tests/signedness/c_bool.py @@ -0,0 +1,49 @@ +# c_bool is C's _Bool: a value narrows to it by comparing with zero (5 is +# true), and it widens to 0 or 1 (zero-extended, never sign-extended), as a +# local, a struct field, a map value and a global. In BTF it is a one-byte +# _Bool, so the map's value size is 1. +from ctypes import c_bool, c_int64, c_uint32, c_void_p +from pythonbpf import bpf, map, struct, section, bpfglobal, compile +from pythonbpf.maps import HashMap + + +@bpf +@struct +class flags: + on: c_bool + n: c_uint32 + + +@bpf +@map +def seen() -> HashMap: + return HashMap(key=c_uint32, value=c_bool, max_entries=4) + + +@bpf +@bpfglobal +def armed() -> c_bool: + return c_bool(1) + + +@bpf +@section("tracepoint/raw_syscalls/sys_enter") +def prog(ctx: c_void_p) -> c_int64: + global armed + b = c_bool(5) + f = flags() + f.on = 2 + k = c_uint32(1) + seen.update(k, c_bool(1)) + armed = 7 + print(f"{b} {f.on} {armed}") + return c_int64(b) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/vmlinux/ctx_field_into_slots.py b/tests/passing_tests/vmlinux/ctx_field_into_slots.py new file mode 100644 index 0000000..c13a12e --- /dev/null +++ b/tests/passing_tests/vmlinux/ctx_field_into_slots.py @@ -0,0 +1,47 @@ +# A context field stored into a slot of any integer width: the field is loaded +# at its declared width, zero-extended (all of these are unsigned), then cut to +# the slot's width, as C's `__u8 x = skb->len` does. Covers locals declared +# with every width and struct fields of every width. +from ctypes import c_int8, c_uint16, c_int32, c_uint64, c_uint8, c_uint32, c_int64 +from pythonbpf import bpf, struct, section, bpfglobal, compile +from vmlinux import struct___sk_buff + + +@bpf +@struct +class rec: + f8: c_uint8 + f16: c_uint16 + f32: c_uint32 + f64: c_uint64 + + +@bpf +@section("tc") +def prog(ctx: struct___sk_buff) -> c_int64: + a8 = c_int8(0) + a8 = ctx.len # u32 -> i8 + b16 = c_uint16(0) + b16 = ctx.len # u32 -> u16 + c32 = c_int32(0) + c32 = ctx.tstamp # u64 -> i32 + d64 = c_uint64(0) + d64 = ctx.tstamp_type # u8 -> u64 + e = ctx.len # undeclared: a 64-bit slot + r = rec() + r.f8 = ctx.len + r.f16 = ctx.tstamp + r.f32 = ctx.tstamp + r.f64 = ctx.tstamp_type + print(f"{a8} {b16} {c32}") + print(f"{d64} {e}") + 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..96b003e 100644 --- a/tests/test_signedness_ir.py +++ b/tests/test_signedness_ir.py @@ -74,6 +74,25 @@ CASES = { [r'%"queue" = alloca i16', r"trunc i64 .* to i16"], [r'%"queue" = alloca i64'], ), + # A context field into slots of every width: loaded, zero-extended, + # truncated to the slot; nothing sign-extended on the way in. + "vmlinux/ctx_field_into_slots.py": ( + [ + r'%"a8" = alloca i8', + r'%"b16" = alloca i16', + r'%"c32" = alloca i32', + r'%"d64" = alloca i64', + r'trunc i64 %[^\n]* to i8\n\s*store i8 [^\n]*%"a8"', + r'trunc i64 %[^\n]* to i16\n\s*store i16 [^\n]*%"b16"', + r'trunc i64 %[^\n]* to i32\n\s*store i32 [^\n]*%"c32"', + ], + [r'alloca i64[^\n]*\n[^\n]*%"a8"'], + ), + # c_bool: narrowing is != 0, widening is zext; never trunc to i1 or sext. + "signedness/c_bool.py": ( + [r"icmp ne i64 5, 0", r"icmp ne i64 2, 0", r"icmp ne i64 7, 0", r"zext i1"], + [r"sext i1 ", r"trunc i64 [^\n]* to i1"], + ), # A bool widens with zext and an integer narrows to it by != 0, never by # trunc; sext of an i1 would return -1 for True. "signedness/bool_int.py": (