mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
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:
@ -382,25 +382,41 @@ def _handle_ctypes_call(
|
||||
return value, expected_type
|
||||
|
||||
|
||||
def holds_map_int_value(sym):
|
||||
"""Whether a local is a lookup result on a map with an integer value: a
|
||||
pointer to the value, with the value's ctype as its metadata. Such a local
|
||||
is used by value, read through the pointer (see get_typed_operand)."""
|
||||
return (
|
||||
isinstance(sym.ir_type, ir.PointerType)
|
||||
and isinstance(sym.metadata, str)
|
||||
and is_ctypes(sym.metadata)
|
||||
)
|
||||
|
||||
|
||||
def _compare_operand(func, compilation_context, builder, operand, local_sym_tab):
|
||||
"""Evaluate one side of a comparison. A map-lookup local compares by its
|
||||
value, read at the map value's width and typed by its declared ctype,
|
||||
exactly as it is read as an arithmetic operand."""
|
||||
if (
|
||||
isinstance(operand, ast.Name)
|
||||
and operand.id in local_sym_tab
|
||||
and holds_map_int_value(local_sym_tab[operand.id])
|
||||
):
|
||||
return get_typed_operand(
|
||||
func, compilation_context, operand, builder, local_sym_tab
|
||||
)
|
||||
return eval_expr(func, compilation_context, builder, operand, local_sym_tab)
|
||||
|
||||
|
||||
def _handle_compare(func, compilation_context, builder, cond, local_sym_tab):
|
||||
"""Handle ast.Compare expressions."""
|
||||
|
||||
if len(cond.ops) != 1 or len(cond.comparators) != 1:
|
||||
logger.error("Only single comparisons are supported")
|
||||
return None
|
||||
lhs = eval_expr(
|
||||
func,
|
||||
compilation_context,
|
||||
builder,
|
||||
cond.left,
|
||||
local_sym_tab,
|
||||
)
|
||||
rhs = eval_expr(
|
||||
func,
|
||||
compilation_context,
|
||||
builder,
|
||||
cond.comparators[0],
|
||||
local_sym_tab,
|
||||
lhs = _compare_operand(func, compilation_context, builder, cond.left, local_sym_tab)
|
||||
rhs = _compare_operand(
|
||||
func, compilation_context, builder, cond.comparators[0], local_sym_tab
|
||||
)
|
||||
|
||||
if lhs is None or rhs is None:
|
||||
|
||||
Reference in New Issue
Block a user