Skip to content

Commit d872934

Browse files
add bpf_passthrough generation
1 parent 4179fbf commit d872934

File tree

5 files changed

+47
-150004
lines changed

5 files changed

+47
-150004
lines changed

pythonbpf/vmlinux_parser/import_detector.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import importlib
44
import inspect
5+
import llvmlite.ir as ir
56

67
from .assignment_info import AssignmentInfo, AssignmentType
78
from .dependency_handler import DependencyHandler
@@ -76,9 +77,28 @@ def detect_import_statement(tree: ast.AST) -> list[tuple[str, ast.ImportFrom]]:
7677
return vmlinux_imports
7778

7879

80+
def bpf_passthrough_gen(module):
81+
i32_ty = ir.IntType(32)
82+
ptr_ty = ir.PointerType(ir.IntType(8))
83+
fnty = ir.FunctionType(ptr_ty, [i32_ty, ptr_ty])
84+
85+
# Declare the intrinsic
86+
passthrough = ir.Function(module, fnty, "llvm.bpf.passthrough.p0.p0")
87+
88+
# Set function attributes
89+
# TODO: the ones commented are supposed to be there but cannot be added due to llvmlite limitations at the moment
90+
# passthrough.attributes.add("nofree")
91+
# passthrough.attributes.add("nosync")
92+
passthrough.attributes.add("nounwind")
93+
# passthrough.attributes.add("memory(none)")
94+
95+
return passthrough
96+
97+
7998
def vmlinux_proc(tree: ast.AST, module):
8099
import_statements = detect_import_statement(tree)
81100

101+
bpf_passthrough_gen(module)
82102
# initialise dependency handler
83103
handler = DependencyHandler()
84104
# initialise assignment dictionary of name to type

tests/c-form/ex5.bpf.c

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/c-form/struct_field_tests.bpf.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,33 @@
44
#include <bpf/bpf_helpers.h>
55
#include <bpf/bpf_tracing.h>
66

7+
/*
8+
Information gained from reversing this (multiple kernel versions):
9+
There is no point of
10+
```llvm
11+
tail call void @llvm.dbg.value(metadata ptr %0, metadata !60, metadata !DIExpression()), !dbg !70
12+
```
13+
and the first argument of passthrough is fucking useless. It just needs to be a distinct integer:
14+
```llvm
15+
%9 = tail call ptr @llvm.bpf.passthrough.p0.p0(i32 3, ptr %8)
16+
```
17+
*/
18+
719
SEC("tp/syscalls/sys_enter_execve")
820
int handle_setuid_entry(struct trace_event_raw_sys_enter *ctx) {
9-
bpf_printk("args: %u", (unsigned int)ctx->args[0]);
21+
// Access each argument separately with clear variable assignments
22+
unsigned long arg0 = ctx->args[0];
23+
bpf_printk("args[0]: %u", arg0);
24+
25+
unsigned long arg1 = ctx->args[1];
26+
bpf_printk("args[1]: %u", arg1);
27+
28+
// Remove the duplicate access to args[1]
29+
30+
unsigned long arg2 = ctx->args[2];
31+
bpf_printk("args[3]: %u", arg2);
32+
bpf_printk("args[4]: %u", ctx->args[2]);
33+
1034
return 0;
1135
}
1236

0 commit comments

Comments
 (0)