Tests: Packet pointers through locals, bounds checks, data_meta, and the refusals

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pragyansh Chaturvedi
2026-09-25 13:03:32 +05:30
parent b9b582a7d8
commit db894087a2
8 changed files with 146 additions and 0 deletions

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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"}

View File

@ -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": (