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
|
// Copyright 2021 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 main
import (
"context"
"encoding/json"
"fmt"
"io"
"time"
"cloud.google.com/go/civil"
"github.com/google/syzkaller/dashboard/api"
"github.com/google/syzkaller/pkg/cover"
"github.com/google/syzkaller/pkg/coveragedb"
)
func getExtAPIDescrForBug(bug *uiBugDetails) *api.Bug {
return &api.Bug{
Version: api.Version,
Title: bug.Title,
ID: bug.ID,
Status: bug.Status,
FirstCrash: bug.FirstTime,
LastCrash: bug.LastTime,
FixTime: func() *time.Time {
if bug.FixTime.IsZero() {
return nil
}
return &bug.FixTime
}(),
CloseTime: func() *time.Time {
if bug.ClosedTime.IsZero() {
return nil
}
return &bug.ClosedTime
}(),
Discussions: func() []string {
if bug.ExternalLink == "" {
return nil
}
return []string{bug.ExternalLink}
}(),
FixCommits: getBugFixCommits(bug.uiBug),
CauseCommit: func() *api.Commit {
bisectCause := bug.BisectCauseJob
if bisectCause == nil || bisectCause.Commit == nil {
return nil
}
commit := &api.Commit{
Title: bisectCause.Commit.Title,
Link: bisectCause.Commit.Link,
Hash: bisectCause.Commit.Hash,
Repo: bisectCause.KernelRepo,
Branch: bisectCause.KernelBranch,
}
if !bisectCause.Commit.Date.IsZero() {
commit.Date = &bisectCause.Commit.Date
}
return commit
}(),
Crashes: func() []api.Crash {
var res []api.Crash
for _, crash := range bug.Crashes {
res = append(res, api.Crash{
Title: crash.Title,
SyzReproducerLink: crash.ReproSyzLink,
CReproducerLink: crash.ReproCLink,
KernelConfigLink: crash.KernelConfigLink,
KernelSourceGit: crash.KernelCommitLink,
KernelSourceCommit: crash.KernelCommit,
SyzkallerGit: crash.SyzkallerCommitLink,
SyzkallerCommit: crash.SyzkallerCommit,
// TODO: add the CompilerDescription
// TODO: add the Architecture
CrashReportLink: crash.ReportLink,
})
}
return res
}(),
}
}
func getBugFixCommits(bug *uiBug) []api.Commit {
var res []api.Commit
for _, commit := range bug.Commits {
apiCommit := api.Commit{
Title: commit.Title,
Link: commit.Link,
Hash: commit.Hash,
Repo: commit.Repo,
Branch: commit.Branch,
}
if !commit.Date.IsZero() {
apiCommit.Date = &commit.Date
}
res = append(res, apiCommit)
}
return res
}
func getExtAPIDescrForBugGroups(bugGroups []*uiBugGroup) *api.BugGroup {
var bugs []api.BugSummary
for _, group := range bugGroups {
for _, bug := range group.Bugs {
bugs = append(bugs, api.BugSummary{
Title: bug.Title,
Link: bug.Link,
FixCommits: getBugFixCommits(bug),
})
}
}
return &api.BugGroup{
Version: api.Version,
Bugs: bugs,
}
}
type publicKernelTree struct {
Repo string `json:"repo"`
Branch string `json:"branch"`
}
type publicBackportBug struct {
Namespace string `json:"namespace"`
Title string `json:"title"`
ConfigLink string `json:"config_link"`
SyzReproLink string `json:"syz_repro_link"`
CReproLink string `json:"c_repro_link"`
SyzkallerCommit string `json:"syzkaller_commit"`
}
type publicMissingBackport struct {
From publicKernelTree `json:"from"`
To publicKernelTree `json:"to"`
Commit string `json:"commit"`
Title string `json:"title"`
Bugs []publicBackportBug `json:"bugs"`
}
type publicAPIBackports struct {
Version int `json:"version"`
List []publicMissingBackport `json:"list"`
}
func getExtAPIDescrForBackports(groups []*uiBackportGroup) *publicAPIBackports {
return &publicAPIBackports{
Version: api.Version,
List: func() []publicMissingBackport {
var res []publicMissingBackport
for _, group := range groups {
from := publicKernelTree{
Repo: group.From.URL,
Branch: group.From.Branch,
}
to := publicKernelTree{
Repo: group.To.URL,
Branch: group.To.Branch,
}
for _, backport := range group.List {
record := publicMissingBackport{
From: from,
To: to,
Commit: backport.Commit.Hash,
Title: backport.Commit.Title,
}
for ns, bugs := range backport.Bugs {
for _, info := range bugs {
record.Bugs = append(record.Bugs, publicBackportBug{
Namespace: ns,
Title: info.Bug.Title,
ConfigLink: info.Crash.KernelConfigLink,
CReproLink: info.Crash.ReproCLink,
SyzReproLink: info.Crash.ReproSyzLink,
SyzkallerCommit: info.Crash.SyzkallerCommit,
})
}
}
res = append(res, record)
}
}
return res
}(),
}
}
func GetJSONDescrFor(page any) ([]byte, error) {
var res any
switch i := page.(type) {
case *uiBugPage:
res = getExtAPIDescrForBug(i.Bug)
case *uiTerminalPage:
res = getExtAPIDescrForBugGroups([]*uiBugGroup{i.Bugs})
case *uiMainPage:
res = getExtAPIDescrForBugGroups(i.Groups)
case *uiBackportsPage:
res = getExtAPIDescrForBackports(i.Groups)
default:
return nil, ErrClientNotFound
}
return json.MarshalIndent(res, "", "\t")
}
func writeExtAPICoverageFor(ctx context.Context, w io.Writer, ns, repo string, p *coverageHeatmapParams) error {
// By default, return the previous month coverage. It guarantees the good numbers.
//
// The alternative is to return the current month.
// The numbers will jump every day, on the 1st date may drop down.
tps, err := coveragedb.GenNPeriodsTill(1, civil.DateOf(time.Now()).AddDays(-31), "month")
if err != nil {
return fmt.Errorf("coveragedb.GenNPeriodsTill: %w", err)
}
covDBClient := getCoverageDBClient(ctx)
ff, err := coveragedb.MakeFuncFinder(ctx, covDBClient, ns, tps[0])
if err != nil {
return fmt.Errorf("coveragedb.MakeFuncFinder: %w", err)
}
subsystem := ""
manager := ""
if p != nil {
subsystem = p.subsystem
manager = p.manager
}
covCh, errCh := coveragedb.FilesCoverageStream(ctx, covDBClient,
&coveragedb.SelectScope{
Ns: ns,
Subsystem: subsystem,
Manager: manager,
Periods: tps,
})
if err := writeFileCoverage(ctx, w, repo, ff, covCh); err != nil {
return fmt.Errorf("populateFileCoverage: %w", err)
}
if err := <-errCh; err != nil {
return fmt.Errorf("coveragedb.FilesCoverageStream: %w", err)
}
return nil
}
func writeFileCoverage(ctx context.Context, w io.Writer, repo string, ff *coveragedb.FunctionFinder,
covCh <-chan *coveragedb.FileCoverageWithLineInfo) error {
enc := json.NewEncoder(w)
enc.SetIndent("", "\t")
for {
select {
case fileCov := <-covCh:
if fileCov == nil {
return nil
}
funcsCov, err := genFuncsCov(fileCov, ff)
if err != nil {
return fmt.Errorf("genFuncsCov: %w", err)
}
if err := enc.Encode(&cover.FileCoverage{
Repo: repo,
Commit: fileCov.Commit,
FilePath: fileCov.Filepath,
Functions: funcsCov,
}); err != nil {
return fmt.Errorf("enc.Encode: %w", err)
}
case <-ctx.Done():
return nil
}
}
}
func genFuncsCov(fc *coveragedb.FileCoverageWithLineInfo, ff *coveragedb.FunctionFinder,
) ([]*cover.FuncCoverage, error) {
nameToLines := map[string][]*cover.Block{}
for i, hitCount := range fc.HitCounts {
lineNum := int(fc.LinesInstrumented[i])
funcName, err := ff.FileLineToFuncName(fc.Filepath, lineNum)
if err != nil {
return nil, fmt.Errorf("ff.FileLineToFuncName: %w", err)
}
nameToLines[funcName] = append(nameToLines[funcName], &cover.Block{
HitCount: int(hitCount),
FromLine: lineNum,
FromCol: 0,
ToLine: lineNum,
ToCol: -1,
})
}
var res []*cover.FuncCoverage
for funcName, blocks := range nameToLines {
res = append(res, &cover.FuncCoverage{
FuncName: funcName,
Blocks: blocks,
})
}
return res, nil
}
|