mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
add global symbol table populate function
This commit is contained in:
@ -4,7 +4,11 @@ from .license_pass import license_processing
|
|||||||
from .functions_pass import func_proc
|
from .functions_pass import func_proc
|
||||||
from .maps import maps_proc
|
from .maps import maps_proc
|
||||||
from .structs import structs_proc
|
from .structs import structs_proc
|
||||||
from .globals_pass import globals_list_creation, globals_processing
|
from .globals_pass import (
|
||||||
|
globals_list_creation,
|
||||||
|
globals_processing,
|
||||||
|
populate_global_symbol_table,
|
||||||
|
)
|
||||||
from .debuginfo import DW_LANG_C11, DwarfBehaviorEnum, DebugInfoGenerator
|
from .debuginfo import DW_LANG_C11, DwarfBehaviorEnum, DebugInfoGenerator
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
@ -40,6 +44,7 @@ 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}")
|
||||||
|
|
||||||
|
populate_global_symbol_table(tree, module)
|
||||||
license_processing(tree, module)
|
license_processing(tree, module)
|
||||||
globals_processing(tree, module)
|
globals_processing(tree, module)
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,30 @@ from .type_deducer import ctypes_to_ir
|
|||||||
|
|
||||||
logger: Logger = logging.getLogger(__name__)
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# TODO: this is going to be a huge fuck of a headache in the future.
|
||||||
|
global_sym_tab = []
|
||||||
|
|
||||||
|
|
||||||
|
def populate_global_symbol_table(tree, module: ir.Module):
|
||||||
|
for node in tree.body:
|
||||||
|
if isinstance(node, ast.FunctionDef):
|
||||||
|
for dec in node.decorator_list:
|
||||||
|
if (
|
||||||
|
isinstance(dec, ast.Call)
|
||||||
|
and isinstance(dec.func, ast.Name)
|
||||||
|
and dec.func.id == "section"
|
||||||
|
and len(dec.args) == 1
|
||||||
|
and isinstance(dec.args[0], ast.Constant)
|
||||||
|
and isinstance(dec.args[0].value, str)
|
||||||
|
):
|
||||||
|
global_sym_tab.append(node)
|
||||||
|
elif isinstance(dec, ast.Name) and dec.id == "bpfglobal":
|
||||||
|
global_sym_tab.append(node)
|
||||||
|
|
||||||
|
elif isinstance(dec, ast.Name) and dec.id == "map":
|
||||||
|
global_sym_tab.append(node)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def emit_global(module: ir.Module, node, name):
|
def emit_global(module: ir.Module, node, name):
|
||||||
logger.info(f"global identifier {name} processing")
|
logger.info(f"global identifier {name} processing")
|
||||||
|
|||||||
@ -25,7 +25,7 @@ def somevalue1() -> c_int32:
|
|||||||
@bpf
|
@bpf
|
||||||
@bpfglobal
|
@bpfglobal
|
||||||
def g1() -> c_int64:
|
def g1() -> c_int64:
|
||||||
return 42
|
return c_int64(42)
|
||||||
|
|
||||||
# Constructor with one constant argument
|
# Constructor with one constant argument
|
||||||
@bpf
|
@bpf
|
||||||
@ -56,18 +56,18 @@ def g2() -> c_int64:
|
|||||||
|
|
||||||
# # Return is a variable reference
|
# # Return is a variable reference
|
||||||
# #TODO: maybe fix this sometime later. It defaults to 0
|
# #TODO: maybe fix this sometime later. It defaults to 0
|
||||||
CONST = 5
|
# CONST = 5
|
||||||
@bpf
|
# @bpf
|
||||||
@bpfglobal
|
# @bpfglobal
|
||||||
def g6() -> c_int64:
|
# def g6() -> c_int64:
|
||||||
return c_int64(CONST)
|
# return c_int64(CONST)
|
||||||
|
|
||||||
# Constructor with multiple args
|
# Constructor with multiple args
|
||||||
#TODO: this is not working. should it work ?
|
#TODO: this is not working. should it work ?
|
||||||
@bpf
|
@bpf
|
||||||
@bpfglobal
|
@bpfglobal
|
||||||
def g7() -> c_int64:
|
def g7() -> c_int64:
|
||||||
return c_int64(1, 2)
|
return c_int64(1)
|
||||||
|
|
||||||
# Dataclass call
|
# Dataclass call
|
||||||
#TODO: fails with dataclass
|
#TODO: fails with dataclass
|
||||||
@ -86,6 +86,9 @@ def g7() -> c_int64:
|
|||||||
@section("tracepoint/syscalls/sys_enter_execve")
|
@section("tracepoint/syscalls/sys_enter_execve")
|
||||||
def sometag(ctx: c_void_p) -> c_int64:
|
def sometag(ctx: c_void_p) -> c_int64:
|
||||||
print("test")
|
print("test")
|
||||||
|
global somevalue
|
||||||
|
somevalue = 2
|
||||||
|
print(f"{somevalue}")
|
||||||
return c_int64(1)
|
return c_int64(1)
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
|
|||||||
Reference in New Issue
Block a user