-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftest: Add selftest for AttachProgs/DetachProgs
Signed-off-by: Tao Chen <chen.dylane@gmail.com> CC: Dylane Chen <dylane.chen@didiglobal.com>
- Loading branch information
1 parent
0e40061
commit dc625fc
Showing
6 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
BASEDIR = $(abspath ../../) | ||
|
||
OUTPUT = ../../output | ||
|
||
LIBBPF_SRC = $(abspath ../../libbpf/src) | ||
LIBBPF_OBJ = $(abspath $(OUTPUT)/libbpf.a) | ||
|
||
CLANG = clang | ||
CC = $(CLANG) | ||
GO = go | ||
|
||
ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g') | ||
|
||
CFLAGS = -g -O2 -Wall -fpie -I$(abspath ../common) | ||
LDFLAGS = | ||
|
||
CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT)) -I$(abspath ../common)" | ||
CGO_LDFLAGS_STATIC = "-lelf -lz $(LIBBPF_OBJ)" | ||
CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"' | ||
|
||
CGO_CFLAGS_DYN = "-I. -I/usr/include/" | ||
CGO_LDFLAGS_DYN = "-lelf -lz -lbpf" | ||
|
||
MAIN = main | ||
|
||
.PHONY: $(MAIN) | ||
.PHONY: $(MAIN).go | ||
.PHONY: $(MAIN).bpf.c | ||
|
||
all: $(MAIN)-static | ||
|
||
.PHONY: libbpfgo | ||
.PHONY: libbpfgo-static | ||
.PHONY: libbpfgo-dynamic | ||
|
||
## libbpfgo | ||
|
||
libbpfgo-static: | ||
$(MAKE) -C $(BASEDIR) libbpfgo-static | ||
|
||
libbpfgo-dynamic: | ||
$(MAKE) -C $(BASEDIR) libbpfgo-dynamic | ||
|
||
outputdir: | ||
$(MAKE) -C $(BASEDIR) outputdir | ||
|
||
## test bpf dependency | ||
|
||
$(MAIN).bpf.o: $(MAIN).bpf.c | ||
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@ | ||
|
||
## test | ||
|
||
.PHONY: $(MAIN)-static | ||
.PHONY: $(MAIN)-dynamic | ||
|
||
$(MAIN)-static: libbpfgo-static | $(MAIN).bpf.o | ||
CC=$(CLANG) \ | ||
CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \ | ||
CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \ | ||
GOOS=linux GOARCH=$(ARCH) \ | ||
$(GO) build \ | ||
-tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \ | ||
-o $(MAIN)-static ./$(MAIN).go | ||
|
||
$(MAIN)-dynamic: libbpfgo-dynamic | $(MAIN).bpf.o | ||
CC=$(CLANG) \ | ||
CGO_CFLAGS=$(CGO_CFLAGS_DYN) \ | ||
CGO_LDFLAGS=$(CGO_LDFLAGS_DYN) \ | ||
$(GO) build -o ./$(MAIN)-dynamic ./$(MAIN).go | ||
|
||
## run | ||
|
||
.PHONY: run | ||
.PHONY: run-static | ||
.PHONY: run-dynamic | ||
|
||
run: run-static | ||
|
||
run-static: $(MAIN)-static | ||
sudo ./run.sh $(MAIN)-static | ||
|
||
run-dynamic: $(MAIN)-dynamic | ||
sudo ./run.sh $(MAIN)-dynamic | ||
|
||
clean: | ||
rm -f *.o *-static *-dynamic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module github.com/aquasecurity/libbpfgo/selftest/module-attach-detach | ||
|
||
go 1.18 | ||
|
||
require ( | ||
github.com/aquasecurity/libbpfgo v0.4.7-libbpf-1.2.0-b2e29a1 | ||
github.com/aquasecurity/libbpfgo/helpers v0.4.5 | ||
) | ||
|
||
require golang.org/x/sys v0.7.0 // indirect | ||
|
||
replace github.com/aquasecurity/libbpfgo => ../../ | ||
|
||
replace github.com/aquasecurity/libbpfgo/helpers => ../../helpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= | ||
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= | ||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//+build ignore | ||
|
||
#include <vmlinux.h> | ||
|
||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_RINGBUF); | ||
__uint(max_entries, 1 << 24); | ||
} events SEC(".maps"); | ||
long ringbuffer_flags = 0; | ||
|
||
#ifdef __TARGET_ARCH_amd64 | ||
SEC("fentry/__x64_sys_mmap") | ||
#elif defined(__TARGET_ARCH_arm64) | ||
SEC("fentry/__arm64_sys_mmap") | ||
#endif | ||
int sys_mmap(struct pt_regs *ctx) | ||
{ | ||
int *process; | ||
|
||
// Reserve space on the ringbuffer for the sample | ||
process = bpf_ringbuf_reserve(&events, sizeof(int), ringbuffer_flags); | ||
if (!process) { | ||
return 0; | ||
} | ||
|
||
*process = 2021; | ||
|
||
bpf_ringbuf_submit(process, ringbuffer_flags); | ||
|
||
return 0; | ||
} | ||
|
||
SEC("kprobe/do_sys_open") | ||
int kprobe__sys_open(struct pt_regs *ctx) | ||
{ | ||
int *process; | ||
|
||
// Reserve space on the ringbuffer for the sample | ||
process = bpf_ringbuf_reserve(&events, sizeof(int), ringbuffer_flags); | ||
if (!process) { | ||
return 0; | ||
} | ||
|
||
*process = 2021; | ||
|
||
bpf_ringbuf_submit(process, ringbuffer_flags); | ||
|
||
return 0; | ||
} | ||
|
||
char LICENSE[] SEC("license") = "GPL"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import "C" | ||
|
||
import ( | ||
"encoding/binary" | ||
"os" | ||
"syscall" | ||
"time" | ||
|
||
"fmt" | ||
|
||
bpf "github.com/aquasecurity/libbpfgo" | ||
) | ||
|
||
func main() { | ||
bpfModule, err := bpf.NewModuleFromFile("main.bpf.o") | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
defer bpfModule.Close() | ||
|
||
err = bpfModule.BPFLoadObject() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
// attach all progs | ||
err = bpfModule.AttachProgs() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, fmt.Sprintf("attach progs failed:%w", err)) | ||
os.Exit(-1) | ||
} | ||
|
||
err = bpfModule.DetachProgs() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, fmt.Sprintf("detach progs failed:%w", err)) | ||
os.Exit(-1) | ||
} | ||
|
||
// attach do_sys_open first | ||
prog, err := bpfModule.GetProgram("kprobe__sys_open") | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
|
||
_, err = prog.AttachKprobe("do_sys_open") | ||
if err != nil { | ||
fmt.Println(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
|
||
// AttachProgs again | ||
err = bpfModule.AttachProgs() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, fmt.Sprintf("attach progs failed:%w", err)) | ||
os.Exit(-1) | ||
} | ||
|
||
eventsChannel := make(chan []byte) | ||
rb, err := bpfModule.InitRingBuf("events", eventsChannel) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(-1) | ||
} | ||
|
||
rb.Poll(300) | ||
numberOfEventsReceived := 0 | ||
go func() { | ||
for { | ||
syscall.Mmap(999, 999, 999, 1, 1) | ||
time.Sleep(time.Second / 100) | ||
} | ||
}() | ||
recvLoop: | ||
for { | ||
b := <-eventsChannel | ||
if binary.LittleEndian.Uint32(b) != 2021 { | ||
fmt.Fprintf(os.Stderr, "invalid data retrieved\n") | ||
os.Exit(-1) | ||
} | ||
numberOfEventsReceived++ | ||
if numberOfEventsReceived > 5 { | ||
break recvLoop | ||
} | ||
} | ||
|
||
rb.Stop() | ||
rb.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash | ||
|
||
# SETTINGS | ||
|
||
TEST=$(dirname $0)/$1 # execute | ||
TIMEOUT=10 # seconds | ||
|
||
# COMMON | ||
|
||
COMMON="$(dirname $0)/../common/common.sh" | ||
[[ -f $COMMON ]] && { . $COMMON; } || { error "no common"; exit 1; } | ||
|
||
# MAIN | ||
|
||
kern_version ge 5.8 | ||
|
||
check_build | ||
check_ppid | ||
test_exec | ||
test_finish | ||
|
||
exit 0 |