diff --git a/pythonbpf/vmlinux_parser/class_handler.py b/pythonbpf/vmlinux_parser/class_handler.py index b75b85d..f23d0e5 100644 --- a/pythonbpf/vmlinux_parser/class_handler.py +++ b/pythonbpf/vmlinux_parser/class_handler.py @@ -99,13 +99,32 @@ def process_vmlinux_post_ast( local_module_name = getattr(elem_type, "__module__", None) new_dep_node.add_field(elem_name, elem_type, ready=False) if local_module_name == ctypes.__name__: - #TODO: need to process pointer to ctype and also CFUNCTYPES here + #TODO: need to process pointer to ctype and also CFUNCTYPES here recursively. + # for now, function pointers should give an error new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size) - print(elem_type) - new_dep_node.set_field_ready(elem_name, is_ready=True) - logger.debug( - f"Field {elem_name} is direct ctypes type: {elem_type}" - ) + + # Process pointer to ctype + if isinstance(elem_type, type) and issubclass(elem_type, ctypes._Pointer): + # Get the pointed-to type + pointed_type = elem_type._type_ + logger.debug(f"Found pointer to type: {pointed_type}") + new_dep_node.set_field_containing_type(elem_name, pointed_type) + new_dep_node.set_field_ctype_complex_type(elem_name, ctypes._Pointer) + new_dep_node.set_field_ready(elem_name, is_ready=True) + + # Process function pointers (CFUNCTYPE) + elif hasattr(elem_type, '_restype_') and hasattr(elem_type, '_argtypes_'): + # This is a CFUNCTYPE or similar + logger.info(f"Function pointer detected for {elem_name} with return type {elem_type._restype_} and arguments {elem_type._argtypes_}") + # Set the field as ready but mark it with special handling + new_dep_node.set_field_ctype_complex_type(elem_name, ctypes.CFUNCTYPE) + new_dep_node.set_field_ready(elem_name, is_ready=True) + logger.warning("Blindly processing CFUNCTYPE ctypes to ensure compilation. Unsupported") + + else: + # Regular ctype + new_dep_node.set_field_ready(elem_name, is_ready=True) + logger.debug(f"Field {elem_name} is direct ctypes type: {elem_type}") elif local_module_name == "vmlinux": new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size) logger.debug( diff --git a/tests/passing_tests/vmlinux/simple_struct_test.py b/tests/passing_tests/vmlinux/simple_struct_test.py index b40e620..9a26ccf 100644 --- a/tests/passing_tests/vmlinux/simple_struct_test.py +++ b/tests/passing_tests/vmlinux/simple_struct_test.py @@ -1,7 +1,7 @@ from pythonbpf import bpf, section, bpfglobal, compile_to_ir, compile from vmlinux import TASK_COMM_LEN # noqa: F401 from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401 -# from vmlinux import struct_uinput_device +from vmlinux import struct_uinput_device from vmlinux import struct_blk_integrity_iter from ctypes import c_int64