2016-10-18 19:07:40 +00:00
|
|
|
// Copyright 2016 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 repro
|
|
|
|
|
|
|
|
import (
|
2017-06-21 17:47:18 +00:00
|
|
|
"bytes"
|
2016-10-18 19:07:40 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-06-17 10:46:42 +00:00
|
|
|
"github.com/google/syzkaller/pkg/csource"
|
2017-06-01 16:52:11 +00:00
|
|
|
. "github.com/google/syzkaller/pkg/log"
|
2017-07-03 12:00:47 +00:00
|
|
|
"github.com/google/syzkaller/pkg/osutil"
|
2017-06-17 10:50:11 +00:00
|
|
|
"github.com/google/syzkaller/pkg/report"
|
2016-10-18 19:07:40 +00:00
|
|
|
"github.com/google/syzkaller/prog"
|
2017-06-17 10:29:35 +00:00
|
|
|
"github.com/google/syzkaller/syz-manager/mgrconfig"
|
2016-10-18 19:07:40 +00:00
|
|
|
"github.com/google/syzkaller/vm"
|
|
|
|
)
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
type Stats struct {
|
|
|
|
Log []byte
|
|
|
|
ExtractProgTime time.Duration
|
|
|
|
MinimizeProgTime time.Duration
|
|
|
|
SimplifyProgTime time.Duration
|
|
|
|
ExtractCTime time.Duration
|
|
|
|
SimplifyCTime time.Duration
|
|
|
|
}
|
|
|
|
|
2016-10-18 19:07:40 +00:00
|
|
|
type Result struct {
|
2017-06-19 12:23:17 +00:00
|
|
|
Prog *prog.Prog
|
|
|
|
Duration time.Duration
|
|
|
|
Opts csource.Options
|
|
|
|
CRepro bool
|
2017-06-26 11:57:55 +00:00
|
|
|
Stats Stats
|
2017-07-18 14:31:27 +00:00
|
|
|
// Description, log and report of the final crash that we reproduced.
|
2017-07-06 09:16:21 +00:00
|
|
|
// Can be different from what we started reproducing.
|
|
|
|
Desc string
|
2017-07-18 14:31:27 +00:00
|
|
|
Log []byte
|
2017-07-06 09:16:21 +00:00
|
|
|
Report []byte
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type context struct {
|
2017-06-17 10:29:35 +00:00
|
|
|
cfg *mgrconfig.Config
|
2016-10-18 19:07:40 +00:00
|
|
|
crashDesc string
|
|
|
|
instances chan *instance
|
|
|
|
bootRequests chan int
|
2017-06-26 11:57:55 +00:00
|
|
|
stats Stats
|
2017-07-06 09:16:21 +00:00
|
|
|
desc string
|
2017-07-18 14:31:27 +00:00
|
|
|
log []byte
|
2017-07-05 12:41:12 +00:00
|
|
|
report []byte
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type instance struct {
|
2017-06-02 18:09:00 +00:00
|
|
|
*vm.Instance
|
2016-10-18 19:07:40 +00:00
|
|
|
index int
|
|
|
|
execprogBin string
|
|
|
|
executorBin string
|
|
|
|
}
|
|
|
|
|
2017-06-17 10:29:35 +00:00
|
|
|
func Run(crashLog []byte, cfg *mgrconfig.Config, vmPool *vm.Pool, vmIndexes []int) (*Result, error) {
|
2016-10-18 19:07:40 +00:00
|
|
|
if len(vmIndexes) == 0 {
|
|
|
|
return nil, fmt.Errorf("no VMs provided")
|
|
|
|
}
|
|
|
|
entries := prog.ParseLog(crashLog)
|
|
|
|
if len(entries) == 0 {
|
|
|
|
return nil, fmt.Errorf("crash log does not contain any programs")
|
|
|
|
}
|
2016-12-19 16:39:03 +00:00
|
|
|
crashDesc, _, crashStart, _ := report.Parse(crashLog, cfg.ParsedIgnores)
|
2016-10-18 19:07:40 +00:00
|
|
|
if crashDesc == "" {
|
|
|
|
crashStart = len(crashLog) // assuming VM hanged
|
|
|
|
crashDesc = "hang"
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := &context{
|
|
|
|
cfg: cfg,
|
|
|
|
crashDesc: crashDesc,
|
|
|
|
instances: make(chan *instance, len(vmIndexes)),
|
|
|
|
bootRequests: make(chan int, len(vmIndexes)),
|
|
|
|
}
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(0, "%v programs, %v VMs", len(entries), len(vmIndexes))
|
2016-10-18 19:07:40 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(vmIndexes))
|
|
|
|
for _, vmIndex := range vmIndexes {
|
|
|
|
ctx.bootRequests <- vmIndex
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for vmIndex := range ctx.bootRequests {
|
|
|
|
var inst *instance
|
2017-05-16 08:53:33 +00:00
|
|
|
maxTry := 3
|
|
|
|
for try := 0; try < maxTry; try++ {
|
|
|
|
select {
|
|
|
|
case <-vm.Shutdown:
|
|
|
|
try = maxTry
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
}
|
2017-06-02 18:09:00 +00:00
|
|
|
vmInst, err := vmPool.Create(vmIndex)
|
2016-10-18 19:07:40 +00:00
|
|
|
if err != nil {
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(0, "failed to create VM: %v", err)
|
2016-10-18 19:07:40 +00:00
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
execprogBin, err := vmInst.Copy(filepath.Join(cfg.Syzkaller, "bin/syz-execprog"))
|
|
|
|
if err != nil {
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(0, "failed to copy to VM: %v", err)
|
2016-10-18 19:07:40 +00:00
|
|
|
vmInst.Close()
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
executorBin, err := vmInst.Copy(filepath.Join(cfg.Syzkaller, "bin/syz-executor"))
|
|
|
|
if err != nil {
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(0, "failed to copy to VM: %v", err)
|
2016-10-18 19:07:40 +00:00
|
|
|
vmInst.Close()
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2017-06-02 18:09:00 +00:00
|
|
|
inst = &instance{
|
|
|
|
Instance: vmInst,
|
|
|
|
index: vmIndex,
|
|
|
|
execprogBin: execprogBin,
|
|
|
|
executorBin: executorBin,
|
|
|
|
}
|
2016-10-18 19:07:40 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if inst == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ctx.instances <- inst
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(ctx.instances)
|
|
|
|
}()
|
|
|
|
|
|
|
|
res, err := ctx.repro(entries, crashStart)
|
2017-07-05 14:22:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-26 11:57:55 +00:00
|
|
|
if res != nil {
|
2017-07-05 12:41:12 +00:00
|
|
|
ctx.reproLog(3, "repro crashed as:\n%s", string(ctx.report))
|
2017-06-26 11:57:55 +00:00
|
|
|
res.Stats = ctx.stats
|
2017-07-06 09:16:21 +00:00
|
|
|
res.Desc = ctx.desc
|
2017-07-18 14:31:27 +00:00
|
|
|
res.Log = ctx.log
|
2017-07-05 12:41:12 +00:00
|
|
|
res.Report = ctx.report
|
2017-06-26 11:57:55 +00:00
|
|
|
}
|
2016-10-18 19:07:40 +00:00
|
|
|
|
|
|
|
close(ctx.bootRequests)
|
|
|
|
for inst := range ctx.instances {
|
|
|
|
inst.Close()
|
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:47:18 +00:00
|
|
|
func (ctx *context) repro(entries []*prog.LogEntry, crashStart int) (*Result, error) {
|
|
|
|
// Cut programs that were executed after crash.
|
|
|
|
for i, ent := range entries {
|
|
|
|
if ent.Start > crashStart {
|
|
|
|
entries = entries[:i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
reproStart := time.Now()
|
|
|
|
defer func() {
|
|
|
|
ctx.reproLog(3, "reproducing took %s", time.Since(reproStart))
|
|
|
|
}()
|
|
|
|
|
|
|
|
res, err := ctx.extractProg(entries)
|
2017-06-21 17:47:18 +00:00
|
|
|
if err != nil {
|
2017-07-05 14:22:53 +00:00
|
|
|
return nil, err
|
2017-06-21 17:47:18 +00:00
|
|
|
}
|
|
|
|
if res == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-07-05 14:22:53 +00:00
|
|
|
defer func() {
|
2017-07-27 12:15:08 +00:00
|
|
|
if res != nil {
|
|
|
|
res.Opts.Repro = false
|
|
|
|
}
|
2017-07-05 14:22:53 +00:00
|
|
|
}()
|
2017-06-26 11:57:55 +00:00
|
|
|
res, err = ctx.minimizeProg(res)
|
2017-06-21 17:47:18 +00:00
|
|
|
if err != nil {
|
2017-07-05 14:22:53 +00:00
|
|
|
return nil, err
|
2017-06-26 11:57:55 +00:00
|
|
|
}
|
|
|
|
|
2017-07-05 14:22:53 +00:00
|
|
|
// Try extracting C repro without simplifying options first.
|
2017-06-26 11:57:55 +00:00
|
|
|
res, err = ctx.extractC(res)
|
2017-06-21 17:47:18 +00:00
|
|
|
if err != nil {
|
2017-07-05 14:22:53 +00:00
|
|
|
return nil, err
|
2017-06-21 17:47:18 +00:00
|
|
|
}
|
2017-07-05 14:22:53 +00:00
|
|
|
|
|
|
|
// Simplify options and try extracting C repro.
|
2017-06-21 17:47:18 +00:00
|
|
|
if !res.CRepro {
|
2017-07-05 14:22:53 +00:00
|
|
|
res, err = ctx.simplifyProg(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-21 17:47:18 +00:00
|
|
|
}
|
|
|
|
|
2017-07-05 14:22:53 +00:00
|
|
|
// Simplify C related options.
|
|
|
|
if res.CRepro {
|
|
|
|
res, err = ctx.simplifyC(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-21 17:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
func (ctx *context) extractProg(entries []*prog.LogEntry) (*Result, error) {
|
|
|
|
ctx.reproLog(2, "extracting reproducer from %v programs", len(entries))
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
ctx.stats.ExtractProgTime = time.Since(start)
|
|
|
|
}()
|
2017-06-19 13:19:10 +00:00
|
|
|
|
2016-10-18 19:07:40 +00:00
|
|
|
// Extract last program on every proc.
|
|
|
|
procs := make(map[int]int)
|
|
|
|
for i, ent := range entries {
|
|
|
|
procs[ent.Proc] = i
|
|
|
|
}
|
|
|
|
var indices []int
|
|
|
|
for _, idx := range procs {
|
|
|
|
indices = append(indices, idx)
|
|
|
|
}
|
|
|
|
sort.Ints(indices)
|
2017-06-19 13:19:10 +00:00
|
|
|
var lastEntries []*prog.LogEntry
|
2016-10-18 19:07:40 +00:00
|
|
|
for i := len(indices) - 1; i >= 0; i-- {
|
2017-06-19 13:19:10 +00:00
|
|
|
lastEntries = append(lastEntries, entries[indices[i]])
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
2017-06-19 13:19:10 +00:00
|
|
|
|
2017-06-23 17:09:50 +00:00
|
|
|
// The shortest duration is 10 seconds to detect simple crashes (i.e. no races and no hangs).
|
|
|
|
// The longest duration is 5 minutes to catch races and hangs. Note that this value must be larger
|
2017-06-21 17:47:18 +00:00
|
|
|
// than hang/no output detection duration in vm.MonitorExecution, which is currently set to 3 mins.
|
2017-06-26 11:57:55 +00:00
|
|
|
timeouts := []time.Duration{10 * time.Second, 1 * time.Minute, 5 * time.Minute}
|
2017-06-23 17:09:50 +00:00
|
|
|
|
|
|
|
for _, timeout := range timeouts {
|
|
|
|
// Execute each program separately to detect simple crashes caused by a single program.
|
|
|
|
// Programs are executed in reverse order, usually the last program is the guilty one.
|
2017-06-26 11:57:55 +00:00
|
|
|
res, err := ctx.extractProgSingle(reverseEntries(lastEntries), timeout)
|
2017-06-23 17:09:50 +00:00
|
|
|
if err != nil {
|
2017-07-05 14:22:53 +00:00
|
|
|
return nil, err
|
2017-06-23 17:09:50 +00:00
|
|
|
}
|
|
|
|
if res != nil {
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "found reproducer with %d syscalls", len(res.Prog.Calls))
|
2017-06-23 17:09:50 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
2017-06-21 17:47:18 +00:00
|
|
|
|
2017-06-23 17:09:50 +00:00
|
|
|
// Execute all programs and bisect the log to find multiple guilty programs.
|
2017-06-26 11:57:55 +00:00
|
|
|
res, err = ctx.extractProgBisect(reverseEntries(entries), timeout)
|
2017-06-23 17:09:50 +00:00
|
|
|
if err != nil {
|
2017-07-05 14:22:53 +00:00
|
|
|
return nil, err
|
2017-06-23 17:09:50 +00:00
|
|
|
}
|
|
|
|
if res != nil {
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "found reproducer with %d syscalls", len(res.Prog.Calls))
|
2017-06-23 17:09:50 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
2017-06-21 17:47:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(0, "failed to extract reproducer")
|
2017-06-21 17:47:18 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-07-19 14:57:16 +00:00
|
|
|
func (ctx *context) createDefaultOps() csource.Options {
|
2016-10-18 19:07:40 +00:00
|
|
|
opts := csource.Options{
|
2017-05-18 12:54:02 +00:00
|
|
|
Threaded: true,
|
|
|
|
Collide: true,
|
|
|
|
Repeat: true,
|
|
|
|
Procs: ctx.cfg.Procs,
|
|
|
|
Sandbox: ctx.cfg.Sandbox,
|
|
|
|
EnableTun: true,
|
|
|
|
UseTmpDir: true,
|
|
|
|
HandleSegv: true,
|
2017-05-18 16:17:16 +00:00
|
|
|
WaitRepeat: true,
|
2017-05-18 12:54:02 +00:00
|
|
|
Repro: true,
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
2017-07-19 14:57:16 +00:00
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *context) extractProgSingle(entries []*prog.LogEntry, duration time.Duration) (*Result, error) {
|
|
|
|
ctx.reproLog(3, "single: executing %d programs separately with timeout %s", len(entries), duration)
|
|
|
|
|
|
|
|
opts := ctx.createDefaultOps()
|
2017-06-19 12:42:50 +00:00
|
|
|
|
2017-06-21 17:47:18 +00:00
|
|
|
for _, ent := range entries {
|
|
|
|
opts.Fault = ent.Fault
|
|
|
|
opts.FaultCall = ent.FaultCall
|
|
|
|
opts.FaultNth = ent.FaultNth
|
|
|
|
if opts.FaultCall < 0 || opts.FaultCall >= len(ent.P.Calls) {
|
|
|
|
opts.FaultCall = len(ent.P.Calls) - 1
|
|
|
|
}
|
|
|
|
crashed, err := ctx.testProg(ent.P, duration, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if crashed {
|
|
|
|
res := &Result{
|
|
|
|
Prog: ent.P,
|
|
|
|
Duration: duration * 3 / 2,
|
|
|
|
Opts: opts,
|
|
|
|
}
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "single: successfully extracted reproducer")
|
2017-06-21 17:47:18 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "single: failed to extract reproducer")
|
2017-06-21 17:47:18 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
func (ctx *context) extractProgBisect(entries []*prog.LogEntry, baseDuration time.Duration) (*Result, error) {
|
|
|
|
ctx.reproLog(3, "bisect: bisecting %d programs with base timeout %s", len(entries), baseDuration)
|
|
|
|
|
2017-07-19 14:57:16 +00:00
|
|
|
opts := ctx.createDefaultOps()
|
2017-06-21 17:47:18 +00:00
|
|
|
|
|
|
|
duration := func(entries int) time.Duration {
|
|
|
|
return baseDuration + time.Duration((entries/4))*time.Second
|
|
|
|
}
|
|
|
|
|
2017-06-23 13:54:57 +00:00
|
|
|
// Bisect the log to find multiple guilty programs.
|
2017-06-26 11:57:55 +00:00
|
|
|
entries, err := ctx.bisectProgs(entries, func(progs []*prog.LogEntry) (bool, error) {
|
2017-06-23 13:54:57 +00:00
|
|
|
return ctx.testProgs(progs, duration(len(progs)), opts)
|
|
|
|
})
|
2017-06-21 17:47:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-23 13:54:57 +00:00
|
|
|
if entries == nil {
|
2017-06-21 17:47:18 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
// TODO: Minimize each program before concatenation.
|
|
|
|
// TODO: Return multiple programs if concatenation fails.
|
|
|
|
|
|
|
|
ctx.reproLog(3, "bisect: %d programs left: \n\n%s\n", len(entries), encodeEntries(entries))
|
|
|
|
ctx.reproLog(3, "bisect: trying to concatenate")
|
|
|
|
|
2017-06-21 17:47:18 +00:00
|
|
|
// Concatenate all programs into one.
|
|
|
|
var prog prog.Prog
|
2017-06-23 13:54:57 +00:00
|
|
|
for _, entry := range entries {
|
|
|
|
prog.Calls = append(prog.Calls, entry.P.Calls...)
|
2017-06-21 17:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the program without fault injection.
|
|
|
|
dur := duration(len(entries)) * 3 / 2
|
2017-06-23 13:54:57 +00:00
|
|
|
crashed, err := ctx.testProg(&prog, dur, opts)
|
2017-06-21 17:47:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if crashed {
|
|
|
|
res := &Result{
|
|
|
|
Prog: &prog,
|
|
|
|
Duration: dur,
|
|
|
|
Opts: opts,
|
|
|
|
}
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: concatenation succeded")
|
2017-06-21 17:47:18 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with fault injection.
|
|
|
|
calls := 0
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.Fault {
|
|
|
|
opts.FaultCall = calls + entry.FaultCall
|
|
|
|
opts.FaultNth = entry.FaultNth
|
|
|
|
if entry.FaultCall < 0 || entry.FaultCall >= len(entry.P.Calls) {
|
|
|
|
opts.FaultCall = calls + len(entry.P.Calls) - 1
|
|
|
|
}
|
|
|
|
crashed, err := ctx.testProg(&prog, dur, opts)
|
2016-10-18 19:07:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if crashed {
|
2017-06-21 17:47:18 +00:00
|
|
|
res := &Result{
|
|
|
|
Prog: &prog,
|
|
|
|
Duration: dur,
|
2017-06-19 12:23:17 +00:00
|
|
|
Opts: opts,
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: concatenation succeeded with fault injection")
|
2017-06-21 17:47:18 +00:00
|
|
|
return res, nil
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-21 17:47:18 +00:00
|
|
|
calls += len(entry.P.Calls)
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: concatenation failed")
|
2017-06-21 17:47:18 +00:00
|
|
|
return nil, nil
|
2017-06-19 12:42:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
// Minimize calls and arguments.
|
|
|
|
func (ctx *context) minimizeProg(res *Result) (*Result, error) {
|
|
|
|
ctx.reproLog(2, "minimizing guilty program")
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
ctx.stats.MinimizeProgTime = time.Since(start)
|
|
|
|
}()
|
2017-06-19 12:42:50 +00:00
|
|
|
|
2017-05-25 14:07:10 +00:00
|
|
|
call := -1
|
|
|
|
if res.Opts.Fault {
|
|
|
|
call = res.Opts.FaultCall
|
|
|
|
}
|
|
|
|
res.Prog, res.Opts.FaultCall = prog.Minimize(res.Prog, call, func(p1 *prog.Prog, callIndex int) bool {
|
2017-06-19 12:23:17 +00:00
|
|
|
crashed, err := ctx.testProg(p1, res.Duration, res.Opts)
|
2016-10-18 19:07:40 +00:00
|
|
|
if err != nil {
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(0, "minimization failed with %v", err)
|
2016-10-18 19:07:40 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return crashed
|
2016-10-21 16:24:33 +00:00
|
|
|
}, true)
|
2016-10-18 19:07:40 +00:00
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simplify repro options (threaded, collide, sandbox, etc).
|
|
|
|
func (ctx *context) simplifyProg(res *Result) (*Result, error) {
|
|
|
|
ctx.reproLog(2, "simplifying guilty program")
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
ctx.stats.SimplifyProgTime = time.Since(start)
|
|
|
|
}()
|
|
|
|
|
2017-07-05 14:22:53 +00:00
|
|
|
for _, simplify := range progSimplifies {
|
|
|
|
opts := res.Opts
|
|
|
|
if simplify(&opts) {
|
|
|
|
crashed, err := ctx.testProg(res.Prog, res.Duration, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if crashed {
|
|
|
|
res.Opts = opts
|
|
|
|
// Simplification successfull, try extracting C repro.
|
|
|
|
res, err := ctx.extractC(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.CRepro {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 12:42:50 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2017-07-05 14:22:53 +00:00
|
|
|
// Try triggering crash with a C reproducer.
|
2017-06-26 11:57:55 +00:00
|
|
|
func (ctx *context) extractC(res *Result) (*Result, error) {
|
|
|
|
ctx.reproLog(2, "extracting C reproducer")
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
ctx.stats.ExtractCTime = time.Since(start)
|
|
|
|
}()
|
2017-06-19 12:42:50 +00:00
|
|
|
|
|
|
|
crashed, err := ctx.testCProg(res.Prog, res.Duration, res.Opts)
|
2016-10-18 19:07:40 +00:00
|
|
|
if err != nil {
|
2017-07-05 14:22:53 +00:00
|
|
|
return nil, err
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
2017-05-17 18:20:23 +00:00
|
|
|
res.CRepro = crashed
|
2017-06-19 12:42:50 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2017-07-05 14:22:53 +00:00
|
|
|
// Try to simplify the C reproducer.
|
2017-06-26 11:57:55 +00:00
|
|
|
func (ctx *context) simplifyC(res *Result) (*Result, error) {
|
|
|
|
ctx.reproLog(2, "simplifying C reproducer")
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
ctx.stats.SimplifyCTime = time.Since(start)
|
|
|
|
}()
|
2017-05-17 18:20:23 +00:00
|
|
|
|
2017-07-05 14:22:53 +00:00
|
|
|
for _, simplify := range cSimplifies {
|
2017-06-19 12:42:50 +00:00
|
|
|
opts := res.Opts
|
2017-07-05 14:22:53 +00:00
|
|
|
if simplify(&opts) {
|
|
|
|
crashed, err := ctx.testCProg(res.Prog, res.Duration, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if crashed {
|
|
|
|
res.Opts = opts
|
|
|
|
}
|
2017-05-18 17:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-18 19:07:40 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:47:18 +00:00
|
|
|
func (ctx *context) testProg(p *prog.Prog, duration time.Duration, opts csource.Options) (crashed bool, err error) {
|
|
|
|
entry := prog.LogEntry{P: p}
|
2017-08-09 11:17:15 +00:00
|
|
|
if opts.Fault {
|
2017-06-21 17:47:18 +00:00
|
|
|
entry.Fault = true
|
|
|
|
entry.FaultCall = opts.FaultCall
|
|
|
|
entry.FaultNth = opts.FaultNth
|
2017-06-19 12:42:50 +00:00
|
|
|
}
|
2017-06-21 17:47:18 +00:00
|
|
|
return ctx.testProgs([]*prog.LogEntry{&entry}, duration, opts)
|
2017-06-19 12:42:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 17:47:18 +00:00
|
|
|
func (ctx *context) testProgs(entries []*prog.LogEntry, duration time.Duration, opts csource.Options) (crashed bool, err error) {
|
2016-10-18 19:07:40 +00:00
|
|
|
inst := <-ctx.instances
|
|
|
|
if inst == nil {
|
|
|
|
return false, fmt.Errorf("all VMs failed to boot")
|
|
|
|
}
|
2017-02-02 19:19:44 +00:00
|
|
|
defer ctx.returnInstance(inst)
|
2017-06-21 17:47:18 +00:00
|
|
|
if len(entries) == 0 {
|
|
|
|
return false, fmt.Errorf("no programs to execute")
|
|
|
|
}
|
2016-10-18 19:07:40 +00:00
|
|
|
|
2017-06-21 17:47:18 +00:00
|
|
|
pstr := encodeEntries(entries)
|
2017-07-03 12:00:47 +00:00
|
|
|
progFile, err := osutil.WriteTempFile(pstr)
|
2016-10-18 19:07:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer os.Remove(progFile)
|
|
|
|
vmProgFile, err := inst.Copy(progFile)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to copy to VM: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-25 14:07:10 +00:00
|
|
|
repeat := 1
|
2016-10-18 19:07:40 +00:00
|
|
|
if opts.Repeat {
|
2017-05-25 14:07:10 +00:00
|
|
|
repeat = 0
|
|
|
|
}
|
|
|
|
if !opts.Fault {
|
|
|
|
opts.FaultCall = -1
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
2017-06-21 17:47:18 +00:00
|
|
|
program := entries[0].P.String()
|
|
|
|
if len(entries) > 1 {
|
|
|
|
program = "["
|
|
|
|
for i, entry := range entries {
|
|
|
|
program += fmt.Sprintf("%v", len(entry.P.Calls))
|
|
|
|
if i != len(entries)-1 {
|
|
|
|
program += ", "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
program += "]"
|
|
|
|
}
|
2017-09-14 07:49:26 +00:00
|
|
|
command := fmt.Sprintf("%v -executor %v -arch=%v -cover=0 -procs=%v -repeat=%v"+
|
|
|
|
" -sandbox %v -threaded=%v -collide=%v %v",
|
|
|
|
inst.execprogBin, inst.executorBin, ctx.cfg.TargetArch, opts.Procs, repeat,
|
|
|
|
opts.Sandbox, opts.Threaded, opts.Collide, vmProgFile)
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(2, "testing program (duration=%v, %+v): %s", duration, opts, program)
|
2017-06-02 18:09:00 +00:00
|
|
|
return ctx.testImpl(inst.Instance, command, duration)
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 18:20:23 +00:00
|
|
|
func (ctx *context) testCProg(p *prog.Prog, duration time.Duration, opts csource.Options) (crashed bool, err error) {
|
|
|
|
src, err := csource.Write(p, opts)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-07-03 12:00:47 +00:00
|
|
|
srcf, err := osutil.WriteTempFile(src)
|
2017-05-17 18:20:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
bin, err := csource.Build("c", srcf)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer os.Remove(bin)
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(2, "testing compiled C program (duration=%v, %+v): %s", duration, opts, p)
|
2017-05-17 18:20:23 +00:00
|
|
|
crashed, err = ctx.testBin(bin, duration)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return crashed, nil
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:19:44 +00:00
|
|
|
func (ctx *context) testBin(bin string, duration time.Duration) (crashed bool, err error) {
|
2016-10-18 19:07:40 +00:00
|
|
|
inst := <-ctx.instances
|
|
|
|
if inst == nil {
|
|
|
|
return false, fmt.Errorf("all VMs failed to boot")
|
|
|
|
}
|
2017-02-02 19:19:44 +00:00
|
|
|
defer ctx.returnInstance(inst)
|
2016-10-18 19:07:40 +00:00
|
|
|
|
|
|
|
bin, err = inst.Copy(bin)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to copy to VM: %v", err)
|
|
|
|
}
|
2017-06-02 18:09:00 +00:00
|
|
|
return ctx.testImpl(inst.Instance, bin, duration)
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 18:09:00 +00:00
|
|
|
func (ctx *context) testImpl(inst *vm.Instance, command string, duration time.Duration) (crashed bool, err error) {
|
2016-11-19 10:14:11 +00:00
|
|
|
outc, errc, err := inst.Run(duration, nil, command)
|
2016-10-18 19:07:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to run command in VM: %v", err)
|
|
|
|
}
|
2017-07-18 14:31:27 +00:00
|
|
|
desc, report, output, crashed, _ := vm.MonitorExecution(outc, errc, false, ctx.cfg.ParsedIgnores)
|
2016-10-18 19:07:40 +00:00
|
|
|
if !crashed {
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(2, "program did not crash")
|
2016-10-18 19:07:40 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2017-07-06 09:16:21 +00:00
|
|
|
ctx.desc = desc
|
2017-07-18 14:31:27 +00:00
|
|
|
ctx.log = output
|
2017-07-05 12:41:12 +00:00
|
|
|
ctx.report = report
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(2, "program crashed: %v", desc)
|
2016-10-18 19:07:40 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:19:44 +00:00
|
|
|
func (ctx *context) returnInstance(inst *instance) {
|
|
|
|
ctx.bootRequests <- inst.index
|
|
|
|
inst.Close()
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
2017-06-21 17:47:18 +00:00
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
func (ctx *context) reproLog(level int, format string, args ...interface{}) {
|
|
|
|
prefix := fmt.Sprintf("reproducing crash '%v': ", ctx.crashDesc)
|
|
|
|
Logf(level, prefix+format, args...)
|
|
|
|
ctx.stats.Log = append(ctx.stats.Log, []byte(fmt.Sprintf(format, args...)+"\n")...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *context) bisectProgs(progs []*prog.LogEntry, pred func([]*prog.LogEntry) (bool, error)) ([]*prog.LogEntry, error) {
|
|
|
|
ctx.reproLog(3, "bisect: bisecting %d programs", len(progs))
|
2017-06-23 13:54:57 +00:00
|
|
|
|
|
|
|
compose := func(guilty1, guilty2 [][]*prog.LogEntry, chunk []*prog.LogEntry) []*prog.LogEntry {
|
|
|
|
progs := []*prog.LogEntry{}
|
|
|
|
for _, c := range guilty1 {
|
|
|
|
progs = append(progs, c...)
|
|
|
|
}
|
|
|
|
progs = append(progs, chunk...)
|
|
|
|
for _, c := range guilty2 {
|
|
|
|
progs = append(progs, c...)
|
|
|
|
}
|
|
|
|
return progs
|
|
|
|
}
|
|
|
|
|
|
|
|
logGuilty := func(guilty [][]*prog.LogEntry) string {
|
|
|
|
log := "["
|
|
|
|
for i, chunk := range guilty {
|
|
|
|
log += fmt.Sprintf("<%d>", len(chunk))
|
|
|
|
if i != len(guilty)-1 {
|
|
|
|
log += ", "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log += "]"
|
|
|
|
return log
|
|
|
|
}
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: executing all %d programs", len(progs))
|
2017-06-23 13:54:57 +00:00
|
|
|
crashed, err := pred(progs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !crashed {
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: didn't crash")
|
2017-06-23 13:54:57 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
guilty := [][]*prog.LogEntry{progs}
|
|
|
|
again:
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: guilty chunks: %v", logGuilty(guilty))
|
2017-06-23 13:54:57 +00:00
|
|
|
for i, chunk := range guilty {
|
|
|
|
if len(chunk) == 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
guilty1 := guilty[:i]
|
|
|
|
guilty2 := guilty[i+1:]
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: guilty chunks split: %v, <%v>, %v", logGuilty(guilty1), len(chunk), logGuilty(guilty2))
|
2017-06-23 13:54:57 +00:00
|
|
|
|
|
|
|
chunk1 := chunk[0 : len(chunk)/2]
|
|
|
|
chunk2 := chunk[len(chunk)/2 : len(chunk)]
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: chunk split: <%v> => <%v>, <%v>", len(chunk), len(chunk1), len(chunk2))
|
2017-06-23 13:54:57 +00:00
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: triggering crash without chunk #1")
|
2017-06-23 13:54:57 +00:00
|
|
|
progs := compose(guilty1, guilty2, chunk2)
|
|
|
|
crashed, err := pred(progs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if crashed {
|
|
|
|
guilty = nil
|
|
|
|
guilty = append(guilty, guilty1...)
|
|
|
|
guilty = append(guilty, chunk2)
|
|
|
|
guilty = append(guilty, guilty2...)
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: crashed, chunk #1 evicted")
|
2017-06-23 13:54:57 +00:00
|
|
|
goto again
|
|
|
|
}
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: triggering crash without chunk #2")
|
2017-06-23 13:54:57 +00:00
|
|
|
progs = compose(guilty1, guilty2, chunk1)
|
|
|
|
crashed, err = pred(progs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if crashed {
|
|
|
|
guilty = nil
|
|
|
|
guilty = append(guilty, guilty1...)
|
|
|
|
guilty = append(guilty, chunk1)
|
|
|
|
guilty = append(guilty, guilty2...)
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: crashed, chunk #2 evicted")
|
2017-06-23 13:54:57 +00:00
|
|
|
goto again
|
|
|
|
}
|
|
|
|
|
|
|
|
guilty = nil
|
|
|
|
guilty = append(guilty, guilty1...)
|
|
|
|
guilty = append(guilty, chunk1)
|
|
|
|
guilty = append(guilty, chunk2)
|
|
|
|
guilty = append(guilty, guilty2...)
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: not crashed, both chunks required")
|
2017-06-23 13:54:57 +00:00
|
|
|
|
|
|
|
goto again
|
|
|
|
}
|
|
|
|
|
|
|
|
progs = nil
|
|
|
|
for _, chunk := range guilty {
|
|
|
|
if len(chunk) != 1 {
|
|
|
|
return nil, fmt.Errorf("bad bisect result: %v", guilty)
|
|
|
|
}
|
|
|
|
progs = append(progs, chunk[0])
|
|
|
|
}
|
|
|
|
|
2017-06-26 11:57:55 +00:00
|
|
|
ctx.reproLog(3, "bisect: success, %d programs left", len(progs))
|
2017-06-23 13:54:57 +00:00
|
|
|
return progs, nil
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:47:18 +00:00
|
|
|
func reverseEntries(entries []*prog.LogEntry) []*prog.LogEntry {
|
|
|
|
last := len(entries) - 1
|
|
|
|
for i := 0; i < len(entries)/2; i++ {
|
|
|
|
entries[i], entries[last-i] = entries[last-i], entries[i]
|
|
|
|
}
|
|
|
|
return entries
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeEntries(entries []*prog.LogEntry) []byte {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for _, ent := range entries {
|
|
|
|
opts := ""
|
|
|
|
if ent.Fault {
|
|
|
|
opts = fmt.Sprintf(" (fault-call:%v fault-nth:%v)", ent.FaultCall, ent.FaultNth)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(buf, "executing program %v%v:\n%v", ent.Proc, opts, string(ent.P.Serialize()))
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
2017-07-05 14:22:53 +00:00
|
|
|
|
|
|
|
type Simplify func(opts *csource.Options) bool
|
|
|
|
|
|
|
|
var progSimplifies = []Simplify{
|
2017-08-09 11:17:15 +00:00
|
|
|
func(opts *csource.Options) bool {
|
|
|
|
if !opts.Fault {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
opts.Fault = false
|
|
|
|
opts.FaultCall = 0
|
|
|
|
opts.FaultNth = 0
|
|
|
|
return true
|
|
|
|
},
|
2017-07-05 14:22:53 +00:00
|
|
|
func(opts *csource.Options) bool {
|
|
|
|
if !opts.Collide {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
opts.Collide = false
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
func(opts *csource.Options) bool {
|
|
|
|
if opts.Collide || !opts.Threaded {
|
|
|
|
return false
|
|
|
|
}
|
2017-07-26 13:07:07 +00:00
|
|
|
opts.Threaded = false
|
2017-07-05 14:22:53 +00:00
|
|
|
return true
|
|
|
|
},
|
|
|
|
func(opts *csource.Options) bool {
|
|
|
|
if !opts.Repeat {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
opts.Repeat = false
|
2017-08-09 10:57:23 +00:00
|
|
|
opts.Procs = 1
|
2017-07-05 14:22:53 +00:00
|
|
|
return true
|
|
|
|
},
|
|
|
|
func(opts *csource.Options) bool {
|
|
|
|
if opts.Procs == 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
opts.Procs = 1
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
func(opts *csource.Options) bool {
|
|
|
|
if opts.Sandbox == "none" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
opts.Sandbox = "none"
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var cSimplifies = append(progSimplifies, []Simplify{
|
|
|
|
func(opts *csource.Options) bool {
|
|
|
|
if opts.Sandbox == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
opts.Sandbox = ""
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
func(opts *csource.Options) bool {
|
|
|
|
if !opts.EnableTun {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
opts.EnableTun = false
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
func(opts *csource.Options) bool {
|
2017-08-09 11:11:14 +00:00
|
|
|
if !opts.UseTmpDir || opts.Sandbox == "namespace" {
|
2017-07-05 14:22:53 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
opts.UseTmpDir = false
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
func(opts *csource.Options) bool {
|
|
|
|
if !opts.HandleSegv {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
opts.HandleSegv = false
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
func(opts *csource.Options) bool {
|
|
|
|
if !opts.WaitRepeat {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
opts.WaitRepeat = false
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}...)
|