Skip to content

Commit 1ad4f6c

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

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

pythonbpf/vmlinux_parser/class_handler.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,32 +99,45 @@ 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 recursively.
103-
# for now, function pointers should give an error
102+
# TODO: need to process pointer to ctype and also CFUNCTYPES here recursively. Current processing is a single dereference
104103
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
105104

106105
# Process pointer to ctype
107-
if isinstance(elem_type, type) and issubclass(elem_type, ctypes._Pointer):
106+
if isinstance(elem_type, type) and issubclass(
107+
elem_type, ctypes._Pointer
108+
):
108109
# Get the pointed-to type
109110
pointed_type = elem_type._type_
110111
logger.debug(f"Found pointer to type: {pointed_type}")
111112
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_ctype_complex_type(
114+
elem_name, ctypes._Pointer
115+
)
113116
new_dep_node.set_field_ready(elem_name, is_ready=True)
114117

115118
# Process function pointers (CFUNCTYPE)
116-
elif hasattr(elem_type, '_restype_') and hasattr(elem_type, '_argtypes_'):
119+
elif hasattr(elem_type, "_restype_") and hasattr(
120+
elem_type, "_argtypes_"
121+
):
117122
# 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_}")
123+
logger.info(
124+
f"Function pointer detected for {elem_name} with return type {elem_type._restype_} and arguments {elem_type._argtypes_}"
125+
)
119126
# Set the field as ready but mark it with special handling
120-
new_dep_node.set_field_ctype_complex_type(elem_name, ctypes.CFUNCTYPE)
127+
new_dep_node.set_field_ctype_complex_type(
128+
elem_name, ctypes.CFUNCTYPE
129+
)
121130
new_dep_node.set_field_ready(elem_name, is_ready=True)
122-
logger.warning("Blindly processing CFUNCTYPE ctypes to ensure compilation. Unsupported")
131+
logger.warning(
132+
"Blindly processing CFUNCTYPE ctypes to ensure compilation. Unsupported"
133+
)
123134

124135
else:
125136
# Regular ctype
126137
new_dep_node.set_field_ready(elem_name, is_ready=True)
127-
logger.debug(f"Field {elem_name} is direct ctypes type: {elem_type}")
138+
logger.debug(
139+
f"Field {elem_name} is direct ctypes type: {elem_type}"
140+
)
128141
elif local_module_name == "vmlinux":
129142
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
130143
logger.debug(
@@ -149,7 +162,9 @@ def process_vmlinux_post_ast(
149162
elif issubclass(elem_type, ctypes._Pointer):
150163
ctype_complex_type = ctypes._Pointer
151164
else:
152-
raise ImportError("Non Array and Pointer type ctype imports not supported in current version")
165+
raise ImportError(
166+
"Non Array and Pointer type ctype imports not supported in current version"
167+
)
153168
else:
154169
raise TypeError("Unsupported ctypes subclass")
155170
else:

tests/passing_tests/vmlinux/simple_struct_test.py

Lines changed: 2 additions & 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

@@ -26,3 +26,4 @@ def LICENSE() -> str:
2626

2727

2828
compile_to_ir("simple_struct_test.py", "simple_struct_test.ll")
29+
compile()

0 commit comments

Comments
 (0)