-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetector_test.go
More file actions
246 lines (224 loc) · 6.54 KB
/
detector_test.go
File metadata and controls
246 lines (224 loc) · 6.54 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package resurgo_test
import (
"bytes"
"debug/elf"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/maxgio92/resurgo"
)
const (
demoAppSource = "testdata/demo-app.go"
demoAppBinary = "demo-app"
)
// TestDetectFunctionsFromELF verifies that DetectFunctionsFromELF runs the
// full detector and filter pipeline and produces the expected detection types
// for both Go and C binaries.
func TestDetectFunctionsFromELF(t *testing.T) {
tests := []struct {
name string
build func(t *testing.T, dir string) string
wantTypes []resurgo.DetectionType
}{{
name: "go",
build: func(t *testing.T, dir string) string {
t.Helper()
binPath := filepath.Join(dir, demoAppBinary)
cmd := exec.Command("go", "build", "-o", binPath, demoAppSource)
cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GOARCH=amd64")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to compile demo-app: %v\n%s", err, out)
}
return binPath
},
// Go binaries use .gopclntab instead of .eh_frame; no DetectionCFI expected.
wantTypes: []resurgo.DetectionType{
resurgo.DetectionPrologueOnly,
resurgo.DetectionPrologueCallSite,
},
}, {
name: "c",
build: func(t *testing.T, dir string) string {
t.Helper()
if _, err := exec.LookPath("gcc"); err != nil {
t.Skip("gcc not found, skipping")
}
outPath := filepath.Join(dir, "demo-app-c")
cmd := exec.Command("gcc", "-O0", "-o", outPath, "testdata/demo-app.c")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to compile demo-app.c: %v\n%s", err, out)
}
return outPath
},
// C binaries carry .eh_frame FDE records; expect CFI candidates.
wantTypes: []resurgo.DetectionType{
resurgo.DetectionCFI,
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
binPath := tt.build(t, t.TempDir())
f, err := elf.Open(binPath)
if err != nil {
t.Fatalf("failed to open ELF binary: %v", err)
}
defer f.Close()
candidates, err := resurgo.DetectFunctionsFromELF(f)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(candidates) == 0 {
t.Fatal("expected at least one function candidate, got none")
}
counts := make(map[resurgo.DetectionType]int)
for _, c := range candidates {
counts[c.DetectionType]++
}
t.Logf("total candidates: %d, by type: %v", len(candidates), counts)
for _, typ := range tt.wantTypes {
if counts[typ] == 0 {
t.Errorf("expected at least one %s candidate, got none", typ)
}
}
})
}
}
// TestWithDetectors verifies that a detector registered via WithDetectors is
// actually invoked by the pipeline.
func TestWithDetectors(t *testing.T) {
exe, err := os.Executable()
if err != nil {
t.Fatalf("os.Executable: %v", err)
}
f, err := elf.Open(exe)
if err != nil {
t.Fatalf("elf.Open: %v", err)
}
defer f.Close()
called := false
fake := func(*elf.File) ([]resurgo.FunctionCandidate, error) {
called = true
return nil, nil
}
if _, err := resurgo.DetectFunctionsFromELF(f, resurgo.WithDetectors(fake)); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !called {
t.Error("WithDetectors: detector was not called")
}
}
// TestWithFilters verifies that a filter registered via WithFilters is
// actually invoked by the pipeline.
func TestWithFilters(t *testing.T) {
exe, err := os.Executable()
if err != nil {
t.Fatalf("os.Executable: %v", err)
}
f, err := elf.Open(exe)
if err != nil {
t.Fatalf("elf.Open: %v", err)
}
defer f.Close()
called := false
fakeDetector := func(*elf.File) ([]resurgo.FunctionCandidate, error) {
return []resurgo.FunctionCandidate{{Address: 0x1000}}, nil
}
fakeFilter := func(cs []resurgo.FunctionCandidate, _ *elf.File) ([]resurgo.FunctionCandidate, error) {
called = true
return cs, nil
}
if _, err := resurgo.DetectFunctionsFromELF(f,
resurgo.WithDetectors(fakeDetector),
resurgo.WithFilters(fakeFilter),
); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !called {
t.Error("WithFilters: filter was not called")
}
}
func TestDetectFunctionsFromELF_InvalidELF(t *testing.T) {
r := bytes.NewReader([]byte{0x00, 0x01, 0x02, 0x03})
f, err := elf.NewFile(r)
if err == nil {
f.Close()
t.Fatal("expected elf.NewFile to fail on invalid data")
}
}
// TestDetectors verifies that each detector, when run against a C ELF binary,
// returns non-empty results that include at least one candidate of the expected
// detection type.
func TestDetectors(t *testing.T) {
if _, err := exec.LookPath("gcc"); err != nil {
t.Skip("gcc not found, skipping")
}
outPath := filepath.Join(t.TempDir(), "demo-app-c")
cmd := exec.Command("gcc", "-O0", "-o", outPath, "testdata/demo-app.c")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to compile demo-app.c: %v\n%s", err, out)
}
f, err := elf.Open(outPath)
if err != nil {
t.Fatalf("failed to open ELF binary: %v", err)
}
defer f.Close()
tests := []struct {
name string
detector resurgo.CandidateDetector
wantType resurgo.DetectionType
check func(t *testing.T, candidates []resurgo.FunctionCandidate)
}{{
name: "disasm",
detector: resurgo.DisasmDetector,
wantType: resurgo.DetectionPrologueOnly,
}, {
// Verify that functions confirmed by both prologue and call-site signals
// are merged into DetectionPrologueCallSite with ConfidenceHigh and at
// least one caller or jump source.
name: "disasm/merge",
detector: resurgo.DisasmDetector,
wantType: resurgo.DetectionPrologueCallSite,
check: func(t *testing.T, candidates []resurgo.FunctionCandidate) {
for _, c := range candidates {
if c.DetectionType != resurgo.DetectionPrologueCallSite {
continue
}
if c.Confidence != resurgo.ConfidenceHigh {
t.Errorf("0x%x: expected ConfidenceHigh, got %s", c.Address, c.Confidence)
}
if len(c.CalledFrom) == 0 && len(c.JumpedFrom) == 0 {
t.Errorf("0x%x: no CalledFrom or JumpedFrom", c.Address)
}
}
},
}, {
name: "ehframe",
detector: resurgo.EhFrameDetector,
wantType: resurgo.DetectionCFI,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
candidates, err := tt.detector(f)
if err != nil {
t.Fatalf("%s: %v", tt.name, err)
}
if len(candidates) == 0 {
t.Fatalf("%s: expected at least one candidate, got none", tt.name)
}
found := false
for _, c := range candidates {
if c.DetectionType == tt.wantType {
found = true
break
}
}
if !found {
t.Errorf("%s: expected at least one %s candidate", tt.name, tt.wantType)
}
if tt.check != nil {
tt.check(t, candidates)
}
})
}
}