From 1dfb22d3dc8b8bd92f845aebb756435ca966f01a Mon Sep 17 00:00:00 2001 From: Varun R Mallya Date: Fri, 25 Sep 2026 18:25:53 +0530 Subject: [PATCH] Core: Compare map values by value, not by address With a map lookup on both sides, both operands are pointers of one type, so the depth-normalising path emitted a pointer compare. Load both sides through get_typed_operand instead, which also picks the predicate from the map's declared value sign. --- pythonbpf/expr/expr_pass.py | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index b9984b8..19f0a6b 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -383,12 +383,57 @@ def _handle_ctypes_call( return value, expected_type +def _is_map_int_value(operand, compilation_context, local_sym_tab): + """Whether the operand is an integer held in a map: a `m.lookup(k)` call on + a map with an integer value, or a local bound to one. Both evaluate to a + pointer into the map, and comparing one means comparing the value.""" + if isinstance(operand, ast.Name): + sym = local_sym_tab.get(operand.id) + if sym is None or not isinstance(sym.var.type, ir.PointerType): + return False + _, depth = get_base_type_and_depth(sym.var.type) + return depth == 2 and isinstance(sym.metadata, str) and is_ctypes(sym.metadata) + if ( + isinstance(operand, ast.Call) + and isinstance(operand.func, ast.Attribute) + and operand.func.attr == "lookup" + and isinstance(operand.func.value, ast.Name) + ): + map_sym = compilation_context.map_sym_tab.get(operand.func.value.id) + if map_sym is None: + return False + value_type = map_sym.params.get("value") + return isinstance(value_type, str) and is_ctypes(value_type) + return False + + 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 + + if any( + _is_map_int_value(operand, compilation_context, local_sym_tab) + for operand in (cond.left, cond.comparators[0]) + ): + # A map value compares by what it holds (null-checked, 0 when the key + # is absent), never by address: `x == m.lookup(i)` with both sides + # pointers would otherwise compare two map slots' addresses. + lhs, lhs_ty = get_typed_operand( + func, compilation_context, cond.left, builder, local_sym_tab + ) + rhs, rhs_ty = get_typed_operand( + func, compilation_context, cond.comparators[0], builder, local_sym_tab + ) + cmp_ty = usual_arithmetic_conversions(lhs_ty, rhs_ty) + lhs = to_promoted(builder, lhs, lhs_ty, cmp_ty) + rhs = to_promoted(builder, rhs, rhs_ty, cmp_ty) + return handle_comparator( + func, builder, cond.ops[0], lhs, rhs, signed=signedness(cmp_ty) + ) + lhs = eval_expr( func, compilation_context,