Skip to content

Commit 5dafa5b

Browse files
add function pointer detection warning as well as identify ctypes non recursion error
1 parent 33aa794 commit 5dafa5b

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

pythonbpf/vmlinux_parser/class_handler.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,32 @@ def process_vmlinux_post_ast(
9999
local_module_name = getattr(elem_type, "__module__", None)
100100
new_dep_node.add_field(elem_name, elem_type, ready=False)
101101
if local_module_name == ctypes.__name__:
102-
#TODO: need to process pointer to ctype and also CFUNCTYPES here
102+
#TODO: need to process pointer to ctype and also CFUNCTYPES here recursively.
103+
# for now, function pointers should give an error
103104
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
104-
print(elem_type)
105-
new_dep_node.set_field_ready(elem_name, is_ready=True)
106-
logger.debug(
107-
f"Field {elem_name} is direct ctypes type: {elem_type}"
108-
)
105+
106+
# Process pointer to ctype
107+
if isinstance(elem_type, type) and issubclass(elem_type, ctypes._Pointer):
108+
# Get the pointed-to type
109+
pointed_type = elem_type._type_
110+
logger.debug(f"Found pointer to type: {pointed_type}")
111+
new_dep_node.set_field_containing_type(elem_name, pointed_type)
112+
new_dep_node.set_field_ctype_complex_type(elem_name, ctypes._Pointer)
113+
new_dep_node.set_field_ready(elem_name, is_ready=True)
114+
115+
# Process function pointers (CFUNCTYPE)
116+
elif hasattr(elem_type, '_restype_') and hasattr(elem_type, '_argtypes_'):
117+
# This is a CFUNCTYPE or similar
118+
logger.info(f"Function pointer detected for {elem_name} with return type {elem_type._restype_} and arguments {elem_type._argtypes_}")
119+
# Set the field as ready but mark it with special handling
120+
new_dep_node.set_field_ctype_complex_type(elem_name, ctypes.CFUNCTYPE)
121+
new_dep_node.set_field_ready(elem_name, is_ready=True)
122+
logger.warning("Blindly processing CFUNCTYPE ctypes to ensure compilation. Unsupported")
123+
124+
else:
125+
# Regular ctype
126+
new_dep_node.set_field_ready(elem_name, is_ready=True)
127+
logger.debug(f"Field {elem_name} is direct ctypes type: {elem_type}")
109128
elif local_module_name == "vmlinux":
110129
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
111130
logger.debug(

tests/passing_tests/vmlinux/simple_struct_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pythonbpf import bpf, section, bpfglobal, compile_to_ir, compile
22
from vmlinux import TASK_COMM_LEN # noqa: F401
33
from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
4-
# from vmlinux import struct_uinput_device
4+
from vmlinux import struct_uinput_device
55
from vmlinux import struct_blk_integrity_iter
66
from ctypes import c_int64
77

0 commit comments

Comments
 (0)