mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Seven programs under passing_tests/signedness, one per case in tests/c-form/signedness.bpf.c: unsigned and signed widening, mixed division and remainder, unsigned comparison, u32 * u32 wrapping before the widening, right shifts, and literal rank. Levels 1 and 2 compile them like any other case; test_signedness_ir.py asserts the shape of the IR against what clang emits for the C (zext vs sext, udiv vs sdiv, icmp ugt vs sgt, lshr vs ashr, the trunc/zext pair around the wrap). The XDP xfail note no longer claims the guard is a signed compare. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
45 lines
1000 B
Python
45 lines
1000 B
Python
# Mirrors tests/c-form/signedness.bpf.c; the IR clang emits for that file is
|
|
# the specification (see tests/test_signedness_ir.py for the ops asserted).
|
|
from pythonbpf import bpf, section, bpfglobal, compile
|
|
from ctypes import c_void_p, c_int64, c_int32, c_uint32
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def u32_max() -> c_uint32:
|
|
return c_uint32(0xFFFFFFFF)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def widen_unsigned() -> c_int64:
|
|
return c_int64(0)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def reinterpret() -> c_int32:
|
|
return c_int32(0)
|
|
|
|
|
|
@bpf
|
|
@section("tracepoint/raw_syscalls/sys_enter")
|
|
def prog(ctx: c_void_p) -> c_int64:
|
|
global widen_unsigned, reinterpret
|
|
# s64 = u32: value-preserving, so 0xFFFFFFFF stays 4294967295 (zext, not sext)
|
|
widen_unsigned = u32_max
|
|
# s32 = u32: same width, the bits are reinterpreted (-1)
|
|
reinterpret = u32_max
|
|
# a local copied from a global takes the global's type: a c_uint32 slot
|
|
also = u32_max
|
|
return c_int64(also)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def LICENSE() -> str:
|
|
return "GPL"
|
|
|
|
|
|
compile()
|