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
|
2018-01-24 18:28:36 +00:00
|
|
|
outer:
|
2017-02-01 15:27:13 +00:00
|
|
|
for stop := false; !stop || retry; stop = r.oneOf(3) {
|
|
|
|
retry = false
|
|
|
|
switch {
|
2018-02-24 13:33:36 +00:00
|
|
|
case r.oneOf(5):
|
|
|
|
// Not all calls have anything squashable,
|
|
|
|
// so this has lower priority in reality.
|
|
|
|
complexPtrs := p.complexPtrs()
|
|
|
|
if len(complexPtrs) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ptr := complexPtrs[r.Intn(len(complexPtrs))]
|
|
|
|
if !p.Target.isAnyPtr(ptr.Type()) {
|
|
|
|
p.Target.squashPtr(ptr, true)
|
|
|
|
}
|
|
|
|
var blobs []*DataArg
|
2018-02-26 12:33:11 +00:00
|
|
|
var bases []*PointerArg
|
|
|
|
ForeachSubArg(ptr, func(arg Arg, ctx *ArgCtx) {
|
2018-02-24 13:33:36 +00:00
|
|
|
if data, ok := arg.(*DataArg); ok && arg.Type().Dir() != DirOut {
|
|
|
|
blobs = append(blobs, data)
|
2018-02-26 12:33:11 +00:00
|
|
|
bases = append(bases, ctx.Base)
|
2018-02-24 13:33:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
if len(blobs) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO(dvyukov): we probably want special mutation for ANY.
|
|
|
|
// E.g. merging adjacent ANYBLOBs (we don't create them,
|
|
|
|
// but they can appear in future); or replacing ANYRES
|
|
|
|
// with a blob (and merging it with adjacent blobs).
|
2018-02-26 12:33:11 +00:00
|
|
|
idx := r.Intn(len(blobs))
|
|
|
|
arg := blobs[idx]
|
|
|
|
base := bases[idx]
|
|
|
|
baseSize := base.Res.Size()
|
2018-02-24 13:33:36 +00:00
|
|
|
arg.data = mutateData(r, arg.Data(), 0, maxBlobLen)
|
2018-02-26 12:33:11 +00:00
|
|
|
// Update base pointer if size has increased.
|
|
|
|
if baseSize < base.Res.Size() {
|
|
|
|
s := analyze(ct, p, p.Calls[0])
|
|
|
|
newArg := r.allocAddr(s, base.Type(), base.Res.Size(), base.Res)
|
|
|
|
*base = *newArg
|
|
|
|
}
|
2017-02-01 15:27:13 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
s := analyze(ct, p, c)
|
2017-11-27 07:59:37 +00:00
|
|
|
updateSizes := true
|
2018-01-24 18:28:36 +00:00
|
|
|
retryArg := false
|
|
|
|
for stop := false; !stop || retryArg; stop = r.oneOf(3) {
|
|
|
|
retryArg = false
|
2018-02-18 13:45:32 +00:00
|
|
|
ma := &mutationArgs{target: p.Target}
|
|
|
|
ForeachArg(c, ma.collectArg)
|
|
|
|
if len(ma.args) == 0 {
|
2017-01-20 22:55:25 +00:00
|
|
|
retry = true
|
2018-01-24 18:28:36 +00:00
|
|
|
continue outer
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2018-02-18 13:45:32 +00:00
|
|
|
idx := r.Intn(len(ma.args))
|
|
|
|
arg, ctx := ma.args[idx], ma.ctxes[idx]
|
2018-02-18 12:49:48 +00:00
|
|
|
calls, ok := p.Target.mutateArg(r, s, arg, ctx, &updateSizes)
|
2018-01-24 18:28:36 +00:00
|
|
|
if !ok {
|
|
|
|
retryArg = true
|
|
|
|
continue
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
p.insertBefore(c, calls)
|
2017-11-27 07:59:37 +00:00
|
|
|
if updateSizes {
|
|
|
|
p.Target.assignSizesCall(c)
|
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
p.Target.SanitizeCall(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 12:49:48 +00:00
|
|
|
func (target *Target) mutateArg(r *randGen, s *state, arg Arg, ctx ArgCtx, updateSizes *bool) (calls []*Call, ok bool) {
|
2018-01-24 18:28:36 +00:00
|
|
|
var baseSize uint64
|
2018-02-18 12:49:48 +00:00
|
|
|
if ctx.Base != nil {
|
|
|
|
baseSize = ctx.Base.Res.Size()
|
2015-12-23 12:47:45 +00:00
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
switch t := arg.Type().(type) {
|
|
|
|
case *IntType, *FlagsType:
|
|
|
|
a := arg.(*ConstArg)
|
|
|
|
if r.bin() {
|
|
|
|
var newArg Arg
|
|
|
|
newArg, calls = r.generateArg(s, arg.Type())
|
|
|
|
replaceArg(arg, newArg)
|
|
|
|
} else {
|
|
|
|
switch {
|
|
|
|
case r.nOutOf(1, 3):
|
|
|
|
a.Val += uint64(r.Intn(4)) + 1
|
|
|
|
case r.nOutOf(1, 2):
|
|
|
|
a.Val -= uint64(r.Intn(4)) + 1
|
|
|
|
default:
|
|
|
|
a.Val ^= 1 << uint64(r.Intn(64))
|
2017-08-08 14:50:22 +00:00
|
|
|
}
|
2015-12-23 12:47:45 +00:00
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
case *LenType:
|
2018-02-18 12:49:48 +00:00
|
|
|
if !r.mutateSize(arg.(*ConstArg), *ctx.Parent) {
|
2018-01-24 18:28:36 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
*updateSizes = false
|
|
|
|
case *ResourceType, *VmaType, *ProcType:
|
|
|
|
var newArg Arg
|
|
|
|
newArg, calls = r.generateArg(s, arg.Type())
|
|
|
|
replaceArg(arg, newArg)
|
|
|
|
case *BufferType:
|
|
|
|
a := arg.(*DataArg)
|
|
|
|
switch t.Kind {
|
|
|
|
case BufferBlobRand, BufferBlobRange:
|
|
|
|
data := append([]byte{}, a.Data()...)
|
|
|
|
minLen, maxLen := uint64(0), maxBlobLen
|
|
|
|
if t.Kind == BufferBlobRange {
|
|
|
|
minLen, maxLen = t.RangeBegin, t.RangeEnd
|
|
|
|
}
|
|
|
|
a.data = mutateData(r, data, minLen, maxLen)
|
|
|
|
case BufferString:
|
|
|
|
data := append([]byte{}, a.Data()...)
|
|
|
|
if r.bin() {
|
|
|
|
minLen, maxLen := uint64(0), maxBlobLen
|
|
|
|
if t.TypeSize != 0 {
|
|
|
|
minLen, maxLen = t.TypeSize, t.TypeSize
|
2015-12-23 12:47:45 +00:00
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
a.data = mutateData(r, data, minLen, maxLen)
|
|
|
|
} else {
|
|
|
|
a.data = r.randString(s, t)
|
|
|
|
}
|
|
|
|
case BufferFilename:
|
2018-03-02 15:14:57 +00:00
|
|
|
a.data = []byte(r.filename(s, t))
|
2018-01-24 18:28:36 +00:00
|
|
|
case BufferText:
|
|
|
|
data := append([]byte{}, a.Data()...)
|
|
|
|
a.data = r.mutateText(t.Text, data)
|
|
|
|
default:
|
|
|
|
panic("unknown buffer kind")
|
|
|
|
}
|
|
|
|
case *ArrayType:
|
|
|
|
a := arg.(*GroupArg)
|
|
|
|
count := uint64(0)
|
|
|
|
switch t.Kind {
|
|
|
|
case ArrayRandLen:
|
|
|
|
for count == uint64(len(a.Inner)) {
|
|
|
|
count = r.randArrayLen()
|
|
|
|
}
|
|
|
|
case ArrayRangeLen:
|
|
|
|
if t.RangeBegin == t.RangeEnd {
|
|
|
|
panic("trying to mutate fixed length array")
|
|
|
|
}
|
|
|
|
for count == uint64(len(a.Inner)) {
|
|
|
|
count = r.randRange(t.RangeBegin, t.RangeEnd)
|
2015-12-23 12:47:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
if count > uint64(len(a.Inner)) {
|
|
|
|
for count > uint64(len(a.Inner)) {
|
|
|
|
newArg, newCalls := r.generateArg(s, t.Type)
|
|
|
|
a.Inner = append(a.Inner, newArg)
|
|
|
|
calls = append(calls, newCalls...)
|
|
|
|
for _, c := range newCalls {
|
|
|
|
s.analyze(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if count < uint64(len(a.Inner)) {
|
|
|
|
for _, arg := range a.Inner[count:] {
|
|
|
|
removeArg(arg)
|
|
|
|
}
|
|
|
|
a.Inner = a.Inner[:count]
|
2015-12-23 12:47:45 +00:00
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
// TODO: swap elements of the array
|
|
|
|
case *PtrType:
|
2018-02-19 18:35:04 +00:00
|
|
|
a := arg.(*PointerArg)
|
|
|
|
newArg := r.allocAddr(s, t, a.Res.Size(), a.Res)
|
2018-01-24 18:28:36 +00:00
|
|
|
replaceArg(arg, newArg)
|
|
|
|
case *StructType:
|
|
|
|
gen := target.SpecialTypes[t.Name()]
|
|
|
|
if gen == nil {
|
|
|
|
panic("bad arg returned by mutationArgs: StructType")
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
var newArg Arg
|
|
|
|
newArg, calls = gen(&Gen{r, s}, t, arg)
|
|
|
|
for i, f := range newArg.(*GroupArg).Inner {
|
|
|
|
replaceArg(arg.(*GroupArg).Inner[i], f)
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
case *UnionType:
|
|
|
|
if gen := target.SpecialTypes[t.Name()]; gen != nil {
|
|
|
|
var newArg Arg
|
|
|
|
newArg, calls = gen(&Gen{r, s}, t, arg)
|
|
|
|
replaceArg(arg, newArg)
|
|
|
|
} else {
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*UnionArg)
|
2018-01-24 18:28:36 +00:00
|
|
|
current := -1
|
|
|
|
for i, option := range t.Fields {
|
|
|
|
if a.Option.Type().FieldName() == option.FieldName() {
|
|
|
|
current = i
|
2016-10-21 16:24:33 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
if current == -1 {
|
|
|
|
panic("can't find current option in union")
|
2016-10-21 16:24:33 +00:00
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
newIdx := r.Intn(len(t.Fields) - 1)
|
|
|
|
if newIdx >= current {
|
|
|
|
newIdx++
|
|
|
|
}
|
|
|
|
optType := t.Fields[newIdx]
|
|
|
|
removeArg(a.Option)
|
|
|
|
var newOpt Arg
|
|
|
|
newOpt, calls = r.generateArg(s, optType)
|
|
|
|
replaceArg(arg, MakeUnionArg(t, newOpt))
|
2016-10-21 16:24:33 +00:00
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
case *CsumType:
|
|
|
|
panic("bad arg returned by mutationArgs: CsumType")
|
|
|
|
case *ConstType:
|
|
|
|
panic("bad arg returned by mutationArgs: ConstType")
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("bad arg returned by mutationArgs: %#v, type=%#v", arg, arg.Type()))
|
2016-10-21 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 18:28:36 +00:00
|
|
|
// Update base pointer if size has increased.
|
2018-02-26 12:33:11 +00:00
|
|
|
if base := ctx.Base; base != nil && baseSize < base.Res.Size() {
|
|
|
|
newArg := r.allocAddr(s, base.Type(), base.Res.Size(), base.Res)
|
|
|
|
*base = *newArg
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
for _, c := range calls {
|
|
|
|
target.SanitizeCall(c)
|
|
|
|
}
|
|
|
|
return calls, true
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 13:45:32 +00:00
|
|
|
type mutationArgs struct {
|
|
|
|
target *Target
|
|
|
|
args []Arg
|
|
|
|
ctxes []ArgCtx
|
|
|
|
ignoreSpecial bool
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 13:45:32 +00:00
|
|
|
func (ma *mutationArgs) collectArg(arg Arg, ctx *ArgCtx) {
|
|
|
|
ignoreSpecial := ma.ignoreSpecial
|
|
|
|
ma.ignoreSpecial = false
|
2018-01-24 18:28:36 +00:00
|
|
|
switch typ := arg.Type().(type) {
|
|
|
|
case *StructType:
|
2018-02-18 13:45:32 +00:00
|
|
|
if ma.target.SpecialTypes[typ.Name()] == nil || ignoreSpecial {
|
|
|
|
return // For structs only individual fields are updated.
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
|
|
|
// These special structs are mutated as a whole.
|
2018-02-18 13:45:32 +00:00
|
|
|
ctx.Stop = true
|
2018-01-24 18:28:36 +00:00
|
|
|
case *UnionType:
|
2018-02-18 13:45:32 +00:00
|
|
|
if ma.target.SpecialTypes[typ.Name()] == nil && len(typ.Fields) == 1 || ignoreSpecial {
|
|
|
|
return
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
2018-02-18 13:45:32 +00:00
|
|
|
ctx.Stop = true
|
2018-01-24 18:28:36 +00:00
|
|
|
case *ArrayType:
|
|
|
|
// Don't mutate fixed-size arrays.
|
|
|
|
if typ.Kind == ArrayRangeLen && typ.RangeBegin == typ.RangeEnd {
|
2018-02-18 13:45:32 +00:00
|
|
|
return
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
|
|
|
case *CsumType:
|
2018-02-18 13:45:32 +00:00
|
|
|
return // Checksum is updated when the checksummed data changes.
|
2018-01-24 18:28:36 +00:00
|
|
|
case *ConstType:
|
2018-02-18 13:45:32 +00:00
|
|
|
return // Well, this is const.
|
2018-01-24 18:28:36 +00:00
|
|
|
case *BufferType:
|
|
|
|
if typ.Kind == BufferString && len(typ.Values) == 1 {
|
2018-02-18 13:45:32 +00:00
|
|
|
return // string const
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
2018-02-19 18:35:04 +00:00
|
|
|
case *PtrType:
|
|
|
|
if arg.(*PointerArg).IsNull() {
|
|
|
|
// TODO: we ought to mutate this, but we don't have code for this yet.
|
|
|
|
return
|
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
|
|
|
typ := arg.Type()
|
2018-02-18 12:49:48 +00:00
|
|
|
if typ == nil || typ.Dir() == DirOut || !typ.Varlen() && typ.Size() == 0 {
|
2018-02-18 13:45:32 +00:00
|
|
|
return
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
2018-02-18 13:45:32 +00:00
|
|
|
ma.args = append(ma.args, arg)
|
|
|
|
ma.ctxes = append(ma.ctxes, *ctx)
|
2016-09-13 10:43:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
2018-02-24 13:33:36 +00:00
|
|
|
// TODO(dvyukov): duplicate part of data.
|
|
|
|
switch r.Intn(7) {
|
2017-01-20 22:55:25 +00:00
|
|
|
case 0:
|
|
|
|
// 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)
|
2018-02-24 13:33:36 +00:00
|
|
|
case 1:
|
|
|
|
// Insert random bytes.
|
|
|
|
if len(data) == 0 || uint64(len(data)) >= maxLen {
|
2017-01-20 22:55:25 +00:00
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
2018-02-24 13:33:36 +00:00
|
|
|
n := r.Intn(16) + 1
|
|
|
|
if r := int(maxLen) - len(data); n > r {
|
|
|
|
n = r
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2018-02-24 13:33:36 +00:00
|
|
|
pos := r.Intn(len(data))
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
data = append(data, 0)
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2018-02-24 13:33:36 +00:00
|
|
|
copy(data[pos+n:], data[pos:])
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
data[pos+i] = byte(r.Int31())
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
|
|
|
if r.bin() {
|
2018-02-24 13:33:36 +00:00
|
|
|
data = data[:len(data)-n] // preserve original length
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2018-02-24 13:33:36 +00:00
|
|
|
case 2:
|
|
|
|
// Remove bytes.
|
|
|
|
if uint64(len(data)) <= minLen {
|
2017-01-20 22:55:25 +00:00
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
2018-02-24 13:33:36 +00:00
|
|
|
n := r.Intn(16) + 1
|
|
|
|
if n > len(data) {
|
|
|
|
n = len(data)
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2018-02-24 13:33:36 +00:00
|
|
|
pos := 0
|
|
|
|
if n < len(data) {
|
|
|
|
pos = r.Intn(len(data) - n)
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2018-02-24 13:33:36 +00:00
|
|
|
copy(data[pos:], data[pos+n:])
|
|
|
|
data = data[:len(data)-n]
|
2017-01-20 22:55:25 +00:00
|
|
|
if r.bin() {
|
2018-02-24 13:33:36 +00:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
data = append(data, 0) // preserve original length
|
|
|
|
}
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2018-02-24 13:33:36 +00:00
|
|
|
case 3:
|
2017-12-08 09:22:56 +00:00
|
|
|
// 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)))
|
|
|
|
}
|
2018-02-24 13:33:36 +00:00
|
|
|
case 4:
|
|
|
|
// Replace int8/int16/int32/int64 with a random value.
|
|
|
|
switch r.Intn(4) {
|
|
|
|
case 0: // int8
|
|
|
|
if len(data) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
data[r.Intn(len(data))] = byte(r.rand(1 << 8))
|
|
|
|
case 1: // int16
|
|
|
|
if len(data) < 2 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 1)
|
|
|
|
p := (*uint16)(unsafe.Pointer(&data[i]))
|
|
|
|
*p = uint16(r.rand(1 << 16))
|
|
|
|
case 2: // int32
|
|
|
|
if len(data) < 4 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 3)
|
|
|
|
p := (*uint32)(unsafe.Pointer(&data[i]))
|
2018-03-05 11:08:00 +00:00
|
|
|
*p = r.Uint32()
|
2018-02-24 13:33:36 +00:00
|
|
|
case 3: // int64
|
|
|
|
if len(data) < 8 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 7)
|
|
|
|
p := (*uint64)(unsafe.Pointer(&data[i]))
|
|
|
|
*p = r.Uint64()
|
|
|
|
}
|
|
|
|
case 5:
|
|
|
|
// Add/subtract from an int8/int16/int32/int64.
|
|
|
|
switch r.Intn(4) {
|
|
|
|
case 0: // int8
|
|
|
|
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 1: // int16
|
|
|
|
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.oneOf(10) {
|
|
|
|
*p = swap16(swap16(*p) + delta)
|
|
|
|
} else {
|
|
|
|
*p += delta
|
|
|
|
}
|
|
|
|
case 2: // int32
|
|
|
|
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.oneOf(10) {
|
|
|
|
*p = swap32(swap32(*p) + delta)
|
|
|
|
} else {
|
|
|
|
*p += delta
|
|
|
|
}
|
|
|
|
case 3: // int64
|
|
|
|
if len(data) < 8 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 7)
|
|
|
|
p := (*uint64)(unsafe.Pointer(&data[i]))
|
|
|
|
delta := r.rand(2*maxInc+1) - maxInc
|
|
|
|
if delta == 0 {
|
|
|
|
delta = 1
|
|
|
|
}
|
|
|
|
if r.oneOf(10) {
|
|
|
|
*p = swap64(swap64(*p) + delta)
|
|
|
|
} else {
|
|
|
|
*p += delta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 6:
|
|
|
|
// Set int8/int16/int32/int64 to an interesting value.
|
|
|
|
switch r.Intn(4) {
|
|
|
|
case 0: // int8
|
|
|
|
if len(data) == 0 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
data[r.Intn(len(data))] = byte(r.randInt())
|
|
|
|
case 1: // int16
|
|
|
|
if len(data) < 2 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 1)
|
|
|
|
value := uint16(r.randInt())
|
|
|
|
if r.oneOf(10) {
|
|
|
|
value = swap16(value)
|
|
|
|
}
|
|
|
|
*(*uint16)(unsafe.Pointer(&data[i])) = value
|
|
|
|
case 2: // int32
|
|
|
|
if len(data) < 4 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 3)
|
|
|
|
value := uint32(r.randInt())
|
|
|
|
if r.oneOf(10) {
|
|
|
|
value = swap32(value)
|
|
|
|
}
|
|
|
|
*(*uint32)(unsafe.Pointer(&data[i])) = value
|
|
|
|
case 3: // int64
|
|
|
|
if len(data) < 8 {
|
|
|
|
retry = true
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - 7)
|
|
|
|
value := r.randInt()
|
|
|
|
if r.oneOf(10) {
|
|
|
|
value = swap64(value)
|
|
|
|
}
|
|
|
|
*(*uint64)(unsafe.Pointer(&data[i])) = value
|
|
|
|
}
|
2017-01-20 22:55:25 +00:00
|
|
|
default:
|
|
|
|
panic("bad")
|
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
2018-01-24 18:28:36 +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
|
|
|
|
}
|
2018-04-01 13:28:01 +00:00
|
|
|
|
|
|
|
func swap(v, size uint64) uint64 {
|
|
|
|
switch size {
|
|
|
|
case 64:
|
|
|
|
return swap64(v)
|
|
|
|
case 32:
|
|
|
|
return uint64(swap32(uint32(v)))
|
|
|
|
case 16:
|
|
|
|
return uint64(swap16(uint16(v)))
|
|
|
|
case 8:
|
|
|
|
return v
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("swap: bad size %v", size))
|
|
|
|
}
|
|
|
|
}
|