File: linker_test.go

package info (click to toggle)
golang-github-cilium-ebpf 0.0~git20200413.48fb86d-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, experimental, sid
  • size: 1,436 kB
  • sloc: ansic: 127; sh: 51; makefile: 20
file content (60 lines) | stat: -rw-r--r-- 1,083 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package ebpf

import (
	"testing"

	"github.com/cilium/ebpf/asm"
	"github.com/cilium/ebpf/internal/testutils"
)

func TestLink(t *testing.T) {
	spec := &ProgramSpec{
		Type: SocketFilter,
		Instructions: asm.Instructions{
			// Make sure the call doesn't happen at instruction 0
			// to exercise the relative offset calculation.
			asm.Mov.Reg(asm.R0, asm.R1),
			asm.Call.Label("my_func"),
			asm.Return(),
		},
		License: "MIT",
	}

	libs := []*ProgramSpec{
		{
			Instructions: asm.Instructions{
				asm.LoadImm(asm.R0, 1337, asm.DWord).Sym("my_other_func"),
				asm.Return(),
			},
		},
		{
			Instructions: asm.Instructions{
				asm.Call.Label("my_other_func").Sym("my_func"),
				asm.Return(),
			},
		},
	}

	err := link(spec, libs)
	if err != nil {
		t.Fatal(err)
	}

	t.Log(spec.Instructions)

	testutils.SkipOnOldKernel(t, "4.16", "bpf2bpf calls")

	prog, err := NewProgram(spec)
	if err != nil {
		t.Fatal(err)
	}

	ret, _, err := prog.Test(make([]byte, 14))
	if err != nil {
		t.Fatal(err)
	}

	if ret != 1337 {
		t.Errorf("Expected return code 1337, got %d", ret)
	}
}