From f18a4399ea505269dc34df7816a8637dd6a2f7cb Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Fri, 24 Oct 2025 02:40:07 +0530 Subject: [PATCH] format chore --- pythonbpf/allocation_pass.py | 15 ++++++++++----- pythonbpf/expr/expr_pass.py | 9 +++++++-- pythonbpf/functions/functions_pass.py | 11 ++++++++--- tests/c-form/struct_field_tests.bpf.c | 13 ++----------- tests/passing_tests/assign/comprehensive.py | 3 ++- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/pythonbpf/allocation_pass.py b/pythonbpf/allocation_pass.py index ad49118..4fe3945 100644 --- a/pythonbpf/allocation_pass.py +++ b/pythonbpf/allocation_pass.py @@ -1,5 +1,4 @@ import ast -import ctypes import logging from llvmlite import ir @@ -246,12 +245,16 @@ def _allocate_for_attribute(builder, var_name, rval, local_sym_tab, structs_sym_ # Handle vmlinux struct field access vmlinux_struct_name = struct_type.__name__ if not VmlinuxHandlerRegistry.has_field(vmlinux_struct_name, field_name): - logger.error(f"Field '{field_name}' not found in vmlinux struct '{vmlinux_struct_name}'") + logger.error( + f"Field '{field_name}' not found in vmlinux struct '{vmlinux_struct_name}'" + ) return - field_type: tuple[ir.GlobalVariable, Field] = VmlinuxHandlerRegistry.get_field_type(vmlinux_struct_name, field_name) + field_type: tuple[ir.GlobalVariable, Field] = ( + VmlinuxHandlerRegistry.get_field_type(vmlinux_struct_name, field_name) + ) field_ir, field = field_type - #TODO: For now, we only support integer type allocations. + # TODO: For now, we only support integer type allocations. # loaded_value = builder.load(field_ir, align=8) # #TODO: fatal flaw that this always assumes first argument of function to be the context of what this gets. @@ -270,7 +273,9 @@ def _allocate_for_attribute(builder, var_name, rval, local_sym_tab, structs_sym_ var = _allocate_with_type(builder, var_name, actual_ir_type) local_sym_tab[var_name] = LocalSymbol(var, actual_ir_type, field) - logger.info(f"Pre-allocated {var_name} from vmlinux struct {vmlinux_struct_name}.{field_name}") + logger.info( + f"Pre-allocated {var_name} from vmlinux struct {vmlinux_struct_name}.{field_name}" + ) return else: logger.error(f"Struct type '{struct_type}' not found") diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index a17b48f..1d10fcb 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -72,8 +72,13 @@ def _handle_attribute_expr( if var_name in local_sym_tab: var_ptr, var_type, var_metadata = local_sym_tab[var_name] logger.info(f"Loading attribute {attr_name} from variable {var_name}") - logger.info(f"Variable type: {var_type}, Variable ptr: {var_ptr}, Variable Metadata: {var_metadata}") - if hasattr(var_metadata, "__module__") and var_metadata.__module__ == "vmlinux": + logger.info( + f"Variable type: {var_type}, Variable ptr: {var_ptr}, Variable Metadata: {var_metadata}" + ) + if ( + hasattr(var_metadata, "__module__") + and var_metadata.__module__ == "vmlinux" + ): # Try vmlinux handler when var_metadata is not a string, but has a module attribute. # This has been done to keep everything separate in vmlinux struct handling. vmlinux_result = VmlinuxHandlerRegistry.handle_attribute( diff --git a/pythonbpf/functions/functions_pass.py b/pythonbpf/functions/functions_pass.py index a5449ae..9ef97bf 100644 --- a/pythonbpf/functions/functions_pass.py +++ b/pythonbpf/functions/functions_pass.py @@ -20,7 +20,8 @@ from pythonbpf.assign_pass import ( from pythonbpf.allocation_pass import ( handle_assign_allocation, allocate_temp_pool, - create_targets_and_rvals, LocalSymbol, + create_targets_and_rvals, + LocalSymbol, ) from .return_utils import handle_none_return, handle_xdp_return, is_xdp_name @@ -347,12 +348,16 @@ def process_func_body( resolved_type = VmlinuxHandlerRegistry.get_struct_type( context_type_name ) - context_type = LocalSymbol(None, ir.PointerType(resolved_type), resolved_type) + context_type = LocalSymbol( + None, ir.PointerType(resolved_type), resolved_type + ) else: try: resolved_type = ctypes_to_ir(context_type_name) logger.error("THIS SHOULD NOT HAPPEN. I THINK. PROBABLY.") - context_type = LocalSymbol(None, ir.PointerType(resolved_type), resolved_type) + context_type = LocalSymbol( + None, ir.PointerType(resolved_type), resolved_type + ) except Exception: raise TypeError(f"Type '{context_type_name}' not declared") diff --git a/tests/c-form/struct_field_tests.bpf.c b/tests/c-form/struct_field_tests.bpf.c index bc43190..bb85d84 100644 --- a/tests/c-form/struct_field_tests.bpf.c +++ b/tests/c-form/struct_field_tests.bpf.c @@ -19,17 +19,8 @@ There is no point of SEC("tp/syscalls/sys_enter_execve") int handle_setuid_entry(struct trace_event_raw_sys_enter *ctx) { // Access each argument separately with clear variable assignments - unsigned long arg0 = ctx->args[0]; - bpf_printk("args[0]: %u", arg0); - - unsigned long arg1 = ctx->args[1]; - bpf_printk("args[1]: %u", arg1); - - // Remove the duplicate access to args[1] - - unsigned long arg2 = ctx->args[2]; - bpf_printk("args[3]: %u", arg2); - bpf_printk("args[4]: %u", ctx->args[2]); + long int arg0 = ctx->id; + bpf_printk("args[0]: %d", arg0); return 0; } diff --git a/tests/passing_tests/assign/comprehensive.py b/tests/passing_tests/assign/comprehensive.py index 47a38ca..67500c1 100644 --- a/tests/passing_tests/assign/comprehensive.py +++ b/tests/passing_tests/assign/comprehensive.py @@ -70,5 +70,6 @@ def hello_world(ctx: c_void_p) -> c_int64: def LICENSE() -> str: return "GPL" + compile_to_ir("comprehensive.py", "comprehensive.ll") -# compile() +compile()