Skip to content

Commit 07580da

Browse files
revert struct reference pointer sizes to i8 to ensure that compiler does not optimize
1 parent ac74b03 commit 07580da

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

pythonbpf/codegen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def finalize_module(original_str):
3939

4040
def bpf_passthrough_gen(module):
4141
i32_ty = ir.IntType(32)
42-
ptr_ty = ir.PointerType(ir.IntType(64))
42+
ptr_ty = ir.PointerType(ir.IntType(8))
4343
fnty = ir.FunctionType(ptr_ty, [i32_ty, ptr_ty])
4444

4545
# Declare the intrinsic

pythonbpf/vmlinux_parser/vmlinux_exports_handler.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def handle_vmlinux_struct_field(
9797
globvar_ir, field_data = self.get_field_type(
9898
python_type.__name__, field_name
9999
)
100-
builder.function.args[0].type = ir.PointerType(ir.IntType(64))
100+
builder.function.args[0].type = ir.PointerType(ir.IntType(8))
101101
print(builder.function.args[0])
102102
field_ptr = self.load_ctx_field(
103103
builder, builder.function.args[0], globvar_ir
@@ -125,19 +125,18 @@ def load_ctx_field(builder, ctx_arg, offset_global):
125125
# Load the offset value
126126
offset = builder.load(offset_global)
127127

128-
# # Ensure ctx_arg is treated as i8* (byte pointer)
129-
# # i8_type = ir.IntType(8)
130-
# i8_ptr_type = ir.PointerType()
128+
# Ensure ctx_arg is treated as i8* (byte pointer)
129+
i8_ptr_type = ir.PointerType()
131130

132131
# Cast ctx_arg to i8* if it isn't already
133-
# if str(ctx_arg.type) != str(i8_ptr_type):
134-
# ctx_i8_ptr = builder.bitcast(ctx_arg, i8_ptr_type)
135-
# else:
136-
# ctx_i8_ptr = ctx_arg
132+
if str(ctx_arg.type) != str(i8_ptr_type):
133+
ctx_i8_ptr = builder.bitcast(ctx_arg, i8_ptr_type)
134+
else:
135+
ctx_i8_ptr = ctx_arg
137136

138137
# GEP with explicit type - this is the key fix
139138
field_ptr = builder.gep(
140-
ctx_arg,
139+
ctx_i8_ptr,
141140
[offset],
142141
inbounds=False,
143142
)
@@ -151,8 +150,8 @@ def load_ctx_field(builder, ctx_arg, offset_global):
151150
raise KeyError
152151
except (KeyError, AttributeError):
153152
passthrough_type = ir.FunctionType(
154-
ir.PointerType(),
155-
[ir.IntType(32), ir.PointerType()],
153+
i8_ptr_type,
154+
[ir.IntType(32), i8_ptr_type],
156155
)
157156
passthrough_fn = ir.Function(
158157
module,

tests/failing_tests/vmlinux/struct_field_access.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
@bpf
1515
@section("tracepoint/syscalls/sys_enter_execve")
16-
def hello_world(ctx: struct_trace_event_raw_sys_enter) -> c_int32:
16+
def hello_world(ctx: struct_trace_event_raw_sys_enter) -> c_int64:
1717
b = ctx.id
1818
print(f"This is context field {b}")
19-
return c_int32(0)
19+
return c_int64(0)
2020

2121

2222
@bpf

0 commit comments

Comments
 (0)