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.
|
|
|
|
|
|
|
|
// This file does serialization of programs for executor binary.
|
|
|
|
// The format aims at simple parsing: binary and irreversible.
|
|
|
|
|
|
|
|
package prog
|
|
|
|
|
2015-12-23 12:38:31 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-12-28 11:58:10 +00:00
|
|
|
|
|
|
|
"github.com/google/syzkaller/sys"
|
2015-12-23 12:38:31 +00:00
|
|
|
)
|
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
const (
|
2015-12-23 12:38:31 +00:00
|
|
|
ExecInstrEOF = ^uintptr(iota)
|
|
|
|
ExecInstrCopyin
|
|
|
|
ExecInstrCopyout
|
2015-10-12 08:16:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-12-23 12:38:31 +00:00
|
|
|
ExecArgConst = uintptr(iota)
|
|
|
|
ExecArgResult
|
|
|
|
ExecArgData
|
2015-10-12 08:16:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-01-20 22:55:25 +00:00
|
|
|
ExecBufferSize = 2 << 20
|
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
ptrSize = 8
|
|
|
|
pageSize = 4 << 10
|
|
|
|
dataOffset = 512 << 20
|
|
|
|
)
|
|
|
|
|
2017-01-20 22:55:25 +00:00
|
|
|
// SerializeForExec serializes program p for execution by process pid into the provided buffer.
|
|
|
|
// If the provided buffer is too small for the program an error is returned.
|
|
|
|
func (p *Prog) SerializeForExec(buffer []byte, pid int) error {
|
2017-01-24 09:51:38 +00:00
|
|
|
if debug {
|
|
|
|
if err := p.validate(); err != nil {
|
|
|
|
panic(fmt.Errorf("serializing invalid program: %v", err))
|
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
var instrSeq uintptr
|
2017-01-20 22:55:25 +00:00
|
|
|
w := &execContext{
|
|
|
|
buf: buffer,
|
|
|
|
eof: false,
|
|
|
|
args: make(map[*Arg]argInfo),
|
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
for _, c := range p.Calls {
|
2017-01-25 15:18:05 +00:00
|
|
|
// Calculate checksums.
|
|
|
|
csumMap := calcChecksumsCall(c, pid)
|
2015-10-12 08:16:57 +00:00
|
|
|
// Calculate arg offsets within structs.
|
|
|
|
// Generate copyin instructions that fill in data into pointer arguments.
|
|
|
|
foreachArg(c, func(arg, _ *Arg, _ *[]*Arg) {
|
|
|
|
if arg.Kind == ArgPointer && arg.Res != nil {
|
2017-01-25 15:18:05 +00:00
|
|
|
foreachSubargOffset(arg.Res, func(arg1 *Arg, offset uintptr) {
|
2017-01-20 22:55:25 +00:00
|
|
|
if len(arg1.Uses) != 0 {
|
|
|
|
w.args[arg1] = argInfo{Offset: offset}
|
|
|
|
}
|
2017-01-18 15:14:28 +00:00
|
|
|
if !sys.IsPad(arg1.Type) &&
|
|
|
|
!(arg1.Kind == ArgData && len(arg1.Data) == 0) &&
|
|
|
|
arg1.Type.Dir() != sys.DirOut {
|
2015-12-23 18:58:15 +00:00
|
|
|
w.write(ExecInstrCopyin)
|
2017-01-18 15:14:28 +00:00
|
|
|
w.write(physicalAddr(arg) + offset)
|
2017-01-25 15:18:05 +00:00
|
|
|
w.writeArg(arg1, pid, csumMap)
|
2015-12-11 14:42:14 +00:00
|
|
|
instrSeq++
|
|
|
|
}
|
2017-01-25 15:18:05 +00:00
|
|
|
})
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
// Generate the call itself.
|
|
|
|
w.write(uintptr(c.Meta.ID))
|
|
|
|
w.write(uintptr(len(c.Args)))
|
|
|
|
for _, arg := range c.Args {
|
2017-01-25 15:18:05 +00:00
|
|
|
w.writeArg(arg, pid, csumMap)
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2017-01-20 22:55:25 +00:00
|
|
|
if len(c.Ret.Uses) != 0 {
|
|
|
|
w.args[c.Ret] = argInfo{Idx: instrSeq}
|
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
instrSeq++
|
|
|
|
// Generate copyout instructions that persist interesting return values.
|
|
|
|
foreachArg(c, func(arg, base *Arg, _ *[]*Arg) {
|
2015-12-23 18:58:15 +00:00
|
|
|
if len(arg.Uses) == 0 {
|
2015-10-12 08:16:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
switch arg.Kind {
|
|
|
|
case ArgReturn:
|
|
|
|
// Idx is already assigned above.
|
|
|
|
case ArgConst, ArgResult:
|
|
|
|
// Create a separate copyout instruction that has own Idx.
|
|
|
|
if base.Kind != ArgPointer {
|
|
|
|
panic("arg base is not a pointer")
|
|
|
|
}
|
|
|
|
info := w.args[arg]
|
|
|
|
info.Idx = instrSeq
|
|
|
|
instrSeq++
|
2017-01-20 22:55:25 +00:00
|
|
|
w.args[arg] = info
|
2015-12-23 12:38:31 +00:00
|
|
|
w.write(ExecInstrCopyout)
|
2015-10-12 08:16:57 +00:00
|
|
|
w.write(physicalAddr(base) + info.Offset)
|
2016-10-29 21:42:36 +00:00
|
|
|
w.write(arg.Size())
|
2015-10-12 08:16:57 +00:00
|
|
|
default:
|
|
|
|
panic("bad arg kind in copyout")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2015-12-23 12:38:31 +00:00
|
|
|
w.write(ExecInstrEOF)
|
2017-01-20 22:55:25 +00:00
|
|
|
if w.eof {
|
|
|
|
return fmt.Errorf("provided buffer is too small")
|
|
|
|
}
|
|
|
|
return nil
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func physicalAddr(arg *Arg) uintptr {
|
|
|
|
if arg.Kind != ArgPointer {
|
|
|
|
panic("physicalAddr: bad arg kind")
|
|
|
|
}
|
|
|
|
addr := arg.AddrPage*pageSize + dataOffset
|
|
|
|
if arg.AddrOffset >= 0 {
|
|
|
|
addr += uintptr(arg.AddrOffset)
|
|
|
|
} else {
|
|
|
|
addr += pageSize - uintptr(-arg.AddrOffset)
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
|
|
|
type execContext struct {
|
|
|
|
buf []byte
|
2017-01-20 22:55:25 +00:00
|
|
|
eof bool
|
|
|
|
args map[*Arg]argInfo
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type argInfo struct {
|
2017-01-18 15:14:28 +00:00
|
|
|
Offset uintptr // from base pointer
|
|
|
|
Idx uintptr // instruction index
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *execContext) write(v uintptr) {
|
2017-01-20 22:55:25 +00:00
|
|
|
if len(w.buf) < 8 {
|
|
|
|
w.eof = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.buf[0] = byte(v >> 0)
|
|
|
|
w.buf[1] = byte(v >> 8)
|
|
|
|
w.buf[2] = byte(v >> 16)
|
|
|
|
w.buf[3] = byte(v >> 24)
|
|
|
|
w.buf[4] = byte(v >> 32)
|
|
|
|
w.buf[5] = byte(v >> 40)
|
|
|
|
w.buf[6] = byte(v >> 48)
|
|
|
|
w.buf[7] = byte(v >> 56)
|
|
|
|
w.buf = w.buf[8:]
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 15:18:05 +00:00
|
|
|
func (w *execContext) writeArg(arg *Arg, pid int, csumMap map[*Arg]*Arg) {
|
2015-10-12 08:16:57 +00:00
|
|
|
switch arg.Kind {
|
|
|
|
case ArgConst:
|
2017-01-25 15:18:05 +00:00
|
|
|
if _, ok := arg.Type.(*sys.CsumType); ok {
|
|
|
|
if arg, ok = csumMap[arg]; !ok {
|
|
|
|
panic("csum arg is not in csum map")
|
|
|
|
}
|
|
|
|
}
|
2015-12-23 12:38:31 +00:00
|
|
|
w.write(ExecArgConst)
|
2016-10-29 21:42:36 +00:00
|
|
|
w.write(arg.Size())
|
2016-11-25 14:53:10 +00:00
|
|
|
w.write(arg.Value(pid))
|
2017-01-10 15:45:52 +00:00
|
|
|
w.write(arg.Type.BitfieldOffset())
|
|
|
|
w.write(arg.Type.BitfieldLength())
|
2015-10-12 08:16:57 +00:00
|
|
|
case ArgResult:
|
2015-12-23 12:38:31 +00:00
|
|
|
w.write(ExecArgResult)
|
2016-10-29 21:42:36 +00:00
|
|
|
w.write(arg.Size())
|
2015-10-12 08:16:57 +00:00
|
|
|
w.write(w.args[arg.Res].Idx)
|
|
|
|
w.write(arg.OpDiv)
|
|
|
|
w.write(arg.OpAdd)
|
|
|
|
case ArgPointer:
|
2015-12-23 12:38:31 +00:00
|
|
|
w.write(ExecArgConst)
|
2016-10-29 21:42:36 +00:00
|
|
|
w.write(arg.Size())
|
2015-10-12 08:16:57 +00:00
|
|
|
w.write(physicalAddr(arg))
|
2017-01-10 15:45:52 +00:00
|
|
|
w.write(0) // bit field offset
|
|
|
|
w.write(0) // bit field length
|
2015-10-12 08:16:57 +00:00
|
|
|
case ArgPageSize:
|
2015-12-23 12:38:31 +00:00
|
|
|
w.write(ExecArgConst)
|
2016-10-29 21:42:36 +00:00
|
|
|
w.write(arg.Size())
|
2015-10-12 08:16:57 +00:00
|
|
|
w.write(arg.AddrPage * pageSize)
|
2017-01-10 15:45:52 +00:00
|
|
|
w.write(0) // bit field offset
|
|
|
|
w.write(0) // bit field length
|
2015-10-12 08:16:57 +00:00
|
|
|
case ArgData:
|
2015-12-23 12:38:31 +00:00
|
|
|
w.write(ExecArgData)
|
2015-10-12 08:16:57 +00:00
|
|
|
w.write(uintptr(len(arg.Data)))
|
2017-01-20 22:55:25 +00:00
|
|
|
padded := len(arg.Data)
|
|
|
|
if pad := 8 - len(arg.Data)%8; pad != 8 {
|
|
|
|
padded += pad
|
|
|
|
}
|
|
|
|
if len(w.buf) < padded {
|
|
|
|
w.eof = true
|
|
|
|
} else {
|
|
|
|
copy(w.buf, arg.Data)
|
|
|
|
w.buf = w.buf[padded:]
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("unknown arg type")
|
|
|
|
}
|
|
|
|
}
|