mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Compare commits
16 Commits
v0.1.5
...
eee212795f
| Author | SHA1 | Date | |
|---|---|---|---|
| eee212795f | |||
| 8da50b7068 | |||
| e636fcaea7 | |||
| 5512bf52e4 | |||
| 079ceaa0d6 | |||
| 328b792e4e | |||
| 5dafa5bd0d | |||
| 33aa794718 | |||
| d855e9ef2e | |||
| de19c8fc90 | |||
| dc1b243e82 | |||
| 1b4272b408 | |||
| 101183c315 | |||
| 3a3116253f | |||
| 9b7aa6d8be | |||
| 51a1be0b0b |
@ -10,6 +10,20 @@ authors = [
|
|||||||
{ name = "r41k0u", email="pragyanshchaturvedi18@gmail.com" },
|
{ name = "r41k0u", email="pragyanshchaturvedi18@gmail.com" },
|
||||||
{ name = "varun-r-mallya", email="varunrmallya@gmail.com" }
|
{ name = "varun-r-mallya", email="varunrmallya@gmail.com" }
|
||||||
]
|
]
|
||||||
|
classifiers = [
|
||||||
|
"Development Status :: 3 - Alpha",
|
||||||
|
"Intended Audience :: Developers",
|
||||||
|
"Operating System :: POSIX :: Linux",
|
||||||
|
"Programming Language :: Python :: 3",
|
||||||
|
"Programming Language :: Python :: 3.8",
|
||||||
|
"Programming Language :: Python :: 3.9",
|
||||||
|
"Programming Language :: Python :: 3.10",
|
||||||
|
"Programming Language :: Python :: 3.11",
|
||||||
|
"Programming Language :: Python :: 3.12",
|
||||||
|
"Programming Language :: Python",
|
||||||
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
||||||
|
"Topic :: System :: Operating System Kernels :: Linux",
|
||||||
|
]
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = {text = "Apache-2.0"}
|
license = {text = "Apache-2.0"}
|
||||||
requires-python = ">=3.8"
|
requires-python = ">=3.8"
|
||||||
|
|||||||
@ -55,11 +55,11 @@ def processor(source_code, filename, module):
|
|||||||
for func_node in bpf_chunks:
|
for func_node in bpf_chunks:
|
||||||
logger.info(f"Found BPF function/struct: {func_node.name}")
|
logger.info(f"Found BPF function/struct: {func_node.name}")
|
||||||
|
|
||||||
vmlinux_proc(tree, module)
|
vmlinux_symtab = vmlinux_proc(tree, module)
|
||||||
populate_global_symbol_table(tree, module)
|
populate_global_symbol_table(tree, module)
|
||||||
license_processing(tree, module)
|
license_processing(tree, module)
|
||||||
globals_processing(tree, module)
|
globals_processing(tree, module)
|
||||||
|
print("DEBUG:", vmlinux_symtab)
|
||||||
structs_sym_tab = structs_proc(tree, module, bpf_chunks)
|
structs_sym_tab = structs_proc(tree, module, bpf_chunks)
|
||||||
map_sym_tab = maps_proc(tree, module, bpf_chunks)
|
map_sym_tab = maps_proc(tree, module, bpf_chunks)
|
||||||
func_proc(tree, module, bpf_chunks, map_sym_tab, structs_sym_tab)
|
func_proc(tree, module, bpf_chunks, map_sym_tab, structs_sym_tab)
|
||||||
|
|||||||
@ -81,6 +81,20 @@ class DebugInfoGenerator:
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def create_array_type_vmlinux(self, type_info: Any, count: int) -> Any:
|
||||||
|
"""Create an array type of the given base type with specified count"""
|
||||||
|
base_type, type_sizing = type_info
|
||||||
|
subrange = self.module.add_debug_info("DISubrange", {"count": count})
|
||||||
|
return self.module.add_debug_info(
|
||||||
|
"DICompositeType",
|
||||||
|
{
|
||||||
|
"tag": dc.DW_TAG_array_type,
|
||||||
|
"baseType": base_type,
|
||||||
|
"size": type_sizing,
|
||||||
|
"elements": [subrange],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _compute_array_size(base_type: Any, count: int) -> int:
|
def _compute_array_size(base_type: Any, count: int) -> int:
|
||||||
# Extract size from base_type if possible
|
# Extract size from base_type if possible
|
||||||
@ -101,6 +115,23 @@ class DebugInfoGenerator:
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def create_struct_member_vmlinux(
|
||||||
|
self, name: str, base_type_with_size: Any, offset: int
|
||||||
|
) -> Any:
|
||||||
|
"""Create a struct member with the given name, type, and offset"""
|
||||||
|
base_type, type_size = base_type_with_size
|
||||||
|
return self.module.add_debug_info(
|
||||||
|
"DIDerivedType",
|
||||||
|
{
|
||||||
|
"tag": dc.DW_TAG_member,
|
||||||
|
"name": name,
|
||||||
|
"file": self.module._file_metadata,
|
||||||
|
"baseType": base_type,
|
||||||
|
"size": type_size,
|
||||||
|
"offset": offset,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
def create_struct_type(
|
def create_struct_type(
|
||||||
self, members: List[Any], size: int, is_distinct: bool
|
self, members: List[Any], size: int, is_distinct: bool
|
||||||
) -> Any:
|
) -> Any:
|
||||||
@ -116,6 +147,22 @@ class DebugInfoGenerator:
|
|||||||
is_distinct=is_distinct,
|
is_distinct=is_distinct,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def create_struct_type_with_name(
|
||||||
|
self, name: str, members: List[Any], size: int, is_distinct: bool
|
||||||
|
) -> Any:
|
||||||
|
"""Create a struct type with the given members and size"""
|
||||||
|
return self.module.add_debug_info(
|
||||||
|
"DICompositeType",
|
||||||
|
{
|
||||||
|
"name": name,
|
||||||
|
"tag": dc.DW_TAG_structure_type,
|
||||||
|
"file": self.module._file_metadata,
|
||||||
|
"size": size,
|
||||||
|
"elements": members,
|
||||||
|
},
|
||||||
|
is_distinct=is_distinct,
|
||||||
|
)
|
||||||
|
|
||||||
def create_global_var_debug_info(
|
def create_global_var_debug_info(
|
||||||
self, name: str, var_type: Any, is_local: bool = False
|
self, name: str, var_type: Any, is_local: bool = False
|
||||||
) -> Any:
|
) -> Any:
|
||||||
|
|||||||
35
pythonbpf/vmlinux_parser/assignment_info.py
Normal file
35
pythonbpf/vmlinux_parser/assignment_info.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
from enum import Enum, auto
|
||||||
|
from typing import Any, Dict, List, Optional, TypedDict
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from pythonbpf.vmlinux_parser.dependency_node import Field
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class AssignmentType(Enum):
|
||||||
|
CONSTANT = auto()
|
||||||
|
STRUCT = auto()
|
||||||
|
ARRAY = auto() # probably won't be used
|
||||||
|
FUNCTION_POINTER = auto()
|
||||||
|
POINTER = auto() # again, probably won't be used
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class FunctionSignature(TypedDict):
|
||||||
|
return_type: str
|
||||||
|
param_types: List[str]
|
||||||
|
varargs: bool
|
||||||
|
|
||||||
|
|
||||||
|
# Thew name of the assignment will be in the dict that uses this class
|
||||||
|
@dataclass
|
||||||
|
class AssignmentInfo(TypedDict):
|
||||||
|
value_type: AssignmentType
|
||||||
|
python_type: type
|
||||||
|
value: Optional[Any]
|
||||||
|
pointer_level: Optional[int]
|
||||||
|
signature: Optional[FunctionSignature] # For function pointers
|
||||||
|
# The key of the dict is the name of the field.
|
||||||
|
# Value is a tuple that contains the global variable representing that field
|
||||||
|
# along with all the information about that field as a Field type.
|
||||||
|
members: Optional[Dict[str, tuple[str, Field]]] # For structs.
|
||||||
@ -1,8 +1,10 @@
|
|||||||
import logging
|
import logging
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
import importlib
|
import importlib
|
||||||
|
|
||||||
|
from .assignment_info import AssignmentInfo, AssignmentType
|
||||||
from .dependency_handler import DependencyHandler
|
from .dependency_handler import DependencyHandler
|
||||||
from .dependency_node import DependencyNode
|
from .dependency_node import DependencyNode, Field
|
||||||
import ctypes
|
import ctypes
|
||||||
from typing import Optional, Any, Dict
|
from typing import Optional, Any, Dict
|
||||||
|
|
||||||
@ -15,17 +17,26 @@ def get_module_symbols(module_name: str):
|
|||||||
return [name for name in dir(imported_module)], imported_module
|
return [name for name in dir(imported_module)], imported_module
|
||||||
|
|
||||||
|
|
||||||
def process_vmlinux_class(node, llvm_module, handler: DependencyHandler):
|
def process_vmlinux_class(
|
||||||
|
node,
|
||||||
|
llvm_module,
|
||||||
|
handler: DependencyHandler,
|
||||||
|
assignments: dict[str, AssignmentInfo],
|
||||||
|
):
|
||||||
symbols_in_module, imported_module = get_module_symbols("vmlinux")
|
symbols_in_module, imported_module = get_module_symbols("vmlinux")
|
||||||
if node.name in symbols_in_module:
|
if node.name in symbols_in_module:
|
||||||
vmlinux_type = getattr(imported_module, node.name)
|
vmlinux_type = getattr(imported_module, node.name)
|
||||||
process_vmlinux_post_ast(vmlinux_type, llvm_module, handler)
|
process_vmlinux_post_ast(vmlinux_type, llvm_module, handler, assignments)
|
||||||
else:
|
else:
|
||||||
raise ImportError(f"{node.name} not in vmlinux")
|
raise ImportError(f"{node.name} not in vmlinux")
|
||||||
|
|
||||||
|
|
||||||
def process_vmlinux_post_ast(
|
def process_vmlinux_post_ast(
|
||||||
elem_type_class, llvm_handler, handler: DependencyHandler, processing_stack=None
|
elem_type_class,
|
||||||
|
llvm_handler,
|
||||||
|
handler: DependencyHandler,
|
||||||
|
assignments: dict[str, AssignmentInfo],
|
||||||
|
processing_stack=None,
|
||||||
):
|
):
|
||||||
# Initialize processing stack on first call
|
# Initialize processing stack on first call
|
||||||
if processing_stack is None:
|
if processing_stack is None:
|
||||||
@ -46,7 +57,7 @@ def process_vmlinux_post_ast(
|
|||||||
logger.debug(f"Node {current_symbol_name} already processed and ready")
|
logger.debug(f"Node {current_symbol_name} already processed and ready")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# XXX:Check it's use. It's probably not being used.
|
# XXX:Check its use. It's probably not being used.
|
||||||
if current_symbol_name in processing_stack:
|
if current_symbol_name in processing_stack:
|
||||||
logger.debug(
|
logger.debug(
|
||||||
f"Dependency already in processing stack for {current_symbol_name}, skipping"
|
f"Dependency already in processing stack for {current_symbol_name}, skipping"
|
||||||
@ -92,18 +103,61 @@ def process_vmlinux_post_ast(
|
|||||||
else:
|
else:
|
||||||
raise TypeError("Could not get required class and definition")
|
raise TypeError("Could not get required class and definition")
|
||||||
|
|
||||||
|
# Create a members dictionary for AssignmentInfo
|
||||||
|
members_dict: Dict[str, tuple[str, Field]] = {}
|
||||||
|
|
||||||
logger.debug(f"Extracted fields for {current_symbol_name}: {field_table}")
|
logger.debug(f"Extracted fields for {current_symbol_name}: {field_table}")
|
||||||
for elem in field_table.items():
|
for elem in field_table.items():
|
||||||
elem_name, elem_temp_list = elem
|
elem_name, elem_temp_list = elem
|
||||||
[elem_type, elem_bitfield_size] = elem_temp_list
|
[elem_type, elem_bitfield_size] = elem_temp_list
|
||||||
local_module_name = getattr(elem_type, "__module__", None)
|
local_module_name = getattr(elem_type, "__module__", None)
|
||||||
new_dep_node.add_field(elem_name, elem_type, ready=False)
|
new_dep_node.add_field(elem_name, elem_type, ready=False)
|
||||||
|
|
||||||
|
# Store field reference for struct assignment info
|
||||||
|
field_ref = new_dep_node.get_field(elem_name)
|
||||||
|
if field_ref:
|
||||||
|
members_dict[elem_name] = (elem_name, field_ref)
|
||||||
|
|
||||||
if local_module_name == ctypes.__name__:
|
if local_module_name == ctypes.__name__:
|
||||||
|
# TODO: need to process pointer to ctype and also CFUNCTYPES here recursively. Current processing is a single dereference
|
||||||
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
|
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
|
||||||
new_dep_node.set_field_ready(elem_name, is_ready=True)
|
|
||||||
logger.debug(
|
# Process pointer to ctype
|
||||||
f"Field {elem_name} is direct ctypes type: {elem_type}"
|
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":
|
elif local_module_name == "vmlinux":
|
||||||
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
|
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
|
||||||
logger.debug(
|
logger.debug(
|
||||||
@ -127,6 +181,10 @@ def process_vmlinux_post_ast(
|
|||||||
ctype_complex_type = ctypes.Array
|
ctype_complex_type = ctypes.Array
|
||||||
elif issubclass(elem_type, ctypes._Pointer):
|
elif issubclass(elem_type, ctypes._Pointer):
|
||||||
ctype_complex_type = ctypes._Pointer
|
ctype_complex_type = ctypes._Pointer
|
||||||
|
else:
|
||||||
|
raise ImportError(
|
||||||
|
"Non Array and Pointer type ctype imports not supported in current version"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
raise TypeError("Unsupported ctypes subclass")
|
raise TypeError("Unsupported ctypes subclass")
|
||||||
else:
|
else:
|
||||||
@ -171,6 +229,7 @@ def process_vmlinux_post_ast(
|
|||||||
containing_type,
|
containing_type,
|
||||||
llvm_handler,
|
llvm_handler,
|
||||||
handler,
|
handler,
|
||||||
|
assignments, # Pass assignments to recursive call
|
||||||
processing_stack,
|
processing_stack,
|
||||||
)
|
)
|
||||||
new_dep_node.set_field_ready(elem_name, True)
|
new_dep_node.set_field_ready(elem_name, True)
|
||||||
@ -188,7 +247,11 @@ def process_vmlinux_post_ast(
|
|||||||
else str(elem_type)
|
else str(elem_type)
|
||||||
)
|
)
|
||||||
process_vmlinux_post_ast(
|
process_vmlinux_post_ast(
|
||||||
elem_type, llvm_handler, handler, processing_stack
|
elem_type,
|
||||||
|
llvm_handler,
|
||||||
|
handler,
|
||||||
|
assignments,
|
||||||
|
processing_stack,
|
||||||
)
|
)
|
||||||
new_dep_node.set_field_ready(elem_name, True)
|
new_dep_node.set_field_ready(elem_name, True)
|
||||||
else:
|
else:
|
||||||
@ -196,10 +259,21 @@ def process_vmlinux_post_ast(
|
|||||||
f"{elem_name} with type {elem_type} from module {module_name} not supported in recursive resolver"
|
f"{elem_name} with type {elem_type} from module {module_name} not supported in recursive resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Add struct to assignments dictionary
|
||||||
|
assignments[current_symbol_name] = AssignmentInfo(
|
||||||
|
value_type=AssignmentType.STRUCT,
|
||||||
|
python_type=elem_type_class,
|
||||||
|
value=None,
|
||||||
|
pointer_level=None,
|
||||||
|
signature=None,
|
||||||
|
members=members_dict,
|
||||||
|
)
|
||||||
|
logger.info(f"Added struct assignment info for {current_symbol_name}")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ImportError("UNSUPPORTED Module")
|
raise ImportError("UNSUPPORTED Module")
|
||||||
|
|
||||||
logging.info(
|
logger.info(
|
||||||
f"{current_symbol_name} processed and handler readiness {handler.is_ready}"
|
f"{current_symbol_name} processed and handler readiness {handler.is_ready}"
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
import ast
|
import ast
|
||||||
import logging
|
import logging
|
||||||
from typing import List, Tuple, Any
|
|
||||||
import importlib
|
import importlib
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
|
from .assignment_info import AssignmentInfo, AssignmentType
|
||||||
from .dependency_handler import DependencyHandler
|
from .dependency_handler import DependencyHandler
|
||||||
from .ir_gen import IRGenerator
|
from .ir_gen import IRGenerator
|
||||||
from .class_handler import process_vmlinux_class
|
from .class_handler import process_vmlinux_class
|
||||||
@ -11,7 +11,7 @@ from .class_handler import process_vmlinux_class
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def detect_import_statement(tree: ast.AST) -> List[Tuple[str, ast.ImportFrom]]:
|
def detect_import_statement(tree: ast.AST) -> list[tuple[str, ast.ImportFrom]]:
|
||||||
"""
|
"""
|
||||||
Parse AST and detect import statements from vmlinux.
|
Parse AST and detect import statements from vmlinux.
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ def vmlinux_proc(tree: ast.AST, module):
|
|||||||
# initialise dependency handler
|
# initialise dependency handler
|
||||||
handler = DependencyHandler()
|
handler = DependencyHandler()
|
||||||
# initialise assignment dictionary of name to type
|
# initialise assignment dictionary of name to type
|
||||||
assignments: dict[str, tuple[type, Any]] = {}
|
assignments: dict[str, AssignmentInfo] = {}
|
||||||
|
|
||||||
if not import_statements:
|
if not import_statements:
|
||||||
logger.info("No vmlinux imports found")
|
logger.info("No vmlinux imports found")
|
||||||
@ -112,7 +112,7 @@ def vmlinux_proc(tree: ast.AST, module):
|
|||||||
isinstance(mod_node, ast.ClassDef)
|
isinstance(mod_node, ast.ClassDef)
|
||||||
and mod_node.name == imported_name
|
and mod_node.name == imported_name
|
||||||
):
|
):
|
||||||
process_vmlinux_class(mod_node, module, handler)
|
process_vmlinux_class(mod_node, module, handler, assignments)
|
||||||
found = True
|
found = True
|
||||||
break
|
break
|
||||||
if isinstance(mod_node, ast.Assign):
|
if isinstance(mod_node, ast.Assign):
|
||||||
@ -132,16 +132,31 @@ def vmlinux_proc(tree: ast.AST, module):
|
|||||||
return assignments
|
return assignments
|
||||||
|
|
||||||
|
|
||||||
def process_vmlinux_assign(node, module, assignments: dict[str, tuple[type, Any]]):
|
def process_vmlinux_assign(node, module, assignments: dict[str, AssignmentInfo]):
|
||||||
# Check if this is a simple assignment with a constant value
|
"""Process assignments from vmlinux module."""
|
||||||
|
# Only handle single-target assignments
|
||||||
if len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
|
if len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
|
||||||
target_name = node.targets[0].id
|
target_name = node.targets[0].id
|
||||||
|
|
||||||
|
# Handle constant value assignments
|
||||||
if isinstance(node.value, ast.Constant):
|
if isinstance(node.value, ast.Constant):
|
||||||
assignments[target_name] = (type(node.value.value), node.value.value)
|
# Fixed: using proper TypedDict creation syntax with named arguments
|
||||||
|
assignments[target_name] = AssignmentInfo(
|
||||||
|
value_type=AssignmentType.CONSTANT,
|
||||||
|
python_type=type(node.value.value),
|
||||||
|
value=node.value.value,
|
||||||
|
pointer_level=None,
|
||||||
|
signature=None,
|
||||||
|
members=None,
|
||||||
|
)
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Added assignment: {target_name} = {node.value.value!r} of type {type(node.value.value)}"
|
f"Added assignment: {target_name} = {node.value.value!r} of type {type(node.value.value)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Handle other assignment types that we may need to support
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Unsupported assignment type for {target_name}")
|
logger.warning(
|
||||||
|
f"Unsupported assignment type for {target_name}: {ast.dump(node.value)}"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
raise ValueError("Not a simple assignment")
|
raise ValueError("Not a simple assignment")
|
||||||
|
|||||||
@ -1,15 +1,161 @@
|
|||||||
from pythonbpf.debuginfo import DebugInfoGenerator
|
from pythonbpf.debuginfo import DebugInfoGenerator, dwarf_constants as dc
|
||||||
|
from ..dependency_node import DependencyNode
|
||||||
|
import ctypes
|
||||||
|
import logging
|
||||||
|
from typing import List, Any, Tuple
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def debug_info_generation(struct, llvm_module):
|
def debug_info_generation(
|
||||||
|
struct: DependencyNode,
|
||||||
|
llvm_module,
|
||||||
|
generated_debug_info: List[Tuple[DependencyNode, Any]],
|
||||||
|
) -> Any:
|
||||||
|
"""
|
||||||
|
Generate DWARF debug information for a struct defined in a DependencyNode.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
struct: The dependency node containing struct information
|
||||||
|
llvm_module: The LLVM module to add debug info to
|
||||||
|
generated_debug_info: List of tuples (struct, debug_info) to track generated debug info
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The generated global variable debug info
|
||||||
|
"""
|
||||||
|
# Set up debug info generator
|
||||||
generator = DebugInfoGenerator(llvm_module)
|
generator = DebugInfoGenerator(llvm_module)
|
||||||
# this is sample debug info generation
|
|
||||||
# i64type = generator.get_uint64_type()
|
|
||||||
|
|
||||||
struct_type = generator.create_struct_type([], 64 * 4, is_distinct=True)
|
# Check if debug info for this struct has already been generated
|
||||||
|
for existing_struct, debug_info in generated_debug_info:
|
||||||
|
if existing_struct.name == struct.name:
|
||||||
|
return debug_info
|
||||||
|
|
||||||
global_var = generator.create_global_var_debug_info(
|
# Process all fields and create members for the struct
|
||||||
struct.name, struct_type, is_local=False
|
members = []
|
||||||
|
for field_name, field in struct.fields.items():
|
||||||
|
# Get appropriate debug type for this field
|
||||||
|
field_type = _get_field_debug_type(
|
||||||
|
field_name, field, generator, struct, generated_debug_info
|
||||||
|
)
|
||||||
|
# Create struct member with proper offset
|
||||||
|
member = generator.create_struct_member_vmlinux(
|
||||||
|
field_name, field_type, field.offset * 8
|
||||||
|
)
|
||||||
|
members.append(member)
|
||||||
|
|
||||||
|
if struct.name.startswith("struct_"):
|
||||||
|
struct_name = struct.name.removeprefix("struct_")
|
||||||
|
else:
|
||||||
|
raise ValueError("Unions are not supported in the current version")
|
||||||
|
# Create struct type with all members
|
||||||
|
struct_type = generator.create_struct_type_with_name(
|
||||||
|
struct_name, members, struct.__sizeof__() * 8, is_distinct=True
|
||||||
)
|
)
|
||||||
|
|
||||||
return global_var
|
return struct_type
|
||||||
|
|
||||||
|
|
||||||
|
def _get_field_debug_type(
|
||||||
|
field_name: str,
|
||||||
|
field,
|
||||||
|
generator: DebugInfoGenerator,
|
||||||
|
parent_struct: DependencyNode,
|
||||||
|
generated_debug_info: List[Tuple[DependencyNode, Any]],
|
||||||
|
) -> tuple[Any, int]:
|
||||||
|
"""
|
||||||
|
Determine the appropriate debug type for a field based on its Python/ctypes type.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
field_name: Name of the field
|
||||||
|
field: Field object containing type information
|
||||||
|
generator: DebugInfoGenerator instance
|
||||||
|
parent_struct: The parent struct containing this field
|
||||||
|
generated_debug_info: List of already generated debug info
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The debug info type for this field
|
||||||
|
"""
|
||||||
|
# Handle complex types (arrays, pointers)
|
||||||
|
if field.ctype_complex_type is not None:
|
||||||
|
if issubclass(field.ctype_complex_type, ctypes.Array):
|
||||||
|
# Handle array types
|
||||||
|
element_type, base_type_size = _get_basic_debug_type(
|
||||||
|
field.containing_type, generator
|
||||||
|
)
|
||||||
|
return generator.create_array_type_vmlinux(
|
||||||
|
(element_type, base_type_size * field.type_size), field.type_size
|
||||||
|
), field.type_size * base_type_size
|
||||||
|
elif issubclass(field.ctype_complex_type, ctypes._Pointer):
|
||||||
|
# Handle pointer types
|
||||||
|
pointee_type, _ = _get_basic_debug_type(field.containing_type, generator)
|
||||||
|
return generator.create_pointer_type(pointee_type), 64
|
||||||
|
|
||||||
|
# Handle other vmlinux types (nested structs)
|
||||||
|
if field.type.__module__ == "vmlinux":
|
||||||
|
# If it's a struct from vmlinux, check if we've already generated debug info for it
|
||||||
|
struct_name = field.type.__name__
|
||||||
|
|
||||||
|
# Look for existing debug info in the list
|
||||||
|
for existing_struct, debug_info in generated_debug_info:
|
||||||
|
if existing_struct.name == struct_name:
|
||||||
|
# Use existing debug info
|
||||||
|
return debug_info, existing_struct.__sizeof__()
|
||||||
|
|
||||||
|
# If not found, create a forward declaration
|
||||||
|
# This will be completed when the actual struct is processed
|
||||||
|
logger.warning("Forward declaration in struct created")
|
||||||
|
forward_type = generator.create_struct_type([], 0, is_distinct=True)
|
||||||
|
return forward_type, 0
|
||||||
|
|
||||||
|
# Handle basic C types
|
||||||
|
return _get_basic_debug_type(field.type, generator)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_basic_debug_type(ctype, generator: DebugInfoGenerator) -> Any:
|
||||||
|
"""
|
||||||
|
Map a ctypes type to a DWARF debug type.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
ctype: A ctypes type or Python type
|
||||||
|
generator: DebugInfoGenerator instance
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The corresponding debug type
|
||||||
|
"""
|
||||||
|
# Map ctypes to debug info types
|
||||||
|
if ctype == ctypes.c_char or ctype == ctypes.c_byte:
|
||||||
|
return generator.get_basic_type("char", 8, dc.DW_ATE_signed_char), 8
|
||||||
|
elif ctype == ctypes.c_ubyte or ctype == ctypes.c_uint8:
|
||||||
|
return generator.get_basic_type("unsigned char", 8, dc.DW_ATE_unsigned_char), 8
|
||||||
|
elif ctype == ctypes.c_short or ctype == ctypes.c_int16:
|
||||||
|
return generator.get_basic_type("short", 16, dc.DW_ATE_signed), 16
|
||||||
|
elif ctype == ctypes.c_ushort or ctype == ctypes.c_uint16:
|
||||||
|
return generator.get_basic_type("unsigned short", 16, dc.DW_ATE_unsigned), 16
|
||||||
|
elif ctype == ctypes.c_int or ctype == ctypes.c_int32:
|
||||||
|
return generator.get_basic_type("int", 32, dc.DW_ATE_signed), 32
|
||||||
|
elif ctype == ctypes.c_uint or ctype == ctypes.c_uint32:
|
||||||
|
return generator.get_basic_type("unsigned int", 32, dc.DW_ATE_unsigned), 32
|
||||||
|
elif ctype == ctypes.c_long:
|
||||||
|
return generator.get_basic_type("long", 64, dc.DW_ATE_signed), 64
|
||||||
|
elif ctype == ctypes.c_ulong:
|
||||||
|
return generator.get_basic_type("unsigned long", 64, dc.DW_ATE_unsigned), 64
|
||||||
|
elif ctype == ctypes.c_longlong or ctype == ctypes.c_int64:
|
||||||
|
return generator.get_basic_type("long long", 64, dc.DW_ATE_signed), 64
|
||||||
|
elif ctype == ctypes.c_ulonglong or ctype == ctypes.c_uint64:
|
||||||
|
return generator.get_basic_type(
|
||||||
|
"unsigned long long", 64, dc.DW_ATE_unsigned
|
||||||
|
), 64
|
||||||
|
elif ctype == ctypes.c_float:
|
||||||
|
return generator.get_basic_type("float", 32, dc.DW_ATE_float), 32
|
||||||
|
elif ctype == ctypes.c_double:
|
||||||
|
return generator.get_basic_type("double", 64, dc.DW_ATE_float), 64
|
||||||
|
elif ctype == ctypes.c_bool:
|
||||||
|
return generator.get_basic_type("bool", 8, dc.DW_ATE_boolean), 8
|
||||||
|
elif ctype == ctypes.c_char_p:
|
||||||
|
char_type = generator.get_basic_type("char", 8, dc.DW_ATE_signed_char), 8
|
||||||
|
return generator.create_pointer_type(char_type)
|
||||||
|
elif ctype == ctypes.c_void_p:
|
||||||
|
return generator.create_pointer_type(None), 64
|
||||||
|
else:
|
||||||
|
return generator.get_uint64_type(), 64
|
||||||
|
|||||||
@ -14,6 +14,7 @@ class IRGenerator:
|
|||||||
self.llvm_module = llvm_module
|
self.llvm_module = llvm_module
|
||||||
self.handler: DependencyHandler = handler
|
self.handler: DependencyHandler = handler
|
||||||
self.generated: list[str] = []
|
self.generated: list[str] = []
|
||||||
|
self.generated_debug_info: list = []
|
||||||
if not handler.is_ready:
|
if not handler.is_ready:
|
||||||
raise ImportError(
|
raise ImportError(
|
||||||
"Semantic analysis of vmlinux imports failed. Cannot generate IR"
|
"Semantic analysis of vmlinux imports failed. Cannot generate IR"
|
||||||
@ -67,18 +68,22 @@ class IRGenerator:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Actual processor logic here after dependencies are resolved
|
# Actual processor logic here after dependencies are resolved
|
||||||
self.gen_ir(struct)
|
self.generated_debug_info.append(
|
||||||
|
(struct, self.gen_ir(struct, self.generated_debug_info))
|
||||||
|
)
|
||||||
self.generated.append(struct.name)
|
self.generated.append(struct.name)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# Remove from processing stack after we're done
|
# Remove from processing stack after we're done
|
||||||
processing_stack.discard(struct.name)
|
processing_stack.discard(struct.name)
|
||||||
|
|
||||||
def gen_ir(self, struct):
|
def gen_ir(self, struct, generated_debug_info):
|
||||||
# TODO: we add the btf_ama attribute by monkey patching in the end of compilation, but once llvmlite
|
# TODO: we add the btf_ama attribute by monkey patching in the end of compilation, but once llvmlite
|
||||||
# accepts our issue, we will resort to normal accessed attribute based attribute addition
|
# accepts our issue, we will resort to normal accessed attribute based attribute addition
|
||||||
# currently we generate all possible field accesses for CO-RE and put into the assignment table
|
# currently we generate all possible field accesses for CO-RE and put into the assignment table
|
||||||
debug_info = debug_info_generation(struct, self.llvm_module)
|
debug_info = debug_info_generation(
|
||||||
|
struct, self.llvm_module, generated_debug_info
|
||||||
|
)
|
||||||
field_index = 0
|
field_index = 0
|
||||||
for field_name, field in struct.fields.items():
|
for field_name, field in struct.fields.items():
|
||||||
# does not take arrays and similar types into consideration yet.
|
# does not take arrays and similar types into consideration yet.
|
||||||
@ -126,6 +131,7 @@ class IRGenerator:
|
|||||||
)
|
)
|
||||||
globvar.linkage = "external"
|
globvar.linkage = "external"
|
||||||
globvar.set_metadata("llvm.preserve.access.index", debug_info)
|
globvar.set_metadata("llvm.preserve.access.index", debug_info)
|
||||||
|
return debug_info
|
||||||
|
|
||||||
def _struct_name_generator(
|
def _struct_name_generator(
|
||||||
self,
|
self,
|
||||||
@ -136,6 +142,7 @@ class IRGenerator:
|
|||||||
index: int = 0,
|
index: int = 0,
|
||||||
containing_type_size: int = 0,
|
containing_type_size: int = 0,
|
||||||
) -> str:
|
) -> str:
|
||||||
|
# TODO: Does not support Unions as well as recursive pointer and array type naming
|
||||||
if is_indexed:
|
if is_indexed:
|
||||||
name = (
|
name = (
|
||||||
"llvm."
|
"llvm."
|
||||||
|
|||||||
@ -19,7 +19,7 @@ struct {
|
|||||||
SEC("tp/syscalls/sys_enter_setuid")
|
SEC("tp/syscalls/sys_enter_setuid")
|
||||||
int handle_setuid_entry(struct trace_event_raw_sys_enter *ctx) {
|
int handle_setuid_entry(struct trace_event_raw_sys_enter *ctx) {
|
||||||
struct event data = {};
|
struct event data = {};
|
||||||
|
struct blk_integrity_iter it = {};
|
||||||
// Extract UID from the syscall arguments
|
// Extract UID from the syscall arguments
|
||||||
data.uid = (unsigned int)ctx->args[0];
|
data.uid = (unsigned int)ctx->args[0];
|
||||||
data.ts = bpf_ktime_get_ns();
|
data.ts = bpf_ktime_get_ns();
|
||||||
|
|||||||
29
tests/passing_tests/vmlinux/simple_struct_test.py
Normal file
29
tests/passing_tests/vmlinux/simple_struct_test.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from pythonbpf import bpf, section, bpfglobal, compile_to_ir
|
||||||
|
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_blk_integrity_iter
|
||||||
|
from ctypes import c_int64
|
||||||
|
|
||||||
|
|
||||||
|
# Instructions to how to run this program
|
||||||
|
# 1. Install PythonBPF: pip install pythonbpf
|
||||||
|
# 2. Run the program: python examples/simple_struct_test.py
|
||||||
|
# 3. Run the program with sudo: sudo tools/check.sh run examples/simple_struct_test.o
|
||||||
|
# 4. Attach object file to any network device with something like ./check.sh run examples/simple_struct_test.o tailscale0
|
||||||
|
# 5. send traffic through the device and observe effects
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/syscalls/sys_enter_execve")
|
||||||
|
def hello_world(ctx: struct_trace_event_raw_sys_enter) -> c_int64:
|
||||||
|
print("Hello, World!")
|
||||||
|
return c_int64(0)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile_to_ir("simple_struct_test.py", "simple_struct_test.ll")
|
||||||
Reference in New Issue
Block a user