Core: Special-case packet-pointer context fields in arithmetic

xdp_md.data/data_end/data_meta and __sk_buff.data/data_end/data_meta are
u32 in C but packet pointers to the verifier, which rewrites their load
into a pointer load and prohibits 32-bit arithmetic on them. The
signedness rules ranked them as u32, so ctx.data - k lowered to a 32-bit
add (w0 += -1) and was rejected; C code avoids this with a cast through
(void *)(long).

A binary operation with a packet-pointer operand now bypasses the usual
arithmetic conversions: the pointer is used at 64 bits, an offset added
to or subtracted from it is taken as an unsigned 16-bit value (truncated,
zero-extended) because the verifier only tracks packet ranges up to
MAX_PACKET_OFF (0xffff), and the result is a 64-bit value. Pointer minus
pointer is an ordinary 64-bit length. A constant offset outside
0..65535, any other operator, and offset minus pointer are compile
errors rather than code the verifier would reject. Local inference
agrees (64-bit unsigned).

Only direct field reads are recognised; a local copied from one is an
integer again, and comparisons between packet pointers are the next site
of the same rule.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pragyansh Chaturvedi
2026-09-25 11:58:42 +05:30
parent fab456b83d
commit 6a859ac0cd
11 changed files with 239 additions and 30 deletions

View File

@ -24,6 +24,7 @@ from .type_normalization import (
get_base_type_and_depth,
)
from .vmlinux_registry import VmlinuxHandlerRegistry
from .packet_pointer import MAX_PACKET_OFF, is_packet_pointer
from ..vmlinux_parser.dependency_node import Field
logger: Logger = logging.getLogger(__name__)
@ -284,12 +285,18 @@ def _handle_binary_op_impl(func, compilation_context, rval, builder, local_sym_t
narrowed to it -- so u32 * u32 wraps at 32 bits even though the arithmetic
itself runs in an i64 register. Returns (value, IntTy)."""
op = rval.op
left_pkt = is_packet_pointer(rval.left, local_sym_tab)
right_pkt = is_packet_pointer(rval.right, local_sym_tab)
left, left_ty = get_typed_operand(
func, compilation_context, rval.left, builder, local_sym_tab
)
right, right_ty = get_typed_operand(
func, compilation_context, rval.right, builder, local_sym_tab
)
if left_pkt or right_pkt:
return _packet_pointer_binop(
builder, rval, left, left_ty, left_pkt, right, right_ty, right_pkt
)
result_ty = usual_arithmetic_conversions(left_ty, right_ty)
logger.info(
f"binop {type(op).__name__}: {left_ty.describe()} x {right_ty.describe()} "
@ -301,6 +308,51 @@ def _handle_binary_op_impl(func, compilation_context, rval, builder, local_sym_t
return canonicalise(builder, result, result_ty), result_ty
def _packet_pointer_binop(
builder, rval, left, left_ty, left_pkt, right, right_ty, right_pkt
):
"""A binary operation with a packet-pointer operand (see packet_pointer.py):
64-bit, never ranked as the field's declared u32, with the offset taken as
an unsigned 16-bit value so the verifier can track the packet range."""
op = rval.op
where = f"line {rval.lineno}: {ast.unparse(rval)}"
ptr_ty = IntTy(64, False)
if left_pkt and right_pkt:
if isinstance(op, ast.Sub):
# data_end - data: a length, an ordinary 64-bit scalar.
left = convert(builder, left, left_ty, ptr_ty)
right = convert(builder, right, right_ty, ptr_ty)
return builder.sub(left, right), ptr_ty
raise SyntaxError(
f"only subtraction is defined between packet pointers ({where})"
)
if not isinstance(op, (ast.Add, ast.Sub)):
raise SyntaxError(
f"only + and - with an offset are allowed on a packet pointer ({where})"
)
if right_pkt and isinstance(op, ast.Sub):
raise SyntaxError(f"cannot subtract a packet pointer from an offset ({where})")
ptr, ptr_src_ty, off, off_ty = (
(left, left_ty, right, right_ty)
if left_pkt
else (right, right_ty, left, left_ty)
)
if isinstance(off, ir.Constant) and isinstance(off.constant, int):
if not 0 <= off.constant <= MAX_PACKET_OFF:
raise SyntaxError(
f"packet offset {off.constant} is outside 0..{MAX_PACKET_OFF} "
f"({where}); subtract instead of adding a negative offset"
)
ptr = convert(builder, ptr, ptr_src_ty, ptr_ty)
# The offset as u16, widened with zero-extension: a value the verifier can
# bound to [0, 0xffff].
off = builder.trunc(off, ir.IntType(16)) if off.type.width > 16 else off
off = builder.zext(off, ir.IntType(64)) if off.type.width < 64 else off
result = builder.add(ptr, off) if isinstance(op, ast.Add) else builder.sub(ptr, off)
return result, ptr_ty
def _handle_binary_op(
func,
compilation_context,