From ee03ac04d08f244c0ed2d8630dddb5b0c4a76ed3 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 26 Sep 2025 01:02:10 +0530 Subject: [PATCH] Fix printk handler to comply with new symtab convention --- pythonbpf/bpf_helper_handler.py | 4 ++-- pythonbpf/codegen.py | 4 +++- pythonbpf/expr_pass.py | 2 +- pythonbpf/functions_pass.py | 9 ++++----- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pythonbpf/bpf_helper_handler.py b/pythonbpf/bpf_helper_handler.py index c6c6947..ae4f97b 100644 --- a/pythonbpf/bpf_helper_handler.py +++ b/pythonbpf/bpf_helper_handler.py @@ -121,7 +121,8 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, "Warning: bpf_printk supports up to 3 arguments, extra arguments will be ignored.") for expr in exprs[:3]: - val = eval_expr(func, module, builder, expr, local_sym_tab, None) + val, _ = eval_expr(func, module, builder, + expr, local_sym_tab, None) if val: if isinstance(val.type, ir.PointerType): val = builder.ptrtoint(val, ir.IntType(64)) @@ -137,7 +138,6 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, print( "Warning: Failed to evaluate expression for bpf_printk argument. It will be converted to 0.") args.append(ir.Constant(ir.IntType(64), 0)) - fn_type = ir.FunctionType(ir.IntType( 64), [ir.PointerType(), ir.IntType(32)], var_arg=True) fn_ptr_type = ir.PointerType(fn_type) diff --git a/pythonbpf/codegen.py b/pythonbpf/codegen.py index df30cde..52a8044 100644 --- a/pythonbpf/codegen.py +++ b/pythonbpf/codegen.py @@ -93,6 +93,7 @@ def compile_to_ir(filename: str, output: str): module.add_named_metadata("llvm.ident", ["llvmlite PythonBPF v0.0.1"]) + print(f"IR written to {output}") with open(output, "w") as f: f.write(f"source_filename = \"{filename}\"\n") f.write(str(module)) @@ -118,6 +119,7 @@ def compile(): print(f"Object written to {o_file}, {ll_file} can be removed") + def BPF() -> BpfProgram: caller_frame = inspect.stack()[1] caller_file = Path(caller_frame.filename).resolve() @@ -129,5 +131,5 @@ def BPF() -> BpfProgram: "llc", "-march=bpf", "-filetype=obj", "-O2", str(ll_file), "-o", str(o_file) ], check=True) - + return BpfProgram(str(o_file)) diff --git a/pythonbpf/expr_pass.py b/pythonbpf/expr_pass.py index 479d153..22641bf 100644 --- a/pythonbpf/expr_pass.py +++ b/pythonbpf/expr_pass.py @@ -3,7 +3,7 @@ from llvmlite import ir def eval_expr(func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab=None, local_var_metadata=None): - print(f"Evaluating expression: {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] diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index d7188f2..4b3a17f 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -61,7 +61,7 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc if val is None: print("Failed to evaluate struct field assignment") return - builder.store(val, field_ptr) + builder.store(val[0], field_ptr) print(f"Assigned to struct field {var_name}.{field_name}") return elif isinstance(rval, ast.Constant): @@ -114,7 +114,7 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc # var.align = 8 val = handle_helper_call( rval, module, builder, func, local_sym_tab, map_sym_tab, structs_sym_tab, local_var_metadata) - builder.store(val, local_sym_tab[var_name][0]) + builder.store(val[0], local_sym_tab[var_name][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: @@ -125,7 +125,7 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc print("Failed to evaluate deref argument") return print(f"Dereferenced value: {val}, storing in {var_name}") - builder.store(val, local_sym_tab[var_name][0]) + builder.store(val[0], local_sym_tab[var_name][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: @@ -155,7 +155,7 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc rval, module, builder, func, 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, local_sym_tab[var_name][0]) + builder.store(val[0], local_sym_tab[var_name][0]) # local_sym_tab[var_name] = var else: print("Unsupported assignment call structure") @@ -462,7 +462,6 @@ def process_bpf_chunk(func_node, module, return_type, map_sym_tab, structs_sym_t process_func_body(module, builder, func_node, func, ret_type, map_sym_tab, structs_sym_tab) - return func