From 0bdea85020b7ee31c1b9aed53797254ef176fab6 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 24 Sep 2026 15:59:35 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01DLMW3X7opqAPH6bjmTVkr7 --- pythonbpf/assign_pass.py | 22 ++++--------- .../vmlinux/ctx_field_narrow_store.py | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 tests/passing_tests/vmlinux/ctx_field_narrow_store.py diff --git a/pythonbpf/assign_pass.py b/pythonbpf/assign_pass.py index f243d8a..f6526bd 100644 --- a/pythonbpf/assign_pass.py +++ b/pythonbpf/assign_pass.py @@ -197,22 +197,14 @@ def handle_variable_assignment( logger.info("Handling assignment to struct field") field_ir_type = ctypes_to_ir(val_type.type.__name__) # Sub-register-width context fields are zero-extended to i64 by - # load_ctx_field, so val is already i64 even though the field type - # says otherwise (c_uint for xdp_md, c_ushort for pt_regs.cs/ss). - if ( - isinstance(field_ir_type, ir.IntType) - and field_ir_type.width < 64 - and isinstance(var_type, ir.IntType) - and var_type.width == 64 + # load_ctx_field, so val may be wider than the field type says + # (c_uint for xdp_md, c_ushort for pt_regs.cs/ss). convert() + # sizes from the physical value and signs from the field, so it + # is a no-op into an i64 slot and a trunc into a narrower one. + if isinstance(field_ir_type, ir.IntType) and isinstance( + var_type, ir.IntType ): - builder.store(val, var_ptr) - logger.info( - f"Assigned zero-extended i{field_ir_type.width} context field " - f"to {var_name} (i64)" - ) - return True - # TODO: handling only ctype struct fields for now. Handle other stuff too later. - elif var_type == field_ir_type: + val = convert(builder, val, field_ir_type, var_type) builder.store(val, var_ptr) logger.info(f"Assigned ctype struct field to {var_name}") return True diff --git a/tests/passing_tests/vmlinux/ctx_field_narrow_store.py b/tests/passing_tests/vmlinux/ctx_field_narrow_store.py new file mode 100644 index 0000000..5f0d8bf --- /dev/null +++ b/tests/passing_tests/vmlinux/ctx_field_narrow_store.py @@ -0,0 +1,32 @@ +# 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()