2017-01-24 12:58:59 +00:00
|
|
|
// Copyright 2017 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"
|
|
|
|
)
|
|
|
|
|
2019-05-14 13:45:36 +00:00
|
|
|
const (
|
|
|
|
// Special reference to the outer struct used in len targets.
|
|
|
|
ParentRef = "parent"
|
2019-05-14 14:21:19 +00:00
|
|
|
// Special reference directly to syscall arguments used in len targets.
|
|
|
|
SyscallRef = "syscall"
|
2019-05-14 13:45:36 +00:00
|
|
|
)
|
|
|
|
|
2019-05-14 14:21:19 +00:00
|
|
|
func (target *Target) assignSizes(args []Arg, parentsMap map[Arg]Arg, syscallArgs []Arg, autos map[Arg]bool) {
|
2017-01-24 12:58:59 +00:00
|
|
|
for _, arg := range args {
|
2017-07-11 14:49:08 +00:00
|
|
|
if arg = InnerArg(arg); arg == nil {
|
2017-01-24 12:58:59 +00:00
|
|
|
continue // Pointer to optional len field, no need to fill in value.
|
|
|
|
}
|
2018-12-10 14:49:04 +00:00
|
|
|
typ, ok := arg.Type().(*LenType)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if autos != nil {
|
|
|
|
if !autos[arg] {
|
2017-01-24 12:58:59 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-12-10 14:49:04 +00:00
|
|
|
delete(autos, arg)
|
|
|
|
}
|
|
|
|
a := arg.(*ConstArg)
|
2019-05-14 14:21:19 +00:00
|
|
|
if typ.Path[0] == SyscallRef {
|
|
|
|
target.assignSize(a, nil, typ.Path[1:], syscallArgs, parentsMap)
|
|
|
|
} else {
|
|
|
|
target.assignSize(a, a, typ.Path, args, parentsMap)
|
|
|
|
}
|
2019-05-14 08:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 12:58:59 +00:00
|
|
|
|
2019-05-14 08:18:11 +00:00
|
|
|
func (target *Target) assignSize(dst *ConstArg, pos Arg, path []string, args []Arg, parentsMap map[Arg]Arg) {
|
|
|
|
elem := path[0]
|
|
|
|
path = path[1:]
|
2019-05-16 16:05:05 +00:00
|
|
|
var offset uint64
|
2019-05-14 08:18:11 +00:00
|
|
|
for _, buf := range args {
|
|
|
|
if elem != buf.Type().FieldName() {
|
2019-12-18 17:30:39 +00:00
|
|
|
offset += buf.Size()
|
2018-12-10 14:49:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-05-01 09:56:50 +00:00
|
|
|
if typ := buf.Type().Name(); typ == target.any.ptrPtr.Name() || typ == target.any.ptr64.Name() {
|
|
|
|
// If path points into squashed argument, we don't have the target argument.
|
|
|
|
// In such case we simply leave size argument as is. It can't happen during generation,
|
|
|
|
// only during mutation and mutation can set size to random values, so it should be fine.
|
|
|
|
return
|
|
|
|
}
|
2019-05-14 08:18:11 +00:00
|
|
|
buf = InnerArg(buf)
|
2019-05-14 18:58:33 +00:00
|
|
|
if buf == nil {
|
|
|
|
dst.Val = 0 // target is an optional pointer
|
|
|
|
return
|
|
|
|
}
|
2019-05-14 08:18:11 +00:00
|
|
|
if len(path) == 0 {
|
2019-05-16 16:05:05 +00:00
|
|
|
dst.Val = target.computeSize(buf, offset, dst.Type().(*LenType))
|
2019-05-14 08:18:11 +00:00
|
|
|
} else {
|
|
|
|
target.assignSize(dst, buf, path, buf.(*GroupArg).Inner, parentsMap)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-05-14 13:45:36 +00:00
|
|
|
if elem == ParentRef {
|
2019-05-14 08:18:11 +00:00
|
|
|
buf := parentsMap[pos]
|
|
|
|
if len(path) == 0 {
|
2019-05-16 16:05:05 +00:00
|
|
|
dst.Val = target.computeSize(buf, noOffset, dst.Type().(*LenType))
|
2019-05-14 08:18:11 +00:00
|
|
|
} else {
|
|
|
|
target.assignSize(dst, buf, path, buf.(*GroupArg).Inner, parentsMap)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for buf := parentsMap[pos]; buf != nil; buf = parentsMap[buf] {
|
2020-01-03 08:58:37 +00:00
|
|
|
if elem != buf.Type().TemplateName() {
|
2018-12-10 14:49:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-05-14 08:18:11 +00:00
|
|
|
if len(path) == 0 {
|
2019-05-16 16:05:05 +00:00
|
|
|
dst.Val = target.computeSize(buf, noOffset, dst.Type().(*LenType))
|
2019-05-14 08:18:11 +00:00
|
|
|
} else {
|
|
|
|
target.assignSize(dst, buf, path, buf.(*GroupArg).Inner, parentsMap)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var argNames []string
|
|
|
|
for _, arg := range args {
|
|
|
|
argNames = append(argNames, arg.Type().FieldName())
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("len field %q references non existent field %q, pos=%q/%q, argsMap: %+v",
|
|
|
|
dst.Type().FieldName(), elem, pos.Type().Name(), pos.Type().FieldName(), argNames))
|
|
|
|
}
|
2017-01-24 12:58:59 +00:00
|
|
|
|
2019-05-16 16:05:05 +00:00
|
|
|
const noOffset = ^uint64(0)
|
|
|
|
|
|
|
|
func (target *Target) computeSize(arg Arg, offset uint64, lenType *LenType) uint64 {
|
|
|
|
if lenType.Offset {
|
|
|
|
if offset == noOffset {
|
|
|
|
panic("offset of a non-field")
|
|
|
|
}
|
|
|
|
return offset * 8 / lenType.BitSize
|
2019-05-14 08:18:11 +00:00
|
|
|
}
|
|
|
|
bitSize := lenType.BitSize
|
|
|
|
if bitSize == 0 {
|
|
|
|
bitSize = 8
|
|
|
|
}
|
|
|
|
switch arg.Type().(type) {
|
|
|
|
case *VmaType:
|
|
|
|
a := arg.(*PointerArg)
|
|
|
|
return a.VmaSize * 8 / bitSize
|
|
|
|
case *ArrayType:
|
|
|
|
a := arg.(*GroupArg)
|
|
|
|
if lenType.BitSize != 0 {
|
|
|
|
return a.Size() * 8 / bitSize
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
2019-05-14 08:18:11 +00:00
|
|
|
return uint64(len(a.Inner))
|
|
|
|
default:
|
|
|
|
return arg.Size() * 8 / bitSize
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 14:49:04 +00:00
|
|
|
func (target *Target) assignSizesArray(args []Arg, autos map[Arg]bool) {
|
2017-07-11 14:49:08 +00:00
|
|
|
parentsMap := make(map[Arg]Arg)
|
2018-02-18 12:49:48 +00:00
|
|
|
for _, arg := range args {
|
|
|
|
ForeachSubArg(arg, func(arg Arg, _ *ArgCtx) {
|
|
|
|
if _, ok := arg.Type().(*StructType); ok {
|
|
|
|
for _, field := range arg.(*GroupArg).Inner {
|
|
|
|
parentsMap[InnerArg(field)] = arg
|
|
|
|
}
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
2018-02-18 12:49:48 +00:00
|
|
|
})
|
|
|
|
}
|
2019-05-14 14:21:19 +00:00
|
|
|
target.assignSizes(args, parentsMap, args, autos)
|
2018-02-18 12:49:48 +00:00
|
|
|
for _, arg := range args {
|
|
|
|
ForeachSubArg(arg, func(arg Arg, _ *ArgCtx) {
|
|
|
|
if _, ok := arg.Type().(*StructType); ok {
|
2019-05-14 14:21:19 +00:00
|
|
|
target.assignSizes(arg.(*GroupArg).Inner, parentsMap, args, autos)
|
2018-02-18 12:49:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:25:01 +00:00
|
|
|
func (target *Target) assignSizesCall(c *Call) {
|
2018-12-10 14:49:04 +00:00
|
|
|
target.assignSizesArray(c.Args, nil)
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
2017-11-27 07:59:37 +00:00
|
|
|
|
|
|
|
func (r *randGen) mutateSize(arg *ConstArg, parent []Arg) bool {
|
|
|
|
typ := arg.Type().(*LenType)
|
2017-12-31 13:58:00 +00:00
|
|
|
elemSize := typ.BitSize / 8
|
2017-11-27 07:59:37 +00:00
|
|
|
if elemSize == 0 {
|
|
|
|
elemSize = 1
|
2019-05-14 08:18:11 +00:00
|
|
|
// TODO(dvyukov): implement path support for size mutation.
|
2019-05-13 16:52:46 +00:00
|
|
|
if len(typ.Path) == 1 {
|
|
|
|
for _, field := range parent {
|
|
|
|
if typ.Path[0] != field.Type().FieldName() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if inner := InnerArg(field); inner != nil {
|
|
|
|
switch targetType := inner.Type().(type) {
|
|
|
|
case *VmaType:
|
2018-02-17 15:51:52 +00:00
|
|
|
return false
|
2019-05-13 16:52:46 +00:00
|
|
|
case *ArrayType:
|
|
|
|
if targetType.Type.Varlen() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
elemSize = targetType.Type.Size()
|
2018-02-17 15:51:52 +00:00
|
|
|
}
|
2017-11-27 07:59:37 +00:00
|
|
|
}
|
2019-05-13 16:52:46 +00:00
|
|
|
break
|
2017-11-27 07:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.oneOf(100) {
|
|
|
|
arg.Val = r.rand64()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if r.bin() {
|
|
|
|
// Small adjustment to trigger missed size checks.
|
|
|
|
if arg.Val != 0 && r.bin() {
|
2019-10-18 12:45:31 +00:00
|
|
|
arg.Val = r.randRangeInt(0, arg.Val-1, arg.Type().TypeBitSize(), 0)
|
2017-11-27 07:59:37 +00:00
|
|
|
} else {
|
2020-01-18 19:57:47 +00:00
|
|
|
arg.Val = r.randRangeInt(arg.Val+1, arg.Val+100, arg.Type().TypeBitSize(), 0)
|
2017-11-27 07:59:37 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Try to provoke int overflows.
|
|
|
|
max := ^uint64(0)
|
|
|
|
if r.oneOf(3) {
|
|
|
|
max = 1<<32 - 1
|
|
|
|
if r.oneOf(2) {
|
|
|
|
max = 1<<16 - 1
|
|
|
|
if r.oneOf(2) {
|
|
|
|
max = 1<<8 - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n := max / elemSize
|
|
|
|
delta := uint64(1000 - r.biasedRand(1000, 10))
|
|
|
|
if elemSize == 1 || r.oneOf(10) {
|
|
|
|
n -= delta
|
|
|
|
} else {
|
|
|
|
n += delta
|
|
|
|
}
|
|
|
|
arg.Val = n
|
|
|
|
return true
|
|
|
|
}
|