2015-10-12 08:16:57 +00:00
|
|
|
// Copyright 2015 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 prog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2016-09-13 10:43:03 +00:00
|
|
|
"unsafe"
|
2015-10-12 08:16:57 +00:00
|
|
|
)
|
|
|
|
|
2017-12-08 09:22:56 +00:00
|
|
|
const maxBlobLen = uint64(100 << 10)
|
|
|
|
|
2016-10-21 16:19:56 +00:00
|
|
|
func (p *Prog) Mutate(rs rand.Source, ncalls int, ct *ChoiceTable, corpus []*Prog) {
|
2017-09-14 17:25:01 +00:00
|
|
|
r := newRand(p.Target, rs)
|
2016-10-21 16:19:56 +00:00
|
|
|
|
2017-02-01 15:27:13 +00:00
|
|
|
retry := false
|
|
|
|
for stop := false; !stop || retry; stop = r.oneOf(3) {
|
|
|
|
retry = false
|
|
|
|
switch {
|
|
|
|
case r.nOutOf(1, 100):
|
|
|
|
// Splice with another prog from corpus.
|
|
|
|
if len(corpus) == 0 || len(p.Calls) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p0 := corpus[r.Intn(len(corpus))]
|
|
|
|
p0c := p0.Clone()
|
|
|
|
idx := r.Intn(len(p.Calls))
|
|
|
|
p.Calls = append(p.Calls[:idx], append(p0c.Calls, p.Calls[idx:]...)...)
|
2017-08-01 13:28:12 +00:00
|
|
|
for i := len(p.Calls) - 1; i >= ncalls; i-- {
|
|
|
|
p.removeCall(i)
|
2017-02-01 15:27:13 +00:00
|
|
|
}
|
|
|
|
case r.nOutOf(20, 31):
|
|
|
|
// Insert a new call.
|
|
|
|
if len(p.Calls) >= ncalls {
|
|
|
|
retry = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
idx := r.biasedRand(len(p.Calls)+1, 5)
|
|
|
|
var c *Call
|
|
|
|
if idx < len(p.Calls) {
|
|
|
|
c = p.Calls[idx]
|
|
|
|
}
|
|
|
|
s := analyze(ct, p, c)
|
|
|
|
calls := r.generateCall(s, p)
|
|
|
|
p.insertBefore(c, calls)
|
|
|
|
case r.nOutOf(10, 11):
|
|
|
|
// Change args of a call.
|
|
|
|
if len(p.Calls) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c := p.Calls[r.Intn(len(p.Calls))]
|
|
|
|
if len(c.Args) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue
|
|
|
|
}
|
2017-08-02 14:03:57 +00:00
|
|
|
// Mutating mmap() arguments almost certainly doesn't give us new coverage.
|
2017-09-14 17:25:01 +00:00
|
|
|
if c.Meta == p.Target.MmapSyscall && r.nOutOf(99, 100) {
|
2017-08-02 14:03:57 +00:00
|
|
|
retry = true
|
|
|
|
continue
|
|
|
|
}
|
2017-02-01 15:27:13 +00:00
|
|
|
s := analyze(ct, p, c)
|
|
|
|
for stop := false; !stop; stop = r.oneOf(3) {
|
2017-09-14 17:25:01 +00:00
|
|
|
args, bases := p.Target.mutationArgs(c)
|
2017-02-01 15:27:13 +00:00
|
|
|
if len(args) == 0 {
|
2017-01-20 22:55:25 +00:00
|
|
|
retry = true
|
|
|
|
continue
|
|
|
|
}
|
2017-02-01 15:27:13 +00:00
|
|
|
idx := r.Intn(len(args))
|
|
|
|
arg, base := args[idx], bases[idx]
|
2017-08-19 07:46:43 +00:00
|
|
|
var baseSize uint64
|
2017-02-01 15:27:13 +00:00
|
|
|
if base != nil {
|
2017-07-11 14:49:08 +00:00
|
|
|
b, ok := base.(*PointerArg)
|
|
|
|
if !ok || b.Res == nil {
|
2017-02-01 15:27:13 +00:00
|
|
|
panic("bad base arg")
|
2016-10-21 16:19:56 +00:00
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
baseSize = b.Res.Size()
|
2017-02-01 15:27:13 +00:00
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
switch t := arg.Type().(type) {
|
2017-09-05 08:46:34 +00:00
|
|
|
case *IntType, *FlagsType:
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*ConstArg)
|
2017-02-01 15:27:13 +00:00
|
|
|
if r.bin() {
|
2017-07-11 14:49:08 +00:00
|
|
|
arg1, calls1 := r.generateArg(s, arg.Type())
|
2017-02-01 15:27:13 +00:00
|
|
|
p.replaceArg(c, arg, arg1, calls1)
|
|
|
|
} else {
|
|
|
|
switch {
|
|
|
|
case r.nOutOf(1, 3):
|
2017-08-19 07:46:43 +00:00
|
|
|
a.Val += uint64(r.Intn(4)) + 1
|
2017-02-01 15:27:13 +00:00
|
|
|
case r.nOutOf(1, 2):
|
2017-08-19 07:46:43 +00:00
|
|
|
a.Val -= uint64(r.Intn(4)) + 1
|
2017-02-01 15:27:13 +00:00
|
|
|
default:
|
2017-08-19 07:46:43 +00:00
|
|
|
a.Val ^= 1 << uint64(r.Intn(64))
|
2016-10-21 16:19:56 +00:00
|
|
|
}
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case *ResourceType, *VmaType, *ProcType:
|
2017-07-11 14:49:08 +00:00
|
|
|
arg1, calls1 := r.generateArg(s, arg.Type())
|
2017-02-01 15:27:13 +00:00
|
|
|
p.replaceArg(c, arg, arg1, calls1)
|
2017-09-05 08:46:34 +00:00
|
|
|
case *BufferType:
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*DataArg)
|
|
|
|
switch t.Kind {
|
2017-09-05 08:46:34 +00:00
|
|
|
case BufferBlobRand, BufferBlobRange:
|
2017-02-01 15:27:13 +00:00
|
|
|
var data []byte
|
2017-07-11 14:49:08 +00:00
|
|
|
data = append([]byte{}, a.Data...)
|
2017-12-08 09:22:56 +00:00
|
|
|
minLen, maxLen := uint64(0), maxBlobLen
|
2017-09-05 08:46:34 +00:00
|
|
|
if t.Kind == BufferBlobRange {
|
2017-12-08 09:22:56 +00:00
|
|
|
minLen, maxLen = t.RangeBegin, t.RangeEnd
|
2017-02-01 15:27:13 +00:00
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
a.Data = mutateData(r, data, minLen, maxLen)
|
2017-09-05 08:46:34 +00:00
|
|
|
case BufferString:
|
2017-02-01 15:27:13 +00:00
|
|
|
if r.bin() {
|
2017-12-08 09:22:56 +00:00
|
|
|
minLen, maxLen := uint64(0), maxBlobLen
|
2017-09-04 17:53:05 +00:00
|
|
|
if t.TypeSize != 0 {
|
|
|
|
minLen, maxLen = t.TypeSize, t.TypeSize
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
a.Data = mutateData(r, append([]byte{}, a.Data...), minLen, maxLen)
|
2017-02-01 15:27:13 +00:00
|
|
|
} else {
|
2017-07-11 14:49:08 +00:00
|
|
|
a.Data = r.randString(s, t.Values, t.Dir())
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case BufferFilename:
|
2017-07-11 14:49:08 +00:00
|
|
|
a.Data = []byte(r.filename(s))
|
2017-09-05 08:46:34 +00:00
|
|
|
case BufferText:
|
2017-07-11 14:49:08 +00:00
|
|
|
a.Data = r.mutateText(t.Text, a.Data)
|
2017-02-01 15:27:13 +00:00
|
|
|
default:
|
|
|
|
panic("unknown buffer kind")
|
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case *ArrayType:
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*GroupArg)
|
2017-08-19 07:46:43 +00:00
|
|
|
count := uint64(0)
|
2017-07-11 14:49:08 +00:00
|
|
|
switch t.Kind {
|
2017-09-05 08:46:34 +00:00
|
|
|
case ArrayRandLen:
|
2017-08-19 07:46:43 +00:00
|
|
|
for count == uint64(len(a.Inner)) {
|
2017-02-01 15:27:13 +00:00
|
|
|
count = r.randArrayLen()
|
2015-12-31 14:24:08 +00:00
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case ArrayRangeLen:
|
2017-07-11 14:49:08 +00:00
|
|
|
if t.RangeBegin == t.RangeEnd {
|
2017-02-01 15:27:13 +00:00
|
|
|
panic("trying to mutate fixed length array")
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2017-08-19 07:46:43 +00:00
|
|
|
for count == uint64(len(a.Inner)) {
|
|
|
|
count = r.randRange(t.RangeBegin, t.RangeEnd)
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2017-02-01 15:27:13 +00:00
|
|
|
}
|
2017-08-19 07:46:43 +00:00
|
|
|
if count > uint64(len(a.Inner)) {
|
2017-02-01 15:27:13 +00:00
|
|
|
var calls []*Call
|
2017-08-19 07:46:43 +00:00
|
|
|
for count > uint64(len(a.Inner)) {
|
2017-07-11 14:49:08 +00:00
|
|
|
arg1, calls1 := r.generateArg(s, t.Type)
|
|
|
|
a.Inner = append(a.Inner, arg1)
|
2017-02-01 15:27:13 +00:00
|
|
|
for _, c1 := range calls1 {
|
|
|
|
calls = append(calls, c1)
|
|
|
|
s.analyze(c1)
|
|
|
|
}
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2017-02-01 15:27:13 +00:00
|
|
|
for _, c1 := range calls {
|
2017-09-14 17:25:01 +00:00
|
|
|
p.Target.SanitizeCall(c1)
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2017-09-14 17:25:01 +00:00
|
|
|
p.Target.SanitizeCall(c)
|
2017-02-01 15:27:13 +00:00
|
|
|
p.insertBefore(c, calls)
|
2017-08-19 07:46:43 +00:00
|
|
|
} else if count < uint64(len(a.Inner)) {
|
2017-07-11 14:49:08 +00:00
|
|
|
for _, arg := range a.Inner[count:] {
|
2017-02-01 15:27:13 +00:00
|
|
|
p.removeArg(c, arg)
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
a.Inner = a.Inner[:count]
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2017-02-01 15:27:13 +00:00
|
|
|
// TODO: swap elements of the array
|
2017-09-05 08:46:34 +00:00
|
|
|
case *PtrType:
|
2017-07-11 14:49:08 +00:00
|
|
|
a, ok := arg.(*PointerArg)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2017-02-01 15:27:13 +00:00
|
|
|
// TODO: we don't know size for out args
|
2017-08-19 07:46:43 +00:00
|
|
|
size := uint64(1)
|
2017-07-11 14:49:08 +00:00
|
|
|
if a.Res != nil {
|
|
|
|
size = a.Res.Size()
|
2017-02-01 15:27:13 +00:00
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
arg1, calls1 := r.addr(s, t, size, a.Res)
|
2017-02-01 15:27:13 +00:00
|
|
|
p.replaceArg(c, arg, arg1, calls1)
|
2017-09-05 08:46:34 +00:00
|
|
|
case *StructType:
|
2017-09-14 17:25:01 +00:00
|
|
|
gen := p.Target.SpecialStructs[t.Name()]
|
2017-09-05 11:31:14 +00:00
|
|
|
if gen == nil {
|
2017-02-01 15:27:13 +00:00
|
|
|
panic("bad arg returned by mutationArgs: StructType")
|
|
|
|
}
|
2017-09-05 11:31:14 +00:00
|
|
|
arg1, calls1 := gen(&Gen{r, s}, t, arg.(*GroupArg))
|
2017-07-11 14:49:08 +00:00
|
|
|
for i, f := range arg1.(*GroupArg).Inner {
|
|
|
|
p.replaceArg(c, arg.(*GroupArg).Inner[i], f, calls1)
|
2017-02-01 15:27:13 +00:00
|
|
|
calls1 = nil
|
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case *UnionType:
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*UnionArg)
|
2017-09-04 17:53:05 +00:00
|
|
|
optType := t.Fields[r.Intn(len(t.Fields))]
|
2017-02-01 15:27:13 +00:00
|
|
|
maxIters := 1000
|
2017-07-11 14:49:08 +00:00
|
|
|
for i := 0; optType.FieldName() == a.OptionType.FieldName(); i++ {
|
2017-09-04 17:53:05 +00:00
|
|
|
optType = t.Fields[r.Intn(len(t.Fields))]
|
2017-02-01 15:27:13 +00:00
|
|
|
if i >= maxIters {
|
2017-07-11 14:49:08 +00:00
|
|
|
panic(fmt.Sprintf("couldn't generate a different union option after %v iterations, type: %+v", maxIters, t))
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2016-10-21 16:19:56 +00:00
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
p.removeArg(c, a.Option)
|
2017-02-01 15:27:13 +00:00
|
|
|
opt, calls := r.generateArg(s, optType)
|
2017-11-22 10:08:20 +00:00
|
|
|
arg1 := MakeUnionArg(t, opt, optType)
|
2017-02-01 15:27:13 +00:00
|
|
|
p.replaceArg(c, arg, arg1, calls)
|
2017-09-05 08:46:34 +00:00
|
|
|
case *LenType:
|
2017-02-01 15:27:13 +00:00
|
|
|
panic("bad arg returned by mutationArgs: LenType")
|
2017-09-05 08:46:34 +00:00
|
|
|
case *CsumType:
|
2017-02-01 15:27:13 +00:00
|
|
|
panic("bad arg returned by mutationArgs: CsumType")
|
2017-09-05 08:46:34 +00:00
|
|
|
case *ConstType:
|
2017-02-01 15:27:13 +00:00
|
|
|
panic("bad arg returned by mutationArgs: ConstType")
|
|
|
|
default:
|
2017-07-11 14:49:08 +00:00
|
|
|
panic(fmt.Sprintf("bad arg returned by mutationArgs: %#v, type=%#v", arg, arg.Type()))
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2017-02-01 15:27:13 +00:00
|
|
|
|
|
|
|
// Update base pointer if size has increased.
|
2017-07-11 14:49:08 +00:00
|
|
|
if base != nil {
|
|
|
|
b := base.(*PointerArg)
|
|
|
|
if baseSize < b.Res.Size() {
|
|
|
|
arg1, calls1 := r.addr(s, b.Type(), b.Res.Size(), b.Res)
|
|
|
|
for _, c1 := range calls1 {
|
2017-09-14 17:25:01 +00:00
|
|
|
p.Target.SanitizeCall(c1)
|
2017-07-11 14:49:08 +00:00
|
|
|
}
|
|
|
|
p.insertBefore(c, calls1)
|
|
|
|
a1 := arg1.(*PointerArg)
|
|
|
|
b.PageIndex = a1.PageIndex
|
|
|
|
b.PageOffset = a1.PageOffset
|
|
|
|
b.PagesNum = a1.PagesNum
|
2017-02-01 15:27:13 +00:00
|
|
|
}
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2017-02-01 15:27:13 +00:00
|
|
|
|
|
|
|
// Update all len fields.
|
2017-09-14 17:25:01 +00:00
|
|
|
p.Target.assignSizesCall(c)
|
2017-02-01 15:27:13 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Remove a random call.
|
|
|
|
if len(p.Calls) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2017-02-01 15:27:13 +00:00
|
|
|
idx := r.Intn(len(p.Calls))
|
|
|
|
p.removeCall(idx)
|
2016-10-21 16:19:56 +00:00
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2016-10-21 16:19:56 +00:00
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
for _, c := range p.Calls {
|
2017-09-14 17:25:01 +00:00
|
|
|
p.Target.SanitizeCall(c)
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2017-01-24 09:51:38 +00:00
|
|
|
if debug {
|
|
|
|
if err := p.validate(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Minimize minimizes program p into an equivalent program using the equivalence
|
|
|
|
// predicate pred. It iteratively generates simpler programs and asks pred
|
|
|
|
// whether it is equal to the orginal program or not. If it is equivalent then
|
|
|
|
// the simplification attempt is committed and the process continues.
|
2017-09-05 11:31:14 +00:00
|
|
|
func Minimize(p0 *Prog, callIndex0 int, pred0 func(*Prog, int) bool, crash bool) (*Prog, int) {
|
|
|
|
pred := pred0
|
|
|
|
if debug {
|
|
|
|
pred = func(p *Prog, callIndex int) bool {
|
|
|
|
if err := p.validate(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return pred0(p, callIndex)
|
|
|
|
}
|
|
|
|
}
|
2015-12-23 12:47:45 +00:00
|
|
|
name0 := ""
|
|
|
|
if callIndex0 != -1 {
|
|
|
|
if callIndex0 < 0 || callIndex0 >= len(p0.Calls) {
|
|
|
|
panic("bad call index")
|
|
|
|
}
|
|
|
|
name0 = p0.Calls[callIndex0].Meta.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to glue all mmap's together.
|
|
|
|
s := analyze(nil, p0, nil)
|
|
|
|
hi := -1
|
2017-08-08 14:50:22 +00:00
|
|
|
lo := -1
|
2015-12-23 12:47:45 +00:00
|
|
|
for i := 0; i < maxPages; i++ {
|
|
|
|
if s.pages[i] {
|
|
|
|
hi = i
|
2017-08-08 14:50:22 +00:00
|
|
|
if lo == -1 {
|
|
|
|
lo = i
|
|
|
|
}
|
2015-12-23 12:47:45 +00:00
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2015-12-23 12:47:45 +00:00
|
|
|
if hi != -1 {
|
|
|
|
p := p0.Clone()
|
|
|
|
callIndex := callIndex0
|
|
|
|
// Remove all mmaps.
|
|
|
|
for i := 0; i < len(p.Calls); i++ {
|
|
|
|
c := p.Calls[i]
|
2017-09-14 17:25:01 +00:00
|
|
|
if i != callIndex && c.Meta == p.Target.MmapSyscall {
|
2015-12-31 14:24:08 +00:00
|
|
|
p.removeCall(i)
|
2015-12-23 12:47:45 +00:00
|
|
|
if i < callIndex {
|
|
|
|
callIndex--
|
|
|
|
}
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Prepend uber-mmap.
|
2017-09-14 17:25:01 +00:00
|
|
|
mmap := p0.Target.MakeMmap(uint64(lo), uint64(hi-lo)+1)
|
2015-12-23 12:47:45 +00:00
|
|
|
p.Calls = append([]*Call{mmap}, p.Calls...)
|
|
|
|
if callIndex != -1 {
|
|
|
|
callIndex++
|
|
|
|
}
|
|
|
|
if pred(p, callIndex) {
|
|
|
|
p0 = p
|
|
|
|
callIndex0 = callIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
// Try to remove all calls except the last one one-by-one.
|
|
|
|
for i := len(p0.Calls) - 1; i >= 0; i-- {
|
|
|
|
if i == callIndex0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
callIndex := callIndex0
|
|
|
|
if i < callIndex {
|
|
|
|
callIndex--
|
|
|
|
}
|
|
|
|
p := p0.Clone()
|
2015-12-31 14:24:08 +00:00
|
|
|
p.removeCall(i)
|
2015-10-12 08:16:57 +00:00
|
|
|
if !pred(p, callIndex) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p0 = p
|
|
|
|
callIndex0 = callIndex
|
|
|
|
}
|
2016-10-21 16:24:33 +00:00
|
|
|
|
|
|
|
var triedPaths map[string]bool
|
|
|
|
|
2017-07-11 14:49:08 +00:00
|
|
|
var rec func(p *Prog, call *Call, arg Arg, path string) bool
|
|
|
|
rec = func(p *Prog, call *Call, arg Arg, path string) bool {
|
|
|
|
path += fmt.Sprintf("-%v", arg.Type().FieldName())
|
|
|
|
switch typ := arg.Type().(type) {
|
2017-09-05 08:46:34 +00:00
|
|
|
case *StructType:
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*GroupArg)
|
|
|
|
for _, innerArg := range a.Inner {
|
2016-10-21 16:24:33 +00:00
|
|
|
if rec(p, call, innerArg, path) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case *UnionType:
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*UnionArg)
|
|
|
|
if rec(p, call, a.Option, path) {
|
2016-10-21 16:24:33 +00:00
|
|
|
return true
|
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case *PtrType:
|
2016-10-21 16:24:33 +00:00
|
|
|
// TODO: try to remove optional ptrs
|
2017-07-11 14:49:08 +00:00
|
|
|
a, ok := arg.(*PointerArg)
|
|
|
|
if !ok {
|
|
|
|
// Can also be *ConstArg.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if a.Res != nil {
|
|
|
|
return rec(p, call, a.Res, path)
|
2016-10-21 16:24:33 +00:00
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case *ArrayType:
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*GroupArg)
|
|
|
|
for i, innerArg := range a.Inner {
|
2016-10-21 16:24:33 +00:00
|
|
|
innerPath := fmt.Sprintf("%v-%v", path, i)
|
|
|
|
if !triedPaths[innerPath] && !crash {
|
2017-09-05 08:46:34 +00:00
|
|
|
if (typ.Kind == ArrayRangeLen && len(a.Inner) > int(typ.RangeBegin)) ||
|
|
|
|
(typ.Kind == ArrayRandLen) {
|
2017-07-11 14:49:08 +00:00
|
|
|
copy(a.Inner[i:], a.Inner[i+1:])
|
|
|
|
a.Inner = a.Inner[:len(a.Inner)-1]
|
2016-10-21 16:24:33 +00:00
|
|
|
p.removeArg(call, innerArg)
|
2017-09-14 17:25:01 +00:00
|
|
|
p.Target.assignSizesCall(call)
|
2016-10-21 16:24:33 +00:00
|
|
|
|
|
|
|
if pred(p, callIndex0) {
|
|
|
|
p0 = p
|
|
|
|
} else {
|
|
|
|
triedPaths[innerPath] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if rec(p, call, innerArg, innerPath) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case *IntType, *FlagsType, *ProcType:
|
2016-10-21 16:24:33 +00:00
|
|
|
// TODO: try to reset bits in ints
|
|
|
|
// TODO: try to set separate flags
|
|
|
|
if crash {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if triedPaths[path] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
triedPaths[path] = true
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*ConstArg)
|
|
|
|
if a.Val == typ.Default() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
v0 := a.Val
|
|
|
|
a.Val = typ.Default()
|
|
|
|
if pred(p, callIndex0) {
|
|
|
|
p0 = p
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
a.Val = v0
|
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case *ResourceType:
|
2017-07-11 14:49:08 +00:00
|
|
|
if crash {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if triedPaths[path] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
triedPaths[path] = true
|
|
|
|
a := arg.(*ResultArg)
|
|
|
|
if a.Res == nil {
|
2016-10-21 16:24:33 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
r0 := a.Res
|
|
|
|
a.Res = nil
|
|
|
|
a.Val = typ.Default()
|
2016-10-21 16:24:33 +00:00
|
|
|
if pred(p, callIndex0) {
|
|
|
|
p0 = p
|
|
|
|
return true
|
|
|
|
} else {
|
2017-07-11 14:49:08 +00:00
|
|
|
a.Res = r0
|
|
|
|
a.Val = 0
|
2016-10-21 16:24:33 +00:00
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case *BufferType:
|
2016-10-21 16:24:33 +00:00
|
|
|
// TODO: try to set individual bytes to 0
|
|
|
|
if triedPaths[path] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
triedPaths[path] = true
|
2017-09-05 08:46:34 +00:00
|
|
|
if typ.Kind != BufferBlobRand && typ.Kind != BufferBlobRange {
|
2016-10-21 16:24:33 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*DataArg)
|
2016-10-21 16:24:33 +00:00
|
|
|
minLen := int(typ.RangeBegin)
|
2017-07-11 14:49:08 +00:00
|
|
|
for step := len(a.Data) - minLen; len(a.Data) > minLen && step > 0; {
|
|
|
|
if len(a.Data)-step >= minLen {
|
|
|
|
a.Data = a.Data[:len(a.Data)-step]
|
2017-09-14 17:25:01 +00:00
|
|
|
p.Target.assignSizesCall(call)
|
2016-10-21 16:24:33 +00:00
|
|
|
if pred(p, callIndex0) {
|
|
|
|
continue
|
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
a.Data = a.Data[:len(a.Data)+step]
|
2017-09-14 17:25:01 +00:00
|
|
|
p.Target.assignSizesCall(call)
|
2016-10-21 16:24:33 +00:00
|
|
|
}
|
|
|
|
step /= 2
|
|
|
|
if crash {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p0 = p
|
2017-09-05 08:46:34 +00:00
|
|
|
case *VmaType, *LenType, *CsumType, *ConstType:
|
2016-10-21 16:24:33 +00:00
|
|
|
// TODO: try to remove offset from vma
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown arg type '%+v'", typ))
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to minimize individual args.
|
|
|
|
for i := 0; i < len(p0.Calls); i++ {
|
|
|
|
triedPaths = make(map[string]bool)
|
|
|
|
again:
|
|
|
|
p := p0.Clone()
|
|
|
|
call := p.Calls[i]
|
|
|
|
for j, arg := range call.Args {
|
|
|
|
if rec(p, call, arg, fmt.Sprintf("%v", j)) {
|
|
|
|
goto again
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:47:45 +00:00
|
|
|
if callIndex0 != -1 {
|
|
|
|
if callIndex0 < 0 || callIndex0 >= len(p0.Calls) || name0 != p0.Calls[callIndex0].Meta.Name {
|
2017-10-10 08:41:27 +00:00
|
|
|
panic(fmt.Sprintf("bad call index after minimization: ncalls=%v index=%v call=%v/%v",
|
2015-12-23 12:47:45 +00:00
|
|
|
len(p0.Calls), callIndex0, name0, p0.Calls[callIndex0].Meta.Name))
|
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
return p0, callIndex0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Prog) TrimAfter(idx int) {
|
|
|
|
if idx < 0 || idx >= len(p.Calls) {
|
|
|
|
panic("trimming non-existing call")
|
|
|
|
}
|
|
|
|
for i := len(p.Calls) - 1; i > idx; i-- {
|
|
|
|
c := p.Calls[i]
|
2017-07-11 14:49:08 +00:00
|
|
|
foreachArg(c, func(arg, _ Arg, _ *[]Arg) {
|
|
|
|
if a, ok := arg.(*ResultArg); ok && a.Res != nil {
|
|
|
|
if used, ok := a.Res.(ArgUsed); ok {
|
|
|
|
delete(*used.Used(), arg)
|
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
p.Calls = p.Calls[:idx+1]
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:25:01 +00:00
|
|
|
func (target *Target) mutationArgs(c *Call) (args, bases []Arg) {
|
2017-07-11 14:49:08 +00:00
|
|
|
foreachArg(c, func(arg, base Arg, _ *[]Arg) {
|
|
|
|
switch typ := arg.Type().(type) {
|
2017-09-05 08:46:34 +00:00
|
|
|
case *StructType:
|
2017-09-14 17:25:01 +00:00
|
|
|
if target.SpecialStructs[typ.Name()] == nil {
|
2015-10-12 08:16:57 +00:00
|
|
|
// For structs only individual fields are updated.
|
|
|
|
return
|
|
|
|
}
|
2015-12-29 14:00:57 +00:00
|
|
|
// These special structs are mutated as a whole.
|
2017-09-05 08:46:34 +00:00
|
|
|
case *ArrayType:
|
2015-12-30 18:08:20 +00:00
|
|
|
// Don't mutate fixed-size arrays.
|
2017-09-05 08:46:34 +00:00
|
|
|
if typ.Kind == ArrayRangeLen && typ.RangeBegin == typ.RangeEnd {
|
2015-12-30 18:08:20 +00:00
|
|
|
return
|
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
case *LenType:
|
2015-10-12 08:16:57 +00:00
|
|
|
// Size is updated when the size-of arg change.
|
|
|
|
return
|
2017-09-05 08:46:34 +00:00
|
|
|
case *CsumType:
|
2017-01-25 15:18:05 +00:00
|
|
|
// Checksum is updated when the checksummed data changes.
|
|
|
|
return
|
2017-09-05 08:46:34 +00:00
|
|
|
case *ConstType:
|
2015-10-16 16:16:10 +00:00
|
|
|
// Well, this is const.
|
|
|
|
return
|
2017-09-05 08:46:34 +00:00
|
|
|
case *BufferType:
|
|
|
|
if typ.Kind == BufferString && len(typ.Values) == 1 {
|
2016-10-31 21:15:13 +00:00
|
|
|
return // string const
|
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
if arg.Type().Dir() == DirOut {
|
2015-10-12 08:16:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if base != nil {
|
2017-09-14 17:25:01 +00:00
|
|
|
if _, ok := base.Type().(*StructType); ok &&
|
|
|
|
target.SpecialStructs[base.Type().Name()] != nil {
|
2015-10-12 08:16:57 +00:00
|
|
|
// These special structs are mutated as a whole.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args = append(args, arg)
|
|
|
|
bases = append(bases, base)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-13 10:43:03 +00:00
|
|
|
func swap16(v uint16) uint16 {
|
|
|
|
v0 := byte(v >> 0)
|
|
|
|
v1 := byte(v >> 8)
|
|
|
|
v = 0
|
|
|
|
v |= uint16(v1) << 0
|
|
|
|
v |= uint16(v0) << 8
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func swap32(v uint32) uint32 {
|
|
|
|
v0 := byte(v >> 0)
|
|
|
|
v1 := byte(v >> 8)
|
|
|
|
v2 := byte(v >> 16)
|
|
|
|
v3 := byte(v >> 24)
|
|
|
|
v = 0
|
|
|
|
v |= uint32(v3) << 0
|
|
|
|
v |= uint32(v2) << 8
|
|
|
|
v |= uint32(v1) << 16
|
|
|
|
v |= uint32(v0) << 24
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func swap64(v uint64) uint64 {
|
|
|
|
v0 := byte(v >> 0)
|
|
|
|
v1 := byte(v >> 8)
|
|
|
|
v2 := byte(v >> 16)
|
|
|
|
v3 := byte(v >> 24)
|
|
|
|
v4 := byte(v >> 32)
|
|
|
|
v5 := byte(v >> 40)
|
|
|
|
v6 := byte(v >> 48)
|
|
|
|
v7 := byte(v >> 56)
|
|
|
|
v = 0
|
|
|
|
v |= uint64(v7) << 0
|
|
|
|
v |= uint64(v6) << 8
|
|
|
|
v |= uint64(v5) << 16
|
|
|
|
v |= uint64(v4) << 24
|
|
|
|
v |= uint64(v3) << 32
|
|
|
|
v |= uint64(v2) << 40
|
|
|
|
v |= uint64(v1) << 48
|
|
|
|
v |= uint64(v0) << 56
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-08-19 07:46:43 +00:00
|
|
|
func mutateData(r *randGen, data []byte, minLen, maxLen uint64) []byte {
|
2016-09-13 10:43:03 +00:00
|
|
|
const maxInc = 35
|
2017-01-20 22:55:25 +00:00
|
|
|
retry := false
|
|
|
|
loop:
|
|
|
|
for stop := false; !stop || retry; stop = r.oneOf(3) {
|
|
|
|
retry = false
|
2017-12-08 09:22:56 +00:00
|
|
|
switch r.Intn(14) {
|
2017-01-20 22:55:25 +00:00
|
|
|
case 0:
|
|
|
|
// Append byte.
|
2017-08-19 07:46:43 +00:00
|
|
|
if uint64(len(data)) >= maxLen {
|
2017-01-20 22:55:25 +00:00
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
data = append(data, byte(r.rand(256)))
|
|
|
|
case 1:
|
|
|
|
// Remove byte.
|
2017-08-19 07:46:43 +00:00
|
|
|
if len(data) == 0 || uint64(len(data)) <= minLen {
|
2017-01-20 22:55:25 +00:00
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data))
|
|
|
|
copy(data[i:], data[i+1:])
|
|
|
|
data = data[:len(data)-1]
|
|
|
|
case 2:
|
|
|
|
// Replace byte with random value.
|
|
|
|
if len(data) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
data[r.Intn(len(data))] = byte(r.rand(256))
|
|
|
|
case 3:
|
|
|
|
// Flip bit in byte.
|
|
|
|
if len(data) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
byt := r.Intn(len(data))
|
|
|
|
bit := r.Intn(8)
|
|
|
|
data[byt] ^= 1 << uint(bit)
|
|
|
|
case 4:
|
|
|
|
// Swap two bytes.
|
|
|
|
if len(data) < 2 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i1 := r.Intn(len(data))
|
|
|
|
i2 := r.Intn(len(data))
|
|
|
|
data[i1], data[i2] = data[i2], data[i1]
|
|
|
|
case 5:
|
|
|
|
// Add / subtract from a byte.
|
|
|
|
if len(data) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data))
|
|
|
|
delta := byte(r.rand(2*maxInc+1) - maxInc)
|
|
|
|
if delta == 0 {
|
|
|
|
delta = 1
|
|
|
|
}
|
|
|
|
data[i] += delta
|
|
|
|
case 6:
|
|
|
|
// Add / subtract from a uint16.
|
|
|
|
if len(data) < 2 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 1)
|
|
|
|
p := (*uint16)(unsafe.Pointer(&data[i]))
|
|
|
|
delta := uint16(r.rand(2*maxInc+1) - maxInc)
|
|
|
|
if delta == 0 {
|
|
|
|
delta = 1
|
|
|
|
}
|
|
|
|
if r.bin() {
|
|
|
|
*p += delta
|
|
|
|
} else {
|
|
|
|
*p = swap16(swap16(*p) + delta)
|
|
|
|
}
|
|
|
|
case 7:
|
|
|
|
// Add / subtract from a uint32.
|
|
|
|
if len(data) < 4 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 3)
|
|
|
|
p := (*uint32)(unsafe.Pointer(&data[i]))
|
|
|
|
delta := uint32(r.rand(2*maxInc+1) - maxInc)
|
|
|
|
if delta == 0 {
|
|
|
|
delta = 1
|
|
|
|
}
|
|
|
|
if r.bin() {
|
|
|
|
*p += delta
|
|
|
|
} else {
|
|
|
|
*p = swap32(swap32(*p) + delta)
|
|
|
|
}
|
|
|
|
case 8:
|
|
|
|
// Add / subtract from a uint64.
|
|
|
|
if len(data) < 8 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 7)
|
|
|
|
p := (*uint64)(unsafe.Pointer(&data[i]))
|
2017-08-19 07:46:43 +00:00
|
|
|
delta := r.rand(2*maxInc+1) - maxInc
|
2017-01-20 22:55:25 +00:00
|
|
|
if delta == 0 {
|
|
|
|
delta = 1
|
|
|
|
}
|
|
|
|
if r.bin() {
|
|
|
|
*p += delta
|
|
|
|
} else {
|
|
|
|
*p = swap64(swap64(*p) + delta)
|
|
|
|
}
|
|
|
|
case 9:
|
|
|
|
// Set byte to an interesting value.
|
|
|
|
if len(data) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
data[r.Intn(len(data))] = byte(r.randInt())
|
|
|
|
case 10:
|
|
|
|
// Set uint16 to an interesting value.
|
|
|
|
if len(data) < 2 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 1)
|
|
|
|
value := uint16(r.randInt())
|
|
|
|
if r.bin() {
|
|
|
|
value = swap16(value)
|
|
|
|
}
|
|
|
|
*(*uint16)(unsafe.Pointer(&data[i])) = value
|
|
|
|
case 11:
|
|
|
|
// Set uint32 to an interesting value.
|
|
|
|
if len(data) < 4 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 3)
|
|
|
|
value := uint32(r.randInt())
|
|
|
|
if r.bin() {
|
|
|
|
value = swap32(value)
|
|
|
|
}
|
|
|
|
*(*uint32)(unsafe.Pointer(&data[i])) = value
|
|
|
|
case 12:
|
|
|
|
// Set uint64 to an interesting value.
|
|
|
|
if len(data) < 8 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 7)
|
2017-08-19 07:46:43 +00:00
|
|
|
value := r.randInt()
|
2017-01-20 22:55:25 +00:00
|
|
|
if r.bin() {
|
|
|
|
value = swap64(value)
|
|
|
|
}
|
|
|
|
*(*uint64)(unsafe.Pointer(&data[i])) = value
|
2017-12-08 09:22:56 +00:00
|
|
|
case 13:
|
|
|
|
// Append a bunch of bytes.
|
|
|
|
if uint64(len(data)) >= maxLen {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
const max = 256
|
|
|
|
n := max - r.biasedRand(max, 10)
|
|
|
|
if r := int(maxLen) - len(data); n > r {
|
|
|
|
n = r
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
data = append(data, byte(r.rand(256)))
|
|
|
|
}
|
2017-01-20 22:55:25 +00:00
|
|
|
default:
|
|
|
|
panic("bad")
|
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|