2016-08-08 13:32:48 +00:00
|
|
|
// Copyright 2015/2016 syzkaller project authors. All rights reserved.
|
2015-10-12 08:16:57 +00:00
|
|
|
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
|
|
|
|
|
2017-09-05 11:31:14 +00:00
|
|
|
package prog
|
2015-10-12 08:16:57 +00:00
|
|
|
|
2016-01-28 07:20:09 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2017-09-05 08:38:22 +00:00
|
|
|
type Syscall struct {
|
2015-10-12 08:16:57 +00:00
|
|
|
ID int
|
2017-08-28 13:59:22 +00:00
|
|
|
NR uint64 // kernel syscall number
|
2015-10-12 08:16:57 +00:00
|
|
|
Name string
|
|
|
|
CallName string
|
|
|
|
Args []Type
|
|
|
|
Ret Type
|
|
|
|
}
|
|
|
|
|
2016-10-19 12:41:46 +00:00
|
|
|
type Dir int
|
|
|
|
|
|
|
|
const (
|
|
|
|
DirIn Dir = iota
|
|
|
|
DirOut
|
|
|
|
DirInOut
|
|
|
|
)
|
|
|
|
|
2018-02-24 13:33:36 +00:00
|
|
|
func (dir Dir) String() string {
|
|
|
|
switch dir {
|
|
|
|
case DirIn:
|
|
|
|
return "in"
|
|
|
|
case DirOut:
|
|
|
|
return "out"
|
|
|
|
case DirInOut:
|
|
|
|
return "inout"
|
|
|
|
default:
|
|
|
|
panic("unknown dir")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
type Type interface {
|
2018-02-24 13:33:36 +00:00
|
|
|
String() string
|
2015-10-12 08:16:57 +00:00
|
|
|
Name() string
|
2017-01-23 14:02:47 +00:00
|
|
|
FieldName() string
|
2016-10-19 12:41:46 +00:00
|
|
|
Dir() Dir
|
2015-10-12 08:16:57 +00:00
|
|
|
Optional() bool
|
2017-08-19 07:46:43 +00:00
|
|
|
Default() uint64
|
2017-01-19 13:42:29 +00:00
|
|
|
Varlen() bool
|
2017-08-19 07:46:43 +00:00
|
|
|
Size() uint64
|
|
|
|
BitfieldOffset() uint64
|
|
|
|
BitfieldLength() uint64
|
2017-09-04 18:51:56 +00:00
|
|
|
BitfieldMiddle() bool // returns true for all but last bitfield in a group
|
2018-05-04 16:03:46 +00:00
|
|
|
|
|
|
|
generate(r *randGen, s *state) (arg Arg, calls []*Call)
|
2018-05-07 14:24:03 +00:00
|
|
|
mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool)
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 11:58:10 +00:00
|
|
|
func IsPad(t Type) bool {
|
2016-10-19 14:20:37 +00:00
|
|
|
if ct, ok := t.(*ConstType); ok && ct.IsPad {
|
2015-12-28 11:58:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
type TypeCommon struct {
|
|
|
|
TypeName string
|
2017-01-23 14:02:47 +00:00
|
|
|
FldName string // for struct fields and named args
|
2017-09-04 17:53:05 +00:00
|
|
|
TypeSize uint64 // static size of the type, or 0 for variable size types
|
2016-10-19 12:41:46 +00:00
|
|
|
ArgDir Dir
|
2015-10-12 08:16:57 +00:00
|
|
|
IsOptional bool
|
2018-01-12 09:45:32 +00:00
|
|
|
IsVarlen bool
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 14:20:37 +00:00
|
|
|
func (t *TypeCommon) Name() string {
|
2015-10-12 08:16:57 +00:00
|
|
|
return t.TypeName
|
|
|
|
}
|
|
|
|
|
2017-01-23 14:02:47 +00:00
|
|
|
func (t *TypeCommon) FieldName() string {
|
|
|
|
return t.FldName
|
|
|
|
}
|
|
|
|
|
2016-10-19 14:20:37 +00:00
|
|
|
func (t *TypeCommon) Optional() bool {
|
2015-10-12 08:16:57 +00:00
|
|
|
return t.IsOptional
|
|
|
|
}
|
|
|
|
|
2017-08-19 07:46:43 +00:00
|
|
|
func (t *TypeCommon) Default() uint64 {
|
2015-10-12 08:16:57 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-09-04 17:53:05 +00:00
|
|
|
func (t *TypeCommon) Size() uint64 {
|
2018-01-12 09:45:32 +00:00
|
|
|
if t.IsVarlen {
|
2017-09-04 17:53:05 +00:00
|
|
|
panic(fmt.Sprintf("static type size is not known: %#v", t))
|
|
|
|
}
|
|
|
|
return t.TypeSize
|
|
|
|
}
|
|
|
|
|
2017-01-19 13:42:29 +00:00
|
|
|
func (t *TypeCommon) Varlen() bool {
|
2018-01-12 09:45:32 +00:00
|
|
|
return t.IsVarlen
|
2017-01-19 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 07:46:43 +00:00
|
|
|
func (t *TypeCommon) BitfieldOffset() uint64 {
|
2017-01-10 15:45:52 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-08-19 07:46:43 +00:00
|
|
|
func (t *TypeCommon) BitfieldLength() uint64 {
|
2017-01-10 15:45:52 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-09-04 18:51:56 +00:00
|
|
|
func (t *TypeCommon) BitfieldMiddle() bool {
|
2017-01-10 15:45:52 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-10-19 12:41:46 +00:00
|
|
|
func (t TypeCommon) Dir() Dir {
|
|
|
|
return t.ArgDir
|
|
|
|
}
|
|
|
|
|
2016-08-27 16:27:50 +00:00
|
|
|
type ResourceDesc struct {
|
|
|
|
Name string
|
|
|
|
Type Type
|
|
|
|
Kind []string
|
2017-08-19 07:46:43 +00:00
|
|
|
Values []uint64
|
2015-12-27 11:20:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
type ResourceType struct {
|
|
|
|
TypeCommon
|
2016-08-27 16:27:50 +00:00
|
|
|
Desc *ResourceDesc
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 13:33:36 +00:00
|
|
|
func (t *ResourceType) String() string {
|
|
|
|
return t.Name()
|
|
|
|
}
|
|
|
|
|
2017-08-19 07:46:43 +00:00
|
|
|
func (t *ResourceType) Default() uint64 {
|
2016-08-27 16:27:50 +00:00
|
|
|
return t.Desc.Values[0]
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 07:46:43 +00:00
|
|
|
func (t *ResourceType) SpecialValues() []uint64 {
|
2016-08-27 16:27:50 +00:00
|
|
|
return t.Desc.Values
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 15:45:52 +00:00
|
|
|
type IntTypeCommon struct {
|
|
|
|
TypeCommon
|
2017-08-19 07:46:43 +00:00
|
|
|
BitfieldOff uint64
|
|
|
|
BitfieldLen uint64
|
2017-09-04 17:52:56 +00:00
|
|
|
BigEndian bool
|
2017-09-04 18:51:56 +00:00
|
|
|
BitfieldMdl bool
|
2017-01-10 15:45:52 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 13:33:36 +00:00
|
|
|
func (t *IntTypeCommon) String() string {
|
|
|
|
return t.Name()
|
|
|
|
}
|
|
|
|
|
2017-08-19 07:46:43 +00:00
|
|
|
func (t *IntTypeCommon) BitfieldOffset() uint64 {
|
2017-01-10 15:45:52 +00:00
|
|
|
return t.BitfieldOff
|
|
|
|
}
|
|
|
|
|
2017-08-19 07:46:43 +00:00
|
|
|
func (t *IntTypeCommon) BitfieldLength() uint64 {
|
2017-01-10 15:45:52 +00:00
|
|
|
return t.BitfieldLen
|
|
|
|
}
|
|
|
|
|
2017-09-04 18:51:56 +00:00
|
|
|
func (t *IntTypeCommon) BitfieldMiddle() bool {
|
|
|
|
return t.BitfieldMdl
|
2017-01-10 15:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConstType struct {
|
|
|
|
IntTypeCommon
|
2017-08-19 07:46:43 +00:00
|
|
|
Val uint64
|
2017-01-10 15:45:52 +00:00
|
|
|
IsPad bool
|
|
|
|
}
|
|
|
|
|
2018-02-24 13:33:36 +00:00
|
|
|
func (t *ConstType) String() string {
|
|
|
|
if t.IsPad {
|
|
|
|
return fmt.Sprintf("pad[%v]", t.Size())
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("const[%v, %v]", t.Val, t.IntTypeCommon.String())
|
|
|
|
}
|
|
|
|
|
2017-01-10 15:45:52 +00:00
|
|
|
type IntKind int
|
|
|
|
|
|
|
|
const (
|
2017-09-04 18:14:59 +00:00
|
|
|
IntPlain IntKind = iota
|
|
|
|
IntFileoff // offset within a file
|
2017-01-10 15:45:52 +00:00
|
|
|
IntRange
|
|
|
|
)
|
|
|
|
|
|
|
|
type IntType struct {
|
|
|
|
IntTypeCommon
|
|
|
|
Kind IntKind
|
2017-08-19 07:46:43 +00:00
|
|
|
RangeBegin uint64
|
|
|
|
RangeEnd uint64
|
2017-01-10 15:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FlagsType struct {
|
|
|
|
IntTypeCommon
|
2017-08-19 07:46:43 +00:00
|
|
|
Vals []uint64
|
2017-01-10 15:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LenType struct {
|
|
|
|
IntTypeCommon
|
2017-12-31 13:58:00 +00:00
|
|
|
BitSize uint64 // want size in multiple of bits instead of array size
|
|
|
|
Buf string
|
2017-01-10 15:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProcType struct {
|
|
|
|
IntTypeCommon
|
2017-08-19 07:46:43 +00:00
|
|
|
ValuesStart uint64
|
2017-01-10 15:45:52 +00:00
|
|
|
ValuesPerProc uint64
|
|
|
|
}
|
|
|
|
|
2018-01-01 10:23:45 +00:00
|
|
|
func (t *ProcType) Default() uint64 {
|
|
|
|
// Special value denoting 0 for all procs.
|
|
|
|
return 0xffffffffffffffff
|
|
|
|
}
|
|
|
|
|
2017-01-25 15:18:05 +00:00
|
|
|
type CsumKind int
|
|
|
|
|
|
|
|
const (
|
2017-02-06 18:27:17 +00:00
|
|
|
CsumInet CsumKind = iota
|
2017-02-08 14:49:03 +00:00
|
|
|
CsumPseudo
|
2017-01-25 15:18:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CsumType struct {
|
|
|
|
IntTypeCommon
|
2017-02-08 14:49:03 +00:00
|
|
|
Kind CsumKind
|
|
|
|
Buf string
|
|
|
|
Protocol uint64 // for CsumPseudo
|
2017-01-25 15:18:05 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 13:33:36 +00:00
|
|
|
func (t *CsumType) String() string {
|
|
|
|
return "csum"
|
|
|
|
}
|
|
|
|
|
2017-01-10 15:45:52 +00:00
|
|
|
type VmaType struct {
|
|
|
|
TypeCommon
|
2017-08-19 07:46:43 +00:00
|
|
|
RangeBegin uint64 // in pages
|
|
|
|
RangeEnd uint64
|
2017-01-10 15:45:52 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 13:33:36 +00:00
|
|
|
func (t *VmaType) String() string {
|
|
|
|
return "vma"
|
|
|
|
}
|
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
type BufferKind int
|
|
|
|
|
|
|
|
const (
|
2016-09-21 14:52:55 +00:00
|
|
|
BufferBlobRand BufferKind = iota
|
|
|
|
BufferBlobRange
|
2015-10-12 08:16:57 +00:00
|
|
|
BufferString
|
2016-10-29 21:55:35 +00:00
|
|
|
BufferFilename
|
2017-01-08 16:20:32 +00:00
|
|
|
BufferText
|
|
|
|
)
|
|
|
|
|
|
|
|
type TextKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Text_x86_real TextKind = iota
|
|
|
|
Text_x86_16
|
|
|
|
Text_x86_32
|
|
|
|
Text_x86_64
|
2017-01-12 10:57:17 +00:00
|
|
|
Text_arm64
|
2015-10-12 08:16:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BufferType struct {
|
|
|
|
TypeCommon
|
2016-09-21 14:52:55 +00:00
|
|
|
Kind BufferKind
|
2018-04-02 12:21:45 +00:00
|
|
|
RangeBegin uint64 // for BufferBlobRange kind
|
2017-08-19 07:46:43 +00:00
|
|
|
RangeEnd uint64 // for BufferBlobRange kind
|
2017-01-08 16:20:32 +00:00
|
|
|
Text TextKind // for BufferText
|
2016-10-31 21:15:13 +00:00
|
|
|
SubKind string
|
|
|
|
Values []string // possible values for BufferString kind
|
2018-04-02 12:21:45 +00:00
|
|
|
NoZ bool // non-zero terminated BufferString/BufferFilename
|
2015-12-11 14:42:14 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 13:33:36 +00:00
|
|
|
func (t *BufferType) String() string {
|
|
|
|
return "buffer"
|
|
|
|
}
|
|
|
|
|
2016-09-15 11:45:06 +00:00
|
|
|
type ArrayKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ArrayRandLen ArrayKind = iota
|
|
|
|
ArrayRangeLen
|
|
|
|
)
|
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
type ArrayType struct {
|
|
|
|
TypeCommon
|
2016-09-15 11:45:06 +00:00
|
|
|
Type Type
|
|
|
|
Kind ArrayKind
|
2017-08-19 07:46:43 +00:00
|
|
|
RangeBegin uint64
|
|
|
|
RangeEnd uint64
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 13:33:36 +00:00
|
|
|
func (t *ArrayType) String() string {
|
|
|
|
return fmt.Sprintf("array[%v]", t.Type.String())
|
|
|
|
}
|
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
type PtrType struct {
|
|
|
|
TypeCommon
|
2017-09-04 17:53:05 +00:00
|
|
|
Type Type
|
2015-12-11 14:42:14 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 13:33:36 +00:00
|
|
|
func (t *PtrType) String() string {
|
|
|
|
return fmt.Sprintf("ptr[%v, %v]", t.Dir(), t.Type.String())
|
|
|
|
}
|
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
type StructType struct {
|
2017-09-04 17:53:05 +00:00
|
|
|
Key StructKey
|
|
|
|
FldName string
|
|
|
|
*StructDesc
|
2015-12-11 14:42:14 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 13:33:36 +00:00
|
|
|
func (t *StructType) String() string {
|
|
|
|
return t.Name()
|
|
|
|
}
|
|
|
|
|
2017-09-04 17:53:05 +00:00
|
|
|
func (t *StructType) FieldName() string {
|
|
|
|
return t.FldName
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 14:00:57 +00:00
|
|
|
type UnionType struct {
|
2017-09-04 17:53:05 +00:00
|
|
|
Key StructKey
|
|
|
|
FldName string
|
|
|
|
*StructDesc
|
2017-01-19 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 13:33:36 +00:00
|
|
|
func (t *UnionType) String() string {
|
|
|
|
return t.Name()
|
|
|
|
}
|
|
|
|
|
2017-09-04 17:53:05 +00:00
|
|
|
func (t *UnionType) FieldName() string {
|
|
|
|
return t.FldName
|
2015-12-29 14:00:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 17:53:05 +00:00
|
|
|
type StructDesc struct {
|
|
|
|
TypeCommon
|
|
|
|
Fields []Type
|
|
|
|
AlignAttr uint64
|
2015-12-29 14:00:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 17:53:05 +00:00
|
|
|
func (t *StructDesc) FieldName() string {
|
|
|
|
panic("must not be called")
|
|
|
|
}
|
2017-05-26 13:32:51 +00:00
|
|
|
|
2017-08-28 13:59:22 +00:00
|
|
|
type StructKey struct {
|
|
|
|
Name string
|
|
|
|
Dir Dir
|
2017-05-26 13:32:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 17:53:05 +00:00
|
|
|
type KeyedStruct struct {
|
|
|
|
Key StructKey
|
|
|
|
Desc *StructDesc
|
2017-05-26 13:32:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 17:16:32 +00:00
|
|
|
type ConstValue struct {
|
|
|
|
Name string
|
|
|
|
Value uint64
|
|
|
|
}
|
|
|
|
|
2017-09-05 08:38:22 +00:00
|
|
|
func ForeachType(meta *Syscall, f func(Type)) {
|
2017-09-04 17:53:05 +00:00
|
|
|
seen := make(map[*StructDesc]bool)
|
2016-10-19 12:41:46 +00:00
|
|
|
var rec func(t Type)
|
|
|
|
rec = func(t Type) {
|
|
|
|
f(t)
|
|
|
|
switch a := t.(type) {
|
|
|
|
case *PtrType:
|
|
|
|
rec(a.Type)
|
|
|
|
case *ArrayType:
|
|
|
|
rec(a.Type)
|
|
|
|
case *StructType:
|
2017-09-04 17:53:05 +00:00
|
|
|
if seen[a.StructDesc] {
|
2016-10-19 12:41:46 +00:00
|
|
|
return // prune recursion via pointers to structs/unions
|
|
|
|
}
|
2017-09-04 17:53:05 +00:00
|
|
|
seen[a.StructDesc] = true
|
2016-10-19 12:41:46 +00:00
|
|
|
for _, f := range a.Fields {
|
|
|
|
rec(f)
|
|
|
|
}
|
|
|
|
case *UnionType:
|
2017-09-04 17:53:05 +00:00
|
|
|
if seen[a.StructDesc] {
|
2016-10-19 12:41:46 +00:00
|
|
|
return // prune recursion via pointers to structs/unions
|
|
|
|
}
|
2017-09-04 17:53:05 +00:00
|
|
|
seen[a.StructDesc] = true
|
|
|
|
for _, opt := range a.Fields {
|
2016-10-19 12:41:46 +00:00
|
|
|
rec(opt)
|
|
|
|
}
|
2016-10-31 21:15:13 +00:00
|
|
|
case *ResourceType, *BufferType, *VmaType, *LenType,
|
2017-01-25 15:18:05 +00:00
|
|
|
*FlagsType, *ConstType, *IntType, *ProcType, *CsumType:
|
2016-10-19 12:41:46 +00:00
|
|
|
default:
|
|
|
|
panic("unknown type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, t := range meta.Args {
|
|
|
|
rec(t)
|
|
|
|
}
|
|
|
|
if meta.Ret != nil {
|
|
|
|
rec(meta.Ret)
|
|
|
|
}
|
|
|
|
}
|