Repository navigation
Expand file tree
/
Copy pathmoduledata_test.go
More file actions
138 lines (117 loc) · 4.29 KB
/
Copy pathmoduledata_test.go
File metadata and controls
138 lines (117 loc) · 4.29 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (C) 2019-2026 GoRE Authors.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gore
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestModuledata(t *testing.T) {
r := require.New(t)
cases := []struct {
file string
text, textLen uint64
noptrdata, noptrdataLen uint64
data, dataLen uint64
bss, bssLen uint64
noptrbss, noptrbssLen uint64
gofunc uint64
}{
{"gold-linux-amd64-1.20.0", 0x00401000, 0x80f84, 0x515180, 0x105c0, 0x525740, 0x78f0, 0x52d040, 0x2dd60, 0x55ada0, 0x39d0, 0x4a1e88},
{"gold-linux-amd64-1.16.0", 0x00401000, 0x98277, 0x00538020, 0xe2c4, 0x00546300, 0x7790, 0x0054daa0, 0x2d750, 0x0057b200, 0x5310, 0},
{"gold-linux-amd64-1.8.0", 0x00401000, 0x7c7d0, 0x004f9000, 0x24c8, 0x004fb4e0, 0x1d10, 0x004fd200, 0x1a908, 0x00517b20, 0x46a0, 0},
{"gold-linux-amd64-1.7.0", 0x00401000, 0x7d2a0, 0x004f8000, 0x2048, 0x004fa060, 0x1d70, 0x004fbde0, 0x1a910, 0x00516700, 0x4e80, 0},
{"gold-linux-amd64-1.5.0", 0x00401000, 0xb15f0, 0x00590000, 0x1bc8, 0x00591be0, 0x2550, 0x00594140, 0x23bd8, 0x005b7d20, 0x4e40, 0},
{"gold-linux-386-1.16.0", 0x08049000, 0x846d9, 0x0815a020, 0xdda4, 0x08167de0, 0x3c28, 0x0816ba20, 0x11cfc, 0x0817d720, 0x45e0, 0},
}
for _, test := range cases {
t.Run("moduledata-"+test.file, func(t *testing.T) {
f, err := Open(filepath.Join("testdata", "gold", test.file))
r.NoError(err)
defer func(f *GoFile) {
_ = f.Close()
}(f)
md, err := f.Moduledata()
r.NoError(err)
mdSec := md.Text()
r.Equal(test.text, mdSec.Address)
r.Equal(test.textLen, mdSec.Length)
mdSec = md.NoPtrData()
r.Equal(test.noptrdata, mdSec.Address)
r.Equal(test.noptrdataLen, mdSec.Length)
mdSec = md.Data()
r.Equal(test.data, mdSec.Address)
r.Equal(test.dataLen, mdSec.Length)
mdSec = md.Bss()
r.Equal(test.bss, mdSec.Address)
r.Equal(test.bssLen, mdSec.Length)
mdSec = md.NoPtrBss()
r.Equal(test.noptrbss, mdSec.Address)
r.Equal(test.noptrbssLen, mdSec.Length)
mdSec = md.PCLNTab()
r.NotEqual(0, mdSec.Address)
r.NotEqual(0, mdSec.Length)
mdSec = md.ITabLinks()
r.NotEqual(0, mdSec.Address)
r.NotEqual(0, mdSec.Length)
mdSec = md.FuncTab()
r.NotEqual(0, mdSec.Address)
r.NotEqual(0, mdSec.Length)
r.Equal(test.gofunc, md.GoFuncValue())
})
}
}
func TestValidWasmModuledata(t *testing.T) {
valid := moduledata{
PCLNTabAddr: 8,
PCLNTabLen: 16,
TypesAddr: 24,
TypesLen: 16,
TypelinkAddr: 40,
TypelinkLen: 4,
}
require.True(t, validWasmModuledata(valid, 64, false))
validTypeDescriptors := moduledata{
PCLNTabAddr: 8,
PCLNTabLen: 16,
TypesAddr: 24,
TypesLen: 32,
TypeDescLen: 24,
}
require.True(t, validWasmModuledata(validTypeDescriptors, 64, true))
require.False(t, validWasmModuledata(valid, 64, true))
require.False(t, validWasmModuledata(validTypeDescriptors, 64, false))
tests := []struct {
name string
md moduledata
}{
{name: "empty candidate"},
{name: "empty pclntab", md: moduledata{TypesLen: 1, TypelinkLen: 1}},
{name: "empty types", md: moduledata{PCLNTabLen: 1, TypelinkLen: 1}},
{name: "empty typelinks", md: moduledata{PCLNTabLen: 1, TypesLen: 1}},
{name: "type descriptors outside types", md: moduledata{PCLNTabLen: 1, TypesLen: 1, TypeDescLen: 2}},
{name: "pclntab outside memory", md: moduledata{PCLNTabAddr: 64, PCLNTabLen: 1, TypesLen: 1, TypelinkLen: 1}},
{name: "typelink length overflow", md: moduledata{PCLNTabLen: 1, TypesLen: 1, TypelinkLen: ^uint64(0)/4 + 1}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.False(t, validWasmModuledata(test.md, 64, false))
})
}
}
func TestGo127ModuledataPointerOffsetsExcludeScalars(t *testing.T) {
offsets32 := moduledata_1_27_32{}.pointerOffsets()
require.NotContains(t, offsets32, 152)
require.Contains(t, offsets32, 156)
require.NotContains(t, offsets32, 160)
require.NotContains(t, offsets32, 164)
require.Contains(t, offsets32, 168)
offsets64 := moduledata_1_27_64{}.pointerOffsets()
require.NotContains(t, offsets64, 304)
require.Contains(t, offsets64, 312)
require.NotContains(t, offsets64, 320)
require.NotContains(t, offsets64, 328)
require.Contains(t, offsets64, 336)
}