mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Tests: Rank the ctx-field case on a scalar field, not the packet pointer
xdp_md.data is a u32 in C, but the verifier tracks it as a packet pointer and rejects 32-bit arithmetic on it: "R0 32-bit pointer arithmetic prohibited", the same for a C program, which is why C casts it through (void *)(long) first. The u32 ranking the test pins is the correct one; it just has to be pinned on a field that is a scalar to the verifier too. ingress_ifindex is. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@ -1,7 +1,12 @@
|
||||
# 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.
|
||||
# its declared width: xdp_md.ingress_ifindex is a u32, so ifindex - 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.
|
||||
#
|
||||
# Not xdp_md.data: it is a u32 in C too, but the verifier tracks it as a
|
||||
# packet pointer and rejects 32-bit arithmetic on it ("R0 32-bit pointer
|
||||
# arithmetic prohibited"), for C programs as much as for this one. That is
|
||||
# why C casts it through (void *)(long) before doing anything with it.
|
||||
from ctypes import c_int32, c_int64
|
||||
from pythonbpf import bpf, section, bpfglobal, compile
|
||||
from vmlinux import struct_xdp_md
|
||||
@ -11,7 +16,7 @@ from vmlinux import struct_xdp_md
|
||||
@section("xdp")
|
||||
def prog(ctx: struct_xdp_md) -> c_int64:
|
||||
k = c_int32(1)
|
||||
d = ctx.data - k
|
||||
d = ctx.ingress_ifindex - k
|
||||
return c_int64(d)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user