Skip to content

Commit 03404bb

Browse files
make license handling easier
1 parent d6ba308 commit 03404bb

4 files changed

Lines changed: 35 additions & 21 deletions

File tree

examples/execve2.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ def hello_again(ctx: c_void_p) -> c_int64:
2828
ts = bpf_ktime_get_ns()
2929
return c_int64(0)
3030

31-
# @bpf
32-
# @bpfglobal
33-
# def LICENSE() -> str:
34-
# return "GPL"
35-
36-
37-
LICENSE = "GPL"
31+
@bpf
32+
@bpfglobal
33+
def LICENSE() -> str:
34+
return "GPL"

pythonbpf/functions_pass.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def get_probe_string(func_node):
1212
# For helper functions, we return "helper"
1313

1414
for decorator in func_node.decorator_list:
15+
if isinstance(decorator, ast.Name) and decorator.id == "bpfglobal":
16+
return None
1517
if isinstance(decorator, ast.Call) and isinstance(decorator.func, ast.Name):
1618
if decorator.func.id == "section" and len(decorator.args) == 1:
1719
arg = decorator.args[0]
@@ -134,7 +136,9 @@ def process_bpf_chunk(func_node, module, return_type, map_sym_tab):
134136
param = func.args[0]
135137
param.add_attribute("nocapture")
136138

137-
func.section = get_probe_string(func_node)
139+
probe_string = get_probe_string(func_node)
140+
if probe_string is not None:
141+
func.section = probe_string
138142

139143
block = func.append_basic_block(name="entry")
140144
builder = ir.IRBuilder(block)
@@ -151,6 +155,9 @@ def func_proc(tree, module, chunks, map_sym_tab):
151155
if isinstance(decorator, ast.Name) and decorator.id == "map":
152156
is_global = True
153157
break
158+
elif isinstance(decorator, ast.Name) and decorator.id == "bpfglobal":
159+
is_global = True
160+
break
154161
if is_global:
155162
continue
156163
func_type = get_probe_string(func_node)

pythonbpf/license_pass.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,28 @@ def emit_license(module: ir.Module, license_str: str):
1818
return gvar
1919

2020
def license_processing(tree, module):
21-
"""Process the LICENSE assignment in the given AST tree and return the section name"""
21+
"""Process the LICENSE function decorated with @bpf and @bpfglobal and return the section name"""
2222
count = 0
2323
for node in tree.body:
24-
if isinstance(node, ast.Assign):
25-
for target in node.targets:
26-
if isinstance(target, ast.Name) and target.id == "LICENSE":
27-
if count == 0:
28-
count += 1
29-
if isinstance(node.value, ast.Constant) and isinstance(node.value.value, str):
30-
emit_license(module, node.value.value)
31-
return "LICENSE"
32-
else:
33-
print("ERROR: LICENSE must be a string literal")
34-
return None
24+
if isinstance(node, ast.FunctionDef) and node.name == "LICENSE":
25+
# check decorators
26+
decorators = [dec.id for dec in node.decorator_list if isinstance(dec, ast.Name)]
27+
if "bpf" in decorators and "bpfglobal" in decorators:
28+
if count == 0:
29+
count += 1
30+
# check function body has a return string
31+
if (
32+
len(node.body) == 1
33+
and isinstance(node.body[0], ast.Return)
34+
and isinstance(node.body[0].value, ast.Constant)
35+
and isinstance(node.body[0].value.value, str)
36+
):
37+
emit_license(module, node.body[0].value.value)
38+
return "LICENSE"
3539
else:
36-
print("ERROR: LICENSE already assigned")
40+
print("ERROR: LICENSE() must return a string literal")
3741
return None
42+
else:
43+
print("ERROR: LICENSE already defined")
44+
return None
45+
return None

pythonbpf/type_deducer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def ctypes_to_ir(ctype: str):
1414
"c_float": ir.FloatType(),
1515
"c_double": ir.DoubleType(),
1616
"c_void_p": ir.IntType(64),
17+
# Not so sure about this one
18+
"str": ir.PointerType(ir.IntType(8))
1719
}
1820
if ctype in mapping:
1921
return mapping[ctype]

0 commit comments

Comments
 (0)