Core: Rank ctx fields and map values by their declared types

Two descriptors were built from the physical value instead of the
declaration. A sub-register vmlinux field is widened to i64 by
load_ctx_field, and its descriptor took that width, so ctx.data - k with
a c_int32 k was a 64-bit subtraction where C makes it a u32 one. A
map-lookup local dereferenced past its slot took the physical pointee
(i64, signed by default) and dropped the map's declared value ctype kept
in the symbol's metadata, so p >> 63 on a c_uint64 value was an ashr.
Both now read the declaration; the physical width still comes from the
value, as everywhere else.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Pragyansh Chaturvedi
2026-09-24 19:28:31 +05:30
parent 26604b9846
commit 532dc0f808
2 changed files with 35 additions and 14 deletions

View File

@ -6,6 +6,7 @@ from typing import Dict
from pythonbpf.type_deducer import (
ctypes_to_ir,
field_int_type,
is_ctypes,
IntTy,
int_literal_type,
@ -200,6 +201,11 @@ def _descriptor(val, ty):
value is not an integer at all."""
if isinstance(ty, ir.IntType):
return IntTy(ty.width, signedness(ty))
field = field_int_type(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).
return field
if val is not None and isinstance(val.type, ir.IntType):
return IntTy(val.type.width, signedness(ty))
return None
@ -214,12 +220,21 @@ def get_typed_operand(func, compilation_context, operand, builder, local_sym_tab
sym = local_sym_tab[operand.id]
var = sym.var
base_type, depth = get_base_type_and_depth(var.type)
val = (
builder.load(var)
if depth == 1
else deref_to_depth(func, builder, var, depth)
if depth == 1:
val = builder.load(var)
return val, _descriptor(val, sym.ir_type)
val = deref_to_depth(func, builder, var, depth)
# A map-lookup local: the slot points at the value, and the map's
# declared value ctype is the symbol's metadata. That, not the
# physical pointee, carries the sign (a c_uint64 value is unsigned).
declared = (
ctypes_to_ir(sym.metadata)
if isinstance(sym.metadata, str) and is_ctypes(sym.metadata)
else None
)
return val, _descriptor(
val, declared if declared is not None else base_type
)
return val, _descriptor(val, sym.ir_type if depth == 1 else base_type)
elif operand.id in compilation_context.bpf_globals:
sym = compilation_context.bpf_globals[operand.id]
return builder.load(sym.var), _descriptor(None, sym.ir_type)