From c22911daaff651f1eadfdefbba2ee913d5244004 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sat, 28 Mar 2026 19:02:16 +0530 Subject: [PATCH] Core: FIx inconsistent compilation_context usage in helper/printk_formatter --- pythonbpf/helper/bpf_helper_handler.py | 9 ++++----- pythonbpf/helper/helper_utils.py | 10 +--------- pythonbpf/helper/printk_formatter.py | 14 +++++++------- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/pythonbpf/helper/bpf_helper_handler.py b/pythonbpf/helper/bpf_helper_handler.py index 73ff7a3..f44f05f 100644 --- a/pythonbpf/helper/bpf_helper_handler.py +++ b/pythonbpf/helper/bpf_helper_handler.py @@ -157,17 +157,16 @@ def bpf_printk_emitter( if isinstance(call.args[0], ast.JoinedStr): args = handle_fstring_print( call.args[0], - compilation_context.module, + compilation_context, builder, func, local_sym_tab, - compilation_context.structs_sym_tab, ) elif isinstance(call.args[0], ast.Constant) and isinstance(call.args[0].value, str): # TODO: We are only supporting single arguments for now. # In case of multiple args, the first one will be taken. args = simple_string_print( - call.args[0].value, compilation_context.module, builder, func + call.args[0].value, compilation_context, builder, func ) else: raise NotImplementedError( @@ -397,7 +396,7 @@ def bpf_perf_event_output_handler( 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, compilation_context.structs_sym_tab + data_arg, local_sym_tab, compilation_context ) # BPF_F_CURRENT_CPU is -1 in 32 bit @@ -446,7 +445,7 @@ def bpf_ringbuf_output_emitter( ) data_arg = call.args[0] data_ptr, size_val = get_data_ptr_and_size( - data_arg, local_sym_tab, compilation_context.structs_sym_tab + data_arg, local_sym_tab, compilation_context ) flags_val = ir.Constant(ir.IntType(64), 0) diff --git a/pythonbpf/helper/helper_utils.py b/pythonbpf/helper/helper_utils.py index fa26a2b..6659fae 100644 --- a/pythonbpf/helper/helper_utils.py +++ b/pythonbpf/helper/helper_utils.py @@ -29,15 +29,7 @@ def get_ptr_from_arg(arg, compilation_context, builder, local_sym_tab): return builder.load(sym.var) # Use eval_expr for general case - val = eval_expr( - None, - compilation_context.module, - builder, - arg, - local_sym_tab, - compilation_context.map_sym_tab, - compilation_context.structs_sym_tab, - ) + val = eval_expr(None, compilation_context, builder, arg, local_sym_tab) if val and isinstance(val[0].type, ir.PointerType): return val[0] diff --git a/pythonbpf/helper/printk_formatter.py b/pythonbpf/helper/printk_formatter.py index 4364166..4530662 100644 --- a/pythonbpf/helper/printk_formatter.py +++ b/pythonbpf/helper/printk_formatter.py @@ -9,8 +9,9 @@ from pythonbpf.helper.helper_utils import get_char_array_ptr_and_size logger = logging.getLogger(__name__) -def simple_string_print(string_value, module, builder, func): +def simple_string_print(string_value, compilation_context, builder, func): """Prepare arguments for bpf_printk from a simple string value""" + module = compilation_context.module fmt_str = string_value + "\n\0" fmt_ptr = _create_format_string_global(fmt_str, func, module, builder) @@ -20,11 +21,10 @@ def simple_string_print(string_value, module, builder, func): def handle_fstring_print( joined_str, - module, + compilation_context, builder, func, local_sym_tab=None, - struct_sym_tab=None, ): """Handle f-string formatting for bpf_printk emitter.""" fmt_parts = [] @@ -41,13 +41,13 @@ def handle_fstring_print( fmt_parts, exprs, local_sym_tab, - struct_sym_tab, + compilation_context.struct_sym_tab, ) else: raise NotImplementedError(f"Unsupported f-string value type: {type(value)}") fmt_str = "".join(fmt_parts) - args = simple_string_print(fmt_str, module, builder, func) + args = simple_string_print(fmt_str, compilation_context, builder, func) # NOTE: Process expressions (limited to 3 due to BPF constraints) if len(exprs) > 3: @@ -57,10 +57,10 @@ def handle_fstring_print( arg_value = _prepare_expr_args( expr, func, - module, + compilation_context.module, builder, local_sym_tab, - struct_sym_tab, + compilation_context.struct_sym_tab, ) args.append(arg_value)