From aa612a415176a05726d077d033a1ea50084806b9 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 25 Sep 2026 23:21:12 +0530 Subject: [PATCH] Core: Let a packet pointer be narrowed on a store, as C does convert() refused to narrow a packet pointer below 64 bits, on the premise that the verifier only accepts one at 64 bits. That is true of arithmetic, not of stores: upstream's cgroup_skb_direct_packet_access stores skb->data_end into a __u32 global, and CI's verifier level accepted exactly that on #105. Merging master brought that selftest in and the refusal rejected it. Narrowing now truncates, and the result is an ordinary integer. ctx_data_narrow_store.py moves to the passing tests. Keeping the pointer when a local is declared narrower belongs to allocation (widest type ever stored), noted for later. Co-Authored-By: Claude Opus 5.5 (1M context) --- pythonbpf/expr/packet_pointer.py | 10 ++++++++-- pythonbpf/expr/type_normalization.py | 11 +---------- .../vmlinux/ctx_data_narrow_store.py | 7 +++++-- tests/test_config.toml | 1 - 4 files changed, 14 insertions(+), 15 deletions(-) rename tests/{failing_tests => passing_tests}/vmlinux/ctx_data_narrow_store.py (51%) diff --git a/pythonbpf/expr/packet_pointer.py b/pythonbpf/expr/packet_pointer.py index baa0512..fe0fa2f 100644 --- a/pythonbpf/expr/packet_pointer.py +++ b/pythonbpf/expr/packet_pointer.py @@ -17,8 +17,14 @@ The field read gives its value a PktPtrTy descriptor (a 64-bit unsigned IntTy tagged with the verifier's kind), and the rules key on that descriptor, so they follow the pointer through locals (`d = ctx.data`), through `d + 14`, and into comparisons (`ctx.data + 34 > ctx.data_end` is a 64-bit -unsigned compare). packet-end pointers take no offset at all, and nothing -narrows a packet pointer below 64 bits. +unsigned compare). packet-end pointers take no offset at all. + +Storing a packet pointer into a narrower slot truncates it, as C does, and the +result is an ordinary integer: the verifier forbids 32-bit *arithmetic* on a +packet pointer, not a 32-bit store of one (upstream's +cgroup_skb_direct_packet_access stores data_end into a __u32 global). Keeping +the pointer when a local is declared narrower is a job for allocation, which +could give such a slot the widest type ever stored into it. """ import ast diff --git a/pythonbpf/expr/type_normalization.py b/pythonbpf/expr/type_normalization.py index 5c35fd7..e89815f 100644 --- a/pythonbpf/expr/type_normalization.py +++ b/pythonbpf/expr/type_normalization.py @@ -1,7 +1,7 @@ import logging from llvmlite import ir from .ir_ops import deref_to_depth -from pythonbpf.type_deducer import IntTy, PktPtrTy, signedness +from pythonbpf.type_deducer import IntTy, signedness from .operators import COMPARISON_OPS logger = logging.getLogger(__name__) @@ -52,15 +52,6 @@ def convert(builder, val, from_ty, to_ty): descriptors (see type_deducer.IntTy); the physical width comes from the value itself, which may already be wider than its descriptor says. """ - if ( - isinstance(from_ty, PktPtrTy) - and isinstance(to_ty, ir.IntType) - and to_ty.width < 64 - ): - raise TypeError( - f"a packet pointer ({from_ty.describe()}) cannot be narrowed to " - f"i{to_ty.width}; the verifier only accepts it at 64 bits" - ) if not (isinstance(to_ty, ir.IntType) and isinstance(val.type, ir.IntType)): # Every caller either checks both sides are integers or sits on a path # that only carries integers, so reaching here is a type error that diff --git a/tests/failing_tests/vmlinux/ctx_data_narrow_store.py b/tests/passing_tests/vmlinux/ctx_data_narrow_store.py similarity index 51% rename from tests/failing_tests/vmlinux/ctx_data_narrow_store.py rename to tests/passing_tests/vmlinux/ctx_data_narrow_store.py index 984ae76..ef7c6ae 100644 --- a/tests/failing_tests/vmlinux/ctx_data_narrow_store.py +++ b/tests/passing_tests/vmlinux/ctx_data_narrow_store.py @@ -1,5 +1,8 @@ -# 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. +# A packet pointer stored into a 32-bit local is truncated, as in C +# (`__u32 x = ctx->data`), and is an ordinary integer from then on. The +# verifier forbids 32-bit arithmetic on a packet pointer, not a narrow store +# of one; upstream's cgroup_skb_direct_packet_access does the same with a +# __u32 global. from ctypes import c_int64, c_uint32 # noqa: F401 from pythonbpf import bpf, section, bpfglobal, compile from vmlinux import struct_xdp_md diff --git a/tests/test_config.toml b/tests/test_config.toml index 3d5e9b6..eb680d6 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -68,7 +68,6 @@ "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"} "kernel_selftest_equivalent/ringbuf/reserve_submit_discard.py" = {reason = "RingBuffer reserve/typed record/discard workflow is planned but not implemented yet", level = "ir"}