add support for single depth pointer resolution

This commit is contained in:
2025-10-11 23:18:51 +05:30
parent 3343bedd11
commit f8844104a6
3 changed files with 81 additions and 17 deletions

View File

@ -61,6 +61,7 @@ def process_vmlinux_class(node, llvm_module, handler: DependencyHandler):
return True return True
else: else:
new_dep_node = DependencyNode(name=current_symbol_name) new_dep_node = DependencyNode(name=current_symbol_name)
handler.add_node(new_dep_node)
for elem_name, elem_type in field_table.items(): for elem_name, elem_type in field_table.items():
module_name = getattr(elem_type, "__module__", None) module_name = getattr(elem_type, "__module__", None)
if module_name == ctypes.__name__: 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) new_dep_node.add_field(elem_name, elem_type, ready=False)
print("elem_name:", elem_name, "elem_type:", elem_type) print("elem_name:", elem_name, "elem_type:", elem_type)
# currently fails when a non-normal type appears which is basically everytime # 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 = ( symbol_name = (
elem_type.__name__ elem_type.__name__
if hasattr(elem_type, "__name__") if hasattr(elem_type, "__name__")
else str(elem_type) 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): if process_vmlinux_class(vmlinux_symbol, llvm_module, handler):
new_dep_node.set_field_ready(elem_name, True) new_dep_node.set_field_ready(elem_name, True)
else: else:
raise ValueError( raise ValueError(
f"{elem_name} with type {elem_type} not supported in recursive resolver" 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}") logger.info(f"added node: {current_symbol_name}")
return True return True
def identify_ctypes_type(t): def identify_ctypes_type(elem_name, elem_type, new_dep_node: DependencyNode):
if isinstance(t, type): # t is a type/class if isinstance(elem_type, type):
if issubclass(t, ctypes.Array): if issubclass(elem_type, ctypes.Array):
print("Array type") new_dep_node.set_field_type(elem_name, ctypes.Array)
print("Element type:", t._type_) new_dep_node.set_field_containing_type(elem_name, elem_type._type_)
print("Length:", t._length_) new_dep_node.set_field_type_size(elem_name, elem_type._length_)
elif issubclass(t, ctypes._Pointer): elif issubclass(elem_type, ctypes._Pointer):
print("Pointer type") new_dep_node.set_field_type(elem_name, ctypes._Pointer)
print("Points to:", t._type_) new_dep_node.set_field_containing_type(elem_name, elem_type._type_)
elif issubclass(t, ctypes._SimpleCData):
print("Scalar type")
print("Base type:", t)
else: else:
raise TypeError("Instance sent instead of Class") raise TypeError("Instance sent instead of Class")

View File

@ -1,7 +1,7 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Dict, Any, Optional from typing import Dict, Any, Optional
#TODO: FIX THE FUCKING TYPE NAME CONVENTION.
@dataclass @dataclass
class Field: class Field:
"""Represents a field in a dependency node with its type and readiness state.""" """Represents a field in a dependency node with its type and readiness state."""
@ -23,6 +23,26 @@ class Field:
if mark_ready: if mark_ready:
self.ready = True 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 @dataclass
class DependencyNode: class DependencyNode:
@ -106,6 +126,37 @@ class DependencyNode:
# Invalidate readiness cache # Invalidate readiness cache
self._ready_cache = None 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: def set_field_ready(self, name: str, is_ready: bool = True) -> None:
"""Mark a field as ready or not ready.""" """Mark a field as ready or not ready."""
if name not in self.fields: if name not in self.fields:

View File

@ -2,7 +2,7 @@ from pythonbpf import bpf, map, section, bpfglobal, compile, compile_to_ir
from pythonbpf.maps import HashMap from pythonbpf.maps import HashMap
from pythonbpf.helper import XDP_PASS from pythonbpf.helper import XDP_PASS
from vmlinux import struct_xdp_md 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_buff # noqa: F401
from ctypes import c_int64 from ctypes import c_int64