mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Compare commits
7 Commits
5b36726b7d
...
all_helper
| Author | SHA1 | Date | |
|---|---|---|---|
| cf99b3bb9a | |||
| 6c85b248ce | |||
| b5a3494cc6 | |||
| be62972974 | |||
| 2f4a7d2f90 | |||
| 3ccd3f767e | |||
| 2e37726922 |
@ -8,7 +8,6 @@ from .helper_utils import (
|
|||||||
get_flags_val,
|
get_flags_val,
|
||||||
get_data_ptr_and_size,
|
get_data_ptr_and_size,
|
||||||
get_buffer_ptr_and_size,
|
get_buffer_ptr_and_size,
|
||||||
get_char_array_ptr_and_size,
|
|
||||||
get_ptr_from_arg,
|
get_ptr_from_arg,
|
||||||
get_int_value_from_arg,
|
get_int_value_from_arg,
|
||||||
)
|
)
|
||||||
@ -37,7 +36,11 @@ class BPFHelperID(Enum):
|
|||||||
BPF_PROBE_READ_KERNEL_STR = 115
|
BPF_PROBE_READ_KERNEL_STR = 115
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("ktime")
|
@HelperHandlerRegistry.register(
|
||||||
|
"ktime",
|
||||||
|
param_types=[],
|
||||||
|
return_type=ir.IntType(64),
|
||||||
|
)
|
||||||
def bpf_ktime_get_ns_emitter(
|
def bpf_ktime_get_ns_emitter(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -60,7 +63,11 @@ def bpf_ktime_get_ns_emitter(
|
|||||||
return result, ir.IntType(64)
|
return result, ir.IntType(64)
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("lookup")
|
@HelperHandlerRegistry.register(
|
||||||
|
"lookup",
|
||||||
|
param_types=[ir.PointerType(ir.IntType(64))],
|
||||||
|
return_type=ir.PointerType(ir.IntType(64)),
|
||||||
|
)
|
||||||
def bpf_map_lookup_elem_emitter(
|
def bpf_map_lookup_elem_emitter(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -102,6 +109,7 @@ def bpf_map_lookup_elem_emitter(
|
|||||||
return result, ir.PointerType()
|
return result, ir.PointerType()
|
||||||
|
|
||||||
|
|
||||||
|
# NOTE: This has special handling so we won't reflect the signature here.
|
||||||
@HelperHandlerRegistry.register("print")
|
@HelperHandlerRegistry.register("print")
|
||||||
def bpf_printk_emitter(
|
def bpf_printk_emitter(
|
||||||
call,
|
call,
|
||||||
@ -150,7 +158,15 @@ def bpf_printk_emitter(
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("update")
|
@HelperHandlerRegistry.register(
|
||||||
|
"update",
|
||||||
|
param_types=[
|
||||||
|
ir.PointerType(ir.IntType(64)),
|
||||||
|
ir.PointerType(ir.IntType(64)),
|
||||||
|
ir.IntType(64),
|
||||||
|
],
|
||||||
|
return_type=ir.PointerType(ir.IntType(64)),
|
||||||
|
)
|
||||||
def bpf_map_update_elem_emitter(
|
def bpf_map_update_elem_emitter(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -205,7 +221,11 @@ def bpf_map_update_elem_emitter(
|
|||||||
return result, None
|
return result, None
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("delete")
|
@HelperHandlerRegistry.register(
|
||||||
|
"delete",
|
||||||
|
param_types=[ir.PointerType(ir.IntType(64))],
|
||||||
|
return_type=ir.PointerType(ir.IntType(64)),
|
||||||
|
)
|
||||||
def bpf_map_delete_elem_emitter(
|
def bpf_map_delete_elem_emitter(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -245,7 +265,11 @@ def bpf_map_delete_elem_emitter(
|
|||||||
return result, None
|
return result, None
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("comm")
|
@HelperHandlerRegistry.register(
|
||||||
|
"comm",
|
||||||
|
param_types=[ir.PointerType(ir.IntType(8))],
|
||||||
|
return_type=ir.IntType(64),
|
||||||
|
)
|
||||||
def bpf_get_current_comm_emitter(
|
def bpf_get_current_comm_emitter(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -302,7 +326,11 @@ def bpf_get_current_comm_emitter(
|
|||||||
return result, None
|
return result, None
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("pid")
|
@HelperHandlerRegistry.register(
|
||||||
|
"pid",
|
||||||
|
param_types=[],
|
||||||
|
return_type=ir.IntType(64),
|
||||||
|
)
|
||||||
def bpf_get_current_pid_tgid_emitter(
|
def bpf_get_current_pid_tgid_emitter(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -330,7 +358,11 @@ def bpf_get_current_pid_tgid_emitter(
|
|||||||
return pid, ir.IntType(64)
|
return pid, ir.IntType(64)
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("output")
|
@HelperHandlerRegistry.register(
|
||||||
|
"output",
|
||||||
|
param_types=[ir.PointerType(ir.IntType(8))],
|
||||||
|
return_type=ir.IntType(64),
|
||||||
|
)
|
||||||
def bpf_perf_event_output_handler(
|
def bpf_perf_event_output_handler(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -405,7 +437,14 @@ def emit_probe_read_kernel_str_call(builder, dst_ptr, dst_size, src_ptr):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("probe_read_str")
|
@HelperHandlerRegistry.register(
|
||||||
|
"probe_read_str",
|
||||||
|
param_types=[
|
||||||
|
ir.PointerType(ir.IntType(8)),
|
||||||
|
ir.PointerType(ir.IntType(8)),
|
||||||
|
],
|
||||||
|
return_type=ir.IntType(64),
|
||||||
|
)
|
||||||
def bpf_probe_read_kernel_str_emitter(
|
def bpf_probe_read_kernel_str_emitter(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -424,8 +463,8 @@ def bpf_probe_read_kernel_str_emitter(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Get destination buffer (char array -> i8*)
|
# Get destination buffer (char array -> i8*)
|
||||||
dst_ptr, dst_size = get_char_array_ptr_and_size(
|
dst_ptr, dst_size = get_or_create_ptr_from_arg(
|
||||||
call.args[0], builder, local_sym_tab, struct_sym_tab
|
func, module, call.args[0], builder, local_sym_tab, map_sym_tab, struct_sym_tab
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get source pointer (evaluate expression)
|
# Get source pointer (evaluate expression)
|
||||||
@ -440,7 +479,11 @@ def bpf_probe_read_kernel_str_emitter(
|
|||||||
return result, ir.IntType(64)
|
return result, ir.IntType(64)
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("random")
|
@HelperHandlerRegistry.register(
|
||||||
|
"random",
|
||||||
|
param_types=[],
|
||||||
|
return_type=ir.IntType(32),
|
||||||
|
)
|
||||||
def bpf_get_prandom_u32_emitter(
|
def bpf_get_prandom_u32_emitter(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -462,7 +505,15 @@ def bpf_get_prandom_u32_emitter(
|
|||||||
return result, ir.IntType(32)
|
return result, ir.IntType(32)
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("probe_read")
|
@HelperHandlerRegistry.register(
|
||||||
|
"probe_read",
|
||||||
|
param_types=[
|
||||||
|
ir.PointerType(ir.IntType(8)),
|
||||||
|
ir.IntType(32),
|
||||||
|
ir.PointerType(ir.IntType(8)),
|
||||||
|
],
|
||||||
|
return_type=ir.IntType(64),
|
||||||
|
)
|
||||||
def bpf_probe_read_emitter(
|
def bpf_probe_read_emitter(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -481,7 +532,14 @@ def bpf_probe_read_emitter(
|
|||||||
logger.warn("Expected 3 args for probe_read helper")
|
logger.warn("Expected 3 args for probe_read helper")
|
||||||
return
|
return
|
||||||
dst_ptr = get_or_create_ptr_from_arg(
|
dst_ptr = get_or_create_ptr_from_arg(
|
||||||
func, module, call.args[0], builder, local_sym_tab, map_sym_tab, struct_sym_tab
|
func,
|
||||||
|
module,
|
||||||
|
call.args[0],
|
||||||
|
builder,
|
||||||
|
local_sym_tab,
|
||||||
|
map_sym_tab,
|
||||||
|
struct_sym_tab,
|
||||||
|
ir.IntType(8),
|
||||||
)
|
)
|
||||||
size_val = get_int_value_from_arg(
|
size_val = get_int_value_from_arg(
|
||||||
call.args[1],
|
call.args[1],
|
||||||
@ -493,7 +551,14 @@ def bpf_probe_read_emitter(
|
|||||||
struct_sym_tab,
|
struct_sym_tab,
|
||||||
)
|
)
|
||||||
src_ptr = get_or_create_ptr_from_arg(
|
src_ptr = get_or_create_ptr_from_arg(
|
||||||
func, module, call.args[2], builder, local_sym_tab, map_sym_tab, struct_sym_tab
|
func,
|
||||||
|
module,
|
||||||
|
call.args[2],
|
||||||
|
builder,
|
||||||
|
local_sym_tab,
|
||||||
|
map_sym_tab,
|
||||||
|
struct_sym_tab,
|
||||||
|
ir.IntType(8),
|
||||||
)
|
)
|
||||||
fn_type = ir.FunctionType(
|
fn_type = ir.FunctionType(
|
||||||
ir.IntType(64),
|
ir.IntType(64),
|
||||||
@ -517,7 +582,11 @@ def bpf_probe_read_emitter(
|
|||||||
return result, ir.IntType(64)
|
return result, ir.IntType(64)
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("smp_processor_id")
|
@HelperHandlerRegistry.register(
|
||||||
|
"smp_processor_id",
|
||||||
|
param_types=[],
|
||||||
|
return_type=ir.IntType(32),
|
||||||
|
)
|
||||||
def bpf_get_smp_processor_id_emitter(
|
def bpf_get_smp_processor_id_emitter(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -540,7 +609,11 @@ def bpf_get_smp_processor_id_emitter(
|
|||||||
return result, ir.IntType(32)
|
return result, ir.IntType(32)
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register("uid")
|
@HelperHandlerRegistry.register(
|
||||||
|
"uid",
|
||||||
|
param_types=[],
|
||||||
|
return_type=ir.IntType(64),
|
||||||
|
)
|
||||||
def bpf_get_current_uid_gid_emitter(
|
def bpf_get_current_uid_gid_emitter(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -622,8 +695,8 @@ def bpf_skb_store_bytes_emitter(
|
|||||||
builder,
|
builder,
|
||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
map_sym_tab,
|
map_sym_tab,
|
||||||
args_signature[2],
|
|
||||||
struct_sym_tab,
|
struct_sym_tab,
|
||||||
|
args_signature[2],
|
||||||
)
|
)
|
||||||
len_val = get_int_value_from_arg(
|
len_val = get_int_value_from_arg(
|
||||||
call.args[2],
|
call.args[2],
|
||||||
|
|||||||
@ -18,7 +18,7 @@ class ScratchPoolManager:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def counter(self):
|
def counter(self):
|
||||||
return sum(self._counter.values())
|
return sum(self._counters.values())
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
self._counters.clear()
|
self._counters.clear()
|
||||||
@ -85,52 +85,6 @@ def create_int_constant_ptr(value, builder, local_sym_tab, int_width=64):
|
|||||||
return ptr
|
return ptr
|
||||||
|
|
||||||
|
|
||||||
def get_struct_char_array_ptr(expr, builder, local_sym_tab, struct_sym_tab):
|
|
||||||
"""Get pointer to first element of char array in struct field, or None."""
|
|
||||||
if not (isinstance(expr, ast.Attribute) and isinstance(expr.value, ast.Name)):
|
|
||||||
return None
|
|
||||||
|
|
||||||
var_name = expr.value.id
|
|
||||||
field_name = expr.attr
|
|
||||||
|
|
||||||
# Check if it's a valid struct field
|
|
||||||
if not (
|
|
||||||
local_sym_tab
|
|
||||||
and var_name in local_sym_tab
|
|
||||||
and struct_sym_tab
|
|
||||||
and local_sym_tab[var_name].metadata in struct_sym_tab
|
|
||||||
):
|
|
||||||
return None
|
|
||||||
|
|
||||||
struct_type = local_sym_tab[var_name].metadata
|
|
||||||
struct_info = struct_sym_tab[struct_type]
|
|
||||||
|
|
||||||
if field_name not in struct_info.fields:
|
|
||||||
return None
|
|
||||||
|
|
||||||
field_type = struct_info.field_type(field_name)
|
|
||||||
|
|
||||||
# Check if it's a char array
|
|
||||||
is_char_array = (
|
|
||||||
isinstance(field_type, ir.ArrayType)
|
|
||||||
and isinstance(field_type.element, ir.IntType)
|
|
||||||
and field_type.element.width == 8
|
|
||||||
)
|
|
||||||
|
|
||||||
if not is_char_array:
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Get field pointer and GEP to first element: [N x i8]* -> i8*
|
|
||||||
struct_ptr = local_sym_tab[var_name].var
|
|
||||||
field_ptr = struct_info.gep(builder, struct_ptr, field_name)
|
|
||||||
|
|
||||||
return builder.gep(
|
|
||||||
field_ptr,
|
|
||||||
[ir.Constant(ir.IntType(32), 0), ir.Constant(ir.IntType(32), 0)],
|
|
||||||
inbounds=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_or_create_ptr_from_arg(
|
def get_or_create_ptr_from_arg(
|
||||||
func,
|
func,
|
||||||
module,
|
module,
|
||||||
@ -138,16 +92,18 @@ def get_or_create_ptr_from_arg(
|
|||||||
builder,
|
builder,
|
||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
map_sym_tab,
|
map_sym_tab,
|
||||||
expected_type=None,
|
|
||||||
struct_sym_tab=None,
|
struct_sym_tab=None,
|
||||||
|
expected_type=None,
|
||||||
):
|
):
|
||||||
"""Extract or create pointer from the call arguments."""
|
"""Extract or create pointer from the call arguments."""
|
||||||
|
|
||||||
logger.info(f"Getting pointer from arg: {ast.dump(arg)}")
|
logger.info(f"Getting pointer from arg: {ast.dump(arg)}")
|
||||||
|
sz = None
|
||||||
if isinstance(arg, ast.Name):
|
if isinstance(arg, ast.Name):
|
||||||
# Stack space is already allocated
|
# Stack space is already allocated
|
||||||
ptr = get_var_ptr_from_name(arg.id, local_sym_tab)
|
ptr = get_var_ptr_from_name(arg.id, local_sym_tab)
|
||||||
elif isinstance(arg, ast.Constant) and isinstance(arg.value, int):
|
elif isinstance(arg, ast.Constant) and isinstance(arg.value, int):
|
||||||
|
int_width = 64 # Default to i64
|
||||||
if expected_type and isinstance(expected_type, ir.IntType):
|
if expected_type and isinstance(expected_type, ir.IntType):
|
||||||
int_width = expected_type.width
|
int_width = expected_type.width
|
||||||
ptr = create_int_constant_ptr(arg.value, builder, local_sym_tab, int_width)
|
ptr = create_int_constant_ptr(arg.value, builder, local_sym_tab, int_width)
|
||||||
@ -178,7 +134,9 @@ def get_or_create_ptr_from_arg(
|
|||||||
and isinstance(field_type.element, ir.IntType)
|
and isinstance(field_type.element, ir.IntType)
|
||||||
and field_type.element.width == 8
|
and field_type.element.width == 8
|
||||||
):
|
):
|
||||||
ptr = get_struct_char_array_ptr(arg, builder, local_sym_tab, struct_sym_tab)
|
ptr, sz = get_char_array_ptr_and_size(
|
||||||
|
arg, builder, local_sym_tab, struct_sym_tab
|
||||||
|
)
|
||||||
if not ptr:
|
if not ptr:
|
||||||
raise ValueError("Failed to get char array pointer from struct field")
|
raise ValueError("Failed to get char array pointer from struct field")
|
||||||
else:
|
else:
|
||||||
@ -203,6 +161,10 @@ def get_or_create_ptr_from_arg(
|
|||||||
val = builder.trunc(val, expected_type)
|
val = builder.trunc(val, expected_type)
|
||||||
builder.store(val, ptr)
|
builder.store(val, ptr)
|
||||||
|
|
||||||
|
# NOTE: For char arrays, also return size
|
||||||
|
if sz:
|
||||||
|
return ptr, sz
|
||||||
|
|
||||||
return ptr
|
return ptr
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import logging
|
|||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
from pythonbpf.expr import eval_expr, get_base_type_and_depth, deref_to_depth
|
from pythonbpf.expr import eval_expr, get_base_type_and_depth, deref_to_depth
|
||||||
from pythonbpf.expr.vmlinux_registry import VmlinuxHandlerRegistry
|
from pythonbpf.expr.vmlinux_registry import VmlinuxHandlerRegistry
|
||||||
from pythonbpf.helper.helper_utils import get_struct_char_array_ptr
|
from pythonbpf.helper.helper_utils import get_char_array_ptr_and_size
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -220,7 +220,7 @@ def _prepare_expr_args(expr, func, module, builder, local_sym_tab, struct_sym_ta
|
|||||||
"""Evaluate and prepare an expression to use as an arg for bpf_printk."""
|
"""Evaluate and prepare an expression to use as an arg for bpf_printk."""
|
||||||
|
|
||||||
# Special case: struct field char array needs pointer to first element
|
# Special case: struct field char array needs pointer to first element
|
||||||
char_array_ptr = get_struct_char_array_ptr(
|
char_array_ptr, _ = get_char_array_ptr_and_size(
|
||||||
expr, builder, local_sym_tab, struct_sym_tab
|
expr, builder, local_sym_tab, struct_sym_tab
|
||||||
)
|
)
|
||||||
if char_array_ptr:
|
if char_array_ptr:
|
||||||
|
|||||||
Reference in New Issue
Block a user