From 4fa6fc59d4242ccdf003978424d6bb9a6798a84f Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 4 Sep 2026 10:14:38 +0530 Subject: [PATCH] Core: Route every integer widening and narrowing through convert() The same widen-to-i64 / truncate-to-slot logic was written out by hand in six places: local assignment, struct-field assignment, augmented assignment, helper-argument temporaries, binary-operation operands, and the comparison normaliser. Each now calls type_normalization.convert, which is the single point where the sign decides between zext and sext. Across the 67-program corpus this changes exactly one emitted instruction, and it is a correction rather than a regression: augmented assignment on a c_uint32 struct field now widens the field's current value with zext instead of sext, because that slot's descriptor already carries its sign from ctypes_to_ir. Every other site still sees plain (signed-by-default) descriptors and is byte-identical. The remaining hand-written widenings -- the printk formatter, the vmlinux field loader, return handling -- have their own rules and are migrated together with the sign sources. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU --- pythonbpf/assign_pass.py | 15 +++------------ pythonbpf/expr/__init__.py | 9 ++++++++- pythonbpf/expr/expr_pass.py | 7 +++---- pythonbpf/expr/type_normalization.py | 4 ++-- pythonbpf/functions/functions_pass.py | 10 ++++------ pythonbpf/helper/helper_utils.py | 9 +++------ 6 files changed, 23 insertions(+), 31 deletions(-) diff --git a/pythonbpf/assign_pass.py b/pythonbpf/assign_pass.py index 5d931f7..1ac680d 100644 --- a/pythonbpf/assign_pass.py +++ b/pythonbpf/assign_pass.py @@ -3,7 +3,7 @@ import logging from inspect import isclass from llvmlite import ir -from pythonbpf.expr import eval_expr +from pythonbpf.expr import eval_expr, convert from pythonbpf.helper import emit_probe_read_kernel_str_call from pythonbpf.type_deducer import ctypes_to_ir from pythonbpf.vmlinux_parser.dependency_node import Field @@ -57,10 +57,7 @@ def handle_struct_field_assignment( # Same implicit widening/truncation as assignment to a local: expressions # evaluate in i64, but a field may be narrower. if isinstance(val_type, ir.IntType) and isinstance(field_type, ir.IntType): - if val_type.width < field_type.width: - val = builder.sext(val, field_type) - elif val_type.width > field_type.width: - val = builder.trunc(val, field_type) + val = convert(builder, val, val_type, field_type) # Regular assignment builder.store(val, field_ptr) @@ -220,13 +217,7 @@ def handle_variable_assignment( ) return False elif isinstance(val_type, ir.IntType) and isinstance(var_type, ir.IntType): - # Allow implicit int widening - if val_type.width < var_type.width: - val = builder.sext(val, var_type) - logger.info(f"Implicitly widened int for variable {var_name}") - elif val_type.width > var_type.width: - val = builder.trunc(val, var_type) - logger.info(f"Implicitly truncated int for variable {var_name}") + val = convert(builder, val, val_type, var_type) elif isinstance(val_type, ir.IntType) and isinstance(var_type, ir.PointerType): # NOTE: This is assignment to a PTR_TO_MAP_VALUE_OR_NULL logger.info( diff --git a/pythonbpf/expr/__init__.py b/pythonbpf/expr/__init__.py index 5002113..f6222b0 100644 --- a/pythonbpf/expr/__init__.py +++ b/pythonbpf/expr/__init__.py @@ -1,5 +1,10 @@ from .expr_pass import eval_expr, handle_expr, get_operand_value -from .type_normalization import convert_to_bool, get_base_type_and_depth +from .type_normalization import ( + convert_to_bool, + get_base_type_and_depth, + convert, + canonicalise, +) from .ir_ops import deref_to_depth, access_struct_field from .operators import apply_binop from .call_registry import CallHandlerRegistry @@ -9,6 +14,8 @@ __all__ = [ "eval_expr", "handle_expr", "convert_to_bool", + "convert", + "canonicalise", "get_base_type_and_depth", "deref_to_depth", "apply_binop", diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index 1441034..9483252 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -9,6 +9,7 @@ from .call_registry import CallHandlerRegistry from .ir_ops import deref_to_depth, access_struct_field from .operators import apply_binop, UNARY_OPS, BOOL_OPS from .type_normalization import ( + convert, convert_to_bool, handle_comparator, get_base_type_and_depth, @@ -235,10 +236,8 @@ def _handle_binary_op_impl(func, compilation_context, rval, builder, local_sym_t # NOTE: Before doing the operation, if the operands are integers # we always extend them to i64. The assignment to LHS will take # care of truncation if needed. - if isinstance(left.type, ir.IntType) and left.type.width < 64: - left = builder.sext(left, ir.IntType(64)) - if isinstance(right.type, ir.IntType) and right.type.width < 64: - right = builder.sext(right, ir.IntType(64)) + left = convert(builder, left, left.type, ir.IntType(64)) + right = convert(builder, right, right.type, ir.IntType(64)) # Map AST operation nodes to LLVM IR builder methods return apply_binop(builder, op, left, right) diff --git a/pythonbpf/expr/type_normalization.py b/pythonbpf/expr/type_normalization.py index f939f84..a857d9d 100644 --- a/pythonbpf/expr/type_normalization.py +++ b/pythonbpf/expr/type_normalization.py @@ -23,9 +23,9 @@ def _normalize_types(func, builder, lhs, rhs): logger.info(f"Normalizing types: {lhs.type} vs {rhs.type}") if isinstance(lhs.type, ir.IntType) and isinstance(rhs.type, ir.IntType): if lhs.type.width < rhs.type.width: - lhs = builder.sext(lhs, rhs.type) + lhs = convert(builder, lhs, lhs.type, rhs.type) else: - rhs = builder.sext(rhs, lhs.type) + rhs = convert(builder, rhs, rhs.type, lhs.type) return lhs, rhs elif not isinstance(lhs.type, ir.PointerType) and not isinstance( rhs.type, ir.PointerType diff --git a/pythonbpf/functions/functions_pass.py b/pythonbpf/functions/functions_pass.py index 192654d..e8c451e 100644 --- a/pythonbpf/functions/functions_pass.py +++ b/pythonbpf/functions/functions_pass.py @@ -13,6 +13,7 @@ from pythonbpf.expr import ( convert_to_bool, get_operand_value, apply_binop, + convert, VmlinuxHandlerRegistry, ) from pythonbpf.assign_pass import ( @@ -281,13 +282,10 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab): ) # Same width discipline as binary-op evaluation: compute in i64, narrow # back to the slot's width on the way out. - if current.type.width < 64: - current = builder.sext(current, ir.IntType(64)) - if isinstance(rhs.type, ir.IntType) and rhs.type.width < 64: - rhs = builder.sext(rhs, ir.IntType(64)) + current = convert(builder, current, slot_type, ir.IntType(64)) + rhs = convert(builder, rhs, rhs.type, ir.IntType(64)) result = apply_binop(builder, stmt.op, current, rhs) - if result.type.width > slot_type.width: - result = builder.trunc(result, slot_type) + result = convert(builder, result, result.type, slot_type) builder.store(result, slot) diff --git a/pythonbpf/helper/helper_utils.py b/pythonbpf/helper/helper_utils.py index 5725117..28b60b3 100644 --- a/pythonbpf/helper/helper_utils.py +++ b/pythonbpf/helper/helper_utils.py @@ -3,6 +3,7 @@ import logging from llvmlite import ir from pythonbpf.expr import ( + convert, eval_expr, access_struct_field, ) @@ -134,12 +135,8 @@ def get_or_create_ptr_from_arg( local_sym_tab, expected_type ) logger.info(f"Using temp variable '{temp_name}' for expression result") - if ( - isinstance(val.type, ir.IntType) - and expected_type - and val.type.width > expected_type.width - ): - val = builder.trunc(val, expected_type) + if expected_type is not None and isinstance(expected_type, ir.IntType): + val = convert(builder, val, val.type, expected_type) builder.store(val, ptr) # NOTE: For char arrays, also return size