Skip to content

Commit

Permalink
link: run TestKprobeOffset on arm64
Browse files Browse the repository at this point in the history
TestKprobeOffset used to hardcode specific offsets to test against,
which meant we had to make it amd64 specific. It turns out that even
on a single arch the offsets aren't stable due to changes in the
compiler. Hence the test was changed to brute force a valid offset,
but we forgot to run it on all arches.

Trying to execute the test on Linux 5.13 on arm64 triggers EINVAL
instead of the expected EILSEQ however. This is because arm64 returns
EINVAL when trying to use an offset that is not a multiple of four.

Relying on EILSEQ is therefore not portable, and probably shouldn't
be encouraged. Drop the check for EILSEQ from TestKprobeOffset and
execute it on all arches.
  • Loading branch information
lmb committed Jul 26, 2022
1 parent 2fb31df commit 3c1038c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 35 deletions.
1 change: 1 addition & 0 deletions internal/unix/types_linux.go
Expand Up @@ -24,6 +24,7 @@ const (
E2BIG = linux.E2BIG
EFAULT = linux.EFAULT
EACCES = linux.EACCES
EILSEQ = linux.EILSEQ

BPF_F_NO_PREALLOC = linux.BPF_F_NO_PREALLOC
BPF_F_NUMA_NODE = linux.BPF_F_NUMA_NODE
Expand Down
1 change: 1 addition & 0 deletions internal/unix/types_other.go
Expand Up @@ -25,6 +25,7 @@ const (
E2BIG = syscall.Errno(0)
EFAULT = syscall.EFAULT
EACCES = syscall.Errno(0)
EILSEQ = syscall.Errno(0)

BPF_F_NO_PREALLOC = 0
BPF_F_NUMA_NODE = 0
Expand Down
7 changes: 4 additions & 3 deletions link/kprobe.go
Expand Up @@ -307,9 +307,10 @@ func pmuProbe(typ probeType, args probeArgs) (*perfEvent, error) {
if errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("symbol '%s+%#x' not found: %w", args.symbol, args.offset, os.ErrNotExist)
}
// Since commit ab105a4fb894, -EILSEQ is returned when a kprobe sym+offset is resolved
// to an invalid insn boundary.
if errors.Is(err, syscall.EILSEQ) {
// Since commit ab105a4fb894, EILSEQ is returned when a kprobe sym+offset is resolved
// to an invalid insn boundary. The exact conditions that trigger this error are
// arch specific however.
if errors.Is(err, unix.EILSEQ) {
return nil, fmt.Errorf("symbol '%s+%#x' not found (bad insn boundary): %w", args.symbol, args.offset, os.ErrNotExist)
}
// Since at least commit cb9a19fe4aa51, ENOTSUPP is returned
Expand Down
32 changes: 0 additions & 32 deletions link/kprobe_amd64_test.go

This file was deleted.

17 changes: 17 additions & 0 deletions link/kprobe_test.go
Expand Up @@ -53,6 +53,23 @@ func TestKprobe(t *testing.T) {
testLink(t, k, prog)
}

func TestKprobeOffset(t *testing.T) {
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")

// The layout of a function is compiler and arch dependent, so we try to
// find a valid attach target in the first few bytes of the function.
for i := uint64(1); i < 16; i++ {
k, err := Kprobe("inet6_release", prog, &KprobeOptions{Offset: i})
if err != nil {
continue
}
k.Close()
return
}

t.Fatal("Can't attach with non-zero offset")
}

func TestKretprobe(t *testing.T) {
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")

Expand Down

0 comments on commit 3c1038c

Please sign in to comment.