Core: Access narrow map values at their own width

A lookup local was always an i64*, whatever the map's value type, so a
c_uint32 value was read with an 8-byte load (the verifier rejects an
access past value_size) and its spare `v_tmp` slot, typed i32, could not
take the i64 a rebind stored into it. The local now points at the
value's own width for an integer value (a struct value keeps the generic
pointer; field access casts it), and the generic pointer the lookup
helper returns is cast to it on assignment.

The sites that read the local by its pointer type follow:
- A comparison reads it as get_typed_operand does, at its width and
  with the map's declared sign, so `u == 3` on a c_uint32 value is a
  zext and an unsigned compare where needed (on c_uint64 values it was a
  signed compare before).
- print() takes the format from the declared value type and widens by
  its sign: a c_uint8 value was about to print as a string (%s on i8*),
  and integers narrower than 32 bits now print as %d, promoted as C
  varargs are.
- A rebind converts the i64 result to the slot's width.

holds_map_int_value names the predicate those sites share.
This commit is contained in:
2026-09-25 11:15:46 +05:30
parent dc628ee5b5
commit c11a25c0ab
7 changed files with 150 additions and 22 deletions

View File

@ -226,15 +226,24 @@ def handle_variable_assignment(
logger.info(
f"Creating temporary variable for pointer assignment to {var_name}"
)
var_ptr_tmp = local_sym_tab[f"{var_name}_tmp"].var
builder.store(val, var_ptr_tmp)
val = var_ptr_tmp
tmp = local_sym_tab[f"{var_name}_tmp"]
# The spare slot has the map value's width; expressions are i64.
builder.store(convert(builder, val, val_type, tmp.ir_type), tmp.var)
val = tmp.var
else:
logger.error(
f"Type mismatch for variable {var_name}: {val_type} vs {var_type}"
)
return False
if (
isinstance(val.type, ir.PointerType)
and isinstance(var_type, ir.PointerType)
and val.type != var_type
):
# A map lookup returns a generic pointer; the local is typed by the
# map's value, so the pointer is cast to it (no instruction in BPF).
val = builder.bitcast(val, var_type)
builder.store(val, var_ptr)
logger.info(f"Assigned value to variable {var_name}")
return True