Skip to content

Commit 78ec75a

Browse files
r41k0uclaude
andcommitted
Tests: Bool widening and narrowing
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent a230b44 commit 78ec75a

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# A bool is a 1-bit integer to LLVM, and C's rules for it are not the integer
2+
# ones: it widens to 0 or 1 (never sign-extends to -1), and an integer narrows
3+
# to it by comparing with zero (2 is true), not by keeping the low bit.
4+
from pythonbpf import bpf, section, bpfglobal, compile
5+
from ctypes import c_void_p, c_int64
6+
7+
8+
@bpf
9+
@section("tracepoint/raw_syscalls/sys_enter")
10+
def prog(ctx: c_void_p) -> c_int64:
11+
t = True
12+
n = True
13+
n = 2 # bool = 2 is true in C; truncation would make it false
14+
s = t + 1 # int + bool promotes the bool to int 1: 2, not 0
15+
return c_int64(t + n + s) # 1 + 1 + 2 = 4, not -1 + 0 + 0
16+
17+
18+
@bpf
19+
@bpfglobal
20+
def LICENSE() -> str:
21+
return "GPL"
22+
23+
24+
compile()

‎tests/test_signedness_ir.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
[r"\budiv i64 .*, 4294967294"],
5151
[r"\bsdiv i64"],
5252
),
53+
# A bool widens with zext and an integer narrows to it by != 0, never by
54+
# trunc; sext of an i1 would return -1 for True.
55+
"signedness/bool_int.py": (
56+
[r"zext i1 .* to i(32|64)", r"icmp ne i64 .*, 0"],
57+
[r"sext i1 ", r"trunc i64 .* to i1"],
58+
),
5359
# An enum constant is a C `int`, so `XDP_PASS - k` with k a c_uint32 is a
5460
# u32 operation: the result is cut to 32 bits and zero-extended. Ranked
5561
# as i64 it would be a signed 64-bit subtraction with no trunc at all.

0 commit comments

Comments
 (0)