Skip to content

Commit ee03ac0

Browse files
committed
Fix printk handler to comply with new symtab convention
1 parent 51595f9 commit ee03ac0

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

pythonbpf/bpf_helper_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None,
121121
"Warning: bpf_printk supports up to 3 arguments, extra arguments will be ignored.")
122122

123123
for expr in exprs[:3]:
124-
val = eval_expr(func, module, builder, expr, local_sym_tab, None)
124+
val, _ = eval_expr(func, module, builder,
125+
expr, local_sym_tab, None)
125126
if val:
126127
if isinstance(val.type, ir.PointerType):
127128
val = builder.ptrtoint(val, ir.IntType(64))
@@ -137,7 +138,6 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None,
137138
print(
138139
"Warning: Failed to evaluate expression for bpf_printk argument. It will be converted to 0.")
139140
args.append(ir.Constant(ir.IntType(64), 0))
140-
141141
fn_type = ir.FunctionType(ir.IntType(
142142
64), [ir.PointerType(), ir.IntType(32)], var_arg=True)
143143
fn_ptr_type = ir.PointerType(fn_type)

pythonbpf/codegen.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def compile_to_ir(filename: str, output: str):
9393

9494
module.add_named_metadata("llvm.ident", ["llvmlite PythonBPF v0.0.1"])
9595

96+
print(f"IR written to {output}")
9697
with open(output, "w") as f:
9798
f.write(f"source_filename = \"{filename}\"\n")
9899
f.write(str(module))
@@ -118,6 +119,7 @@ def compile():
118119

119120
print(f"Object written to {o_file}, {ll_file} can be removed")
120121

122+
121123
def BPF() -> BpfProgram:
122124
caller_frame = inspect.stack()[1]
123125
caller_file = Path(caller_frame.filename).resolve()
@@ -129,5 +131,5 @@ def BPF() -> BpfProgram:
129131
"llc", "-march=bpf", "-filetype=obj", "-O2",
130132
str(ll_file), "-o", str(o_file)
131133
], check=True)
132-
134+
133135
return BpfProgram(str(o_file))

pythonbpf/expr_pass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
def eval_expr(func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab=None, local_var_metadata=None):
6-
print(f"Evaluating expression: {expr}")
6+
print(f"Evaluating expression: {ast.dump(expr)}")
77
if isinstance(expr, ast.Name):
88
if expr.id in local_sym_tab:
99
var = local_sym_tab[expr.id][0]

pythonbpf/functions_pass.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc
6161
if val is None:
6262
print("Failed to evaluate struct field assignment")
6363
return
64-
builder.store(val, field_ptr)
64+
builder.store(val[0], field_ptr)
6565
print(f"Assigned to struct field {var_name}.{field_name}")
6666
return
6767
elif isinstance(rval, ast.Constant):
@@ -114,7 +114,7 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc
114114
# var.align = 8
115115
val = handle_helper_call(
116116
rval, module, builder, func, local_sym_tab, map_sym_tab, structs_sym_tab, local_var_metadata)
117-
builder.store(val, local_sym_tab[var_name][0])
117+
builder.store(val[0], local_sym_tab[var_name][0])
118118
# local_sym_tab[var_name] = var
119119
print(f"Assigned constant {rval.func.id} to {var_name}")
120120
elif call_type == "deref" and len(rval.args) == 1:
@@ -125,7 +125,7 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc
125125
print("Failed to evaluate deref argument")
126126
return
127127
print(f"Dereferenced value: {val}, storing in {var_name}")
128-
builder.store(val, local_sym_tab[var_name][0])
128+
builder.store(val[0], local_sym_tab[var_name][0])
129129
# local_sym_tab[var_name] = var
130130
print(f"Dereferenced and assigned to {var_name}")
131131
elif call_type in structs_sym_tab and len(rval.args) == 0:
@@ -155,7 +155,7 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc
155155
rval, module, builder, func, local_sym_tab, map_sym_tab, structs_sym_tab, local_var_metadata)
156156
# var = builder.alloca(ir.IntType(64), name=var_name)
157157
# var.align = 8
158-
builder.store(val, local_sym_tab[var_name][0])
158+
builder.store(val[0], local_sym_tab[var_name][0])
159159
# local_sym_tab[var_name] = var
160160
else:
161161
print("Unsupported assignment call structure")
@@ -462,7 +462,6 @@ def process_bpf_chunk(func_node, module, return_type, map_sym_tab, structs_sym_t
462462

463463
process_func_body(module, builder, func_node, func,
464464
ret_type, map_sym_tab, structs_sym_tab)
465-
466465
return func
467466

468467

0 commit comments

Comments
 (0)