Skip to content

Commit

Permalink
Add selftest for setting attachments
Browse files Browse the repository at this point in the history
Signed-off-by: grantseltzer <grantseltzer@gmail.com>
  • Loading branch information
grantseltzer committed Apr 6, 2022
1 parent 13798dc commit b3fbc2f
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 2 deletions.
1 change: 1 addition & 0 deletions selftest/set-attach/Makefile
7 changes: 7 additions & 0 deletions selftest/set-attach/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/aquasecurity/libbpfgo/selftest/perfbuffers

go 1.16

require github.com/aquasecurity/libbpfgo v0.2.1-libbpf-0.4.0

replace github.com/aquasecurity/libbpfgo => ../../
11 changes: 11 additions & 0 deletions selftest/set-attach/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
32 changes: 32 additions & 0 deletions selftest/set-attach/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//+build ignore
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#ifdef asm_inline
#undef asm_inline
#define asm_inline asm
#endif

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 24);
} events SEC(".maps");
long ringbuffer_flags = 0;

SEC("fentry/FUNC")
int BPF_PROG(foobar)
{
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";

80 changes: 80 additions & 0 deletions selftest/set-attach/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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()

bpfModule.ListProgramNames()

prog, err := bpfModule.GetProgram("foobar")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

prog.SetProgramType(bpf.BPFProgTypeTracing)
prog.SetAttachType(bpf.BPFAttachTypeTraceFentry)
err = prog.SetAttachTarget(0, "__x64_sys_mmap")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
err = bpfModule.BPFLoadObject()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
_, err = prog.AttachGeneric()
if err != nil {
fmt.Fprintln(os.Stderr, 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.Start()
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()
}
1 change: 1 addition & 0 deletions selftest/set-attach/run.sh
1 change: 0 additions & 1 deletion selftest/tracing/main.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ int BPF_PROG(commit_creds, struct cred *foobar)
return 0;
}


char LICENSE[] SEC("license") = "GPL";

5 changes: 4 additions & 1 deletion selftest/tracing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func main() {
go func() {
for {
syscall.Mmap(999, 999, 999, 1, 1)
time.Sleep(time.Second / 2)
time.Sleep(time.Second / 100)
}
}()
recvLoop:
Expand All @@ -85,4 +85,7 @@ recvLoop:
break recvLoop
}
}

rb.Stop()
rb.Close()
}

0 comments on commit b3fbc2f

Please sign in to comment.