diff --git a/pythonbpf/vmlinux_parser/class_handler.py b/pythonbpf/vmlinux_parser/class_handler.py index 241f7d5..4040722 100644 --- a/pythonbpf/vmlinux_parser/class_handler.py +++ b/pythonbpf/vmlinux_parser/class_handler.py @@ -61,6 +61,7 @@ def process_vmlinux_class(node, llvm_module, handler: DependencyHandler): return True else: new_dep_node = DependencyNode(name=current_symbol_name) + handler.add_node(new_dep_node) for elem_name, elem_type in field_table.items(): module_name = getattr(elem_type, "__module__", None) if module_name == ctypes.__name__: @@ -69,36 +70,48 @@ def process_vmlinux_class(node, llvm_module, handler: DependencyHandler): 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 - identify_ctypes_type(elem_type) + identify_ctypes_type(elem_name, elem_type, new_dep_node) symbol_name = ( elem_type.__name__ if hasattr(elem_type, "__name__") else str(elem_type) ) - vmlinux_symbol = getattr(imported_module, symbol_name) + vmlinux_symbol = None + if hasattr(elem_type, "_type_"): + containing_module_name = getattr( + (elem_type._type_), "__module__", None + ) + if containing_module_name == ctypes.__name__: + new_dep_node.set_field_ready(elem_name, True) + continue + elif containing_module_name == "vmlinux": + symbol_name = ( + (elem_type._type_).__name__ + if hasattr((elem_type._type_), "__name__") + else str(elem_type._type_) + ) + vmlinux_symbol = getattr(imported_module, symbol_name) + else: + 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) else: raise ValueError( f"{elem_name} with type {elem_type} not supported in recursive resolver" ) - handler.add_node(new_dep_node) logger.info(f"added node: {current_symbol_name}") return True -def identify_ctypes_type(t): - if isinstance(t, type): # t is a type/class - if issubclass(t, ctypes.Array): - print("Array type") - print("Element type:", t._type_) - print("Length:", t._length_) - elif issubclass(t, ctypes._Pointer): - print("Pointer type") - print("Points to:", t._type_) - elif issubclass(t, ctypes._SimpleCData): - print("Scalar type") - print("Base type:", t) +def identify_ctypes_type(elem_name, elem_type, new_dep_node: DependencyNode): + if isinstance(elem_type, type): + if issubclass(elem_type, ctypes.Array): + new_dep_node.set_field_type(elem_name, ctypes.Array) + new_dep_node.set_field_containing_type(elem_name, elem_type._type_) + new_dep_node.set_field_type_size(elem_name, elem_type._length_) + elif issubclass(elem_type, ctypes._Pointer): + new_dep_node.set_field_type(elem_name, ctypes._Pointer) + new_dep_node.set_field_containing_type(elem_name, elem_type._type_) else: raise TypeError("Instance sent instead of Class") diff --git a/pythonbpf/vmlinux_parser/dependency_node.py b/pythonbpf/vmlinux_parser/dependency_node.py index 1f586d0..6f816c5 100644 --- a/pythonbpf/vmlinux_parser/dependency_node.py +++ b/pythonbpf/vmlinux_parser/dependency_node.py @@ -1,7 +1,7 @@ from dataclasses import dataclass, field from typing import Dict, Any, Optional - +#TODO: FIX THE FUCKING TYPE NAME CONVENTION. @dataclass class Field: """Represents a field in a dependency node with its type and readiness state.""" @@ -23,6 +23,26 @@ class Field: if mark_ready: self.ready = True + def set_type(self, given_type, mark_ready: bool = True) -> None: + """Set value of the type field and mark as ready""" + self.type = given_type + if mark_ready: + self.ready = True + + def set_containing_type( + self, containing_type: Optional[Any], mark_ready: bool = True + ) -> None: + """Set the containing_type of this field and optionally mark it as ready.""" + self.containing_type = containing_type + if mark_ready: + self.ready = True + + def set_type_size(self, type_size: Any, mark_ready: bool = True) -> None: + """Set the type_size of this field and optionally mark it as ready.""" + self.type_size = type_size + if mark_ready: + self.ready = True + @dataclass class DependencyNode: @@ -106,6 +126,37 @@ class DependencyNode: # Invalidate readiness cache self._ready_cache = None + def set_field_type(self, name: str, type: Any, mark_ready: bool = True) -> None: + """Set a field's type and optionally mark it as ready.""" + if name not in self.fields: + raise KeyError(f"Field '{name}' does not exist in node '{self.name}'") + + self.fields[name].set_type(type, mark_ready) + # Invalidate readiness cache + self._ready_cache = None + + def set_field_containing_type( + self, name: str, containing_type: Any, mark_ready: bool = True + ) -> None: + """Set a field's containing_type and optionally mark it as ready.""" + if name not in self.fields: + raise KeyError(f"Field '{name}' does not exist in node '{self.name}'") + + self.fields[name].set_containing_type(containing_type, mark_ready) + # Invalidate readiness cache + self._ready_cache = None + + def set_field_type_size( + self, name: str, type_size: Any, mark_ready: bool = True + ) -> None: + """Set a field's type_size and optionally mark it as ready.""" + if name not in self.fields: + raise KeyError(f"Field '{name}' does not exist in node '{self.name}'") + + self.fields[name].set_type_size(type_size, mark_ready) + # Invalidate readiness cache + self._ready_cache = None + def set_field_ready(self, name: str, is_ready: bool = True) -> None: """Mark a field as ready or not ready.""" if name not in self.fields: diff --git a/tests/failing_tests/xdp_pass.py b/tests/failing_tests/xdp_pass.py index 57e4a36..eb72f5a 100644 --- a/tests/failing_tests/xdp_pass.py +++ b/tests/failing_tests/xdp_pass.py @@ -2,7 +2,7 @@ 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 ctypes import c_int64