Skip to content

Commit 01bd760

Browse files
add global symbol table populate function
1 parent 7ae84a0 commit 01bd760

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

pythonbpf/codegen.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from .functions_pass import func_proc
55
from .maps import maps_proc
66
from .structs import structs_proc
7-
from .globals_pass import globals_list_creation, globals_processing
7+
from .globals_pass import (
8+
globals_list_creation,
9+
globals_processing,
10+
populate_global_symbol_table,
11+
)
812
from .debuginfo import DW_LANG_C11, DwarfBehaviorEnum, DebugInfoGenerator
913
import os
1014
import subprocess
@@ -40,6 +44,7 @@ def processor(source_code, filename, module):
4044
for func_node in bpf_chunks:
4145
logger.info(f"Found BPF function/struct: {func_node.name}")
4246

47+
populate_global_symbol_table(tree, module)
4348
license_processing(tree, module)
4449
globals_processing(tree, module)
4550

pythonbpf/globals_pass.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@
77

88
logger: Logger = logging.getLogger(__name__)
99

10+
# TODO: this is going to be a huge fuck of a headache in the future.
11+
global_sym_tab = []
12+
13+
14+
def populate_global_symbol_table(tree, module: ir.Module):
15+
for node in tree.body:
16+
if isinstance(node, ast.FunctionDef):
17+
for dec in node.decorator_list:
18+
if (
19+
isinstance(dec, ast.Call)
20+
and isinstance(dec.func, ast.Name)
21+
and dec.func.id == "section"
22+
and len(dec.args) == 1
23+
and isinstance(dec.args[0], ast.Constant)
24+
and isinstance(dec.args[0].value, str)
25+
):
26+
global_sym_tab.append(node)
27+
elif isinstance(dec, ast.Name) and dec.id == "bpfglobal":
28+
global_sym_tab.append(node)
29+
30+
elif isinstance(dec, ast.Name) and dec.id == "map":
31+
global_sym_tab.append(node)
32+
return False
33+
1034

1135
def emit_global(module: ir.Module, node, name):
1236
logger.info(f"global identifier {name} processing")

tests/failing_tests/globals.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def somevalue1() -> c_int32:
2525
@bpf
2626
@bpfglobal
2727
def g1() -> c_int64:
28-
return 42
28+
return c_int64(42)
2929

3030
# Constructor with one constant argument
3131
@bpf
@@ -56,18 +56,18 @@ def g2() -> c_int64:
5656

5757
# # Return is a variable reference
5858
# #TODO: maybe fix this sometime later. It defaults to 0
59-
CONST = 5
60-
@bpf
61-
@bpfglobal
62-
def g6() -> c_int64:
63-
return c_int64(CONST)
59+
# CONST = 5
60+
# @bpf
61+
# @bpfglobal
62+
# def g6() -> c_int64:
63+
# return c_int64(CONST)
6464

6565
# Constructor with multiple args
6666
#TODO: this is not working. should it work ?
6767
@bpf
6868
@bpfglobal
6969
def g7() -> c_int64:
70-
return c_int64(1, 2)
70+
return c_int64(1)
7171

7272
# Dataclass call
7373
#TODO: fails with dataclass
@@ -86,6 +86,9 @@ def g7() -> c_int64:
8686
@section("tracepoint/syscalls/sys_enter_execve")
8787
def sometag(ctx: c_void_p) -> c_int64:
8888
print("test")
89+
global somevalue
90+
somevalue = 2
91+
print(f"{somevalue}")
8992
return c_int64(1)
9093

9194
@bpf

0 commit comments

Comments
 (0)