From a1fe2ed4bcf130499d24d57143d984840a499102 Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Sun, 26 Oct 2025 15:00:53 +0530 Subject: [PATCH] change to 64 bit pointers. May be an issue. revert this commit if issues arise --- pythonbpf/codegen.py | 3 +-- pythonbpf/functions/functions_pass.py | 2 +- .../vmlinux_parser/vmlinux_exports_handler.py | 23 ++++++++++--------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pythonbpf/codegen.py b/pythonbpf/codegen.py index 919236e..61bb5fe 100644 --- a/pythonbpf/codegen.py +++ b/pythonbpf/codegen.py @@ -39,7 +39,7 @@ def finalize_module(original_str): def bpf_passthrough_gen(module): i32_ty = ir.IntType(32) - ptr_ty = ir.PointerType(ir.IntType(8)) + ptr_ty = ir.PointerType(ir.IntType(64)) fnty = ir.FunctionType(ptr_ty, [i32_ty, ptr_ty]) # Declare the intrinsic @@ -158,7 +158,6 @@ def compile_to_ir(filename: str, output: str, loglevel=logging.INFO): module.add_named_metadata("llvm.ident", [f"PythonBPF {VERSION}"]) module_string: str = finalize_module(str(module)) - module_string += '\ndeclare ptr @llvm.preserve.struct.access.index.p0.p0(ptr, i32 immarg, i32 immarg) "nocallback" "nofree" "nosync" "nounwind" "willreturn" "memory(none)"' logger.info(f"IR written to {output}") with open(output, "w") as f: diff --git a/pythonbpf/functions/functions_pass.py b/pythonbpf/functions/functions_pass.py index 3aff061..e2d49fc 100644 --- a/pythonbpf/functions/functions_pass.py +++ b/pythonbpf/functions/functions_pass.py @@ -402,7 +402,7 @@ def process_bpf_chunk(func_node, module, return_type, map_sym_tab, structs_sym_t func.linkage = "dso_local" func.attributes.add("nounwind") func.attributes.add("noinline") - func.attributes.add("optnone") + # func.attributes.add("optnone") if func_node.args.args: # Only look at the first argument for now diff --git a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py index 7f53a15..dc56993 100644 --- a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py +++ b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py @@ -86,7 +86,7 @@ class VmlinuxHandler: globvar_ir, field_data = self.get_field_type( python_type.__name__, field_name ) - builder.function.args[0].type = ir.PointerType(ir.IntType(8)) + builder.function.args[0].type = ir.PointerType(ir.IntType(64)) print(builder.function.args[0]) field_ptr = self.load_ctx_field(builder, builder.function.args[0], globvar_ir) print(field_ptr) @@ -113,18 +113,18 @@ class VmlinuxHandler: offset = builder.load(offset_global) # Ensure ctx_arg is treated as i8* (byte pointer) - i8_type = ir.IntType(8) - i8_ptr_type = ir.PointerType(i8_type) + # i8_type = ir.IntType(8) + i8_ptr_type = ir.PointerType() # Cast ctx_arg to i8* if it isn't already - if str(ctx_arg.type) != str(i8_ptr_type): - ctx_i8_ptr = builder.bitcast(ctx_arg, i8_ptr_type) - else: - ctx_i8_ptr = ctx_arg + # if str(ctx_arg.type) != str(i8_ptr_type): + # ctx_i8_ptr = builder.bitcast(ctx_arg, i8_ptr_type) + # else: + # ctx_i8_ptr = ctx_arg # GEP with explicit type - this is the key fix field_ptr = builder.gep( - ctx_i8_ptr, + ctx_arg, [offset], inbounds=False, ) @@ -138,19 +138,20 @@ class VmlinuxHandler: raise KeyError except (KeyError, AttributeError): passthrough_type = ir.FunctionType( - i8_ptr_type, - [ir.IntType(32), i8_ptr_type] + ir.PointerType(), + [ir.IntType(32), ir.PointerType()], ) passthrough_fn = ir.Function( module, passthrough_type, - name='llvm.bpf.passthrough.p0.p0' + name='llvm.bpf.passthrough.p0.p0', ) # Call passthrough to satisfy BPF verifier verified_ptr = builder.call( passthrough_fn, [ir.Constant(ir.IntType(32), 0), field_ptr], + tail=True ) # Bitcast to i64* (assuming field is 64-bit, adjust if needed)