Skip to content

Commit d992493

Browse files
Core: Let probe_read_kernel read into a scalar, from a register address
A non-char-array destination is sized from its pointee, and a 64-bit integer source (a pt_regs field) is the address to read from, as with (void *)ctx->si in C. Also export probe_read_kernel from pythonbpf.helper; it had an emitter but no importable name.
1 parent 107f035 commit d992493

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎pythonbpf/helper/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
deref,
1212
comm,
1313
probe_read_str,
14+
probe_read_kernel,
1415
random,
1516
probe_read,
1617
smp_processor_id,
@@ -78,6 +79,7 @@ def helper_call_handler(call, compilation_context, builder, func, local_sym_tab)
7879
"deref",
7980
"comm",
8081
"probe_read_str",
82+
"probe_read_kernel",
8183
"random",
8284
"probe_read",
8385
"smp_processor_id",

‎pythonbpf/helper/bpf_helper_handler.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,21 @@ def bpf_probe_read_kernel_emitter(
651651
f"probe_read_kernel expects 2 args (dst, src), got {len(call.args)}"
652652
)
653653

654-
# Get destination buffer (char array -> i8*)
655-
dst_ptr, dst_size = get_or_create_ptr_from_arg(
654+
# Destination: a char array reads its length, a scalar reads its width
655+
dst = get_or_create_ptr_from_arg(
656656
func, compilation_context, call.args[0], builder, local_sym_tab
657657
)
658+
if isinstance(dst, tuple):
659+
dst_ptr, dst_size = dst
660+
else:
661+
dst_ptr = dst
662+
pointee = dst_ptr.type.pointee
663+
if not isinstance(pointee, ir.IntType):
664+
raise ValueError(
665+
f"probe_read_kernel destination must be a char array or an "
666+
f"integer, got {pointee}"
667+
)
668+
dst_size = pointee.width // 8
658669

659670
# Get source pointer (evaluate expression)
660671
src_ptr, src_type = get_ptr_from_arg(

‎pythonbpf/helper/helper_utils.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ def get_ptr_from_arg(arg, func, compilation_context, builder, local_sym_tab):
341341

342342
val, val_type = result
343343

344+
# An address held in a 64-bit integer, such as a pt_regs register field:
345+
# the C spelling is (void *)ctx->si
346+
if isinstance(val.type, ir.IntType) and val.type.width == 64:
347+
ptr_type = ir.PointerType(ir.IntType(8))
348+
return builder.inttoptr(val, ptr_type), ptr_type
349+
344350
if not isinstance(val_type, ir.PointerType):
345351
raise ValueError(f"Expected pointer type, got {val_type}")
346352

‎pythonbpf/helper/helpers.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def probe_read_str(dst, src):
2727
return ctypes.c_int64(0)
2828

2929

30+
def probe_read_kernel(dst, src):
31+
"""Safely read sizeof(dst) bytes from kernel memory"""
32+
return ctypes.c_int64(0)
33+
34+
3035
def random():
3136
"""get a pseudorandom u32 number"""
3237
return ctypes.c_int32(0)

0 commit comments

Comments
 (0)