forked from krhancoc/syzkaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_test.go
446 lines (422 loc) · 10.9 KB
/
linux_test.go
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2015 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package report
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"github.com/google/syzkaller/pkg/mgrconfig"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/pkg/symbolizer"
"github.com/google/syzkaller/sys/targets"
)
func TestLinuxIgnores(t *testing.T) {
cfg := &mgrconfig.Config{
Derived: mgrconfig.Derived{
TargetOS: targets.Linux,
TargetArch: targets.AMD64,
},
}
reporter, err := NewReporter(cfg)
if err != nil {
t.Fatal(err)
}
cfg.Ignores = []string{"BUG: bug3"}
reporter1, err := NewReporter(cfg)
if err != nil {
t.Fatal(err)
}
cfg.Ignores = []string{"BUG: bug3", "BUG: bug1"}
reporter2, err := NewReporter(cfg)
if err != nil {
t.Fatal(err)
}
cfg.Ignores = []string{"BUG: bug3", "BUG: bug1", "BUG: bug2"}
reporter3, err := NewReporter(cfg)
if err != nil {
t.Fatal(err)
}
const log = `
[ 0.000000] BUG: bug1
[ 0.000000] BUG: bug2
`
if !reporter.ContainsCrash([]byte(log)) {
t.Fatalf("no crash")
}
if rep := reporter.Parse([]byte(log)); rep.Title != "BUG: bug1" {
t.Fatalf("want `BUG: bug1`, found `%v`", rep.Title)
}
if !reporter1.ContainsCrash([]byte(log)) {
t.Fatalf("no crash")
}
if rep := reporter1.Parse([]byte(log)); rep.Title != "BUG: bug1" {
t.Fatalf("want `BUG: bug1`, found `%v`", rep.Title)
}
if !reporter2.ContainsCrash([]byte(log)) {
t.Fatalf("no crash")
}
if rep := reporter2.Parse([]byte(log)); rep.Title != "BUG: bug2" {
t.Fatalf("want `BUG: bug2`, found `%v`", rep.Title)
}
if reporter3.ContainsCrash([]byte(log)) {
t.Fatalf("found crash, should be ignored")
}
if rep := reporter3.Parse([]byte(log)); rep != nil {
t.Fatalf("found `%v`, should be ignored", rep.Title)
}
}
func TestLinuxSymbolizeLine(t *testing.T) {
tests := []struct {
line string
result string
}{
// Normal symbolization.
{
"[ 2713.153531] [<ffffffff82d1b1d9>] foo+0x101/0x185\n",
"[ 2713.153531] [<ffffffff82d1b1d9>] foo+0x101/0x185 foo.c:555\n",
},
{
"RIP: 0010:[<ffffffff8188c0e6>] [<ffffffff8188c0e6>] foo+0x101/0x185\n",
"RIP: 0010:[<ffffffff8188c0e6>] [<ffffffff8188c0e6>] foo+0x101/0x185 foo.c:550\n",
},
// Strip "./" file prefix.
{
"[ 2713.153531] [<ffffffff82d1b1d9>] foo+0x111/0x185\n",
"[ 2713.153531] [<ffffffff82d1b1d9>] foo+0x111/0x185 foo.h:111\n",
},
// Needs symbolization, but symbolizer returns nothing.
{
"[ 2713.153531] [<ffffffff82d1b1d9>] foo+0x121/0x185\n",
"[ 2713.153531] [<ffffffff82d1b1d9>] foo+0x121/0x185\n",
},
// Needs symbolization, but symbolizer returns error.
{
"[ 2713.153531] [<ffffffff82d1b1d9>] foo+0x131/0x185\n",
"[ 2713.153531] [<ffffffff82d1b1d9>] foo+0x131/0x185\n",
},
// Needs symbolization, but symbol is missing.
{
"[ 2713.153531] [<ffffffff82d1b1d9>] bar+0x131/0x185\n",
"[ 2713.153531] [<ffffffff82d1b1d9>] bar+0x131/0x185\n",
},
// Bad offset.
{
"[ 2713.153531] [<ffffffff82d1b1d9>] bar+0xffffffffffffffffffff/0x185\n",
"[ 2713.153531] [<ffffffff82d1b1d9>] bar+0xffffffffffffffffffff/0x185\n",
},
// Should not be symbolized.
{
"WARNING: CPU: 2 PID: 2636 at ipc/shm.c:162 foo+0x101/0x185\n",
"WARNING: CPU: 2 PID: 2636 at ipc/shm.c:162 foo+0x101/0x185 foo.c:555\n",
},
// Tricky function name.
{
" [<ffffffff84e5bea0>] do_ipv6_setsockopt.isra.7.part.3+0x101/0x2830 \n",
" [<ffffffff84e5bea0>] do_ipv6_setsockopt.isra.7.part.3+0x101/0x2830 net.c:111 \n",
},
// Old KASAN frame format (with tab).
{
"[ 50.419727] baz+0x101/0x200\n",
"[ 50.419727] baz+0x101/0x200 baz.c:100\n",
},
// Inlined frames.
{
" [<ffffffff84e5bea0>] foo+0x141/0x185\n",
" [<ffffffff84e5bea0>] inlined1 net.c:111 [inline]\n" +
" [<ffffffff84e5bea0>] inlined2 mm.c:222 [inline]\n" +
" [<ffffffff84e5bea0>] foo+0x141/0x185 kasan.c:333\n",
},
// Several symbols with the same name.
{
"[<ffffffff82d1b1d9>] baz+0x101/0x200\n",
"[<ffffffff82d1b1d9>] baz+0x101/0x200 baz.c:100\n",
},
}
symbols := map[string][]symbolizer.Symbol{
"foo": {
{Addr: 0x1000000, Size: 0x190},
},
"do_ipv6_setsockopt.isra.7.part.3": {
{Addr: 0x2000000, Size: 0x2830},
},
"baz": {
{Addr: 0x3000000, Size: 0x100},
{Addr: 0x4000000, Size: 0x200},
{Addr: 0x5000000, Size: 0x300},
},
}
symb := func(bin string, pc uint64) ([]symbolizer.Frame, error) {
if bin != "vmlinux" {
return nil, fmt.Errorf("unknown pc 0x%x", pc)
}
switch pc {
case 0x1000100:
return []symbolizer.Frame{
{
File: "/linux/foo.c",
Line: 555,
},
}, nil
case 0x1000101:
return []symbolizer.Frame{
{
File: "/linux/foo.c",
Line: 550,
},
}, nil
case 0x1000110:
return []symbolizer.Frame{
{
File: "/linux/./foo.h",
Line: 111,
},
}, nil
case 0x1000120:
return nil, nil
case 0x1000130:
return nil, fmt.Errorf("unknown pc 0x%x", pc)
case 0x2000100:
return []symbolizer.Frame{
{
File: "/linux/net.c",
Line: 111,
},
}, nil
case 0x1000140:
return []symbolizer.Frame{
{
Func: "inlined1",
File: "/linux/net.c",
Line: 111,
Inline: true,
},
{
Func: "inlined2",
File: "/linux/mm.c",
Line: 222,
Inline: true,
},
{
Func: "noninlined3",
File: "/linux/kasan.c",
Line: 333,
Inline: false,
},
}, nil
case 0x4000100:
return []symbolizer.Frame{
{
File: "/linux/baz.c",
Line: 100,
},
}, nil
default:
return nil, fmt.Errorf("unknown pc 0x%x", pc)
}
}
for i, test := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
result := symbolizeLine(symb, symbols, "vmlinux", "/linux", []byte(test.line))
if test.result != string(result) {
t.Errorf("want %q\n\t get %q", test.result, string(result))
}
})
}
}
func prepareLinuxReporter(t *testing.T, arch string) (*Reporter, *linux) {
cfg := &mgrconfig.Config{
Derived: mgrconfig.Derived{
TargetOS: targets.Linux,
TargetArch: arch,
SysTarget: targets.Get(targets.Linux, arch),
},
}
reporter, err := NewReporter(cfg)
if err != nil {
t.Errorf("Failed to create a reporter instance for %#v: %v", arch, err)
}
return reporter, reporter.impl.(*linux)
}
func TestParseLinuxOpcodes(t *testing.T) {
type opcodeTest struct {
arch string
input string
output *parsedOpcodes
}
tests := []opcodeTest{
// LE tests.
{
arch: targets.AMD64,
input: "31 c0 <e8> f5 bf f7 ff",
output: &parsedOpcodes{
rawBytes: []byte{0x31, 0xc0, 0xe8, 0xf5, 0xbf, 0xf7, 0xff},
offset: 2,
},
},
{
arch: targets.AMD64,
input: "c031 <f5e8> f7bf fff7 00ff",
output: &parsedOpcodes{
rawBytes: []byte{0x31, 0xc0, 0xe8, 0xf5, 0xbf, 0xf7, 0xf7, 0xff, 0xff, 0x00},
offset: 2,
},
},
{
arch: targets.AMD64,
input: "(33221100) 77665544",
output: &parsedOpcodes{
rawBytes: []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77},
offset: 0,
},
},
// BE tests.
{
arch: targets.S390x,
input: "31 c0 <e8> f5 bf f7 ff",
output: &parsedOpcodes{
rawBytes: []byte{0x31, 0xc0, 0xe8, 0xf5, 0xbf, 0xf7, 0xff},
offset: 2,
},
},
{
arch: targets.S390x,
input: "31c0 <e8f5> bff5 f7ff ff00",
output: &parsedOpcodes{
rawBytes: []byte{0x31, 0xc0, 0xe8, 0xf5, 0xbf, 0xf5, 0xf7, 0xff, 0xff, 0x00},
offset: 2,
},
},
{
arch: targets.S390x,
input: "<00112233> 44556677",
output: &parsedOpcodes{
rawBytes: []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77},
offset: 0,
},
},
// ARM Thumb tests.
{
arch: targets.ARM,
input: "0011 (2233) 4455",
output: &parsedOpcodes{
rawBytes: []byte{0x11, 0x00, 0x33, 0x22, 0x55, 0x44},
decompileFlags: FlagForceArmThumbMode,
offset: 2,
},
},
{
arch: targets.ARM,
input: "(33221100) 77665544",
output: &parsedOpcodes{
rawBytes: []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77},
offset: 0,
},
},
// Bad input tests.
{
arch: targets.AMD64,
input: "00 11 22 33",
output: nil,
},
{
arch: targets.AMD64,
input: "aa bb <cc> zz",
output: nil,
},
{
arch: targets.AMD64,
input: "<00> 11 22 <33>",
output: nil,
},
{
arch: targets.AMD64,
input: "aa <bb>",
output: nil,
},
{
arch: targets.AMD64,
input: "001122 334455",
output: nil,
},
{
arch: targets.AMD64,
input: "0011223344556677",
output: nil,
},
}
for idx, test := range tests {
test := test // Capturing the value.
t.Run(fmt.Sprintf("%s/%v", test.arch, idx), func(t *testing.T) {
t.Parallel()
_, linuxReporter := prepareLinuxReporter(t, test.arch)
ret, err := linuxReporter.parseOpcodes(test.input)
if test.output == nil && err == nil {
t.Errorf("Expected an error on input %#v", test)
} else if test.output != nil && err != nil {
t.Errorf("Unexpected error %v on input %#v", err, test.input)
} else if test.output != nil && !reflect.DeepEqual(ret, *test.output) {
t.Errorf("Expected: %#v, got: %#v", test.output, ret)
}
})
}
}
func TestDisassemblyInReports(t *testing.T) {
if runtime.GOOS != targets.Linux {
t.Skipf("the test is meant to be run only under Linux")
}
archPath := filepath.Join("testdata", "linux", "decompile")
subFolders, err := ioutil.ReadDir(archPath)
if err != nil {
t.Fatalf("disassembly reports failed: %v", err)
}
for _, obj := range subFolders {
if !obj.IsDir() {
continue
}
reporter, linuxReporter := prepareLinuxReporter(t, obj.Name())
if linuxReporter.target.BrokenCompiler != "" {
t.Skip("skipping the test due to broken cross-compiler:\n" + linuxReporter.target.BrokenCompiler)
}
testPath := filepath.Join(archPath, obj.Name())
testFiles, err := ioutil.ReadDir(testPath)
if err != nil {
t.Fatalf("failed to list tests for %v: %v", obj.Name(), err)
}
for _, file := range testFiles {
if !strings.HasSuffix(file.Name(), ".in") {
continue
}
filePath := filepath.Join(testPath, strings.TrimSuffix(file.Name(), ".in"))
t.Run(obj.Name()+"/"+file.Name(), func(t *testing.T) {
testDisassembly(t, reporter, linuxReporter, filePath)
})
}
}
}
func testDisassembly(t *testing.T, reporter *Reporter, linuxReporter *linux, testFilePrefix string) {
t.Parallel()
input, err := ioutil.ReadFile(testFilePrefix + ".in")
if err != nil {
t.Fatalf("failed to read input file: %v", err)
}
report := reporter.Parse(input)
if report == nil {
t.Fatalf("no bug report was found")
}
result := linuxReporter.decompileOpcodes(input, report)
if *flagUpdate {
osutil.WriteFile(testFilePrefix+".out", result)
}
output, err := ioutil.ReadFile(testFilePrefix + ".out")
if err != nil {
t.Fatalf("failed to read output file: %v", err)
}
if !bytes.Equal(output, result) {
t.Fatalf("Expected:\n%s\nGot:\n%s\n", output, result)
}
}