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
|
// 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 (
"encoding/json"
"fmt"
"log"
"path/filepath"
"sync"
"time"
syz_instance "github.com/google/syzkaller/pkg/instance"
"github.com/google/syzkaller/pkg/mgrconfig"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/pkg/report"
"github.com/google/syzkaller/pkg/tool"
"github.com/google/syzkaller/pkg/vcs"
)
type Checkout struct {
Path string
Name string
ManagerConfig json.RawMessage
Running map[Instance]bool
Completed []RunResult
LastRunning time.Time
reporter *report.Reporter
mu sync.Mutex
}
func (checkout *Checkout) GetReporter() *report.Reporter {
checkout.mu.Lock()
defer checkout.mu.Unlock()
if checkout.reporter == nil {
// Unfortunately, we have no other choice but to parse the config to use our own parser.
// TODO: add some tool to syzkaller that would just parse logs and then execute it here?
mgrCfg, err := mgrconfig.LoadPartialData(checkout.ManagerConfig)
if err != nil {
tool.Failf("failed to parse mgr config for %s: %s", checkout.Name, err)
}
checkout.reporter, err = report.NewReporter(mgrCfg)
if err != nil {
tool.Failf("failed to get reporter for %s: %s", checkout.Name, err)
}
}
return checkout.reporter
}
func (checkout *Checkout) AddRunning(instance Instance) {
checkout.mu.Lock()
defer checkout.mu.Unlock()
checkout.Running[instance] = true
checkout.LastRunning = time.Now()
}
func (checkout *Checkout) ArchiveInstance(instance Instance) error {
checkout.mu.Lock()
defer checkout.mu.Unlock()
result, err := instance.FetchResult()
if err != nil {
return err
}
checkout.Completed = append(checkout.Completed, result)
delete(checkout.Running, instance)
return nil
}
func (checkout *Checkout) GetRunningResults() []RunResult {
checkout.mu.Lock()
defer checkout.mu.Unlock()
running := []RunResult{}
for instance := range checkout.Running {
result, err := instance.FetchResult()
if err == nil {
running = append(running, result)
}
}
return running
}
func (checkout *Checkout) GetCompletedResults() []RunResult {
checkout.mu.Lock()
defer checkout.mu.Unlock()
return append([]RunResult{}, checkout.Completed...)
}
func (ctx *TestbedContext) NewCheckout(config *CheckoutConfig, mgrConfig json.RawMessage) (*Checkout, error) {
checkout := &Checkout{
Name: config.Name,
Path: filepath.Join(ctx.Config.Workdir, "checkouts", config.Name),
ManagerConfig: mgrConfig,
Running: make(map[Instance]bool),
}
log.Printf("[%s] Checking out", checkout.Name)
if osutil.IsExist(checkout.Path) {
return nil, fmt.Errorf("path %s already exists", checkout.Path)
}
repo := vcs.NewSyzkallerRepo(checkout.Path)
commit, err := repo.Poll(config.Repo, config.Branch)
if err != nil {
return nil, fmt.Errorf("failed to checkout %s (%s): %w", config.Repo, config.Branch, err)
}
log.Printf("[%s] Done. Latest commit: %s", checkout.Name, commit)
log.Printf("[%s] Building", checkout.Name)
if _, err := osutil.RunCmd(time.Hour, checkout.Path, syz_instance.MakeBin); err != nil {
return nil, fmt.Errorf("[%s] Make failed: %w", checkout.Name, err)
}
return checkout, nil
}
|