Skip to content

Commit 7aeac86

Browse files
fix broken IR generation logic for globals
1 parent ab1c422 commit 7aeac86

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

pythonbpf/globals_pass.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ def emit_global(module: ir.Module, node, name):
3434

3535
# constructor call like "return c_int64(0)" or dataclass(...)
3636
elif isinstance(init_val, ast.Call):
37-
if len(init_val.args) == 1 and isinstance(init_val.args[0], ast.Constant):
37+
if len(init_val.args) >= 1 and isinstance(init_val.args[0], ast.Constant):
3838
llvm_init = ir.Constant(ty, init_val.args[0].value)
3939
else:
40-
raise ValueError(f"Complex constructor not supported: {ast.dump(init_val)}")
41-
40+
logger.info("Defaulting to zero as no constant argument found")
41+
llvm_init = ir.Constant(ty, 0)
4242
else:
4343
raise ValueError(f"Unsupported return expr {ast.dump(init_val)}")
4444

@@ -49,7 +49,6 @@ def emit_global(module: ir.Module, node, name):
4949
gvar.global_constant = False
5050
return gvar
5151

52-
5352
def globals_processing(tree, module):
5453
"""Process stuff decorated with @bpf and @bpfglobal except license and return the section name"""
5554
globals_sym_tab = []

tests/failing_tests/globals.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,73 @@ def somevalue2() -> c_int64:
1818
def somevalue1() -> c_int32:
1919
return c_int32(0)
2020

21+
import ast
22+
from dataclasses import dataclass
23+
from typing import List
24+
25+
# --- Passing examples ---
26+
27+
# Simple constant return
28+
@bpf
29+
@bpfglobal
30+
def g1() -> c_int64:
31+
return 42
32+
33+
# Constructor with one constant argument
34+
@bpf
35+
@bpfglobal
36+
def g2() -> c_int64:
37+
return c_int64(0)
38+
39+
40+
# --- Failing examples ---
41+
42+
# No return annotation
43+
# @bpf
44+
# @bpfglobal
45+
# def g3():
46+
# return 42
47+
48+
# Return annotation is complex
49+
# @bpf
50+
# @bpfglobal
51+
# def g4() -> List[int]:
52+
# return []
53+
54+
# # Return is missing
55+
# @bpf
56+
# @bpfglobal
57+
# def g5() -> c_int64:
58+
# pass
59+
60+
# # Return is a variable reference
61+
# #TODO: maybe fix this sometime later. It defaults to 0
62+
CONST = 5
63+
@bpf
64+
@bpfglobal
65+
def g6() -> c_int64:
66+
return c_int64(CONST)
67+
68+
# Constructor with multiple args
69+
#TODO: this is not working. should it work ?
70+
@bpf
71+
@bpfglobal
72+
def g7() -> c_int64:
73+
return c_int64(1, 2)
74+
75+
# Dataclass call
76+
#TODO: fails with dataclass
77+
# @dataclass
78+
# class Point:
79+
# x: c_int64
80+
# y: c_int64
81+
82+
# @bpf
83+
# @bpfglobal
84+
# def g8() -> Point:
85+
# return Point(1, 2)
86+
87+
2188
@bpf
2289
@section("tracepoint/syscalls/sys_enter_execve")
2390
def sometag(ctx: c_void_p) -> c_int64:

0 commit comments

Comments
 (0)