Files
python-bpf/tests/passing_tests/vmlinux/ctx_field_narrow_store.py
Claude 0bdea85020 Core: Store a context field into a narrower slot through convert()
A sub-register context field is loaded zero-extended to i64, and the
assignment path accepted it only into a 64-bit slot or one whose type was
exactly the field's declared type. The second case never worked in practice:
`data_end = skb.data_end` into a c_uint32 global died in llvmlite with
"cannot store i64 to i32*" because the value was still i64.

Route the store through convert(), sizing from the physical value and signing
from the field, like every other integer store since the signedness work: a
no-op into an i64 slot, a trunc into anything narrower. Found by porting
cgroup_skb_direct_packet_access.c, which is that exact statement.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DLMW3X7opqAPH6bjmTVkr7
2026-09-24 15:59:35 +00:00

33 lines
753 B
Python

# A context field is loaded widened to i64, but it can be stored into a slot
# of its own declared width, or narrower: the store truncates like any other
# integer conversion. The cgroup_skb_direct_packet_access.c selftest does
# exactly this with `__u32 data_end = skb->data_end`.
from ctypes import c_int64, c_uint16, c_uint32
from pythonbpf import bpf, section, bpfglobal, compile
from vmlinux import struct_xdp_md
@bpf
@bpfglobal
def ifindex() -> c_uint32:
return c_uint32(0)
@bpf
@section("xdp")
def prog(ctx: struct_xdp_md) -> c_int64:
global ifindex
ifindex = ctx.ingress_ifindex
queue = c_uint16(0)
queue = ctx.rx_queue_index
return c_int64(queue)
@bpf
@bpfglobal
def LICENSE() -> str:
return "GPL"
compile()