From 8743ea17f366b34f9faf548c4b841be040492b36 Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Sun, 12 Oct 2025 01:33:23 +0530 Subject: [PATCH] one recursion issue solved --- pythonbpf/vmlinux_parser/class_handler.py | 115 ++++++++++++++++------ tests/failing_tests/xdp_pass.py | 4 +- 2 files changed, 86 insertions(+), 33 deletions(-) diff --git a/pythonbpf/vmlinux_parser/class_handler.py b/pythonbpf/vmlinux_parser/class_handler.py index 4040722..83dc960 100644 --- a/pythonbpf/vmlinux_parser/class_handler.py +++ b/pythonbpf/vmlinux_parser/class_handler.py @@ -15,7 +15,20 @@ def get_module_symbols(module_name: str): # Recursive function that gets all the dependent classes and adds them to handler -def process_vmlinux_class(node, llvm_module, handler: DependencyHandler): +def process_vmlinux_class(node, llvm_module, handler: DependencyHandler, processing_stack=None): + """ + Recursively process vmlinux classes and their dependencies. + + Args: + node: The class/type to process + llvm_module: The LLVM module context + handler: DependencyHandler to track all nodes + processing_stack: Set of currently processing nodes to detect cycles + """ + # Initialize processing stack on first call + if processing_stack is None: + processing_stack = set() + symbols_in_module, imported_module = get_module_symbols("vmlinux") # Handle both node objects and type objects @@ -28,80 +41,120 @@ def process_vmlinux_class(node, llvm_module, handler: DependencyHandler): if current_symbol_name not in symbols_in_module: raise ImportError(f"{current_symbol_name} not present in module vmlinux") + + # Check if we're already processing this node (circular dependency) + if current_symbol_name in processing_stack: + logger.debug(f"Circular dependency detected for {current_symbol_name}, skipping") + return True + + # Check if already processed + if handler.has_node(current_symbol_name): + existing_node = handler.get_node(current_symbol_name) + # If the node exists and is ready, we're done + if existing_node and existing_node.is_ready: + logger.info(f"Node {current_symbol_name} already processed and ready") + return True + logger.info(f"Resolving vmlinux class {current_symbol_name}") logger.debug( f"Current handler state: {handler.is_ready} readiness and {handler.get_all_nodes()} all nodes" ) - field_table = {} # should contain the field and it's type. - # Get the class object from the module - class_obj = getattr(imported_module, current_symbol_name) + # Add to processing stack to detect cycles + processing_stack.add(current_symbol_name) - # Below, I've written a general structure that gets class-info - # everytime, no matter the format in which it is present + try: + field_table = {} # should contain the field and it's type. - # Inspect the class fields - # Assuming class_obj has fields stored in some standard way - # If it's a ctypes-like structure with _fields_ - if hasattr(class_obj, "_fields_"): - for field_name, field_type in class_obj._fields_: - field_table[field_name] = field_type + # Get the class object from the module + class_obj = getattr(imported_module, current_symbol_name) - # If it's using __annotations__ - elif hasattr(class_obj, "__annotations__"): - for field_name, field_type in class_obj.__annotations__.items(): - field_table[field_name] = field_type + # Inspect the class fields + if hasattr(class_obj, "_fields_"): + for field_name, field_type in class_obj._fields_: + field_table[field_name] = field_type + elif hasattr(class_obj, "__annotations__"): + for field_name, field_type in class_obj.__annotations__.items(): + field_table[field_name] = field_type + else: + raise TypeError("Could not get required class and definition") - else: - raise TypeError("Could not get required class and definition") + logger.debug(f"Extracted fields for {current_symbol_name}: {field_table}") - logger.debug(f"Extracted fields for {current_symbol_name}: {field_table}") - if handler.has_node(current_symbol_name): - logger.info("Extraction pruned due to already available field") - return True - else: - new_dep_node = DependencyNode(name=current_symbol_name) - handler.add_node(new_dep_node) + # Create or get the node + if handler.has_node(current_symbol_name): + new_dep_node = handler.get_node(current_symbol_name) + else: + new_dep_node = DependencyNode(name=current_symbol_name) + handler.add_node(new_dep_node) + + # Process each field for elem_name, elem_type in field_table.items(): module_name = getattr(elem_type, "__module__", None) + if module_name == ctypes.__name__: + # Simple ctypes - mark as ready immediately new_dep_node.add_field(elem_name, elem_type, ready=True) + elif module_name == "vmlinux": + # Complex vmlinux type - needs recursive processing new_dep_node.add_field(elem_name, elem_type, ready=False) - print("elem_name:", elem_name, "elem_type:", elem_type) - # currently fails when a non-normal type appears which is basically everytime + logger.debug(f"Processing vmlinux field: {elem_name}, type: {elem_type}") + identify_ctypes_type(elem_name, elem_type, new_dep_node) + + # Determine the actual symbol to process symbol_name = ( elem_type.__name__ if hasattr(elem_type, "__name__") else str(elem_type) ) vmlinux_symbol = None + + # Handle pointers/arrays to other types if hasattr(elem_type, "_type_"): containing_module_name = getattr( (elem_type._type_), "__module__", None ) if containing_module_name == ctypes.__name__: + # Pointer/Array to ctypes - mark as ready new_dep_node.set_field_ready(elem_name, True) continue elif containing_module_name == "vmlinux": + # Pointer/Array to vmlinux type symbol_name = ( (elem_type._type_).__name__ if hasattr((elem_type._type_), "__name__") else str(elem_type._type_) ) + + # Self-referential check + if symbol_name == current_symbol_name: + logger.debug(f"Self-referential field {elem_name} in {current_symbol_name}") + # For pointers to self, we can mark as ready since the type is being defined + new_dep_node.set_field_ready(elem_name, True) + continue + vmlinux_symbol = getattr(imported_module, symbol_name) else: + # Direct vmlinux type (not pointer/array) vmlinux_symbol = getattr(imported_module, symbol_name) - if process_vmlinux_class(vmlinux_symbol, llvm_module, handler): - new_dep_node.set_field_ready(elem_name, True) + + # Recursively process the dependency + if vmlinux_symbol is not None: + if process_vmlinux_class(vmlinux_symbol, llvm_module, handler, processing_stack): + new_dep_node.set_field_ready(elem_name, True) else: raise ValueError( f"{elem_name} with type {elem_type} not supported in recursive resolver" ) - logger.info(f"added node: {current_symbol_name}") - return True + logger.info(f"Successfully processed node: {current_symbol_name}") + return True + + finally: + # Remove from processing stack when done + processing_stack.discard(current_symbol_name) def identify_ctypes_type(elem_name, elem_type, new_dep_node: DependencyNode): diff --git a/tests/failing_tests/xdp_pass.py b/tests/failing_tests/xdp_pass.py index eb72f5a..14663d3 100644 --- a/tests/failing_tests/xdp_pass.py +++ b/tests/failing_tests/xdp_pass.py @@ -1,9 +1,9 @@ from pythonbpf import bpf, map, section, bpfglobal, compile, compile_to_ir from pythonbpf.maps import HashMap from pythonbpf.helper import XDP_PASS -from vmlinux import struct_xdp_md -# from vmlinux import struct_ring_buffer_per_cpu # noqa: F401 +from vmlinux import struct_ring_buffer_per_cpu # noqa: F401 from vmlinux import struct_xdp_buff # noqa: F401 +from vmlinux import struct_xdp_md from ctypes import c_int64 # Instructions to how to run this program