diff --git a/pythonbpf/vmlinux_parser/dependency_node.py b/pythonbpf/vmlinux_parser/dependency_node.py index a0e1d45..feebec3 100644 --- a/pythonbpf/vmlinux_parser/dependency_node.py +++ b/pythonbpf/vmlinux_parser/dependency_node.py @@ -240,28 +240,47 @@ class DependencyNode: size_of_field = ctypes.sizeof(processing_field.type) return size_of_field elif processing_field.type.__module__ == "vmlinux": - size_of_field: int = 0 if processing_field.ctype_complex_type is not None: if issubclass(processing_field.ctype_complex_type, ctypes.Array): if processing_field.containing_type.__module__ == ctypes.__name__: - size_of_field = ( - ctypes.sizeof(processing_field.containing_type) - * processing_field.type_size - ) + if ( + processing_field.containing_type is not None + and processing_field.type_size is not None + ): + size_of_field = ( + ctypes.sizeof(processing_field.containing_type) + * processing_field.type_size + ) + else: + raise RuntimeError( + f"{processing_field} has no containing_type or type_size" + ) return size_of_field elif processing_field.containing_type.__module__ == "vmlinux": - size_of_field = ( - size_of_containing_type * processing_field.type_size - ) + if ( + size_of_containing_type is not None + and processing_field.type_size is not None + ): + size_of_field = ( + size_of_containing_type * processing_field.type_size + ) + else: + raise RuntimeError( + f"{processing_field} has no containing_type or type_size" + ) return size_of_field elif issubclass(processing_field.ctype_complex_type, ctypes._Pointer): - return ctypes.sizeof(ctypes.pointer()) + return ctypes.sizeof(ctypes.c_void_p) else: raise NotImplementedError( "This subclass of ctype not supported yet" ) else: # search up pre-created stuff and get size + if size_of_containing_type is None: + raise RuntimeError( + f"Size of containing type {size_of_containing_type} is None" + ) return size_of_containing_type else: diff --git a/pythonbpf/vmlinux_parser/import_detector.py b/pythonbpf/vmlinux_parser/import_detector.py index e314a35..972b1ff 100644 --- a/pythonbpf/vmlinux_parser/import_detector.py +++ b/pythonbpf/vmlinux_parser/import_detector.py @@ -1,6 +1,6 @@ import ast import logging -from typing import List, Tuple, Dict +from typing import List, Tuple, Any import importlib import inspect @@ -82,7 +82,7 @@ def vmlinux_proc(tree: ast.AST, module): # initialise dependency handler handler = DependencyHandler() # initialise assignment dictionary of name to type - assignments: Dict[str, type] = {} + assignments: dict[str, tuple[type, Any]] = {} if not import_statements: logger.info("No vmlinux imports found") @@ -132,10 +132,16 @@ def vmlinux_proc(tree: ast.AST, module): return assignments -def process_vmlinux_assign(node, module, assignments: Dict[str, type]): +def process_vmlinux_assign(node, module, assignments: dict[str, tuple[type, Any]]): # Check if this is a simple assignment with a constant value if len(node.targets) == 1 and isinstance(node.targets[0], ast.Name): target_name = node.targets[0].id if isinstance(node.value, ast.Constant): - assignments[target_name] = node.value.value - logger.info(f"Added assignment: {target_name} = {node.value.value}") + assignments[target_name] = (type(node.value.value), node.value.value) + logger.info( + f"Added assignment: {target_name} = {node.value.value!r} of type {type(node.value.value)}" + ) + else: + raise ValueError(f"Unsupported assignment type for {target_name}") + else: + raise ValueError("Not a simple assignment")