diff --git a/tests/failing_tests/vmlinux/ctx_data_end_offset.py b/tests/failing_tests/vmlinux/ctx_data_end_offset.py new file mode 100644 index 0000000..238fe57 --- /dev/null +++ b/tests/failing_tests/vmlinux/ctx_data_end_offset.py @@ -0,0 +1,21 @@ +# data_end can only be compared against; the verifier rejects arithmetic on +# it, so the compiler refuses it. +from ctypes import c_int64, c_uint32 # noqa: F401 +from pythonbpf import bpf, section, bpfglobal, compile +from vmlinux import struct_xdp_md + + +@bpf +@section("xdp") +def prog(ctx: struct_xdp_md) -> c_int64: + e = ctx.data_end + 1 + return c_int64(e) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/failing_tests/vmlinux/ctx_data_narrow_store.py b/tests/failing_tests/vmlinux/ctx_data_narrow_store.py new file mode 100644 index 0000000..984ae76 --- /dev/null +++ b/tests/failing_tests/vmlinux/ctx_data_narrow_store.py @@ -0,0 +1,22 @@ +# A packet pointer stored into a 32-bit slot would lose it; the verifier only +# accepts packet pointers at 64 bits, so the compiler refuses the narrowing. +from ctypes import c_int64, c_uint32 # noqa: F401 +from pythonbpf import bpf, section, bpfglobal, compile +from vmlinux import struct_xdp_md + + +@bpf +@section("xdp") +def prog(ctx: struct_xdp_md) -> c_int64: + x = c_uint32(0) + x = ctx.data + return c_int64(x) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/vmlinux/ctx_bounds_check.py b/tests/passing_tests/vmlinux/ctx_bounds_check.py new file mode 100644 index 0000000..4e91357 --- /dev/null +++ b/tests/passing_tests/vmlinux/ctx_bounds_check.py @@ -0,0 +1,25 @@ +# The canonical XDP bounds check, through locals: the packet-pointer type +# travels with data and data_end, so data + 34 > data_end is a 64-bit +# unsigned comparison, which is what the verifier needs to prove the range. +from ctypes import c_int64, c_uint32 # noqa: F401 +from pythonbpf import bpf, section, bpfglobal, compile +from vmlinux import struct_xdp_md + + +@bpf +@section("xdp") +def prog(ctx: struct_xdp_md) -> c_int64: + data = ctx.data + data_end = ctx.data_end + if data + 34 > data_end: + return c_int64(1) + return c_int64(2) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/vmlinux/ctx_bounds_check_direct.py b/tests/passing_tests/vmlinux/ctx_bounds_check_direct.py new file mode 100644 index 0000000..cac2cbd --- /dev/null +++ b/tests/passing_tests/vmlinux/ctx_bounds_check_direct.py @@ -0,0 +1,21 @@ +# The same bounds check written on the fields directly. +from ctypes import c_int64, c_uint32 # noqa: F401 +from pythonbpf import bpf, section, bpfglobal, compile +from vmlinux import struct_xdp_md + + +@bpf +@section("xdp") +def prog(ctx: struct_xdp_md) -> c_int64: + if ctx.data + 14 > ctx.data_end: + return c_int64(1) + return c_int64(2) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/vmlinux/ctx_data_meta_offset.py b/tests/passing_tests/vmlinux/ctx_data_meta_offset.py new file mode 100644 index 0000000..05b7c27 --- /dev/null +++ b/tests/passing_tests/vmlinux/ctx_data_meta_offset.py @@ -0,0 +1,20 @@ +# data_meta is a packet-metadata pointer; offsets are allowed on it too. +from ctypes import c_int64, c_uint32 # noqa: F401 +from pythonbpf import bpf, section, bpfglobal, compile +from vmlinux import struct_xdp_md + + +@bpf +@section("xdp") +def prog(ctx: struct_xdp_md) -> c_int64: + m = ctx.data_meta + 4 + return c_int64(m) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/vmlinux/ctx_data_via_local.py b/tests/passing_tests/vmlinux/ctx_data_via_local.py new file mode 100644 index 0000000..1e2d36d --- /dev/null +++ b/tests/passing_tests/vmlinux/ctx_data_via_local.py @@ -0,0 +1,22 @@ +# A local copied from a packet-pointer field is still a packet pointer: its +# slot is 64-bit and d + 14 is 64-bit pointer arithmetic. +from ctypes import c_int64, c_uint32 # noqa: F401 +from pythonbpf import bpf, section, bpfglobal, compile +from vmlinux import struct_xdp_md + + +@bpf +@section("xdp") +def prog(ctx: struct_xdp_md) -> c_int64: + data = ctx.data + eth = data + 14 + return c_int64(eth) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/test_config.toml b/tests/test_config.toml index 3f49730..2e71808 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -64,3 +64,6 @@ # A packet offset has to fit the verifier's 16-bit packet range. "failing_tests/vmlinux/ctx_data_offset_too_big.py" = {reason = "A constant packet offset outside 0..65535 is a compile error (the verifier tracks packet ranges up to MAX_PACKET_OFF)", level = "ir"} +"failing_tests/vmlinux/ctx_data_end_offset.py" = {reason = "A packet-end pointer can only be compared, not offset (the verifier rejects arithmetic on it)", level = "ir"} + +"failing_tests/vmlinux/ctx_data_narrow_store.py" = {reason = "A packet pointer cannot be narrowed below 64 bits", level = "ir"} diff --git a/tests/test_signedness_ir.py b/tests/test_signedness_ir.py index 6bfdca9..6b8ab71 100644 --- a/tests/test_signedness_ir.py +++ b/tests/test_signedness_ir.py @@ -88,6 +88,18 @@ CASES = { [r"\bsub i64"], [r"to i16", r"trunc i64 %.* to i32"], ), + "vmlinux/ctx_bounds_check.py": ( + [r"icmp ugt i64", r"\badd i64"], + [r"trunc i64 %.* to i32", r"icmp \w+ i32"], + ), + "vmlinux/ctx_bounds_check_direct.py": ( + [r"icmp ugt i64"], + [r"trunc i64 %.* to i32", r"icmp \w+ i32"], + ), + "vmlinux/ctx_data_via_local.py": ( + [r"\badd i64"], + [r"trunc i64 %.* to i32"], + ), # 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": (