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
|
// Copyright 2025 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"
"flag"
"fmt"
"maps"
_ "net/http/pprof"
"path/filepath"
"sync"
"time"
"github.com/google/syzkaller/dashboard/dashapi"
"github.com/google/syzkaller/pkg/aflow"
_ "github.com/google/syzkaller/pkg/aflow/flow"
"github.com/google/syzkaller/pkg/aflow/trajectory"
"github.com/google/syzkaller/pkg/config"
"github.com/google/syzkaller/pkg/log"
"github.com/google/syzkaller/pkg/mgrconfig"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/pkg/tool"
"github.com/google/syzkaller/pkg/updater"
"github.com/google/syzkaller/prog"
)
type Config struct {
// Currently serves only net/http/pprof handlers.
HTTP string `json:"http"`
DashboardAddr string `json:"dashboard_addr"`
DashboardClient string `json:"dashboard_client"` // Global non-namespace client.
DashboardKey string `json:"dashboard_key"`
SyzkallerRepo string `json:"syzkaller_repo"`
SyzkallerBranch string `json:"syzkaller_branch"`
// Pre-built tools/clang/codesearch clang tool.
CodesearchToolBin string `json:"codesearch_tool_bin"`
KernelConfig string `json:"kernel_config"`
Target string `json:"target"`
Image string `json:"image"`
Type string `json:"type"`
VM json.RawMessage `json:"vm"`
// Max workdir cache size (defaults to 1TB).
// The whole workdir may be slightly larger, since e.g. kernel checkout is not accounted here.
CacheSize uint64 `json:"cache_size"`
// Use fixed base commit for patching jobs (for testing).
FixedBaseCommit string `json:"fixed_base_commit"`
// Use this LLM model (for testing, if empty use workflow-default model).
Model string `json:"model"`
}
func main() {
var (
flagConfig = flag.String("config", "", "config file")
flagExitOnUpgrade = flag.Bool("exit-on-upgrade", false,
"exit after a syz-ci upgrade is applied; otherwise syz-ci restarts")
flagAutoUpdate = flag.Bool("autoupdate", true, "auto-update the binary (for testing)")
)
defer tool.Init()()
log.SetName("syz-agent")
if err := run(*flagConfig, *flagExitOnUpgrade, *flagAutoUpdate); err != nil {
log.Fatal(err)
}
}
func run(configFile string, exitOnUpgrade, autoUpdate bool) error {
cfg := &Config{
SyzkallerRepo: "https://github.com/google/syzkaller.git",
SyzkallerBranch: "master",
CacheSize: 1 << 40, // 1TB should be enough for everyone!
}
if err := config.LoadFile(configFile, cfg); err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
tool.ServeHTTP(cfg.HTTP)
os, vmarch, arch, _, _, err := mgrconfig.SplitTarget(cfg.Target)
if err != nil {
return err
}
dash, err := dashapi.New(cfg.DashboardClient, cfg.DashboardAddr, cfg.DashboardKey)
if err != nil {
return err
}
buildSem := osutil.NewSemaphore(1)
updater, err := updater.New(&updater.Config{
ExitOnUpdate: exitOnUpgrade,
BuildSem: buildSem,
SyzkallerRepo: cfg.SyzkallerRepo,
SyzkallerBranch: cfg.SyzkallerBranch,
Targets: map[updater.Target]bool{
{
OS: os,
VMArch: vmarch,
Arch: arch,
}: true,
},
})
if err != nil {
return err
}
updatePending := make(chan struct{})
shutdownPending := make(chan struct{})
osutil.HandleInterrupts(shutdownPending)
updater.UpdateOnStart(autoUpdate, updatePending, shutdownPending)
const workdir = "workdir"
cache, err := aflow.NewCache(filepath.Join(workdir, "cache"), cfg.CacheSize)
if err != nil {
return err
}
s := &Server{
cfg: cfg,
dash: dash,
cache: cache,
workdir: workdir,
}
ctx, stop := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for {
ok, err := s.poll(ctx)
if err != nil {
log.Error(err)
dash.LogError("syz-agent", "%v", err)
}
var delay time.Duration
if !ok {
// Don't poll dashboard too often, if there are no jobs,
// or errors are happenning.
delay = 10 * time.Second
}
select {
case <-ctx.Done():
return
case <-time.After(delay):
}
}
}()
select {
case <-shutdownPending:
case <-updatePending:
}
stop()
wg.Wait()
select {
case <-shutdownPending:
default:
updater.UpdateAndRestart()
}
return nil
}
type Server struct {
cfg *Config
dash *dashapi.Dashboard
cache *aflow.Cache
workdir string
}
func (s *Server) poll(ctx context.Context) (
bool, error) {
req := &dashapi.AIJobPollReq{
CodeRevision: prog.GitRevision,
}
for _, flow := range aflow.Flows {
model := flow.Model
if s.cfg.Model != "" {
model = s.cfg.Model
}
req.Workflows = append(req.Workflows, dashapi.AIWorkflow{
Type: flow.Type,
Name: flow.Name,
LLMModel: model,
})
}
resp, err := s.dash.AIJobPoll(req)
if err != nil {
return false, err
}
if resp.ID == "" {
return false, nil
}
doneReq := &dashapi.AIJobDoneReq{
ID: resp.ID,
}
results, jobErr := s.executeJob(ctx, resp)
doneReq.Results = results
if jobErr != nil {
doneReq.Error = jobErr.Error()
}
if err := s.dash.AIJobDone(doneReq); err != nil {
return false, err
}
if jobErr != nil {
return false, jobErr
}
return true, nil
}
func (s *Server) executeJob(ctx context.Context, req *dashapi.AIJobPollResp) (map[string]any, error) {
flow := aflow.Flows[req.Workflow]
if flow == nil {
return nil, fmt.Errorf("unsupported flow %q", req.Workflow)
}
model := flow.Model
if s.cfg.Model != "" {
model = s.cfg.Model
}
inputs := map[string]any{
"Syzkaller": osutil.Abs(filepath.FromSlash("syzkaller/current")),
"CodesearchToolBin": s.cfg.CodesearchToolBin,
"Image": s.cfg.Image,
"Type": s.cfg.Type,
"VM": s.cfg.VM,
"FixedBaseCommit": s.cfg.FixedBaseCommit,
}
maps.Insert(inputs, maps.All(req.Args))
onEvent := func(span *trajectory.Span) error {
log.Logf(0, "%v", span)
return s.dash.AITrajectoryLog(&dashapi.AITrajectoryReq{
JobID: req.ID,
Span: span,
})
}
return flow.Execute(ctx, model, s.workdir, inputs, s.cache, onEvent)
}
|