File: vdso_test.go

package info (click to toggle)
golang-github-cilium-ebpf 0.17.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,684 kB
  • sloc: ansic: 1,259; makefile: 127; python: 113; awk: 29; sh: 24
file content (93 lines) | stat: -rw-r--r-- 1,948 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package linux

import (
	"encoding/binary"
	"errors"
	"os"
	"testing"

	"github.com/go-quicktest/qt"
)

func TestAuxvVDSOMemoryAddress(t *testing.T) {
	for _, testcase := range []struct {
		source  string
		is32bit bool
		address uint64
	}{
		{"auxv64le.bin", false, 0x7ffd377e5000},
		{"auxv32le.bin", true, 0xb7fc3000},
	} {
		t.Run(testcase.source, func(t *testing.T) {
			av, err := newAuxFileReader("testdata/"+testcase.source, binary.LittleEndian, testcase.is32bit)
			if err != nil {
				t.Fatal(err)
			}
			t.Cleanup(func() { av.Close() })

			addr, err := vdsoMemoryAddress(av)
			if err != nil {
				t.Fatal(err)
			}

			if uint64(addr) != testcase.address {
				t.Errorf("Expected vDSO memory address %x, got %x", testcase.address, addr)
			}
		})
	}
}

func TestAuxvNoVDSO(t *testing.T) {
	// Copy of auxv.bin with the vDSO pointer removed.
	av, err := newAuxFileReader("testdata/auxv64le_no_vdso.bin", binary.LittleEndian, false)
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { av.Close() })

	_, err = vdsoMemoryAddress(av)
	if want, got := errAuxvNoVDSO, err; !errors.Is(got, want) {
		t.Fatalf("expected error '%v', got: %v", want, got)
	}
}

func TestVDSOVersion(t *testing.T) {
	_, err := vdsoVersion()
	skipIfNotSupportedOnOS(t, err)
	qt.Assert(t, qt.IsNil(err))
}

func TestLinuxVersionCodeEmbedded(t *testing.T) {
	tests := []struct {
		file    string
		version uint32
	}{
		{
			"testdata/vdso.bin",
			uint32(328828), // 5.4.124
		},
		{
			"testdata/vdso_multiple_notes.bin",
			uint32(328875), // Container Optimized OS v85 with a 5.4.x kernel
		},
	}

	for _, test := range tests {
		t.Run(test.file, func(t *testing.T) {
			vdso, err := os.Open(test.file)
			if err != nil {
				t.Fatal(err)
			}
			defer vdso.Close()

			vc, err := vdsoLinuxVersionCode(vdso)
			if err != nil {
				t.Fatal(err)
			}

			if vc != test.version {
				t.Errorf("Expected version code %d, got %d", test.version, vc)
			}
		})
	}
}