From 2fd2a46838dd9a786e0b452afa79bceb2a720fe5 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 2 Oct 2025 04:13:24 +0530 Subject: [PATCH 01/10] Add LocalSymbol dataclass --- pythonbpf/functions_pass.py | 46 +++++++++++++++++++++++++++---------- pythonbpf/maps/maps_pass.py | 8 ++++--- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index 4282f004..ab068a76 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -1,6 +1,8 @@ from llvmlite import ir import ast +import logging from typing import Any +from dataclasses import dataclass from .helper import HelperHandlerRegistry, handle_helper_call from .type_deducer import ctypes_to_ir @@ -8,6 +10,14 @@ from .expr_pass import eval_expr, handle_expr local_var_metadata: dict[str | Any, Any] = {} +logger = logging.getLogger(__name__) + + +@dataclass +class LocalSymbol: + var: ir.AllocaInstr + ir_type: ir.Type + metadata: Any = None def get_probe_string(func_node): @@ -83,16 +93,19 @@ def handle_assign( elif isinstance(rval, ast.Constant): if isinstance(rval.value, bool): if rval.value: - builder.store(ir.Constant(ir.IntType(1), 1), local_sym_tab[var_name][0]) + builder.store(ir.Constant(ir.IntType(1), 1), + local_sym_tab[var_name][0]) else: - builder.store(ir.Constant(ir.IntType(1), 0), local_sym_tab[var_name][0]) + builder.store(ir.Constant(ir.IntType(1), 0), + local_sym_tab[var_name][0]) print(f"Assigned constant {rval.value} to {var_name}") elif isinstance(rval.value, int): # Assume c_int64 for now # var = builder.alloca(ir.IntType(64), name=var_name) # var.align = 8 builder.store( - ir.Constant(ir.IntType(64), rval.value), local_sym_tab[var_name][0] + ir.Constant(ir.IntType(64), + rval.value), local_sym_tab[var_name][0] ) # local_sym_tab[var_name] = var print(f"Assigned constant {rval.value} to {var_name}") @@ -107,7 +120,8 @@ def handle_assign( global_str.linkage = "internal" global_str.global_constant = True global_str.initializer = str_const - str_ptr = builder.bitcast(global_str, ir.PointerType(ir.IntType(8))) + str_ptr = builder.bitcast( + global_str, ir.PointerType(ir.IntType(8))) builder.store(str_ptr, local_sym_tab[var_name][0]) print(f"Assigned string constant '{rval.value}' to {var_name}") else: @@ -126,7 +140,8 @@ def handle_assign( # var = builder.alloca(ir_type, name=var_name) # var.align = ir_type.width // 8 builder.store( - ir.Constant(ir_type, rval.args[0].value), local_sym_tab[var_name][0] + ir.Constant( + ir_type, rval.args[0].value), local_sym_tab[var_name][0] ) print( f"Assigned {call_type} constant " @@ -172,7 +187,8 @@ def handle_assign( ir_type = struct_info.ir_type # var = builder.alloca(ir_type, name=var_name) # Null init - builder.store(ir.Constant(ir_type, None), local_sym_tab[var_name][0]) + builder.store(ir.Constant(ir_type, None), + local_sym_tab[var_name][0]) local_var_metadata[var_name] = call_type print(f"Assigned struct {call_type} to {var_name}") # local_sym_tab[var_name] = var @@ -243,7 +259,8 @@ def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab): print(f"Undefined variable {cond.id} in condition") return None elif isinstance(cond, ast.Compare): - lhs = eval_expr(func, module, builder, cond.left, local_sym_tab, map_sym_tab)[0] + lhs = eval_expr(func, module, builder, cond.left, + local_sym_tab, map_sym_tab)[0] if len(cond.ops) != 1 or len(cond.comparators) != 1: print("Unsupported complex comparison") return None @@ -296,7 +313,8 @@ def handle_if( else: else_block = None - cond = handle_cond(func, module, builder, stmt.test, local_sym_tab, map_sym_tab) + cond = handle_cond(func, module, builder, stmt.test, + local_sym_tab, map_sym_tab) if else_block: builder.cbranch(cond, then_block, else_block) else: @@ -441,7 +459,8 @@ def allocate_mem( ir_type = ctypes_to_ir(call_type) var = builder.alloca(ir_type, name=var_name) var.align = ir_type.width // 8 - print(f"Pre-allocated variable {var_name} of type {call_type}") + print( + f"Pre-allocated variable {var_name} of type {call_type}") elif HelperHandlerRegistry.has_handler(call_type): # Assume return type is int64 for now ir_type = ir.IntType(64) @@ -662,7 +681,8 @@ def _expr_type(e): if found_type is None: found_type = t elif found_type != t: - raise ValueError("Conflicting return types:" f"{found_type} vs {t}") + raise ValueError("Conflicting return types:" f"{ + found_type} vs {t}") return found_type or "None" @@ -699,7 +719,8 @@ def assign_string_to_array(builder, target_array_ptr, source_string_ptr, array_l char = builder.load(src_ptr) # Store character in target - dst_ptr = builder.gep(target_array_ptr, [ir.Constant(ir.IntType(32), 0), idx]) + dst_ptr = builder.gep( + target_array_ptr, [ir.Constant(ir.IntType(32), 0), idx]) builder.store(char, dst_ptr) # Increment counter @@ -710,5 +731,6 @@ def assign_string_to_array(builder, target_array_ptr, source_string_ptr, array_l # Ensure null termination last_idx = ir.Constant(ir.IntType(32), array_length - 1) - null_ptr = builder.gep(target_array_ptr, [ir.Constant(ir.IntType(32), 0), last_idx]) + null_ptr = builder.gep( + target_array_ptr, [ir.Constant(ir.IntType(32), 0), last_idx]) builder.store(ir.Constant(ir.IntType(8), 0), null_ptr) diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index 920cdfd0..750637a4 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -85,7 +85,7 @@ def create_bpf_map(module, map_name, map_params): def create_map_debug_info(module, map_global, map_name, map_params): - """Generate debug information metadata for BPF maps HASH and PERF_EVENT_ARRAY""" + """Generate debug info metadata for BPF maps HASH and PERF_EVENT_ARRAY""" generator = DebugInfoGenerator(module) uint_type = generator.get_uint32_type() @@ -158,7 +158,8 @@ def create_ringbuf_debug_info(module, map_global, map_name, map_params): type_ptr = generator.create_pointer_type(type_array, 64) type_member = generator.create_struct_member("type", type_ptr, 0) - max_entries_array = generator.create_array_type(int_type, map_params["max_entries"]) + max_entries_array = generator.create_array_type( + int_type, map_params["max_entries"]) max_entries_ptr = generator.create_pointer_type(max_entries_array, 64) max_entries_member = generator.create_struct_member( "max_entries", max_entries_ptr, 64 @@ -166,7 +167,8 @@ def create_ringbuf_debug_info(module, map_global, map_name, map_params): elements_arr = [type_member, max_entries_member] - struct_type = generator.create_struct_type(elements_arr, 128, is_distinct=True) + struct_type = generator.create_struct_type( + elements_arr, 128, is_distinct=True) global_var = generator.create_global_var_debug_info( map_name, struct_type, is_local=False From dadcb69f1c10fc918549827d87f38eda7c9a1065 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 2 Oct 2025 04:27:10 +0530 Subject: [PATCH 02/10] Store LocalSymbol in allocate_mem --- pythonbpf/functions_pass.py | 45 ++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index ab068a76..72d46d0f 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -93,19 +93,16 @@ def handle_assign( elif isinstance(rval, ast.Constant): if isinstance(rval.value, bool): if rval.value: - builder.store(ir.Constant(ir.IntType(1), 1), - local_sym_tab[var_name][0]) + builder.store(ir.Constant(ir.IntType(1), 1), local_sym_tab[var_name][0]) else: - builder.store(ir.Constant(ir.IntType(1), 0), - local_sym_tab[var_name][0]) + builder.store(ir.Constant(ir.IntType(1), 0), local_sym_tab[var_name][0]) print(f"Assigned constant {rval.value} to {var_name}") elif isinstance(rval.value, int): # Assume c_int64 for now # var = builder.alloca(ir.IntType(64), name=var_name) # var.align = 8 builder.store( - ir.Constant(ir.IntType(64), - rval.value), local_sym_tab[var_name][0] + ir.Constant(ir.IntType(64), rval.value), local_sym_tab[var_name][0] ) # local_sym_tab[var_name] = var print(f"Assigned constant {rval.value} to {var_name}") @@ -120,8 +117,7 @@ def handle_assign( global_str.linkage = "internal" global_str.global_constant = True global_str.initializer = str_const - str_ptr = builder.bitcast( - global_str, ir.PointerType(ir.IntType(8))) + str_ptr = builder.bitcast(global_str, ir.PointerType(ir.IntType(8))) builder.store(str_ptr, local_sym_tab[var_name][0]) print(f"Assigned string constant '{rval.value}' to {var_name}") else: @@ -140,8 +136,7 @@ def handle_assign( # var = builder.alloca(ir_type, name=var_name) # var.align = ir_type.width // 8 builder.store( - ir.Constant( - ir_type, rval.args[0].value), local_sym_tab[var_name][0] + ir.Constant(ir_type, rval.args[0].value), local_sym_tab[var_name][0] ) print( f"Assigned {call_type} constant " @@ -187,8 +182,7 @@ def handle_assign( ir_type = struct_info.ir_type # var = builder.alloca(ir_type, name=var_name) # Null init - builder.store(ir.Constant(ir_type, None), - local_sym_tab[var_name][0]) + builder.store(ir.Constant(ir_type, None), local_sym_tab[var_name][0]) local_var_metadata[var_name] = call_type print(f"Assigned struct {call_type} to {var_name}") # local_sym_tab[var_name] = var @@ -259,8 +253,7 @@ def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab): print(f"Undefined variable {cond.id} in condition") return None elif isinstance(cond, ast.Compare): - lhs = eval_expr(func, module, builder, cond.left, - local_sym_tab, map_sym_tab)[0] + lhs = eval_expr(func, module, builder, cond.left, local_sym_tab, map_sym_tab)[0] if len(cond.ops) != 1 or len(cond.comparators) != 1: print("Unsupported complex comparison") return None @@ -313,8 +306,7 @@ def handle_if( else: else_block = None - cond = handle_cond(func, module, builder, stmt.test, - local_sym_tab, map_sym_tab) + cond = handle_cond(func, module, builder, stmt.test, local_sym_tab, map_sym_tab) if else_block: builder.cbranch(cond, then_block, else_block) else: @@ -419,6 +411,7 @@ def allocate_mem( module, builder, body, func, ret_type, map_sym_tab, local_sym_tab, structs_sym_tab ): for stmt in body: + has_metadata = False if isinstance(stmt, ast.If): if stmt.body: local_sym_tab = allocate_mem( @@ -459,8 +452,7 @@ def allocate_mem( ir_type = ctypes_to_ir(call_type) var = builder.alloca(ir_type, name=var_name) var.align = ir_type.width // 8 - print( - f"Pre-allocated variable {var_name} of type {call_type}") + print(f"Pre-allocated variable {var_name} of type {call_type}") elif HelperHandlerRegistry.has_handler(call_type): # Assume return type is int64 for now ir_type = ir.IntType(64) @@ -477,7 +469,7 @@ def allocate_mem( struct_info = structs_sym_tab[call_type] ir_type = struct_info.ir_type var = builder.alloca(ir_type, name=var_name) - local_var_metadata[var_name] = call_type + has_metadata = True print( f"Pre-allocated variable {var_name} " f"for struct {call_type}" @@ -519,7 +511,11 @@ def allocate_mem( else: print("Unsupported assignment value type") continue - local_sym_tab[var_name] = (var, ir_type) + + if has_metadata: + local_sym_tab[var_name] = LocalSymbol(var, ir_type, call_type) + else: + local_sym_tab[var_name] = LocalSymbol(var, ir_type) return local_sym_tab @@ -681,8 +677,7 @@ def _expr_type(e): if found_type is None: found_type = t elif found_type != t: - raise ValueError("Conflicting return types:" f"{ - found_type} vs {t}") + raise ValueError(f"Conflicting return types:{found_type} vs {t}") return found_type or "None" @@ -719,8 +714,7 @@ def assign_string_to_array(builder, target_array_ptr, source_string_ptr, array_l char = builder.load(src_ptr) # Store character in target - dst_ptr = builder.gep( - target_array_ptr, [ir.Constant(ir.IntType(32), 0), idx]) + dst_ptr = builder.gep(target_array_ptr, [ir.Constant(ir.IntType(32), 0), idx]) builder.store(char, dst_ptr) # Increment counter @@ -731,6 +725,5 @@ def assign_string_to_array(builder, target_array_ptr, source_string_ptr, array_l # Ensure null termination last_idx = ir.Constant(ir.IntType(32), array_length - 1) - null_ptr = builder.gep( - target_array_ptr, [ir.Constant(ir.IntType(32), 0), last_idx]) + null_ptr = builder.gep(target_array_ptr, [ir.Constant(ir.IntType(32), 0), last_idx]) builder.store(ir.Constant(ir.IntType(8), 0), null_ptr) From 3b74ade455b84018ca8049c7ef2381e480db4f58 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 2 Oct 2025 04:35:10 +0530 Subject: [PATCH 03/10] Remove occurences of local_var_metadata from functions_pass, use LocalSymbol.var --- pythonbpf/functions_pass.py | 44 ++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index 72d46d0f..7e871710 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -9,7 +9,6 @@ from .binary_ops import handle_binary_op from .expr_pass import eval_expr, handle_expr -local_var_metadata: dict[str | Any, Any] = {} logger = logging.getLogger(__name__) @@ -57,10 +56,9 @@ def handle_assign( if isinstance(target, ast.Attribute): # struct field assignment field_name = target.attr - if var_name in local_sym_tab and var_name in local_var_metadata: - struct_type = local_var_metadata[var_name] + if var_name in local_sym_tab: + struct_type = local_sym_tab[var_name].metadata struct_info = structs_sym_tab[struct_type] - if field_name in struct_info.fields: field_ptr = struct_info.gep( builder, local_sym_tab[var_name][0], field_name @@ -93,16 +91,20 @@ def handle_assign( elif isinstance(rval, ast.Constant): if isinstance(rval.value, bool): if rval.value: - builder.store(ir.Constant(ir.IntType(1), 1), local_sym_tab[var_name][0]) + builder.store( + ir.Constant(ir.IntType(1), 1), local_sym_tab[var_name].var + ) else: - builder.store(ir.Constant(ir.IntType(1), 0), local_sym_tab[var_name][0]) + builder.store( + ir.Constant(ir.IntType(1), 0), local_sym_tab[var_name].var + ) print(f"Assigned constant {rval.value} to {var_name}") elif isinstance(rval.value, int): # Assume c_int64 for now # var = builder.alloca(ir.IntType(64), name=var_name) # var.align = 8 builder.store( - ir.Constant(ir.IntType(64), rval.value), local_sym_tab[var_name][0] + ir.Constant(ir.IntType(64), rval.value), local_sym_tab[var_name].var ) # local_sym_tab[var_name] = var print(f"Assigned constant {rval.value} to {var_name}") @@ -118,7 +120,7 @@ def handle_assign( global_str.global_constant = True global_str.initializer = str_const str_ptr = builder.bitcast(global_str, ir.PointerType(ir.IntType(8))) - builder.store(str_ptr, local_sym_tab[var_name][0]) + builder.store(str_ptr, local_sym_tab[var_name].var) print(f"Assigned string constant '{rval.value}' to {var_name}") else: print("Unsupported constant type") @@ -136,13 +138,13 @@ def handle_assign( # var = builder.alloca(ir_type, name=var_name) # var.align = ir_type.width // 8 builder.store( - ir.Constant(ir_type, rval.args[0].value), local_sym_tab[var_name][0] + ir.Constant(ir_type, rval.args[0].value), + local_sym_tab[var_name].var, ) print( f"Assigned {call_type} constant " f"{rval.args[0].value} to {var_name}" ) - # local_sym_tab[var_name] = var elif HelperHandlerRegistry.has_handler(call_type): # var = builder.alloca(ir.IntType(64), name=var_name) # var.align = 8 @@ -154,10 +156,8 @@ def handle_assign( local_sym_tab, map_sym_tab, structs_sym_tab, - local_var_metadata, ) - builder.store(val[0], local_sym_tab[var_name][0]) - # local_sym_tab[var_name] = var + builder.store(val[0], local_sym_tab[var_name].var) print(f"Assigned constant {rval.func.id} to {var_name}") elif call_type == "deref" and len(rval.args) == 1: print(f"Handling deref assignment {ast.dump(rval)}") @@ -174,18 +174,15 @@ def handle_assign( print("Failed to evaluate deref argument") return print(f"Dereferenced value: {val}, storing in {var_name}") - builder.store(val[0], local_sym_tab[var_name][0]) - # local_sym_tab[var_name] = var + builder.store(val[0], local_sym_tab[var_name].var) print(f"Dereferenced and assigned to {var_name}") elif call_type in structs_sym_tab and len(rval.args) == 0: struct_info = structs_sym_tab[call_type] ir_type = struct_info.ir_type # var = builder.alloca(ir_type, name=var_name) # Null init - builder.store(ir.Constant(ir_type, None), local_sym_tab[var_name][0]) - local_var_metadata[var_name] = call_type + builder.store(ir.Constant(ir_type, None), local_sym_tab[var_name].var) print(f"Assigned struct {call_type} to {var_name}") - # local_sym_tab[var_name] = var else: print(f"Unsupported assignment call type: {call_type}") elif isinstance(rval.func, ast.Attribute): @@ -208,12 +205,10 @@ def handle_assign( local_sym_tab, map_sym_tab, structs_sym_tab, - local_var_metadata, ) # var = builder.alloca(ir.IntType(64), name=var_name) # var.align = 8 - builder.store(val[0], local_sym_tab[var_name][0]) - # local_sym_tab[var_name] = var + builder.store(val[0], local_sym_tab[var_name].var) else: print("Unsupported assignment call structure") else: @@ -237,7 +232,7 @@ def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab): return None elif isinstance(cond, ast.Name): if cond.id in local_sym_tab: - var = local_sym_tab[cond.id][0] + var = local_sym_tab[cond.id].var val = builder.load(var) if val.type != ir.IntType(1): # Convert nonzero values to true, zero to false @@ -352,7 +347,6 @@ def process_stmt( ): print(f"Processing statement: {ast.dump(stmt)}") if isinstance(stmt, ast.Expr): - print(local_var_metadata) handle_expr( func, module, @@ -361,7 +355,6 @@ def process_stmt( local_sym_tab, map_sym_tab, structs_sym_tab, - local_var_metadata, ) elif isinstance(stmt, ast.Assign): handle_assign( @@ -677,7 +670,8 @@ def _expr_type(e): if found_type is None: found_type = t elif found_type != t: - raise ValueError(f"Conflicting return types:{found_type} vs {t}") + raise ValueError(f"Conflicting return types:{ + found_type} vs {t}") return found_type or "None" From 9223d7b5c59f1dc122223ec18bdf77e898e95808 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 2 Oct 2025 04:40:44 +0530 Subject: [PATCH 04/10] Remove local_var_metadata from helpers --- pythonbpf/helper/bpf_helper_handler.py | 14 +------- pythonbpf/helper/helper_utils.py | 48 +++++++------------------- 2 files changed, 13 insertions(+), 49 deletions(-) diff --git a/pythonbpf/helper/bpf_helper_handler.py b/pythonbpf/helper/bpf_helper_handler.py index 87c02308..96382f66 100644 --- a/pythonbpf/helper/bpf_helper_handler.py +++ b/pythonbpf/helper/bpf_helper_handler.py @@ -30,7 +30,6 @@ def bpf_ktime_get_ns_emitter( func, local_sym_tab=None, struct_sym_tab=None, - local_var_metadata=None, ): """ Emit LLVM IR for bpf_ktime_get_ns helper function call. @@ -53,7 +52,6 @@ def bpf_map_lookup_elem_emitter( func, local_sym_tab=None, struct_sym_tab=None, - local_var_metadata=None, ): """ Emit LLVM IR for bpf_map_lookup_elem helper function call. @@ -89,7 +87,6 @@ def bpf_printk_emitter( func, local_sym_tab=None, struct_sym_tab=None, - local_var_metadata=None, ): """Emit LLVM IR for bpf_printk helper function call.""" if not hasattr(func, "_fmt_counter"): @@ -107,7 +104,6 @@ def bpf_printk_emitter( func, local_sym_tab, struct_sym_tab, - local_var_metadata, ) elif isinstance(call.args[0], ast.Constant) and isinstance(call.args[0].value, str): # TODO: We are only supporting single arguments for now. @@ -138,7 +134,6 @@ def bpf_map_update_elem_emitter( func, local_sym_tab=None, struct_sym_tab=None, - local_var_metadata=None, ): """ Emit LLVM IR for bpf_map_update_elem helper function call. @@ -190,7 +185,6 @@ def bpf_map_delete_elem_emitter( func, local_sym_tab=None, struct_sym_tab=None, - local_var_metadata=None, ): """ Emit LLVM IR for bpf_map_delete_elem helper function call. @@ -228,7 +222,6 @@ def bpf_get_current_pid_tgid_emitter( func, local_sym_tab=None, struct_sym_tab=None, - local_var_metadata=None, ): """ Emit LLVM IR for bpf_get_current_pid_tgid helper function call. @@ -255,7 +248,6 @@ def bpf_perf_event_output_handler( func, local_sym_tab=None, struct_sym_tab=None, - local_var_metadata=None, ): if len(call.args) != 1: raise ValueError( @@ -264,9 +256,7 @@ def bpf_perf_event_output_handler( data_arg = call.args[0] ctx_ptr = func.args[0] # First argument to the function is ctx - data_ptr, size_val = get_data_ptr_and_size( - data_arg, local_sym_tab, struct_sym_tab, local_var_metadata - ) + data_ptr, size_val = get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab) # BPF_F_CURRENT_CPU is -1 in 32 bit flags_val = ir.Constant(ir.IntType(64), 0xFFFFFFFF) @@ -304,7 +294,6 @@ def handle_helper_call( local_sym_tab=None, map_sym_tab=None, struct_sym_tab=None, - local_var_metadata=None, ): """Process a BPF helper function call and emit the appropriate LLVM IR.""" @@ -323,7 +312,6 @@ def invoke_helper(method_name, map_ptr=None): func, local_sym_tab, struct_sym_tab, - local_var_metadata, ) # Handle direct function calls (e.g., print(), ktime()) diff --git a/pythonbpf/helper/helper_utils.py b/pythonbpf/helper/helper_utils.py index 8e658f00..4ff5562b 100644 --- a/pythonbpf/helper/helper_utils.py +++ b/pythonbpf/helper/helper_utils.py @@ -100,7 +100,6 @@ def handle_fstring_print( func, local_sym_tab=None, struct_sym_tab=None, - local_var_metadata=None, ): """Handle f-string formatting for bpf_printk emitter.""" fmt_parts = [] @@ -118,7 +117,6 @@ def handle_fstring_print( exprs, local_sym_tab, struct_sym_tab, - local_var_metadata, ) else: raise NotImplementedError(f"Unsupported f-string value type: {type(value)}") @@ -138,7 +136,6 @@ def handle_fstring_print( builder, local_sym_tab, struct_sym_tab, - local_var_metadata, ) args.append(arg_value) @@ -158,9 +155,7 @@ def _process_constant_in_fstring(cst, fmt_parts, exprs): ) -def _process_fval( - fval, fmt_parts, exprs, local_sym_tab, struct_sym_tab, local_var_metadata -): +def _process_fval(fval, fmt_parts, exprs, local_sym_tab, struct_sym_tab): """Process formatted values in f-string.""" logger.debug(f"Processing formatted value: {ast.dump(fval)}") @@ -173,7 +168,6 @@ def _process_fval( exprs, local_sym_tab, struct_sym_tab, - local_var_metadata, ) else: raise NotImplementedError( @@ -188,9 +182,7 @@ def _process_name_in_fval(name_node, fmt_parts, exprs, local_sym_tab): _populate_fval(var_type, name_node, fmt_parts, exprs) -def _process_attr_in_fval( - attr_node, fmt_parts, exprs, local_sym_tab, struct_sym_tab, local_var_metadata -): +def _process_attr_in_fval(attr_node, fmt_parts, exprs, local_sym_tab, struct_sym_tab): """Process attribute nodes in formatted values.""" if ( isinstance(attr_node.value, ast.Name) @@ -200,12 +192,7 @@ def _process_attr_in_fval( var_name = attr_node.value.id field_name = attr_node.attr - if not local_var_metadata or var_name not in local_var_metadata: - raise ValueError( - f"Metadata for '{var_name}' not found in local var metadata" - ) - - var_type = local_var_metadata[var_name] + var_type = local_sym_tab[var_name].metadata if var_type not in struct_sym_tab: raise ValueError( f"Struct '{var_type}' for '{var_name}' not in symbol table" @@ -263,9 +250,7 @@ def _create_format_string_global(fmt_str, func, module, builder): return builder.bitcast(fmt_gvar, ir.PointerType()) -def _prepare_expr_args( - expr, func, module, builder, local_sym_tab, struct_sym_tab, local_var_metadata -): +def _prepare_expr_args(expr, func, module, builder, local_sym_tab, struct_sym_tab): """Evaluate and prepare an expression to use as an arg for bpf_printk.""" val, _ = eval_expr( func, @@ -275,7 +260,6 @@ def _prepare_expr_args( local_sym_tab, None, struct_sym_tab, - local_var_metadata, ) if val: @@ -298,7 +282,7 @@ def _prepare_expr_args( return ir.Constant(ir.IntType(64), 0) -def get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab, local_var_metadata): +def get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab): """Extract data pointer and size information for perf event output.""" if isinstance(data_arg, ast.Name): data_name = data_arg.id @@ -310,22 +294,14 @@ def get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab, local_var_met ) # Check if data_name is a struct - if local_var_metadata and data_name in local_var_metadata: - data_type = local_var_metadata[data_name] - if data_type in struct_sym_tab: - struct_info = struct_sym_tab[data_type] - size_val = ir.Constant(ir.IntType(64), struct_info.size) - return data_ptr, size_val - else: - raise ValueError( - f"Struct {data_type} for {data_name} not in symbol table." - ) + data_type = local_sym_tab[data_name].metadata + if data_type in struct_sym_tab: + struct_info = struct_sym_tab[data_type] + size_val = ir.Constant(ir.IntType(64), struct_info.size) + return data_ptr, size_val else: - raise ValueError( - f"Metadata for variable {data_name} " - "not found in local variable metadata." - ) + raise ValueError(f"Struct {data_type} for {data_name} not in symbol table.") else: raise NotImplementedError( - "Only simple object names are supported " "as data in perf event output." + "Only simple object names are supported as data in perf event output." ) From 0142381ce2b475ab70417e0664bedd45a09dcd28 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 2 Oct 2025 04:44:14 +0530 Subject: [PATCH 05/10] Remove local_var_metadata from expr_pass --- pythonbpf/expr_pass.py | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/pythonbpf/expr_pass.py b/pythonbpf/expr_pass.py index f81ba89c..0f753966 100644 --- a/pythonbpf/expr_pass.py +++ b/pythonbpf/expr_pass.py @@ -10,10 +10,8 @@ def eval_expr( local_sym_tab, map_sym_tab, structs_sym_tab=None, - local_var_metadata=None, ): print(f"Evaluating expression: {ast.dump(expr)}") - print(local_var_metadata) if isinstance(expr, ast.Name): if expr.id in local_sym_tab: var = local_sym_tab[expr.id][0] @@ -72,7 +70,6 @@ def eval_expr( local_sym_tab, map_sym_tab, structs_sym_tab, - local_var_metadata, ) elif isinstance(expr.func, ast.Attribute): print(f"Handling method call: {ast.dump(expr.func)}") @@ -89,7 +86,6 @@ def eval_expr( local_sym_tab, map_sym_tab, structs_sym_tab, - local_var_metadata, ) elif isinstance(expr.func.value, ast.Name): obj_name = expr.func.value.id @@ -104,7 +100,6 @@ def eval_expr( local_sym_tab, map_sym_tab, structs_sym_tab, - local_var_metadata, ) elif isinstance(expr, ast.Attribute): if isinstance(expr.value, ast.Name): @@ -114,14 +109,12 @@ def eval_expr( var_ptr, var_type = local_sym_tab[var_name] print(f"Loading attribute " f"{attr_name} from variable {var_name}") print(f"Variable type: {var_type}, Variable ptr: {var_ptr}") - print(local_var_metadata) - if local_var_metadata and var_name in local_var_metadata: - metadata = structs_sym_tab[local_var_metadata[var_name]] - if attr_name in metadata.fields: - gep = metadata.gep(builder, var_ptr, attr_name) - val = builder.load(gep) - field_type = metadata.field_type(attr_name) - return val, field_type + metadata = structs_sym_tab[local_sym_tab[var_name].metadata] + if attr_name in metadata.fields: + gep = metadata.gep(builder, var_ptr, attr_name) + val = builder.load(gep) + field_type = metadata.field_type(attr_name) + return val, field_type print("Unsupported expression evaluation") return None @@ -134,11 +127,9 @@ def handle_expr( local_sym_tab, map_sym_tab, structs_sym_tab, - local_var_metadata, ): """Handle expression statements in the function body.""" print(f"Handling expression: {ast.dump(expr)}") - print(local_var_metadata) call = expr.value if isinstance(call, ast.Call): eval_expr( @@ -149,7 +140,6 @@ def handle_expr( local_sym_tab, map_sym_tab, structs_sym_tab, - local_var_metadata, ) else: print("Unsupported expression type") From d7427f306f40eb5247f030efac1d7587c5bc7375 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 2 Oct 2025 04:50:31 +0530 Subject: [PATCH 06/10] Fix usage of local_sym_tab in expr_pass --- pythonbpf/expr_pass.py | 14 +++++++------- pythonbpf/functions_pass.py | 3 +-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/pythonbpf/expr_pass.py b/pythonbpf/expr_pass.py index 0f753966..d506f8a7 100644 --- a/pythonbpf/expr_pass.py +++ b/pythonbpf/expr_pass.py @@ -14,9 +14,9 @@ def eval_expr( print(f"Evaluating expression: {ast.dump(expr)}") if isinstance(expr, ast.Name): if expr.id in local_sym_tab: - var = local_sym_tab[expr.id][0] + var = local_sym_tab[expr.id].var val = builder.load(var) - return val, local_sym_tab[expr.id][1] # return value and type + return val, local_sym_tab[expr.id].ir_type # return value and type else: print(f"Undefined variable {expr.id}") return None @@ -49,7 +49,7 @@ def eval_expr( return None if isinstance(arg, ast.Name): if arg.id in local_sym_tab: - arg = local_sym_tab[arg.id][0] + arg = local_sym_tab[arg.id].var else: print(f"Undefined variable {arg.id}") return None @@ -58,7 +58,7 @@ def eval_expr( return None # Since we are handling only name case, directly take type from sym tab val = builder.load(arg) - return val, local_sym_tab[expr.args[0].id][1] + return val, local_sym_tab[expr.args[0].id].ir_type # check for helpers if HelperHandlerRegistry.has_handler(expr.func.id): @@ -106,10 +106,10 @@ def eval_expr( var_name = expr.value.id attr_name = expr.attr if var_name in local_sym_tab: - var_ptr, var_type = local_sym_tab[var_name] - print(f"Loading attribute " f"{attr_name} from variable {var_name}") + var_ptr, var_type, var_metadata = local_sym_tab[var_name] + print(f"Loading attribute {attr_name} from variable {var_name}") print(f"Variable type: {var_type}, Variable ptr: {var_ptr}") - metadata = structs_sym_tab[local_sym_tab[var_name].metadata] + metadata = structs_sym_tab[var_metadata] if attr_name in metadata.fields: gep = metadata.gep(builder, var_ptr, attr_name) val = builder.load(gep) diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index 7e871710..3ba9ed7d 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -61,7 +61,7 @@ def handle_assign( struct_info = structs_sym_tab[struct_type] if field_name in struct_info.fields: field_ptr = struct_info.gep( - builder, local_sym_tab[var_name][0], field_name + builder, local_sym_tab[var_name].var, field_name ) val = eval_expr( func, @@ -106,7 +106,6 @@ def handle_assign( builder.store( ir.Constant(ir.IntType(64), rval.value), local_sym_tab[var_name].var ) - # local_sym_tab[var_name] = var print(f"Assigned constant {rval.value} to {var_name}") elif isinstance(rval.value, str): str_val = rval.value.encode("utf-8") + b"\x00" From 12ba3605e9f58f988ede3f800db3b76071e64490 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 2 Oct 2025 04:53:04 +0530 Subject: [PATCH 07/10] Fix local_sym_tab usage in helpers --- pythonbpf/helper/helper_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pythonbpf/helper/helper_utils.py b/pythonbpf/helper/helper_utils.py index 4ff5562b..d06a72bd 100644 --- a/pythonbpf/helper/helper_utils.py +++ b/pythonbpf/helper/helper_utils.py @@ -37,7 +37,7 @@ def has_handler(cls, helper_name): def get_var_ptr_from_name(var_name, local_sym_tab): """Get a pointer to a variable from the symbol table.""" if local_sym_tab and var_name in local_sym_tab: - return local_sym_tab[var_name][0] + return local_sym_tab[var_name].var raise ValueError(f"Variable '{var_name}' not found in local symbol table") @@ -72,7 +72,7 @@ def get_flags_val(arg, builder, local_sym_tab): if isinstance(arg, ast.Name): if local_sym_tab and arg.id in local_sym_tab: - flags_ptr = local_sym_tab[arg.id][0] + flags_ptr = local_sym_tab[arg.id].var return builder.load(flags_ptr) else: raise ValueError(f"Variable '{arg.id}' not found in local symbol table") @@ -178,7 +178,7 @@ def _process_fval(fval, fmt_parts, exprs, local_sym_tab, struct_sym_tab): def _process_name_in_fval(name_node, fmt_parts, exprs, local_sym_tab): """Process name nodes in formatted values.""" if local_sym_tab and name_node.id in local_sym_tab: - _, var_type = local_sym_tab[name_node.id] + _, var_type, tmp = local_sym_tab[name_node.id] _populate_fval(var_type, name_node, fmt_parts, exprs) @@ -287,7 +287,7 @@ def get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab): if isinstance(data_arg, ast.Name): data_name = data_arg.id if local_sym_tab and data_name in local_sym_tab: - data_ptr = local_sym_tab[data_name][0] + data_ptr = local_sym_tab[data_name].var else: raise ValueError( f"Data variable {data_name} not found in local symbol table." From 71b97e3e209239bb3a7ccaf82abacbf90f4f32ff Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 2 Oct 2025 04:55:37 +0530 Subject: [PATCH 08/10] Add iter to LocalSymbol --- pythonbpf/functions_pass.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index 3ba9ed7d..7b3a95d6 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -18,6 +18,11 @@ class LocalSymbol: ir_type: ir.Type metadata: Any = None + def __iter__(self): + yield self.var + yield self.ir_type + yield self.metadata + def get_probe_string(func_node): """Extract the probe string from the decorator of the function node.""" @@ -669,8 +674,7 @@ def _expr_type(e): if found_type is None: found_type = t elif found_type != t: - raise ValueError(f"Conflicting return types:{ - found_type} vs {t}") + raise ValueError(f"Conflicting return types:{found_type} vs {t}") return found_type or "None" From 80c3519b954bdbf7d824eb2f2ab8c67497f96e16 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 2 Oct 2025 04:58:39 +0530 Subject: [PATCH 09/10] Fix local_sym_tab usage in binary_ops --- pythonbpf/binary_ops.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pythonbpf/binary_ops.py b/pythonbpf/binary_ops.py index e77938d5..9138f957 100644 --- a/pythonbpf/binary_ops.py +++ b/pythonbpf/binary_ops.py @@ -25,7 +25,7 @@ def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab # Handle left operand if isinstance(left, ast.Name): if left.id in local_sym_tab: - left = recursive_dereferencer(local_sym_tab[left.id][0], builder) + left = recursive_dereferencer(local_sym_tab[left.id].var, builder) else: raise SyntaxError(f"Undefined variable: {left.id}") elif isinstance(left, ast.Constant): @@ -35,7 +35,7 @@ def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab if isinstance(right, ast.Name): if right.id in local_sym_tab: - right = recursive_dereferencer(local_sym_tab[right.id][0], builder) + right = recursive_dereferencer(local_sym_tab[right.id].var, builder) else: raise SyntaxError(f"Undefined variable: {right.id}") elif isinstance(right, ast.Constant): @@ -46,26 +46,26 @@ def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab print(f"left is {left}, right is {right}, op is {op}") if isinstance(op, ast.Add): - builder.store(builder.add(left, right), local_sym_tab[var_name][0]) + builder.store(builder.add(left, right), local_sym_tab[var_name].var) elif isinstance(op, ast.Sub): - builder.store(builder.sub(left, right), local_sym_tab[var_name][0]) + builder.store(builder.sub(left, right), local_sym_tab[var_name].var) elif isinstance(op, ast.Mult): - builder.store(builder.mul(left, right), local_sym_tab[var_name][0]) + builder.store(builder.mul(left, right), local_sym_tab[var_name].var) elif isinstance(op, ast.Div): - builder.store(builder.sdiv(left, right), local_sym_tab[var_name][0]) + builder.store(builder.sdiv(left, right), local_sym_tab[var_name].var) elif isinstance(op, ast.Mod): - builder.store(builder.srem(left, right), local_sym_tab[var_name][0]) + builder.store(builder.srem(left, right), local_sym_tab[var_name].var) elif isinstance(op, ast.LShift): - builder.store(builder.shl(left, right), local_sym_tab[var_name][0]) + builder.store(builder.shl(left, right), local_sym_tab[var_name].var) elif isinstance(op, ast.RShift): - builder.store(builder.lshr(left, right), local_sym_tab[var_name][0]) + builder.store(builder.lshr(left, right), local_sym_tab[var_name].var) elif isinstance(op, ast.BitOr): - builder.store(builder.or_(left, right), local_sym_tab[var_name][0]) + builder.store(builder.or_(left, right), local_sym_tab[var_name].var) elif isinstance(op, ast.BitXor): - builder.store(builder.xor(left, right), local_sym_tab[var_name][0]) + builder.store(builder.xor(left, right), local_sym_tab[var_name].var) elif isinstance(op, ast.BitAnd): - builder.store(builder.and_(left, right), local_sym_tab[var_name][0]) + builder.store(builder.and_(left, right), local_sym_tab[var_name].var) elif isinstance(op, ast.FloorDiv): - builder.store(builder.udiv(left, right), local_sym_tab[var_name][0]) + builder.store(builder.udiv(left, right), local_sym_tab[var_name].var) else: raise SyntaxError("Unsupported binary operation") From 7bc711c296835f9757061a054ee06c33ebe426a2 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi <76248539+r41k0u@users.noreply.github.com> Date: Thu, 2 Oct 2025 05:01:32 +0530 Subject: [PATCH 10/10] Update pythonbpf/functions_pass.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- pythonbpf/functions_pass.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index 7b3a95d6..fc3ddc31 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -674,7 +674,7 @@ def _expr_type(e): if found_type is None: found_type = t elif found_type != t: - raise ValueError(f"Conflicting return types:{found_type} vs {t}") + raise ValueError(f"Conflicting return types: {found_type} vs {t}") return found_type or "None"