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"
|
|
|
|
)
|
|
|
|
|
2017-11-29 08:47:55 +00:00
|
|
|
func (target *Target) generateSize(arg Arg, lenType *LenType) uint64 {
|
2017-01-24 12:58:59 +00:00
|
|
|
if arg == nil {
|
|
|
|
// Arg is an optional pointer, set size to 0.
|
2017-11-29 08:47:55 +00:00
|
|
|
return 0
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 08:47:55 +00:00
|
|
|
byteSize := lenType.ByteSize
|
|
|
|
if byteSize == 0 {
|
|
|
|
byteSize = 1
|
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
switch arg.Type().(type) {
|
2017-09-05 08:46:34 +00:00
|
|
|
case *VmaType:
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*PointerArg)
|
2017-11-29 08:47:55 +00:00
|
|
|
return a.PagesNum * target.PageSize / byteSize
|
2017-09-05 08:46:34 +00:00
|
|
|
case *ArrayType:
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*GroupArg)
|
2017-01-24 12:58:59 +00:00
|
|
|
if lenType.ByteSize != 0 {
|
2017-11-29 08:47:55 +00:00
|
|
|
return a.Size() / byteSize
|
2017-01-24 12:58:59 +00:00
|
|
|
} else {
|
2017-11-29 08:47:55 +00:00
|
|
|
return uint64(len(a.Inner))
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
|
|
|
default:
|
2017-11-29 08:47:55 +00:00
|
|
|
return arg.Size() / byteSize
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:25:01 +00:00
|
|
|
func (target *Target) assignSizes(args []Arg, parentsMap map[Arg]Arg) {
|
2017-07-11 14:49:08 +00:00
|
|
|
// Create a map from field names to args.
|
|
|
|
argsMap := make(map[string]Arg)
|
2017-01-24 12:58:59 +00:00
|
|
|
for _, arg := range args {
|
2017-09-05 08:46:34 +00:00
|
|
|
if IsPad(arg.Type()) {
|
2017-01-24 12:58:59 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-07-11 14:49:08 +00:00
|
|
|
argsMap[arg.Type().FieldName()] = arg
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in size arguments.
|
|
|
|
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.
|
|
|
|
}
|
2017-09-05 08:46:34 +00:00
|
|
|
if typ, ok := arg.Type().(*LenType); ok {
|
2017-07-11 14:49:08 +00:00
|
|
|
a := arg.(*ConstArg)
|
|
|
|
|
2017-01-24 12:58:59 +00:00
|
|
|
buf, ok := argsMap[typ.Buf]
|
|
|
|
if ok {
|
2017-11-29 08:47:55 +00:00
|
|
|
a.Val = target.generateSize(InnerArg(buf), typ)
|
2017-01-24 12:58:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if typ.Buf == "parent" {
|
2017-07-11 14:49:08 +00:00
|
|
|
a.Val = parentsMap[arg].Size()
|
2017-01-24 12:58:59 +00:00
|
|
|
if typ.ByteSize != 0 {
|
2017-07-11 14:49:08 +00:00
|
|
|
a.Val /= typ.ByteSize
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sizeAssigned := false
|
|
|
|
for parent := parentsMap[arg]; parent != nil; parent = parentsMap[parent] {
|
2017-07-11 14:49:08 +00:00
|
|
|
if typ.Buf == parent.Type().Name() {
|
|
|
|
a.Val = parent.Size()
|
2017-01-24 12:58:59 +00:00
|
|
|
if typ.ByteSize != 0 {
|
2017-07-11 14:49:08 +00:00
|
|
|
a.Val /= typ.ByteSize
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
|
|
|
sizeAssigned = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sizeAssigned {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Sprintf("len field '%v' references non existent field '%v', argsMap: %+v",
|
|
|
|
typ.FieldName(), typ.Buf, argsMap))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:25:01 +00:00
|
|
|
func (target *Target) assignSizesArray(args []Arg) {
|
2017-07-11 14:49:08 +00:00
|
|
|
parentsMap := make(map[Arg]Arg)
|
|
|
|
foreachArgArray(&args, nil, func(arg, base Arg, _ *[]Arg) {
|
2017-09-05 08:46:34 +00:00
|
|
|
if _, ok := arg.Type().(*StructType); ok {
|
2017-07-11 14:49:08 +00:00
|
|
|
for _, field := range arg.(*GroupArg).Inner {
|
|
|
|
parentsMap[InnerArg(field)] = arg
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-09-14 17:25:01 +00:00
|
|
|
target.assignSizes(args, parentsMap)
|
2017-07-11 14:49:08 +00:00
|
|
|
foreachArgArray(&args, nil, func(arg, base Arg, _ *[]Arg) {
|
2017-09-05 08:46:34 +00:00
|
|
|
if _, ok := arg.Type().(*StructType); ok {
|
2017-09-14 17:25:01 +00:00
|
|
|
target.assignSizes(arg.(*GroupArg).Inner, parentsMap)
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:25:01 +00:00
|
|
|
func (target *Target) assignSizesCall(c *Call) {
|
|
|
|
target.assignSizesArray(c.Args)
|
2017-01-24 12:58:59 +00:00
|
|
|
}
|