mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +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
53 lines
1.0 KiB
Python
53 lines
1.0 KiB
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_ten() -> c_uint32:
|
|
return c_uint32(10)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def s32_neg2() -> c_int32:
|
|
return c_int32(-2)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def mixed_div() -> c_uint32:
|
|
return c_uint32(0)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def mixed_mod() -> c_uint32:
|
|
return c_uint32(0)
|
|
|
|
|
|
@bpf
|
|
@section("tracepoint/raw_syscalls/sys_enter")
|
|
def prog(ctx: c_void_p) -> c_int64:
|
|
global mixed_div, mixed_mod
|
|
# u32 / s32: the usual arithmetic conversions make both u32, so this is an
|
|
# unsigned division: 10 / 0xFFFFFFFE == 0, not -5
|
|
mixed_div = u32_ten / s32_neg2
|
|
mixed_mod = u32_ten % s32_neg2
|
|
# both signed: an ordinary signed division
|
|
a = c_int32(-7)
|
|
b = c_int32(2)
|
|
q = a / b
|
|
return c_int64(q)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def LICENSE() -> str:
|
|
return "GPL"
|
|
|
|
|
|
compile()
|