feat: allocate tmp variable for pointer to vmlinux struct field access.

This commit is contained in:
2025-11-27 14:02:00 +05:30
parent d43d3ad637
commit a8595ff1d2
3 changed files with 48 additions and 16 deletions

View File

@ -384,6 +384,7 @@ def _allocate_for_attribute(builder, var_name, rval, local_sym_tab, structs_sym_
f"Could not determine size for ctypes field {field_name}: {e}" f"Could not determine size for ctypes field {field_name}: {e}"
) )
actual_ir_type = ir.IntType(64) actual_ir_type = ir.IntType(64)
field_size_bits = 64
# Check if it's a nested vmlinux struct or complex type # Check if it's a nested vmlinux struct or complex type
elif field.type.__module__ == "vmlinux": elif field.type.__module__ == "vmlinux":
@ -392,23 +393,34 @@ def _allocate_for_attribute(builder, var_name, rval, local_sym_tab, structs_sym_
field.ctype_complex_type, ctypes._Pointer field.ctype_complex_type, ctypes._Pointer
): ):
actual_ir_type = ir.IntType(64) # Pointer is always 64-bit actual_ir_type = ir.IntType(64) # Pointer is always 64-bit
field_size_bits = 64
# For embedded structs, this is more complex - might need different handling # For embedded structs, this is more complex - might need different handling
else: else:
logger.warning( logger.warning(
f"Field {field_name} is a nested vmlinux struct, using i64 for now" f"Field {field_name} is a nested vmlinux struct, using i64 for now"
) )
actual_ir_type = ir.IntType(64) actual_ir_type = ir.IntType(64)
field_size_bits = 64
else: else:
logger.warning( logger.warning(
f"Unknown field type module {field.type.__module__} for {field_name}" f"Unknown field type module {field.type.__module__} for {field_name}"
) )
actual_ir_type = ir.IntType(64) actual_ir_type = ir.IntType(64)
field_size_bits = 64
# Allocate with the actual IR type # Pre-allocate the tmp storage used by load_struct_field (so we don't alloca inside handler)
tmp_name = f"{struct_var}_{field_name}_tmp"
tmp_ir_type = ir.IntType(field_size_bits)
tmp_var = builder.alloca(tmp_ir_type, name=tmp_name)
tmp_var.align = tmp_ir_type.width // 8
local_sym_tab[tmp_name] = LocalSymbol(tmp_var, tmp_ir_type)
logger.info(
f"Pre-allocated temp {tmp_name} (i{field_size_bits}) for vmlinux field read {vmlinux_struct_name}.{field_name}"
)
# Allocate with the actual IR type for the destination var
var = _allocate_with_type(builder, var_name, actual_ir_type) var = _allocate_with_type(builder, var_name, actual_ir_type)
local_sym_tab[var_name] = LocalSymbol( local_sym_tab[var_name] = LocalSymbol(var, actual_ir_type, field)
var, actual_ir_type, field
) # <-- Store Field metadata
logger.info( logger.info(
f"Pre-allocated {var_name} as {actual_ir_type} from vmlinux struct {vmlinux_struct_name}.{field_name}" f"Pre-allocated {var_name} as {actual_ir_type} from vmlinux struct {vmlinux_struct_name}.{field_name}"

View File

@ -77,7 +77,7 @@ class VmlinuxHandler:
return None return None
def get_vmlinux_enum_value(self, name): def get_vmlinux_enum_value(self, name):
"""Handle vmlinux enum constants by returning LLVM IR constants""" """Handle vmlinux.enum constants by returning LLVM IR constants"""
if self.is_vmlinux_enum(name): if self.is_vmlinux_enum(name):
value = self.vmlinux_symtab[name].value value = self.vmlinux_symtab[name].value
logger.info(f"The value of vmlinux enum {name} = {value}") logger.info(f"The value of vmlinux enum {name} = {value}")
@ -119,6 +119,9 @@ class VmlinuxHandler:
# Load the struct pointer from the local variable # Load the struct pointer from the local variable
struct_ptr = builder.load(var_info.var) struct_ptr = builder.load(var_info.var)
# Determine the preallocated tmp name that assignment pass should have created
tmp_name = f"{struct_var_name}_{field_name}_tmp"
# Use bpf_probe_read_kernel for non-context struct field access # Use bpf_probe_read_kernel for non-context struct field access
field_value = self.load_struct_field( field_value = self.load_struct_field(
builder, builder,
@ -127,6 +130,7 @@ class VmlinuxHandler:
field_data, field_data,
struct_name, struct_name,
local_sym_tab, local_sym_tab,
tmp_name,
) )
# Return field value and field type # Return field value and field type
return field_value, field_data return field_value, field_data
@ -141,6 +145,7 @@ class VmlinuxHandler:
field_data, field_data,
struct_name=None, struct_name=None,
local_sym_tab=None, local_sym_tab=None,
tmp_name: str = None,
): ):
""" """
Generate LLVM IR to load a field from a regular (non-context) struct using bpf_probe_read_kernel. Generate LLVM IR to load a field from a regular (non-context) struct using bpf_probe_read_kernel.
@ -151,6 +156,8 @@ class VmlinuxHandler:
offset_global: Global variable containing the field offset (i64) offset_global: Global variable containing the field offset (i64)
field_data: contains data about the field field_data: contains data about the field
struct_name: Name of the struct being accessed (optional) struct_name: Name of the struct being accessed (optional)
local_sym_tab: symbol table (optional) - used to locate preallocated tmp storage
tmp_name: name of the preallocated temporary storage to use (preferred)
Returns: Returns:
The loaded value The loaded value
""" """
@ -213,10 +220,20 @@ class VmlinuxHandler:
else: else:
logger.warning("Complex vmlinux field type, using default 64 bits") logger.warning("Complex vmlinux field type, using default 64 bits")
# Allocate local storage for the field value # Use preallocated temporary storage if provided by allocation pass
# TODO: CRITICAL BUG. alloca cannot be used anywhere other than the basic block
local_storage = builder.alloca(ir.IntType(int_width)) local_storage_i8_ptr = None
local_storage_i8_ptr = builder.bitcast(local_storage, i8_ptr_type) if tmp_name and local_sym_tab and tmp_name in local_sym_tab:
# Expect the tmp to be an alloca created during allocation pass
tmp_alloca = local_sym_tab[tmp_name].var
local_storage_i8_ptr = builder.bitcast(tmp_alloca, i8_ptr_type)
else:
# Fallback: allocate inline (not ideal, but preserves behavior)
local_storage = builder.alloca(ir.IntType(int_width))
local_storage_i8_ptr = builder.bitcast(local_storage, i8_ptr_type)
logger.warning(
f"Temp storage '{tmp_name}' not found. Allocating inline"
)
# Use bpf_probe_read_kernel to safely read the field # Use bpf_probe_read_kernel to safely read the field
# This generates: # This generates:
@ -230,7 +247,7 @@ class VmlinuxHandler:
) )
# Load the value from local storage # Load the value from local storage
value = builder.load(local_storage) value = builder.load(builder.bitcast(local_storage_i8_ptr, ir.PointerType(ir.IntType(int_width))))
# Zero-extend i32 to i64 if needed # Zero-extend i32 to i64 if needed
if needs_zext: if needs_zext:

View File

@ -26,15 +26,18 @@ class iphdr:
def ip_detector(ctx: struct_xdp_md) -> c_int64: def ip_detector(ctx: struct_xdp_md) -> c_int64:
data = ctx.data data = ctx.data
data_end = ctx.data_end data_end = ctx.data_end
eth = struct_ethhdr(ctx.data) if data + 14 > data_end:
nh = ctx.data + 14 return c_int64(XDP_DROP)
eth = struct_ethhdr(data)
nh = data + 14
if nh + 20 > data_end: if nh + 20 > data_end:
return c_int64(XDP_DROP) return c_int64(XDP_DROP)
iph = iphdr(nh) iph = iphdr(nh)
h_proto = eth.h_proto
h_proto_ext = c_int64(h_proto) print(f"ipaddress: {iph.saddr}")
ipv4 = iph.saddr
print(f"ipaddress: {ipv4}")
return c_int64(XDP_PASS) return c_int64(XDP_PASS)