add i32 support special case and find ctx repetition in multiple functions error.

This commit is contained in:
2025-11-05 17:38:38 +05:30
parent 3489f45b63
commit 2685d0a0ee
6 changed files with 63 additions and 19 deletions

View File

@ -94,12 +94,13 @@ class VmlinuxHandler:
f"Attempting to access field {field_name} of possible vmlinux struct {struct_var_name}"
)
python_type: type = var_info.metadata
struct_name = python_type.__name__
globvar_ir, field_data = self.get_field_type(
python_type.__name__, field_name
struct_name, field_name
)
builder.function.args[0].type = ir.PointerType(ir.IntType(8))
field_ptr = self.load_ctx_field(
builder, builder.function.args[0], globvar_ir, field_data
builder, builder.function.args[0], globvar_ir, field_data, struct_name
)
# Return pointer to field and field type
return field_ptr, field_data
@ -107,7 +108,7 @@ class VmlinuxHandler:
raise RuntimeError("Variable accessed not found in symbol table")
@staticmethod
def load_ctx_field(builder, ctx_arg, offset_global, field_data):
def load_ctx_field(builder, ctx_arg, offset_global, field_data, struct_name=None):
"""
Generate LLVM IR to load a field from BPF context using offset.
@ -116,8 +117,9 @@ class VmlinuxHandler:
ctx_arg: The context pointer argument (ptr/i8*)
offset_global: Global variable containing the field offset (i64)
field_data: contains data about the field
struct_name: Name of the struct being accessed (optional)
Returns:
The loaded value (i64 register)
The loaded value (i64 register or appropriately sized)
"""
# Load the offset value
@ -164,6 +166,7 @@ class VmlinuxHandler:
# Determine the appropriate IR type based on field information
int_width = 64 # Default to 64-bit
needs_zext = False # Track if we need zero-extension for xdp_md
if field_data is not None:
# Try to determine the size from field metadata
@ -175,6 +178,12 @@ class VmlinuxHandler:
if field_size_bits in [8, 16, 32, 64]:
int_width = field_size_bits
logger.info(f"Determined field size: {int_width} bits")
# Special handling for struct_xdp_md i32 fields
# Load as i32 but extend to i64 before storing
if struct_name == "struct_xdp_md" and int_width == 32:
needs_zext = True
logger.info(f"struct_xdp_md i32 field detected, will zero-extend to i64")
else:
logger.warning(
f"Unusual field size {field_size_bits} bits, using default 64"
@ -203,6 +212,11 @@ class VmlinuxHandler:
# Load and return the value
value = builder.load(typed_ptr)
# Zero-extend i32 to i64 for struct_xdp_md fields
if needs_zext:
value = builder.zext(value, ir.IntType(64))
logger.info("Zero-extended i32 value to i64 for struct_xdp_md field")
return value
def has_field(self, struct_name, field_name):