aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/report/bsd.go
blob: ae5715567ccf0b9fc102c665d8f31a0b89f71d8b (plain)
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
// 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 (
	"bytes"
	"fmt"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"

	"github.com/google/syzkaller/pkg/symbolizer"
)

type bsd struct {
	*config
	oopses       []*oops
	symbolizeRes []*regexp.Regexp
	kernelObject string
	symbols      map[string][]symbolizer.Symbol
}

func ctorBSD(cfg *config, oopses []*oops, symbolizeRes []*regexp.Regexp) (reporterImpl, error) {
	var symbols map[string][]symbolizer.Symbol
	kernelObject := ""
	if cfg.kernelDirs.Obj != "" {
		kernelObject = filepath.Join(cfg.kernelDirs.Obj, cfg.target.KernelObject)
		var err error
		symbols, err = symbolizer.ReadTextSymbols(kernelObject)
		if err != nil {
			return nil, err
		}
	}
	ctx := &bsd{
		config:       cfg,
		oopses:       oopses,
		symbolizeRes: symbolizeRes,
		kernelObject: kernelObject,
		symbols:      symbols,
	}
	return ctx, nil
}

func (ctx *bsd) ContainsCrash(output []byte) bool {
	return containsCrash(output, ctx.oopses, ctx.ignores)
}

func (ctx *bsd) Parse(output []byte) *Report {
	return simpleLineParser(output, ctx.oopses, nil, ctx.ignores)
}

func (ctx *bsd) Symbolize(rep *Report) error {
	symb := symbolizer.Make(ctx.config.target)
	defer symb.Close()
	var symbolized []byte
	prefix := rep.reportPrefixLen
	for _, line := range bytes.SplitAfter(rep.Report, []byte("\n")) {
		line := bytes.Clone(line)
		newLine := ctx.symbolizeLine(symb.Symbolize, line)
		if prefix > len(symbolized) {
			prefix += len(newLine) - len(line)
		}
		symbolized = append(symbolized, newLine...)
	}
	rep.Report = symbolized
	rep.reportPrefixLen = prefix
	return nil
}

func (ctx *bsd) symbolizeLine(symbFunc func(string, ...uint64) ([]symbolizer.Frame, error),
	line []byte) []byte {
	var match []int
	// Check whether the line corresponds to the any of the parts that require symbolization.
	for _, re := range ctx.symbolizeRes {
		match = re.FindSubmatchIndex(line)
		if match != nil {
			break
		}
	}
	if match == nil {
		return line
	}
	// First part of the matched regex contains the function name.
	// Second part contains the offset.
	fn := line[match[2]:match[3]]
	off, err := strconv.ParseUint(string(line[match[4]:match[5]]), 16, 64)
	if err != nil {
		return line
	}

	// Get the symbol from the list of symbols generated using the kernel object and addr2line.
	symb := ctx.symbols[string(fn)]
	if len(symb) == 0 {
		return line
	}
	fnStart := (0xffffffff << 32) | symb[0].Addr

	// Retrieve the frames for the corresponding offset of the function.
	frames, err := symbFunc(ctx.kernelObject, fnStart+off)
	if err != nil || len(frames) == 0 {
		return line
	}
	var symbolized []byte
	// Go through each of the frames and add the corresponding file names and line numbers.
	for _, frame := range frames {
		file := frame.File
		file = strings.TrimPrefix(file, ctx.kernelDirs.BuildSrc)
		file = strings.TrimPrefix(file, "/")
		info := fmt.Sprintf(" %v:%v", file, frame.Line)
		modified := append([]byte{}, line...)
		modified = replace(modified, match[5], match[5], []byte(info))
		if frame.Inline {
			// If frames are marked inline then show that in the report also.
			end := match[5] + len(info)
			modified = replace(modified, end, end, []byte(" [inline]"))
			modified = replace(modified, match[5], match[5], []byte(" "+frame.Func))
		}
		symbolized = append(symbolized, modified...)
	}
	return symbolized
}