forked from krhancoc/syzkaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuchsia.go
319 lines (304 loc) · 8.34 KB
/
fuchsia.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
// Copyright 2017 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 (
"bufio"
"bytes"
"fmt"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/google/syzkaller/pkg/symbolizer"
"github.com/ianlancetaylor/demangle"
)
type fuchsia struct {
*config
obj string
}
var (
zirconRIP = regexp.MustCompile(` RIP: (0x[0-9a-f]+) `)
zirconBT = regexp.MustCompile(`^bt#[0-9]+: (0x[0-9a-f]+)`)
zirconReportEnd = []byte("Halted")
zirconAssertFailed = []byte("ASSERT FAILED at")
zirconLinePrefix = regexp.MustCompile(`^\[\d+\.\d+\] \d+\.\d+> `)
zirconUnrelated = []*regexp.Regexp{
regexp.MustCompile(`^$`),
regexp.MustCompile(`stopping other cpus`),
regexp.MustCompile(`^halting cpu`),
regexp.MustCompile(`^dso: `),
regexp.MustCompile(`^UPTIME: `),
regexp.MustCompile(`^BUILDID `),
regexp.MustCompile(`^Halting\.\.\.`),
}
)
func ctorFuchsia(cfg *config) (reporterImpl, []string, error) {
ctx := &fuchsia{
config: cfg,
}
if ctx.kernelObj != "" {
ctx.obj = filepath.Join(ctx.kernelObj, ctx.target.KernelObject)
}
suppressions := []string{
"fatal exception: process /tmp/syz-fuzzer", // OOM presumably
}
return ctx, suppressions, nil
}
func (ctx *fuchsia) ContainsCrash(output []byte) bool {
return containsCrash(output, zirconOopses, ctx.ignores)
}
func (ctx *fuchsia) Parse(output []byte) *Report {
// We symbolize here because zircon output does not contain even function names.
symbolized := ctx.symbolize(output)
rep := simpleLineParser(symbolized, zirconOopses, zirconStackParams, ctx.ignores)
if rep == nil {
return nil
}
rep.Output = output
if report := ctx.shortenReport(rep.Report); len(report) != 0 {
rep.Report = report
}
return rep
}
func (ctx *fuchsia) shortenReport(report []byte) []byte {
out := new(bytes.Buffer)
for s := bufio.NewScanner(bytes.NewReader(report)); s.Scan(); {
line := zirconLinePrefix.ReplaceAll(s.Bytes(), nil)
if matchesAny(line, zirconUnrelated) {
continue
}
if bytes.Contains(line, zirconReportEnd) {
break
}
out.Write(line)
out.WriteByte('\n')
}
return out.Bytes()
}
func (ctx *fuchsia) symbolize(output []byte) []byte {
symb := symbolizer.NewSymbolizer(ctx.config.target)
defer symb.Close()
out := new(bytes.Buffer)
for s := bufio.NewScanner(bytes.NewReader(output)); s.Scan(); {
line := s.Bytes()
if bytes.Contains(line, zirconAssertFailed) && len(line) == 127 {
// This is super hacky: but zircon splits the most important information in long assert lines
// (and they are always long) into several lines in irreversible way. Try to restore full line.
line = append([]byte{}, line...)
if s.Scan() {
line = append(line, s.Bytes()...)
}
}
if ctx.obj != "" {
if match := zirconRIP.FindSubmatchIndex(line); match != nil {
if ctx.processPC(out, symb, line, match, false) {
continue
}
} else if match := zirconBT.FindSubmatchIndex(line); match != nil {
if ctx.processPC(out, symb, line, match, true) {
continue
}
}
}
out.Write(line)
out.WriteByte('\n')
}
return out.Bytes()
}
func (ctx *fuchsia) processPC(out *bytes.Buffer, symb *symbolizer.Symbolizer,
line []byte, match []int, call bool) bool {
prefix := line[match[0]:match[1]]
pcStart := match[2] - match[0]
pcEnd := match[3] - match[0]
pcStr := prefix[pcStart:pcEnd]
pc, err := strconv.ParseUint(string(pcStr), 0, 64)
if err != nil {
return false
}
shortPC := pc & 0xfffffff
pc = 0xffffffff80000000 | shortPC
if call {
pc--
}
frames, err := symb.Symbolize(ctx.obj, pc)
if err != nil || len(frames) == 0 {
return false
}
for _, frame := range frames {
file := ctx.trimFile(frame.File)
name := demangle.Filter(frame.Func, demangle.NoParams, demangle.NoTemplateParams)
if strings.Contains(name, "<lambda(") {
// Demangling produces super long (full) names for lambdas.
name = "lambda"
}
id := "[ inline ]"
if !frame.Inline {
id = fmt.Sprintf("0x%08x", shortPC)
}
start := replace(append([]byte{}, prefix...), pcStart, pcEnd, []byte(id))
fmt.Fprintf(out, "%s %v %v:%v\n", start, name, file, frame.Line)
}
return true
}
func (ctx *fuchsia) trimFile(file string) string {
const (
prefix1 = "zircon/kernel/"
prefix2 = "zircon/"
)
if pos := strings.LastIndex(file, prefix1); pos != -1 {
return file[pos+len(prefix1):]
}
if pos := strings.LastIndex(file, prefix2); pos != -1 {
return file[pos+len(prefix2):]
}
return file
}
func (ctx *fuchsia) Symbolize(rep *Report) error {
// We symbolize in Parse because zircon stacktraces don't contain even function names.
return nil
}
var zirconStackParams = &stackParams{
frameRes: []*regexp.Regexp{
compile(` RIP: 0x[0-9a-f]{8} +([a-zA-Z0-9_:~]+)`),
compile(` RIP: \[ inline \] +([a-zA-Z0-9_:~]+)`),
compile(`^bt#[0-9]+: 0x[0-9a-f]{8} +([a-zA-Z0-9_:~]+)`),
compile(`^bt#[0-9]+: \[ inline \] +([a-zA-Z0-9_:~]+)`),
},
skipPatterns: []string{
"^platform_halt$",
"^exception_die$",
"^_panic$",
},
}
var zirconOopses = append([]*oops{
{
[]byte("ZIRCON KERNEL PANIC"),
[]oopsFormat{
{
title: compile("ZIRCON KERNEL PANIC(?:.*\\n)+?.*ASSERT FAILED(?:.*\\n)+?.*bt#00:"),
fmt: "ASSERT FAILED in %[1]v",
stack: &stackFmt{
parts: []*regexp.Regexp{
parseStackTrace,
},
},
},
{
// Some debug asserts don't contain stack trace.
title: compile("ZIRCON KERNEL PANIC(?:.*\\n)+?.*ASSERT FAILED at \\(.+?\\): (.*)"),
fmt: "ASSERT FAILED: %[1]v",
noStackTrace: true,
},
{
title: compile("ZIRCON KERNEL PANIC(?:.*\\n)+?.*double fault, halting(?:.*\\n)+?.*bt#00:"),
fmt: "double fault in %[1]v",
stack: &stackFmt{
parts: []*regexp.Regexp{
parseStackTrace,
},
},
},
{
// Some double faults don't contain stack trace.
title: compile("ZIRCON KERNEL PANIC(?:.*\\n)+?.*double fault, halting"),
fmt: "double fault",
noStackTrace: true,
},
{
title: compile("ZIRCON KERNEL PANIC(?:.*\\n)+?.*Supervisor Page Fault exception, halting"),
fmt: "Supervisor Fault in %[1]v",
stack: &stackFmt{
parts: []*regexp.Regexp{
parseStackTrace,
},
},
},
{
title: compile("ZIRCON KERNEL PANIC(?:.*\\n)+?.*recursion in interrupt handler"),
fmt: "recursion in interrupt handler in %[1]v",
stack: &stackFmt{
parts: []*regexp.Regexp{
parseStackTrace,
},
},
},
{
title: compile("ZIRCON KERNEL PANIC(?:.*\\n)+?.*KVM internal error"),
fmt: "KVM internal error",
noStackTrace: true,
},
{
title: compile("ZIRCON KERNEL PANIC"),
fmt: "KERNEL PANIC in %[1]v",
stack: &stackFmt{
parts: []*regexp.Regexp{
parseStackTrace,
},
},
},
},
[]*regexp.Regexp{},
},
{
[]byte("recursion in interrupt handler"),
[]oopsFormat{
{
title: compile("recursion in interrupt handler(?:.*\\n)+?.*(?:bt#00:|RIP:)"),
fmt: "recursion in interrupt handler in %[1]v",
stack: &stackFmt{
parts: []*regexp.Regexp{
parseStackTrace,
},
},
},
{
title: compile("recursion in interrupt handler"),
fmt: "recursion in interrupt handler",
noStackTrace: true,
},
},
[]*regexp.Regexp{},
},
// We should detect just "stopping other cpus" as some kernel crash rather then as "lost connection",
// but if we add oops for "stopping other cpus", then it will interfere with other formats,
// because "stopping other cpus" usually goes after "ZIRCON KERNEL PANIC", but sometimes before. Mess.
// {
// []byte("stopping other cpus"),
// },
{
[]byte("welcome to Zircon"),
[]oopsFormat{
{
title: compile("welcome to Zircon"),
fmt: unexpectedKernelReboot,
noStackTrace: true,
},
},
[]*regexp.Regexp{},
},
{
[]byte("KVM internal error"),
[]oopsFormat{
{
title: compile("KVM internal error"),
fmt: "KVM internal error",
noStackTrace: true,
},
},
[]*regexp.Regexp{},
},
{
[]byte("<== fatal exception"),
[]oopsFormat{
{
title: compile("<== fatal exception"),
report: compile("<== fatal exception: process ([a-zA-Z0-9_/-]+)"),
fmt: "fatal exception in %[1]v",
noStackTrace: true,
},
},
[]*regexp.Regexp{
compile("<== fatal exception: process .+?syz.+?\\["),
},
},
}, commonOopses...)