Skip to content

Commit ec2ea83

Browse files
committed
Fix imports and type issues for bpf_probe_read
1 parent 2257c17 commit ec2ea83

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

pythonbpf/helper/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
from .helper_registry import HelperHandlerRegistry
22
from .helper_utils import reset_scratch_pool
33
from .bpf_helper_handler import handle_helper_call, emit_probe_read_kernel_str_call
4-
from .helpers import ktime, pid, deref, comm, probe_read_str, random, XDP_DROP, XDP_PASS
4+
from .helpers import (
5+
ktime,
6+
pid,
7+
deref,
8+
comm,
9+
probe_read_str,
10+
random,
11+
probe_read,
12+
XDP_DROP,
13+
XDP_PASS,
14+
)
515

616

717
# Register the helper handler with expr module
@@ -66,6 +76,7 @@ def helper_call_handler(
6676
"comm",
6777
"probe_read_str",
6878
"random",
79+
"probe_read",
6980
"XDP_DROP",
7081
"XDP_PASS",
7182
]

pythonbpf/helper/bpf_helper_handler.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -476,23 +476,20 @@ def bpf_probe_read_emitter(
476476
if len(call.args) != 3:
477477
logger.warn("Expected 3 args for probe_read helper")
478478
return
479-
dst_ptr, _ = get_ptr_from_arg(
480-
call.args[0], func, module, builder, local_sym_tab, map_sym_tab, struct_sym_tab
479+
dst_ptr = get_or_create_ptr_from_arg(
480+
func, module, call.args[0], builder, local_sym_tab, map_sym_tab, struct_sym_tab
481481
)
482-
size_val = (
483-
get_int_value_from_arg(
484-
call.args[1],
485-
func,
486-
module,
487-
builder,
488-
local_sym_tab,
489-
map_sym_tab,
490-
struct_sym_tab,
491-
)
492-
& 0xFFFFFFFF
482+
size_val = get_int_value_from_arg(
483+
call.args[1],
484+
func,
485+
module,
486+
builder,
487+
local_sym_tab,
488+
map_sym_tab,
489+
struct_sym_tab,
493490
)
494-
src_ptr, _ = get_ptr_from_arg(
495-
call.args[2], func, module, builder, local_sym_tab, map_sym_tab, struct_sym_tab
491+
src_ptr = get_or_create_ptr_from_arg(
492+
func, module, call.args[2], builder, local_sym_tab, map_sym_tab, struct_sym_tab
496493
)
497494
fn_type = ir.FunctionType(
498495
ir.IntType(64),
@@ -507,7 +504,7 @@ def bpf_probe_read_emitter(
507504
fn_ptr,
508505
[
509506
builder.bitcast(dst_ptr, ir.PointerType()),
510-
ir.Constant(ir.IntType(32), size_val),
507+
builder.trunc(size_val, ir.IntType(32)),
511508
builder.bitcast(src_ptr, ir.PointerType()),
512509
],
513510
tail=False,

pythonbpf/helper/helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def random():
3232
return ctypes.c_int32(0)
3333

3434

35+
def probe_read(dst, size, src):
36+
"""Safely read data from kernel memory"""
37+
return ctypes.c_int64(0)
38+
39+
3540
XDP_ABORTED = ctypes.c_int64(0)
3641
XDP_DROP = ctypes.c_int64(1)
3742
XDP_PASS = ctypes.c_int64(2)

0 commit comments

Comments
 (0)