forked from dvyukov/go-fuzz
/
testee.go
452 lines (416 loc) · 11.1 KB
/
testee.go
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright 2015 go-fuzz 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/binary"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"
"unsafe"
. "github.com/oraluben/go-fuzz/go-fuzz-defs"
)
// Testee is a wrapper around one testee subprocess.
// It manages communication with the testee, timeouts and output collection.
type Testee struct {
coverRegion []byte
inputRegion []byte
sonarRegion []byte
cmd *exec.Cmd
inPipe *os.File
outPipe *os.File
stdoutPipe *os.File
writebuf [9]byte // reusable write buffer
resbuf [24]byte // reusable results buffer
startTime int64
execs int
outputC chan []byte
downC chan bool
down bool
fnidx uint8
ddl []string
dataDir string
}
// TestBinary handles communication with and restring of testee subprocesses.
type TestBinary struct {
fileName string
commFile string
comm *Mapping
periodicCheck func()
coverRegion []byte
inputRegion []byte
sonarRegion []byte
testee *Testee
testeeBuffer []byte // reusable buffer for collecting testee output
stats *Stats
fnidx uint8
}
func init() {
if unsafe.Offsetof(Testee{}.startTime)%8 != 0 {
println(unsafe.Offsetof(Testee{}.startTime))
panic("bad atomic field offset")
}
}
// testeeBufferSize is how much output a test binary can emit
// before we start to overwrite old output.
const testeeBufferSize = 1 << 20
func newTestBinary(fileName string, periodicCheck func(), stats *Stats, fnidx uint8) *TestBinary {
comm, err := ioutil.TempFile("", "go-fuzz-comm")
if err != nil {
log.Fatalf("failed to create comm file: %v", err)
}
comm.Truncate(CoverSize + MaxInputSize + SonarRegionSize)
comm.Close()
mapping, mem := createMapping(comm.Name(), CoverSize+MaxInputSize+SonarRegionSize)
return &TestBinary{
fileName: fileName,
commFile: comm.Name(),
comm: mapping,
periodicCheck: periodicCheck,
coverRegion: mem[:CoverSize],
inputRegion: mem[CoverSize : CoverSize+MaxInputSize],
sonarRegion: mem[CoverSize+MaxInputSize:],
stats: stats,
fnidx: fnidx,
testeeBuffer: make([]byte, testeeBufferSize),
}
}
func (bin *TestBinary) close() {
if bin.testee != nil {
bin.testee.shutdown()
bin.testee = nil
}
bin.comm.destroy()
os.Remove(bin.commFile)
}
func (bin *TestBinary) test(data SqlWrap) (res int, ns uint64, cover, sonar, output []byte, crashed, hanged bool) {
if data.len() > MaxInputSize {
panic(fmt.Sprintf("input data is too large (length %v): %v", data.len(), data))
}
ddls := data.getDDLs()
var retry bool
for {
// This is the only function that is executed regularly,
// so we tie some periodic checks to it.
bin.periodicCheck()
var dml string
bin.stats.execs++
if bin.testee == nil {
bin.stats.restarts++
// pass ddl to testee in the first run to init table / data
bin.testee = newTestee(bin.fileName, bin.comm, bin.coverRegion, bin.inputRegion, bin.sonarRegion, bin.fnidx, bin.testeeBuffer, ddls)
for _, ddl := range ddls {
if len(ddl) > MaxInputSize {
panic(fmt.Sprintf("DDL input is too large (length %v): %v", len(ddl), ddl))
}
if *flagV > 0 {
log.Printf("ddl: %s", ddl)
}
res, ns, cover, sonar, crashed, hanged, retry = bin.testee.test([]byte(ddl))
if retry {
goto restartTestee
}
if crashed {
// we now consider crash on ddl impossible
panic(fmt.Sprintf("testee crashed on ddl (please check fuzz.log for more details): %v", ddl))
}
}
}
dml = data.getDML()
if *flagV > 0 {
log.Printf("dml: %s", dml)
}
res, ns, cover, sonar, crashed, hanged, retry = bin.testee.test([]byte(dml))
if *flagV > 1 {
log.Printf("status: crashed=%v, hanged=%v, retry=%v", crashed, hanged, retry)
}
if retry {
goto restartTestee
}
if crashed {
output = bin.testee.shutdown()
if hanged {
hdr := fmt.Sprintf("program hanged (timeout %v seconds)\n\n", *flagTimeout)
output = append([]byte(hdr), output...)
}
bin.testee = nil
return
}
return
restartTestee:
bin.testee.shutdown()
bin.testee = nil
}
}
func newTestee(bin string, comm *Mapping, coverRegion, inputRegion, sonarRegion []byte, fnidx uint8, buffer []byte, ddl []string) *Testee {
retry:
rIn, wIn, err := os.Pipe()
if err != nil {
log.Fatalf("failed to pipe: %v", err)
}
rOut, wOut, err := os.Pipe()
if err != nil {
log.Fatalf("failed to pipe: %v", err)
}
rStdout, wStdout, err := os.Pipe()
if err != nil {
log.Fatalf("failed to pipe: %v", err)
}
cmd := exec.Command(bin)
cmd.Stdout = wStdout
cmd.Stderr = wStdout
cmd.Env = append([]string{}, os.Environ()...)
cmd.Env = append(cmd.Env, "GOTRACEBACK=1")
cmd.Env = append(cmd.Env, fmt.Sprintf("TIFUZZ_VERBOSE=%d", *flagV))
setupCommMapping(cmd, comm, rOut, wIn)
if err = cmd.Start(); err != nil {
// This can be a transient failure like "cannot allocate memory" or "text file is busy".
log.Printf("failed to start test binary: %v", err)
rIn.Close()
wIn.Close()
rOut.Close()
wOut.Close()
rStdout.Close()
wStdout.Close()
time.Sleep(time.Second)
goto retry
}
rOut.Close()
wIn.Close()
wStdout.Close()
const limit = 1024
// handle init()
var rawInitOut [limit]byte
n, err := rStdout.Read(rawInitOut[:])
if err != nil {
panic(fmt.Sprintf("error on reading from tidb: %v", err))
}
initOut := string(rawInitOut[:n])
if n == limit {
panic(fmt.Sprintf("init output (length %d) too long:\n%s\n", n, initOut))
}
dataDir := strings.TrimSpace(initOut)
if _, err := os.Stat(dataDir); os.IsNotExist(err) {
panic(fmt.Sprintf("init failed:\n%s", initOut))
}
log.Printf("testee: started with TiDB data dir: %s\n", dataDir)
t := &Testee{
coverRegion: coverRegion,
inputRegion: inputRegion,
sonarRegion: sonarRegion,
cmd: cmd,
inPipe: rIn,
outPipe: wOut,
stdoutPipe: rStdout,
outputC: make(chan []byte),
downC: make(chan bool),
fnidx: fnidx,
ddl: ddl,
dataDir: dataDir,
}
// Stdout reader goroutine.
go func() {
// The testee should not output unless it crashes.
// But there are still chances that it does. If so, it can overflow
// the stdout pipe during testing and deadlock. To prevent the
// deadlock we periodically read out stdout.
// This goroutine also collects crash output.
ticker := time.NewTicker(time.Second)
data := buffer
filled := 0
for {
select {
case <-ticker.C:
case <-t.downC:
}
n, err := t.stdoutPipe.Read(data[filled:])
if *flagV >= 3 {
log.Printf("testee: %v\n", string(data[filled:filled+n]))
}
filled += n
if filled > testeeBufferSize/4*3 {
copy(data, data[testeeBufferSize/2:filled])
filled -= testeeBufferSize / 2
}
if err != nil {
break
}
}
ticker.Stop()
trimmed := make([]byte, filled)
copy(trimmed, data)
t.outputC <- trimmed
}()
// Hang watcher goroutine.
go func() {
timeout := time.Duration(*flagTimeout) * time.Second
ticker := time.NewTicker(timeout / 2)
for {
select {
case <-ticker.C:
start := atomic.LoadInt64(&t.startTime)
if start != 0 && time.Now().UnixNano()-start > int64(timeout) {
atomic.StoreInt64(&t.startTime, -1)
t.cmd.Process.Signal(syscall.SIGABRT)
time.Sleep(time.Second)
t.cmd.Process.Signal(syscall.SIGKILL)
ticker.Stop()
return
}
case <-t.downC:
ticker.Stop()
return
}
}
}()
// Shutdown watcher goroutine.
go func() {
select {
case <-t.downC:
case <-shutdownC:
t.cmd.Process.Signal(syscall.SIGKILL)
}
}()
return t
}
// test passes data for testing.
func (t *Testee) test(data []byte) (res int, ns uint64, cover, sonar []byte, crashed, hanged, retry bool) {
if t.down {
log.Fatalf("cannot test: testee is already shutdown")
}
// The test binary can accumulate significant amount of memory,
// so we recreate it periodically.
t.execs++
if t.execs > 1000000 {
t.cmd.Process.Signal(syscall.SIGKILL)
retry = true
return
}
copy(t.inputRegion[:], data)
atomic.StoreInt64(&t.startTime, time.Now().UnixNano())
t.writebuf[0] = t.fnidx
binary.LittleEndian.PutUint64(t.writebuf[1:], uint64(len(data)))
if _, err := t.outPipe.Write(t.writebuf[:]); err != nil {
if *flagV >= 1 {
log.Printf("write to testee failed: %v", err)
}
retry = true
return
}
// Once we do the write, the test is running.
// Once we read the reply below, the test is done.
type Reply struct {
Res uint64
Ns uint64
Sonar uint64
}
ec := make(chan error)
var err error
go func() {
_, err := io.ReadFull(t.inPipe, t.resbuf[:])
ec <- err
}()
select {
case err = <-ec:
case stdout := <-t.outputC:
crashed = true
go func() {
t.outputC <- stdout
}()
return
}
r := Reply{
Res: binary.LittleEndian.Uint64(t.resbuf[:]),
Ns: binary.LittleEndian.Uint64(t.resbuf[8:]),
Sonar: binary.LittleEndian.Uint64(t.resbuf[16:]),
}
hanged = atomic.LoadInt64(&t.startTime) == -1
atomic.StoreInt64(&t.startTime, 0)
if err != nil || hanged {
// Should have been crashed.
crashed = true
return
}
res = int(r.Res)
ns = r.Ns
cover = t.coverRegion
sonar = t.sonarRegion[:r.Sonar]
return
}
func (t *Testee) shutdown() (output []byte) {
if t.down {
log.Fatalf("cannot shutdown: testee is already shutdown")
}
t.down = true
t.cmd.Process.Kill() // it is probably already dead, but kill it again to be sure
close(t.downC) // wakeup stdout reader
out := <-t.outputC
if err := t.cmd.Wait(); err != nil {
out = append(out, err.Error()...)
}
t.inPipe.Close()
t.outPipe.Close()
t.stdoutPipe.Close()
mysqlDataDir := strings.ReplaceAll(t.dataDir, "tidb-fuzz", "mysql-fuzz")
if pidStr, err := ioutil.ReadFile(path.Join(mysqlDataDir, "mysql.pid")); err == nil {
if pid, err := strconv.Atoi(strings.Trim(string(pidStr), " \n")); err == nil {
log.Printf("testee: kill mysqld process %v\n", pid)
syscall.Kill(pid, syscall.SIGTERM)
}
}
if *flagMoveLog {
dirsToGo := []string{t.dataDir, mysqlDataDir}
destDir := filepath.Join(*flagWorkdir, "log", filepath.Base(t.dataDir))
if os.MkdirAll(destDir, os.ModePerm) == nil {
for _, dir := range dirsToGo {
if fi, err := ioutil.ReadDir(dir); err == nil {
for _, v := range fi {
if strings.HasSuffix(v.Name(), ".log") {
copyFileContents(filepath.Join(dir, v.Name()), filepath.Join(destDir, v.Name()))
}
}
}
}
}
}
if *flagRemoveDataDir {
os.RemoveAll(t.dataDir)
os.RemoveAll(mysqlDataDir)
}
return out
}
// copyFileContents copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}