Skip to content

Commit 2f4a7d2

Browse files
committed
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
1 parent 3ccd3f7 commit 2f4a7d2

File tree

3 files changed

+11
-51
lines changed

3 files changed

+11
-51
lines changed

pythonbpf/helper/bpf_helper_handler.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
get_flags_val,
99
get_data_ptr_and_size,
1010
get_buffer_ptr_and_size,
11-
get_char_array_ptr_and_size,
1211
get_ptr_from_arg,
1312
get_int_value_from_arg,
1413
)
@@ -464,7 +463,7 @@ def bpf_probe_read_kernel_str_emitter(
464463
)
465464

466465
# Get destination buffer (char array -> i8*)
467-
dst_ptr, dst_size = get_char_array_ptr_and_size(
466+
dst_ptr, dst_size = get_or_create_ptr_from_arg(
468467
call.args[0], builder, local_sym_tab, struct_sym_tab
469468
)
470469

pythonbpf/helper/helper_utils.py

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -85,52 +85,6 @@ def create_int_constant_ptr(value, builder, local_sym_tab, int_width=64):
8585
return ptr
8686

8787

88-
def get_struct_char_array_ptr(expr, builder, local_sym_tab, struct_sym_tab):
89-
"""Get pointer to first element of char array in struct field, or None."""
90-
if not (isinstance(expr, ast.Attribute) and isinstance(expr.value, ast.Name)):
91-
return None
92-
93-
var_name = expr.value.id
94-
field_name = expr.attr
95-
96-
# Check if it's a valid struct field
97-
if not (
98-
local_sym_tab
99-
and var_name in local_sym_tab
100-
and struct_sym_tab
101-
and local_sym_tab[var_name].metadata in struct_sym_tab
102-
):
103-
return None
104-
105-
struct_type = local_sym_tab[var_name].metadata
106-
struct_info = struct_sym_tab[struct_type]
107-
108-
if field_name not in struct_info.fields:
109-
return None
110-
111-
field_type = struct_info.field_type(field_name)
112-
113-
# Check if it's a char array
114-
is_char_array = (
115-
isinstance(field_type, ir.ArrayType)
116-
and isinstance(field_type.element, ir.IntType)
117-
and field_type.element.width == 8
118-
)
119-
120-
if not is_char_array:
121-
return None
122-
123-
# Get field pointer and GEP to first element: [N x i8]* -> i8*
124-
struct_ptr = local_sym_tab[var_name].var
125-
field_ptr = struct_info.gep(builder, struct_ptr, field_name)
126-
127-
return builder.gep(
128-
field_ptr,
129-
[ir.Constant(ir.IntType(32), 0), ir.Constant(ir.IntType(32), 0)],
130-
inbounds=True,
131-
)
132-
133-
13488
def get_or_create_ptr_from_arg(
13589
func,
13690
module,
@@ -148,6 +102,7 @@ def get_or_create_ptr_from_arg(
148102
# Stack space is already allocated
149103
ptr = get_var_ptr_from_name(arg.id, local_sym_tab)
150104
elif isinstance(arg, ast.Constant) and isinstance(arg.value, int):
105+
int_width = 64 # Deafult to i64
151106
if expected_type and isinstance(expected_type, ir.IntType):
152107
int_width = expected_type.width
153108
ptr = create_int_constant_ptr(arg.value, builder, local_sym_tab, int_width)
@@ -178,7 +133,9 @@ def get_or_create_ptr_from_arg(
178133
and isinstance(field_type.element, ir.IntType)
179134
and field_type.element.width == 8
180135
):
181-
ptr = get_struct_char_array_ptr(arg, builder, local_sym_tab, struct_sym_tab)
136+
ptr, sz = get_char_array_ptr_and_size(
137+
arg, builder, local_sym_tab, struct_sym_tab
138+
)
182139
if not ptr:
183140
raise ValueError("Failed to get char array pointer from struct field")
184141
else:
@@ -203,6 +160,10 @@ def get_or_create_ptr_from_arg(
203160
val = builder.trunc(val, expected_type)
204161
builder.store(val, ptr)
205162

163+
# NOTE: For char arrays, also return size
164+
if sz:
165+
return ptr, sz
166+
206167
return ptr
207168

208169

pythonbpf/helper/printk_formatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from llvmlite import ir
55
from pythonbpf.expr import eval_expr, get_base_type_and_depth, deref_to_depth
66
from pythonbpf.expr.vmlinux_registry import VmlinuxHandlerRegistry
7-
from pythonbpf.helper.helper_utils import get_struct_char_array_ptr
7+
from pythonbpf.helper.helper_utils import get_char_array_ptr_and_size
88

99
logger = logging.getLogger(__name__)
1010

@@ -220,7 +220,7 @@ def _prepare_expr_args(expr, func, module, builder, local_sym_tab, struct_sym_ta
220220
"""Evaluate and prepare an expression to use as an arg for bpf_printk."""
221221

222222
# Special case: struct field char array needs pointer to first element
223-
char_array_ptr = get_struct_char_array_ptr(
223+
char_array_ptr, _ = get_char_array_ptr_and_size(
224224
expr, builder, local_sym_tab, struct_sym_tab
225225
)
226226
if char_array_ptr:

0 commit comments

Comments
 (0)