aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/cover/report.go
blob: 10ee7ce49a46e88a3aa9591e36127c57573c196b (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
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
// Copyright 2018 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 cover

import (
	"fmt"
	"sort"

	"github.com/google/syzkaller/pkg/cover/backend"
	"github.com/google/syzkaller/pkg/mgrconfig"
	"github.com/google/syzkaller/pkg/vminfo"
	"github.com/google/syzkaller/sys/targets"
	"golang.org/x/exp/maps"
)

type ReportGenerator struct {
	target          *targets.Target
	srcDir          string
	buildDir        string
	subsystem       []mgrconfig.Subsystem
	rawCoverEnabled bool
	*backend.Impl
}

type Prog struct {
	Sig  string
	Data string
	PCs  []uint64
}

func GetPCBase(cfg *mgrconfig.Config) (uint64, error) {
	return backend.GetPCBase(cfg)
}

func MakeReportGenerator(cfg *mgrconfig.Config, modules []*vminfo.KernelModule) (*ReportGenerator, error) {
	impl, err := backend.Make(cfg, modules)
	if err != nil {
		return nil, err
	}
	cfg.KernelSubsystem = append(cfg.KernelSubsystem, mgrconfig.Subsystem{
		Name:  "all",
		Paths: []string{""},
	})
	rg := &ReportGenerator{
		target:          cfg.SysTarget,
		srcDir:          cfg.KernelSrc,
		buildDir:        cfg.KernelBuildSrc,
		subsystem:       cfg.KernelSubsystem,
		rawCoverEnabled: cfg.RawCover,
		Impl:            impl,
	}
	return rg, nil
}

type file struct {
	module     string
	filename   string
	lines      map[int]line
	functions  []*function
	covered    []backend.Range
	uncovered  []backend.Range
	totalPCs   int
	coveredPCs int
}

type function struct {
	name    string
	pcs     int
	covered int
}

type line struct {
	progCount   map[int]bool   // program indices that cover this line
	progIndex   int            // example program index that covers this line
	pcProgCount map[uint64]int // some lines have multiple BBs
}

type fileMap map[string]*file

func (rg *ReportGenerator) prepareFileMap(progs []Prog, force, debug bool) (fileMap, error) {
	if err := rg.symbolizePCs(uniquePCs(progs...)); err != nil {
		return nil, err
	}
	files := make(fileMap)
	for _, unit := range rg.Units {
		files[unit.Name] = &file{
			module:   unit.Module.Name,
			filename: unit.Path,
			lines:    make(map[int]line),
			totalPCs: len(unit.PCs),
		}
	}
	pcToProgs := make(map[uint64]map[int]bool)
	unmatchedPCs := make(map[uint64]bool)
	for i, prog := range progs {
		for _, pc := range prog.PCs {
			if pcToProgs[pc] == nil {
				pcToProgs[pc] = make(map[int]bool)
			}
			pcToProgs[pc][i] = true
			if rg.PreciseCoverage && !contains(rg.CallbackPoints, pc) {
				unmatchedPCs[pc] = true
			}
		}
	}
	err := rg.frame2line(files, pcToProgs, progs)
	if err != nil {
		return nil, err
	}
	// If the backend provided coverage callback locations for the binaries, use them to
	// verify data returned by kcov.
	if len(unmatchedPCs) > 0 && !force {
		return nil, coverageCallbackMismatch(debug, len(pcToProgs), unmatchedPCs)
	}
	for _, unit := range rg.Units {
		f := files[unit.Name]
		for _, pc := range unit.PCs {
			if pcToProgs[pc] != nil {
				f.coveredPCs++
			}
		}
	}
	for _, s := range rg.Symbols {
		fun := &function{
			name: s.Name,
			pcs:  len(s.PCs),
		}
		for _, pc := range s.PCs {
			if pcToProgs[pc] != nil {
				fun.covered++
			}
		}
		f := files[s.Unit.Name]
		f.functions = append(f.functions, fun)
	}
	for _, f := range files {
		sort.Slice(f.functions, func(i, j int) bool {
			return f.functions[i].name < f.functions[j].name
		})
	}
	return files, nil
}

func (rg *ReportGenerator) frame2line(files fileMap, pcToProgs map[uint64]map[int]bool, progs []Prog) error {
	matchedPC := false
	for _, frame := range rg.Frames {
		if frame.StartLine < 0 {
			continue
		}
		f := fileByFrame(files, frame)
		ln := f.lines[frame.StartLine]
		coveredBy := pcToProgs[frame.PC]
		if len(coveredBy) == 0 {
			f.uncovered = append(f.uncovered, frame.Range)
			continue
		}
		// Covered frame.
		f.covered = append(f.covered, frame.Range)
		matchedPC = true
		if ln.progCount == nil {
			ln.progCount = make(map[int]bool)
			ln.pcProgCount = make(map[uint64]int)
			ln.progIndex = -1
		}
		for progIndex := range coveredBy {
			ln.progCount[progIndex] = true
			if ln.progIndex == -1 || len(progs[progIndex].Data) < len(progs[ln.progIndex].Data) {
				ln.progIndex = progIndex
			}
			ln.pcProgCount[frame.PC]++
		}
		f.lines[frame.StartLine] = ln
	}
	if !matchedPC {
		return fmt.Errorf("coverage doesn't match any coverage callbacks")
	}
	return nil
}

func contains(pcs []uint64, pc uint64) bool {
	idx := sort.Search(len(pcs), func(i int) bool { return pcs[i] >= pc })
	return idx < len(pcs) && pcs[idx] == pc
}

func coverageCallbackMismatch(debug bool, numPCs int, unmatchedPCs map[uint64]bool) error {
	debugStr := ""
	if debug {
		debugStr += "\n\nUnmatched PCs:\n"
		for pc := range unmatchedPCs {
			debugStr += fmt.Sprintf("%x\n", pc)
		}
	}
	return fmt.Errorf("%d out of %d PCs returned by kcov do not have matching coverage callbacks."+
		" Check the discoverModules() code. Use ?force=1 to disable this message.%s",
		len(unmatchedPCs), numPCs, debugStr)
}

func uniquePCs(progs ...Prog) []uint64 {
	PCs := make(map[uint64]bool)
	for _, p := range progs {
		for _, pc := range p.PCs {
			PCs[pc] = true
		}
	}
	return maps.Keys(PCs)
}

func (rg *ReportGenerator) symbolizePCs(PCs []uint64) error {
	if len(PCs) == 0 {
		return fmt.Errorf("no coverage collected so far to symbolize")
	}
	if len(rg.Symbols) == 0 {
		return nil
	}
	symbolize := make(map[*backend.Symbol]bool)
	pcs := make(map[*vminfo.KernelModule][]uint64)
	for _, pc := range PCs {
		sym := rg.findSymbol(pc)
		if sym == nil || sym.Symbolized || symbolize[sym] {
			continue
		}
		symbolize[sym] = true
		pcs[sym.Module] = append(pcs[sym.Module], sym.PCs...)
	}
	frames, err := rg.Symbolize(pcs)
	if err != nil {
		return err
	}
	rg.Frames = append(rg.Frames, frames...)
	for sym := range symbolize {
		sym.Symbolized = true
	}
	return nil
}

func fileByFrame(files map[string]*file, frame *backend.Frame) *file {
	f := files[frame.Name]
	if f == nil {
		f = &file{
			module:   frame.Module.Name,
			filename: frame.Path,
			lines:    make(map[int]line),
			// Special mark for header files, if a file does not have coverage at all it is not shown.
			totalPCs:   1,
			coveredPCs: 1,
		}
		files[frame.Name] = f
	}
	return f
}

func (rg *ReportGenerator) findSymbol(pc uint64) *backend.Symbol {
	idx := sort.Search(len(rg.Symbols), func(i int) bool {
		return pc < rg.Symbols[i].End
	})
	if idx == len(rg.Symbols) {
		return nil
	}
	s := rg.Symbols[idx]
	if pc < s.Start || pc > s.End {
		return nil
	}
	return s
}