Skip to content

Commit 7851827

Browse files
make semantics work other than field diffs
1 parent 80396c7 commit 7851827

File tree

3 files changed

+61
-30
lines changed

3 files changed

+61
-30
lines changed

pythonbpf/vmlinux_parser/class_handler.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,43 +40,22 @@ def process_vmlinux_post_ast(
4040
type_length: Optional[int] = None
4141
module_name = getattr(elem_type_class, "__module__", None)
4242

43-
if hasattr(elem_type_class, "_length_") and is_complex_type:
44-
type_length = elem_type_class._length_
45-
4643
if current_symbol_name in processing_stack:
47-
logger.debug(
44+
logger.info(
4845
f"Circular dependency detected for {current_symbol_name}, skipping"
4946
)
5047
return True
5148

5249
# Check if already processed
5350
if handler.has_node(current_symbol_name):
54-
existing_node = handler.get_node(current_symbol_name)
55-
# If the node exists and is ready, we're done
56-
if existing_node and existing_node.is_ready:
57-
logger.info(f"Node {current_symbol_name} already processed and ready")
58-
return True
51+
logger.info(f"Node {current_symbol_name} already processed and ready")
52+
return True
5953

6054
processing_stack.add(current_symbol_name)
6155

6256
if module_name == "vmlinux":
6357
if hasattr(elem_type_class, "_type_"):
64-
is_complex_type = True
65-
containing_type = elem_type_class._type_
66-
if containing_type.__module__ == "vmlinux":
67-
print("Very weird type ig for containing type", containing_type)
68-
elif containing_type.__module__ == ctypes.__name__:
69-
if isinstance(elem_type_class, type):
70-
if issubclass(elem_type_class, ctypes.Array):
71-
ctype_complex_type = ctypes.Array
72-
elif issubclass(elem_type_class, ctypes._Pointer):
73-
ctype_complex_type = ctypes._Pointer
74-
else:
75-
raise TypeError("Unsupported ctypes subclass")
76-
# handle ctype complex type
77-
78-
else:
79-
raise ImportError(f"Unsupported module of {containing_type}")
58+
pass
8059
else:
8160
new_dep_node = DependencyNode(name=current_symbol_name)
8261
handler.add_node(new_dep_node)
@@ -103,6 +82,40 @@ def process_vmlinux_post_ast(
10382
logger.debug(
10483
f"Processing vmlinux field: {elem_name}, type: {elem_type}"
10584
)
85+
if hasattr(elem_type, "_type_"):
86+
is_complex_type = True
87+
containing_type = elem_type._type_
88+
if hasattr(elem_type, "_length_") and is_complex_type:
89+
type_length = elem_type._length_
90+
if containing_type.__module__ == "vmlinux":
91+
pass
92+
elif containing_type.__module__ == ctypes.__name__:
93+
if isinstance(elem_type, type):
94+
if issubclass(elem_type, ctypes.Array):
95+
ctype_complex_type = ctypes.Array
96+
elif issubclass(elem_type, ctypes._Pointer):
97+
ctype_complex_type = ctypes._Pointer
98+
else:
99+
raise TypeError("Unsupported ctypes subclass")
100+
else:
101+
raise ImportError(
102+
f"Unsupported module of {containing_type}"
103+
)
104+
logger.info(f"{containing_type} containing type of parent {elem_name} with {elem_type} and ctype {ctype_complex_type} and length {type_length}")
105+
new_dep_node.set_field_containing_type(elem_name, containing_type)
106+
new_dep_node.set_field_type_size(elem_name, type_length)
107+
new_dep_node.set_field_ctype_complex_type(elem_name, ctype_complex_type)
108+
new_dep_node.set_field_type(elem_name, elem_type)
109+
if containing_type.__module__ == "vmlinux":
110+
if process_vmlinux_post_ast(
111+
containing_type, llvm_handler, handler, processing_stack
112+
):
113+
new_dep_node.set_field_ready(elem_name, True)
114+
elif containing_type.__module__ == ctypes.__name__:
115+
logger.info(f"Processing ctype internal{containing_type}")
116+
else:
117+
raise TypeError("Module not supported in recursive resolution")
118+
continue
106119
if process_vmlinux_post_ast(
107120
elem_type, llvm_handler, handler, processing_stack
108121
):
@@ -111,7 +124,6 @@ def process_vmlinux_post_ast(
111124
raise ValueError(
112125
f"{elem_name} with type {elem_type} from module {module_name} not supported in recursive resolver"
113126
)
114-
print("")
115127

116128
else:
117129
raise ImportError("UNSUPPORTED Module")

pythonbpf/vmlinux_parser/dependency_node.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Field:
99

1010
name: str
1111
type: type
12+
ctype_complex_type: Optional[Any]
1213
containing_type: Optional[Any]
1314
type_size: Optional[int]
1415
value: Any = None
@@ -44,6 +45,12 @@ def set_type_size(self, type_size: Any, mark_ready: bool = True) -> None:
4445
if mark_ready:
4546
self.ready = True
4647

48+
def set_ctype_complex_type(self, ctype_complex_type: Any, mark_ready: bool = True) -> None:
49+
"""Set the ctype_complex_type of this field and optionally mark it as ready."""
50+
self.ctype_complex_type = ctype_complex_type
51+
if mark_ready:
52+
self.ready = True
53+
4754

4855
@dataclass
4956
class DependencyNode:
@@ -100,6 +107,7 @@ def add_field(
100107
initial_value: Any = None,
101108
containing_type: Optional[Any] = None,
102109
type_size: Optional[int] = None,
110+
ctype_complex_type: Optional[int] = None,
103111
ready: bool = False,
104112
) -> None:
105113
"""Add a field to the node with an optional initial value and readiness state."""
@@ -110,6 +118,7 @@ def add_field(
110118
ready=ready,
111119
containing_type=containing_type,
112120
type_size=type_size,
121+
ctype_complex_type=ctype_complex_type
113122
)
114123
# Invalidate readiness cache
115124
self._ready_cache = None
@@ -158,6 +167,17 @@ def set_field_type_size(
158167
# Invalidate readiness cache
159168
self._ready_cache = None
160169

170+
def set_field_ctype_complex_type(
171+
self, name: str, ctype_complex_type: Any, mark_ready: bool = True
172+
) -> None:
173+
"""Set a field's ctype_complex_type and optionally mark it as ready."""
174+
if name not in self.fields:
175+
raise KeyError(f"Field '{name}' does not exist in node '{self.name}'")
176+
177+
self.fields[name].set_ctype_complex_type(ctype_complex_type, mark_ready)
178+
# Invalidate readiness cache
179+
self._ready_cache = None
180+
161181
def set_field_ready(self, name: str, is_ready: bool = True) -> None:
162182
"""Mark a field as ready or not ready."""
163183
if name not in self.fields:

tests/failing_tests/xdp_pass.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from pythonbpf import bpf, map, section, bpfglobal, compile, compile_to_ir
1+
from pythonbpf import bpf, map, section, bpfglobal, compile_to_ir
22
from pythonbpf.maps import HashMap
33
from pythonbpf.helper import XDP_PASS
44
from vmlinux import struct_xdp_md
5-
from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
6-
7-
# from vmlinux import struct_xdp_buff # noqa: F401
5+
# from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
6+
from vmlinux import struct_xdp_buff # noqa: F401
87
# from vmlinux import struct_xdp_md
98
from ctypes import c_int64
109

0 commit comments

Comments
 (0)