diff --git a/tests/passing_tests/signedness/augassign_unsigned.py b/tests/passing_tests/signedness/augassign_unsigned.py new file mode 100644 index 0000000..1ba2bfa --- /dev/null +++ b/tests/passing_tests/signedness/augassign_unsigned.py @@ -0,0 +1,24 @@ +# Augmented assignment picks its operator from the promoted type like a +# binary operation does: on a c_uint32, >>= is a logical shift and //= and +# %= are unsigned division and remainder. +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64, c_uint32 + + +@bpf +@section("tracepoint/raw_syscalls/sys_enter") +def prog(ctx: c_void_p) -> c_int64: + u = c_uint32(0xF0000000) + u >>= 4 # lshr: 0x0F000000, not sign-filled + u //= 3 # udiv + u %= 7 # urem + return c_int64(u) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/signedness/helper_results.py b/tests/passing_tests/signedness/helper_results.py new file mode 100644 index 0000000..b0033d4 --- /dev/null +++ b/tests/passing_tests/signedness/helper_results.py @@ -0,0 +1,23 @@ +# A helper's value carries the sign its registry entry declares: ktime() +# and pid() are unsigned, so a right shift is logical and a division is +# unsigned, the same as for a c_uint64 local. +from pythonbpf import bpf, section, bpfglobal, compile +from pythonbpf.helper import ktime, pid +from ctypes import c_void_p, c_int64 + + +@bpf +@section("tracepoint/raw_syscalls/sys_enter") +def prog(ctx: c_void_p) -> c_int64: + t = ktime() >> 1 # lshr + q = pid() // 3 # udiv + return c_int64(t + q) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/signedness/map_value_sign.py b/tests/passing_tests/signedness/map_value_sign.py new file mode 100644 index 0000000..1c5c19d --- /dev/null +++ b/tests/passing_tests/signedness/map_value_sign.py @@ -0,0 +1,31 @@ +# A map-lookup local dereferenced to its value carries the map's declared +# value type: on a c_uint64 map, p >> 63 is a logical shift. +from pythonbpf import bpf, map, section, bpfglobal, compile +from pythonbpf.maps import HashMap +from ctypes import c_void_p, c_int64, c_uint32, c_uint64 + + +@bpf +@map +def m() -> HashMap: + return HashMap(key=c_uint32, value=c_uint64, max_entries=4) + + +@bpf +@section("tracepoint/raw_syscalls/sys_enter") +def prog(ctx: c_void_p) -> c_int64: + k = c_uint32(1) + p = m.lookup(k) + if p: + top = p >> 63 # lshr: 0 or 1, never -1 + return c_int64(top) + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/vmlinux/ctx_field_rank.py b/tests/passing_tests/vmlinux/ctx_field_rank.py new file mode 100644 index 0000000..d8456d0 --- /dev/null +++ b/tests/passing_tests/vmlinux/ctx_field_rank.py @@ -0,0 +1,24 @@ +# A sub-register context field is loaded widened to i64, but C ranks it by +# its declared width: xdp_md.data is a u32, so data - k with a c_int32 k is +# a u32 subtraction (the result is cut to 32 bits and zero-extended), not a +# 64-bit one. +from ctypes import c_int32, c_int64 +from pythonbpf import bpf, section, bpfglobal, compile +from vmlinux import struct_xdp_md + + +@bpf +@section("xdp") +def prog(ctx: struct_xdp_md) -> c_int64: + k = c_int32(1) + d = ctx.data - k + return c_int64(d) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/test_signedness_ir.py b/tests/test_signedness_ir.py index b38834e..2166ed8 100644 --- a/tests/test_signedness_ir.py +++ b/tests/test_signedness_ir.py @@ -50,6 +50,24 @@ CASES = { [r"\budiv i64 .*, 4294967294"], [r"\bsdiv i64"], ), + "signedness/augassign_unsigned.py": ( + [r"\blshr i64", r"\budiv i64", r"\burem i64"], + [r"\bashr i64", r"\bsdiv i64", r"\bsrem i64"], + ), + "signedness/helper_results.py": ( + [r"\blshr i64", r"\budiv i64"], + [r"\bashr i64", r"\bsdiv i64"], + ), + "signedness/map_value_sign.py": ( + [r"\blshr i64 [^,]*, 63"], + [r"\bashr i64"], + ), + # u32 - int is a u32 operation: the result is cut to 32 bits and + # zero-extended; ranked as 64-bit there would be no trunc at all. + "vmlinux/ctx_field_rank.py": ( + [r"\bsub i64", r"trunc i64 .* to i32", r"zext i32 .* to i64"], + [], + ), # 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": (