Remove get_struct_char_array_ptr in favour of get_char_array_ptr_and_size, wrap it in get_or_crate_ptr_from_arg to use in bpf_helper_handler

This commit is contained in:
Pragyansh Chaturvedi
2025-11-07 18:54:59 +05:30
parent 3ccd3f767e
commit 2f4a7d2f90
3 changed files with 11 additions and 51 deletions

View File

@ -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,
) )
@ -464,7 +463,7 @@ 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 call.args[0], builder, local_sym_tab, struct_sym_tab
) )

View File

@ -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,
@ -148,6 +102,7 @@ def get_or_create_ptr_from_arg(
# 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 # Deafult 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 +133,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 +160,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

View File

@ -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: