Skip to content

Commit

Permalink
link: add test for uprobe multi link
Browse files Browse the repository at this point in the history
This commit adds test for uprobe multi link.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
  • Loading branch information
olsajiri committed Dec 18, 2023
1 parent d84fcb3 commit 7c8d728
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions link/uprobe_multi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package link

import (
"errors"
"os/exec"
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/internal/testutils"
"github.com/go-quicktest/qt"
)

func TestUprobeMulti(t *testing.T) {
testutils.SkipIfNotSupported(t, haveBPFLinkUprobeMulti())

prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti, "")

um, err := bashEx.UprobeMulti([]string{"bash_logout", "main"}, prog, nil)
if err != nil {
t.Fatal(err)
}
defer um.Close()

testLink(t, um, prog)
}

func TestUprobeMultiInput(t *testing.T) {
testutils.SkipIfNotSupported(t, haveBPFLinkUprobeMulti())

prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti, "")

// One of symbols or offsets must be given.
_, err := bashEx.UprobeMulti([]string{}, prog, nil)
qt.Assert(t, qt.ErrorIs(err, errInvalidInput))

// One offset, two cookies.
_, err = bashEx.UprobeMulti([]string{}, prog, &UprobeMultiOptions{
Offsets: []uint64{1},
Cookies: []uint64{2, 3},
})
qt.Assert(t, qt.ErrorIs(err, errInvalidInput))

// Two offset, ont refctr offset.
_, err = bashEx.UprobeMulti([]string{}, prog, &UprobeMultiOptions{
Offsets: []uint64{1, 2},
RefCtrOffsets: []uint64{4},
})
qt.Assert(t, qt.ErrorIs(err, errInvalidInput))

// It's either symbols or opts.Offsets.
_, err = bashEx.UprobeMulti([]string{"main"}, prog, &UprobeMultiOptions{
Offsets: []uint64{1},
})
qt.Assert(t, qt.ErrorIs(err, errInvalidInput))
}

func TestUprobeMultiCookie(t *testing.T) {
testutils.SkipIfNotSupported(t, haveBPFLinkUprobeMulti())

prog := mustLoadProgram(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti, "")

um, err := bashEx.UprobeMulti([]string{"bash_logout", "main"}, prog,
&UprobeMultiOptions{
Cookies: []uint64{1, 2},
})
if err != nil {
t.Fatal(err)
}
_ = um.Close()
}

func TestUprobeMultiProgramCall(t *testing.T) {
testutils.SkipIfNotSupported(t, haveBPFLinkUprobeMulti())

args := []string{"--help"}
elf := "/bin/bash"

m, p := newUpdaterMapProg(t, ebpf.Kprobe, ebpf.AttachTraceUprobeMulti)

// Load the executable.
ex, err := OpenExecutable(elf)
if err != nil {
t.Fatal(err)
}

// Open UprobeMulti on the executable for the given symbol
// and attach it to the ebpf program created above.
um, err := ex.UprobeMulti([]string{"main", "_start"}, p, nil)
if errors.Is(err, ErrNoSymbol) {
// Assume bash::main and go::main.main always exists
// and skip the test if the symbol can't be found as
// certain OS (eg. Debian) strip binaries.
t.Skipf("executable %s appear to be stripped, skipping", elf)
}
if err != nil {
t.Fatal(err)
}

// Trigger ebpf program call.
trigger := func(t *testing.T) {
if err := exec.Command(elf, args...).Run(); err != nil {
t.Fatal(err)
}
}
trigger(t)

// Assert that the value at index 0 has been updated to 2.
assertMapValueGE(t, m, 0, 2)

// Detach the Uprobe.
if err := um.Close(); err != nil {
t.Fatal(err)
}

// Reset map value to 0 at index 0.
if err := m.Update(uint32(0), uint32(0), ebpf.UpdateExist); err != nil {
t.Fatal(err)
}

// Retrigger the ebpf program call.
trigger(t)

// Assert that this time the value has not been updated.
assertMapValue(t, m, 0, 0)
}

func TestHaveBPFLinkUprobeMulti(t *testing.T) {
testutils.CheckFeatureTest(t, haveBPFLinkUprobeMulti)
}

0 comments on commit 7c8d728

Please sign in to comment.