Skip to content

Commit c055963

Browse files
Get ex2 running
1 parent 734a49b commit c055963

11 files changed

Lines changed: 72 additions & 35 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
compile:
22
chmod +x ./tools/compile.py
3-
./tools/compile.py ./examples/execve.py
3+
./tools/compile.py ./examples/execve2.py
44

55
install:
66
pip install -e .
77

88
clean:
99
rm -rf build dist *.egg-info
10-
rm -rf examples/execve.ll examples/execve.o
10+
rm -rf examples/*.ll examples/*.o
1111

1212
all: install compile
1313

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Step 2. Run `make` to see the compilation output of the example.
88
Step 3. Run `check.sh` to check if generated object file passes through the verifier inside the examples directory.
99
Step 4. Run `make` in the `examples/c-form` directory to modify the example C BPF program to check the actual LLVM IR generated by clang.
1010

11+
### Development Notes
12+
Run ` ./check.sh run execve2.o;` in examples folder
13+
1114
## Authors
1215
- [@r41k0u](https://github.com/r41k0u)
1316
- [@varun-r-mallya](https://github.com/varun-r-mallya)

examples/c-form/Makefile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
BPF_CLANG := clang
22
CFLAGS := -O2 -emit-llvm -target bpf -c
33

4-
SRC := ex3.bpf.c
5-
OUT := ex3.bpf.ll
6-
OBJECT := ex3.bpf.o
4+
SRC := $(wildcard *.bpf.c)
5+
LL := $(SRC:.bpf.c=.bpf.ll)
6+
OBJ := $(SRC:.bpf.c=.bpf.o)
77

88
.PHONY: all clean
99

10-
all: $(OUT)
11-
12-
object: $(SRC)
13-
$(BPF_CLANG) -O2 -g -target bpf -c $< -o $(OBJECT)
10+
all: $(LL) $(OBJ)
1411

15-
$(OUT): $(SRC) object
12+
%.bpf.o: %.bpf.c
13+
$(BPF_CLANG) -O2 -target bpf -c $< -o $@
14+
15+
%.bpf.ll: %.bpf.c
1616
$(BPF_CLANG) $(CFLAGS) -S $< -o $@
1717

1818
clean:
19-
rm -f $(OUT) $(OBJECT)
19+
rm -f $(LL) $(OBJ)

examples/c-form/ex2.bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <linux/bpf.h>
22
#include <bpf/bpf_helpers.h>
33

4-
SEC("kprobe/sys_clone")
4+
SEC("tracepoint/syscalls/sys_enter_execve")
55
int hello(struct pt_regs *ctx) {
66
bpf_printk("Hello, World!\n");
77
return 0;

examples/check.sh

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,33 @@
11
#!/bin/bash
2-
sudo bpftool prog -d load $1 /sys/fs/bpf/tmp && sudo rm -f /sys/fs/bpf/tmp
2+
3+
PIN_PATH="/sys/fs/bpf/bpf_prog"
4+
FILE="$2"
5+
case "$1" in
6+
check)
7+
echo "[*] Checking $FILE"
8+
echo $(sudo bpftool prog load -d "$FILE" "$PIN_PATH")
9+
sudo rm -f "$PIN_PATH"
10+
echo "[+] Verification succeeded"
11+
;;
12+
run)
13+
echo "[*] Loading and running $FILE"
14+
sudo bpftool prog load "$FILE" "$PIN_PATH" autoattach
15+
echo "[+] Program loaded. Press Ctrl+C to stop"
16+
sudo cat /sys/kernel/debug/tracing/trace_pipe
17+
sudo rm -f "$PIN_PATH"
18+
echo "[+] Stopped"
19+
;;
20+
stop)
21+
echo "[*] Stopping program"
22+
sudo rm -f "$PIN_PATH"
23+
echo "[+] Stopped"
24+
;;
25+
*)
26+
echo "Usage: $0 <check|run|stop> <file.o>"
27+
echo "Examples:"
28+
echo " $0 check program.bpf.o"
29+
echo " $0 run program.bpf.o"
30+
echo " $0 stop"
31+
exit 1
32+
;;
33+
esac

examples/execve2.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from pythonbpf.decorators import bpf, section
2-
# from pythonbpf.decorators import tracepoint, syscalls
32
from ctypes import c_void_p, c_int32
43

54

65
@bpf
7-
@section("kprobe/sys_clone")
6+
@section("tracepoint/syscalls/sys_enter_execve")
87
def hello(ctx: c_void_p) -> c_int32:
98
print("Hello, World!")
109
return c_int32(0)
1110

12-
1311
LICENSE = "GPL"

examples/hello_world.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
from pythonbpf.decorators import tracepoint, syscalls
1+
# This is what it is going to look like
2+
# pylint: disable-all# type: ignore
3+
from pythonbpf.decorators import tracepoint, syscalls, bpfglobal, bpf
24
from ctypes import c_void_p, c_int32
35

4-
6+
@bpf
57
@tracepoint(syscalls.sys_clone)
68
def trace_clone(ctx: c_void_p) -> c_int32:
79
print("Hello, World!")
810
return c_int32(0)
911

10-
11-
LICENSE = "GPL"
12+
@bpf
13+
@bpfglobal
14+
def LICENSE() -> str:
15+
return "GPL"

pythonbpf/codegen.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import ast
22
from llvmlite import ir
33
from .license_pass import license_processing
4-
from .functions_pass import func_proc, functions_processing
5-
from .constants_pass import constants_processing
4+
from .functions_pass import func_proc
5+
# from .constants_pass import constants_processing
66
from .globals_pass import globals_processing
77

88

@@ -28,12 +28,10 @@ def processor(source_code, filename, module):
2828

2929
func_proc(tree, module, bpf_chunks)
3030
# For now, we will parse the BPF specific parts of AST
31-
# Big rewrite
3231

33-
# will worry later
3432
# constants_processing(tree, module)
35-
# license_processing(tree, module)
36-
# globals_processing(tree, module)
33+
license_processing(tree, module)
34+
globals_processing(tree, module)
3735
# functions_processing(tree, module)
3836

3937

@@ -57,6 +55,7 @@ def compile_to_ir(filename: str, output: str):
5755
module.add_named_metadata("llvm.ident", ["llvmlite PythonBPF v0.0.0"])
5856

5957
with open(output, "w") as f:
58+
f.write(f"source_filename = \"{filename}\"\n")
6059
f.write(str(module))
6160

6261
return output

pythonbpf/functions_pass.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ def emit_function(module: ir.Module, name: str):
1313
param.add_attribute("nocapture")
1414

1515
func.attributes.add("nounwind")
16-
# func.attributes.add("\"frame-pointer\"=\"all\"")
17-
# func.attributes.add("no-trapping-math", "true")
18-
# func.attributes.add("stack-protector-buffer-size", "8")
16+
# func.attributes.add("\"frame-pointer\"=\"all\"")
17+
# func.attributes.add("no-trapping-math", "true")
18+
# func.attributes.add("stack-protector-buffer-size", "8")
1919

2020
block = func.append_basic_block(name="entry")
2121
builder = ir.IRBuilder(block)
@@ -70,17 +70,17 @@ def process_func_body(module, builder, func_node, func):
7070
# Handle print statement
7171
for arg in call.args:
7272
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
73-
fmt_str = arg.value + "\n"
73+
fmt_str = arg.value + "\n" + "\0"
7474
# Create a global variable for the format string
7575
fmt_gvar = ir.GlobalVariable(
7676
module, ir.ArrayType(ir.IntType(8), len(fmt_str)), name=f"{func.name}____fmt")
7777
fmt_gvar.global_constant = True
78-
fmt_gvar.initializer = ir.Constant(
78+
fmt_gvar.initializer = ir.Constant( # type: ignore
7979
ir.ArrayType(ir.IntType(8), len(fmt_str)),
8080
bytearray(fmt_str.encode("utf8"))
8181
)
8282
fmt_gvar.linkage = "internal"
83-
fmt_gvar.align = 1
83+
fmt_gvar.align = 1 # type: ignore
8484

8585
# Cast the global variable to i8*
8686
fmt_ptr = builder.bitcast(
@@ -129,6 +129,8 @@ def process_bpf_chunk(func_node, module):
129129

130130
func.linkage = "dso_local"
131131
func.attributes.add("nounwind")
132+
func.attributes.add("noinline")
133+
func.attributes.add("optnone")
132134

133135
if func_node.args.args:
134136
# Only look at the first argument for now

pythonbpf/globals_pass.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def globals_processing(tree, module: ir.Module):
3131
collected = ["LICENSE"]
3232

3333
for node in tree.body:
34-
if isinstance(node, ast.FunctionDef) and len(node.decorator_list) == 1:
35-
dec = node.decorator_list[0]
34+
if isinstance(node, ast.FunctionDef) and len(node.decorator_list) == 2:
35+
dec = node.decorator_list[1]
3636
if (
3737
isinstance(dec, ast.Call)
3838
and isinstance(dec.func, ast.Name)

0 commit comments

Comments
 (0)