mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Docs: Note the packet-pointer-field gotcha, with the planned mechanism
xdp_md.data/data_end/data_meta and __sk_buff.data/data_end are u32 in C and packet pointers to the verifier, so arithmetic on them directly is rejected as 32-bit pointer arithmetic, for C and for us alike. A TODO at the point where a field's rank is decided describes the planned fix (64-bit pointer rank for those fields, no cast needed), and the user guide documents the workaround until then: copy to a local or cast through c_void_p. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@ -205,6 +205,17 @@ def _descriptor(val, ty):
|
||||
if field is not None:
|
||||
# A vmlinux field: load_ctx_field already widened the value, but C
|
||||
# ranks it by its declared width (a c_uint32 field is unsigned int).
|
||||
#
|
||||
# TODO(gotcha): some u32 context fields are packet pointers to the
|
||||
# verifier, not numbers: xdp_md.data / data_end / data_meta and
|
||||
# __sk_buff.data / data_end. Ranking them as u32 is what C does, and
|
||||
# the verifier then rejects any arithmetic on them ("32-bit pointer
|
||||
# arithmetic prohibited"), so C code casts them through
|
||||
# (void *)(long) first. The plan is to spare users that: give these
|
||||
# fields 64-bit pointer rank here, so `ctx.data + 34 < ctx.data_end`
|
||||
# lowers to 64-bit pointer arithmetic without a cast. Until then,
|
||||
# copy the field into a local (a 64-bit slot) or cast it via
|
||||
# c_void_p before using it.
|
||||
return field
|
||||
if val is not None and isinstance(val.type, ir.IntType):
|
||||
return IntTy(val.type.width, signedness(ty))
|
||||
|
||||
Reference in New Issue
Block a user