From b0d35693b93dff24775df5214da5428f3d279aa2 Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Wed, 5 Nov 2025 17:44:45 +0530 Subject: [PATCH] format chore --- pythonbpf/allocation_pass.py | 5 ++++- pythonbpf/assign_pass.py | 14 +++++++++----- .../vmlinux_parser/vmlinux_exports_handler.py | 8 ++++---- tests/failing_tests/vmlinux/i32_test.py | 5 ++++- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/pythonbpf/allocation_pass.py b/pythonbpf/allocation_pass.py index 8c66a9d..3b25b6f 100644 --- a/pythonbpf/allocation_pass.py +++ b/pythonbpf/allocation_pass.py @@ -261,7 +261,10 @@ def _allocate_for_attribute(builder, var_name, rval, local_sym_tab, structs_sym_ if field_size_bits in [8, 16, 32, 64]: # Special case: struct_xdp_md i32 fields should allocate as i64 # because load_ctx_field will zero-extend them to i64 - if vmlinux_struct_name == "struct_xdp_md" and field_size_bits == 32: + if ( + vmlinux_struct_name == "struct_xdp_md" + and field_size_bits == 32 + ): actual_ir_type = ir.IntType(64) logger.info( f"Allocating {var_name} as i64 for i32 field from struct_xdp_md.{field_name} " diff --git a/pythonbpf/assign_pass.py b/pythonbpf/assign_pass.py index 34f45a7..0bd48c6 100644 --- a/pythonbpf/assign_pass.py +++ b/pythonbpf/assign_pass.py @@ -154,13 +154,17 @@ def handle_variable_assignment( logger.info("Handling assignment to struct field") # Special handling for struct_xdp_md i32 fields that are zero-extended to i64 # The load_ctx_field already extended them, so val is i64 but val_type.type shows c_uint - if (hasattr(val_type, 'type') and - val_type.type.__name__ == "c_uint" and - isinstance(var_type, ir.IntType) and - var_type.width == 64): + if ( + hasattr(val_type, "type") + and val_type.type.__name__ == "c_uint" + and isinstance(var_type, ir.IntType) + and var_type.width == 64 + ): # This is the struct_xdp_md case - value is already i64 builder.store(val, var_ptr) - logger.info(f"Assigned zero-extended struct_xdp_md i32 field to {var_name} (i64)") + logger.info( + f"Assigned zero-extended struct_xdp_md i32 field to {var_name} (i64)" + ) return True # TODO: handling only ctype struct fields for now. Handle other stuff too later. elif var_type == ctypes_to_ir(val_type.type.__name__): diff --git a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py index 14fae81..30f3058 100644 --- a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py +++ b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py @@ -95,9 +95,7 @@ class VmlinuxHandler: ) python_type: type = var_info.metadata struct_name = python_type.__name__ - globvar_ir, field_data = self.get_field_type( - struct_name, field_name - ) + globvar_ir, field_data = self.get_field_type(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, struct_name @@ -183,7 +181,9 @@ class VmlinuxHandler: # 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") + logger.info( + "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" diff --git a/tests/failing_tests/vmlinux/i32_test.py b/tests/failing_tests/vmlinux/i32_test.py index 737d9c5..4ba0969 100644 --- a/tests/failing_tests/vmlinux/i32_test.py +++ b/tests/failing_tests/vmlinux/i32_test.py @@ -1,8 +1,9 @@ -from ctypes import c_int64, c_int32, c_void_p +from ctypes import c_int64, c_void_p from pythonbpf import bpf, section, bpfglobal, compile_to_ir, compile from vmlinux import struct_xdp_md from vmlinux import XDP_PASS + @bpf @section("xdp") def print_xdp_dat2a(ct2x: struct_xdp_md) -> c_int64: @@ -10,6 +11,7 @@ def print_xdp_dat2a(ct2x: struct_xdp_md) -> c_int64: print(f"ct2x->data = {data}") return c_int64(XDP_PASS) + @bpf @section("xdp") def print_xdp_data(ctx: struct_xdp_md) -> c_int64: @@ -18,6 +20,7 @@ def print_xdp_data(ctx: struct_xdp_md) -> c_int64: print(f"ctx->data = {something}") return c_int64(XDP_PASS) + @bpf @bpfglobal def LICENSE() -> str: