From ec2ea835e5a69742c09294d0d262b136b2bfefcf Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sat, 1 Nov 2025 13:50:23 +0530 Subject: [PATCH] Fix imports and type issues for bpf_probe_read --- pythonbpf/helper/__init__.py | 13 +++++++++++- pythonbpf/helper/bpf_helper_handler.py | 29 ++++++++++++-------------- pythonbpf/helper/helpers.py | 5 +++++ 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/pythonbpf/helper/__init__.py b/pythonbpf/helper/__init__.py index 5c38137..1bfa7ac 100644 --- a/pythonbpf/helper/__init__.py +++ b/pythonbpf/helper/__init__.py @@ -1,7 +1,17 @@ from .helper_registry import HelperHandlerRegistry from .helper_utils import reset_scratch_pool from .bpf_helper_handler import handle_helper_call, emit_probe_read_kernel_str_call -from .helpers import ktime, pid, deref, comm, probe_read_str, random, XDP_DROP, XDP_PASS +from .helpers import ( + ktime, + pid, + deref, + comm, + probe_read_str, + random, + probe_read, + XDP_DROP, + XDP_PASS, +) # Register the helper handler with expr module @@ -66,6 +76,7 @@ __all__ = [ "comm", "probe_read_str", "random", + "probe_read", "XDP_DROP", "XDP_PASS", ] diff --git a/pythonbpf/helper/bpf_helper_handler.py b/pythonbpf/helper/bpf_helper_handler.py index 362bb41..420c1c5 100644 --- a/pythonbpf/helper/bpf_helper_handler.py +++ b/pythonbpf/helper/bpf_helper_handler.py @@ -476,23 +476,20 @@ def bpf_probe_read_emitter( if len(call.args) != 3: logger.warn("Expected 3 args for probe_read helper") return - dst_ptr, _ = get_ptr_from_arg( - call.args[0], func, module, builder, local_sym_tab, map_sym_tab, struct_sym_tab + dst_ptr = get_or_create_ptr_from_arg( + func, module, call.args[0], builder, local_sym_tab, map_sym_tab, struct_sym_tab ) - size_val = ( - get_int_value_from_arg( - call.args[1], - func, - module, - builder, - local_sym_tab, - map_sym_tab, - struct_sym_tab, - ) - & 0xFFFFFFFF + size_val = get_int_value_from_arg( + call.args[1], + func, + module, + builder, + local_sym_tab, + map_sym_tab, + struct_sym_tab, ) - src_ptr, _ = get_ptr_from_arg( - call.args[2], func, module, builder, local_sym_tab, map_sym_tab, struct_sym_tab + src_ptr = get_or_create_ptr_from_arg( + func, module, call.args[2], builder, local_sym_tab, map_sym_tab, struct_sym_tab ) fn_type = ir.FunctionType( ir.IntType(64), @@ -507,7 +504,7 @@ def bpf_probe_read_emitter( fn_ptr, [ builder.bitcast(dst_ptr, ir.PointerType()), - ir.Constant(ir.IntType(32), size_val), + builder.trunc(size_val, ir.IntType(32)), builder.bitcast(src_ptr, ir.PointerType()), ], tail=False, diff --git a/pythonbpf/helper/helpers.py b/pythonbpf/helper/helpers.py index f8f2bd6..440d311 100644 --- a/pythonbpf/helper/helpers.py +++ b/pythonbpf/helper/helpers.py @@ -32,6 +32,11 @@ def random(): return ctypes.c_int32(0) +def probe_read(dst, size, src): + """Safely read data from kernel memory""" + return ctypes.c_int64(0) + + XDP_ABORTED = ctypes.c_int64(0) XDP_DROP = ctypes.c_int64(1) XDP_PASS = ctypes.c_int64(2)