2015-12-23 12:38:31 +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.
|
|
|
|
|
2017-10-15 09:15:27 +00:00
|
|
|
// Package csource generates [almost] equivalent C programs from syzkaller programs.
|
2015-12-23 12:38:31 +00:00
|
|
|
package csource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-06-06 11:52:57 +00:00
|
|
|
"regexp"
|
2018-07-20 18:26:05 +00:00
|
|
|
"sort"
|
2015-12-23 12:50:02 +00:00
|
|
|
"strings"
|
2015-12-23 12:38:31 +00:00
|
|
|
|
|
|
|
"github.com/google/syzkaller/prog"
|
2017-09-15 13:56:48 +00:00
|
|
|
"github.com/google/syzkaller/sys/targets"
|
2015-12-23 12:38:31 +00:00
|
|
|
)
|
|
|
|
|
2016-10-18 19:07:40 +00:00
|
|
|
func Write(p *prog.Prog, opts Options) ([]byte, error) {
|
2018-06-29 08:47:42 +00:00
|
|
|
if err := opts.Check(p.Target.OS); err != nil {
|
2017-08-09 10:57:23 +00:00
|
|
|
return nil, fmt.Errorf("csource: invalid opts: %v", err)
|
|
|
|
}
|
2017-10-15 09:15:27 +00:00
|
|
|
ctx := &context{
|
|
|
|
p: p,
|
|
|
|
opts: opts,
|
|
|
|
target: p.Target,
|
2018-06-05 15:14:47 +00:00
|
|
|
sysTarget: targets.Get(p.Target.OS, p.Target.Arch),
|
2017-10-15 09:15:27 +00:00
|
|
|
calls: make(map[string]uint64),
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2018-02-19 18:35:04 +00:00
|
|
|
|
2018-07-26 17:38:24 +00:00
|
|
|
calls, vars, err := ctx.generateProgCalls(ctx.p, opts.Trace)
|
2018-02-19 18:35:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mmapProg := p.Target.GenerateUberMmapProg()
|
2018-07-26 17:38:24 +00:00
|
|
|
mmapCalls, _, err := ctx.generateProgCalls(mmapProg, false)
|
2018-02-19 18:35:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range append(mmapProg.Calls, p.Calls...) {
|
2017-10-15 09:15:27 +00:00
|
|
|
ctx.calls[c.Meta.CallName] = c.Meta.NR
|
2016-08-28 14:33:32 +00:00
|
|
|
}
|
2017-10-15 09:15:27 +00:00
|
|
|
|
2018-07-26 15:47:27 +00:00
|
|
|
varsBuf := new(bytes.Buffer)
|
2018-02-26 14:00:46 +00:00
|
|
|
if len(vars) != 0 {
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(varsBuf, "uint64 r[%v] = {", len(vars))
|
2018-02-26 14:00:46 +00:00
|
|
|
for i, v := range vars {
|
|
|
|
if i != 0 {
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(varsBuf, ", ")
|
2018-02-26 14:00:46 +00:00
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(varsBuf, "0x%x", v)
|
2018-02-26 14:00:46 +00:00
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(varsBuf, "};\n")
|
2018-07-20 18:26:05 +00:00
|
|
|
}
|
2016-10-18 19:07:40 +00:00
|
|
|
|
2018-07-26 15:47:27 +00:00
|
|
|
sandboxFunc := "loop();"
|
|
|
|
if opts.Sandbox != "" {
|
|
|
|
sandboxFunc = "do_sandbox_" + opts.Sandbox + "();"
|
2018-07-20 18:26:05 +00:00
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
replacements := map[string]string{
|
|
|
|
"PROCS": fmt.Sprint(opts.Procs),
|
2018-07-26 17:38:24 +00:00
|
|
|
"REPEAT_TIMES": fmt.Sprint(opts.RepeatTimes),
|
2018-07-26 15:47:27 +00:00
|
|
|
"NUM_CALLS": fmt.Sprint(len(p.Calls)),
|
|
|
|
"MMAP_DATA": strings.Join(mmapCalls, ""),
|
|
|
|
"SYSCALL_DEFINES": ctx.generateSyscallDefines(),
|
|
|
|
"SANDBOX_FUNC": sandboxFunc,
|
|
|
|
"RESULTS": varsBuf.String(),
|
|
|
|
"SYSCALLS": ctx.generateSyscalls(calls, len(vars) != 0),
|
2018-07-20 18:26:05 +00:00
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
if !opts.Threaded && !opts.Repeat && opts.Sandbox == "" {
|
|
|
|
// This inlines syscalls right into main for the simplest case.
|
|
|
|
replacements["SANDBOX_FUNC"] = replacements["SYSCALLS"]
|
|
|
|
replacements["SYSCALLS"] = "unused"
|
2018-07-20 18:26:05 +00:00
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
result, err := createCommonHeader(p, mmapProg, replacements, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
const header = "// autogenerated by syzkaller (https://github.com/google/syzkaller)\n\n"
|
|
|
|
result = append([]byte(header), result...)
|
|
|
|
result = ctx.postProcess(result)
|
2017-12-27 19:02:58 +00:00
|
|
|
return result, nil
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-15 09:15:27 +00:00
|
|
|
type context struct {
|
|
|
|
p *prog.Prog
|
|
|
|
opts Options
|
|
|
|
target *prog.Target
|
|
|
|
sysTarget *targets.Target
|
|
|
|
calls map[string]uint64 // CallName -> NR
|
|
|
|
}
|
|
|
|
|
2018-07-26 15:47:27 +00:00
|
|
|
func (ctx *context) generateSyscalls(calls []string, hasVars bool) string {
|
2017-10-15 09:15:27 +00:00
|
|
|
opts := ctx.opts
|
2018-07-26 15:47:27 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2015-12-23 12:38:31 +00:00
|
|
|
if !opts.Threaded && !opts.Collide {
|
2018-07-26 17:38:24 +00:00
|
|
|
if hasVars || opts.Trace {
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(buf, "\tlong res = 0;\n")
|
2018-02-26 14:00:46 +00:00
|
|
|
}
|
2016-10-18 19:07:40 +00:00
|
|
|
if opts.Repro {
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(buf, "\tif (write(1, \"executing program\\n\", sizeof(\"executing program\\n\") - 1)) {}\n")
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
2018-07-26 17:38:24 +00:00
|
|
|
if opts.Trace {
|
|
|
|
fmt.Fprintf(buf, "\tprintf(\"### start\\n\");\n")
|
|
|
|
}
|
2015-12-23 12:38:31 +00:00
|
|
|
for _, c := range calls {
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(buf, "%s", c)
|
2015-12-23 12:38:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-26 17:38:24 +00:00
|
|
|
if hasVars || opts.Trace {
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(buf, "\tlong res;")
|
2018-02-26 14:00:46 +00:00
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(buf, "\tswitch (call) {\n")
|
2015-12-23 12:38:31 +00:00
|
|
|
for i, c := range calls {
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(buf, "\tcase %v:\n", i)
|
|
|
|
fmt.Fprintf(buf, "%s", strings.Replace(c, "\t", "\t\t", -1))
|
|
|
|
fmt.Fprintf(buf, "\t\tbreak;\n")
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(buf, "\t}\n")
|
2015-12-23 12:38:31 +00:00
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
return buf.String()
|
2015-12-23 12:38:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 15:47:27 +00:00
|
|
|
func (ctx *context) generateSyscallDefines() string {
|
|
|
|
var calls []string
|
2017-10-15 09:15:27 +00:00
|
|
|
for name, nr := range ctx.calls {
|
2018-06-29 08:47:42 +00:00
|
|
|
if !ctx.sysTarget.SyscallNumbers ||
|
|
|
|
strings.HasPrefix(name, "syz_") || !ctx.sysTarget.NeedSyscallDefine(nr) {
|
2017-10-15 09:15:27 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
calls = append(calls, name)
|
|
|
|
}
|
|
|
|
sort.Strings(calls)
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
prefix := ctx.sysTarget.SyscallPrefix
|
|
|
|
for _, name := range calls {
|
|
|
|
fmt.Fprintf(buf, "#ifndef %v%v\n", prefix, name)
|
|
|
|
fmt.Fprintf(buf, "#define %v%v %v\n", prefix, name, ctx.calls[name])
|
|
|
|
fmt.Fprintf(buf, "#endif\n")
|
2017-10-15 09:15:27 +00:00
|
|
|
}
|
|
|
|
if ctx.target.OS == "linux" && ctx.target.PtrSize == 4 {
|
|
|
|
// This is a dirty hack.
|
|
|
|
// On 32-bit linux mmap translated to old_mmap syscall which has a different signature.
|
2017-11-23 07:51:04 +00:00
|
|
|
// mmap2 has the right signature. syz-extract translates mmap to mmap2, do the same here.
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(buf, "#undef __NR_mmap\n")
|
|
|
|
fmt.Fprintf(buf, "#define __NR_mmap __NR_mmap2\n")
|
2017-10-15 09:15:27 +00:00
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
return buf.String()
|
2017-10-15 09:15:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 17:38:24 +00:00
|
|
|
func (ctx *context) generateProgCalls(p *prog.Prog, trace bool) ([]string, []uint64, error) {
|
2018-02-19 18:35:04 +00:00
|
|
|
exec := make([]byte, prog.ExecBufferSize)
|
|
|
|
progSize, err := p.SerializeForExec(exec)
|
|
|
|
if err != nil {
|
2018-02-26 14:00:46 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to serialize program: %v", err)
|
2018-02-19 18:35:04 +00:00
|
|
|
}
|
|
|
|
decoded, err := ctx.target.DeserializeExec(exec[:progSize])
|
|
|
|
if err != nil {
|
2018-02-26 14:00:46 +00:00
|
|
|
return nil, nil, err
|
2018-02-19 18:35:04 +00:00
|
|
|
}
|
2018-07-26 17:38:24 +00:00
|
|
|
calls, vars := ctx.generateCalls(decoded, trace)
|
2018-02-26 14:00:46 +00:00
|
|
|
return calls, vars, nil
|
2018-02-19 18:35:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 17:38:24 +00:00
|
|
|
func (ctx *context) generateCalls(p prog.ExecProg, trace bool) ([]string, []uint64) {
|
2015-12-23 12:38:31 +00:00
|
|
|
var calls []string
|
2017-12-15 12:42:28 +00:00
|
|
|
csumSeq := 0
|
|
|
|
for ci, call := range p.Calls {
|
|
|
|
w := new(bytes.Buffer)
|
|
|
|
// Copyin.
|
|
|
|
for _, copyin := range call.Copyin {
|
2018-07-07 18:07:30 +00:00
|
|
|
ctx.copyin(w, &csumSeq, copyin)
|
2017-12-15 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call itself.
|
|
|
|
if ctx.opts.Fault && ctx.opts.FaultCall == ci {
|
|
|
|
fmt.Fprintf(w, "\twrite_file(\"/sys/kernel/debug/failslab/ignore-gfp-wait\", \"N\");\n")
|
|
|
|
fmt.Fprintf(w, "\twrite_file(\"/sys/kernel/debug/fail_futex/ignore-private\", \"N\");\n")
|
|
|
|
fmt.Fprintf(w, "\tinject_fault(%v);\n", ctx.opts.FaultNth)
|
|
|
|
}
|
|
|
|
callName := call.Meta.CallName
|
2017-12-16 11:38:49 +00:00
|
|
|
resCopyout := call.Index != prog.ExecNoCopyout
|
|
|
|
argCopyout := len(call.Copyout) != 0
|
|
|
|
emitCall := ctx.opts.EnableTun || callName != "syz_emit_ethernet" &&
|
|
|
|
callName != "syz_extract_tcp_res"
|
2017-12-15 12:42:28 +00:00
|
|
|
// TODO: if we don't emit the call we must also not emit copyin, copyout and fault injection.
|
|
|
|
// However, simply skipping whole iteration breaks tests due to unused static functions.
|
2017-12-16 11:38:49 +00:00
|
|
|
if emitCall {
|
2018-06-29 08:47:42 +00:00
|
|
|
native := ctx.sysTarget.SyscallNumbers && !strings.HasPrefix(callName, "syz_")
|
2017-12-16 11:38:49 +00:00
|
|
|
fmt.Fprintf(w, "\t")
|
2018-07-26 17:38:24 +00:00
|
|
|
if resCopyout || argCopyout || trace {
|
2018-02-26 14:00:46 +00:00
|
|
|
fmt.Fprintf(w, "res = ")
|
2017-12-16 11:38:49 +00:00
|
|
|
}
|
2017-12-15 12:42:28 +00:00
|
|
|
if native {
|
2017-12-16 11:38:49 +00:00
|
|
|
fmt.Fprintf(w, "syscall(%v%v", ctx.sysTarget.SyscallPrefix, callName)
|
2018-06-29 08:47:42 +00:00
|
|
|
} else if strings.HasPrefix(callName, "syz_") {
|
2017-12-16 11:38:49 +00:00
|
|
|
fmt.Fprintf(w, "%v(", callName)
|
2018-06-29 08:47:42 +00:00
|
|
|
} else {
|
|
|
|
args := strings.Repeat(",long", len(call.Args))
|
|
|
|
if args != "" {
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "((long(*)(%v))%v)(", args, callName)
|
2017-05-19 13:53:19 +00:00
|
|
|
}
|
2017-12-15 12:42:28 +00:00
|
|
|
for ai, arg := range call.Args {
|
|
|
|
if native || ai > 0 {
|
2017-05-19 13:53:19 +00:00
|
|
|
fmt.Fprintf(w, ", ")
|
|
|
|
}
|
2017-12-15 12:42:28 +00:00
|
|
|
switch arg := arg.(type) {
|
2015-12-23 12:38:31 +00:00
|
|
|
case prog.ExecArgConst:
|
2018-07-07 18:07:30 +00:00
|
|
|
if arg.Format != prog.FormatNative && arg.Format != prog.FormatBigEndian {
|
|
|
|
panic("sring format in syscall argument")
|
|
|
|
}
|
2017-12-22 10:47:04 +00:00
|
|
|
fmt.Fprintf(w, "%v", ctx.constArgToStr(arg))
|
2015-12-23 12:38:31 +00:00
|
|
|
case prog.ExecArgResult:
|
2018-07-07 18:07:30 +00:00
|
|
|
if arg.Format != prog.FormatNative && arg.Format != prog.FormatBigEndian {
|
|
|
|
panic("sring format in syscall argument")
|
|
|
|
}
|
2017-12-22 10:47:04 +00:00
|
|
|
fmt.Fprintf(w, "%v", ctx.resultArgToStr(arg))
|
2015-12-23 12:38:31 +00:00
|
|
|
default:
|
2017-12-15 12:42:28 +00:00
|
|
|
panic(fmt.Sprintf("unknown arg type: %+v", arg))
|
2015-12-23 12:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-26 14:00:46 +00:00
|
|
|
fmt.Fprintf(w, ");\n")
|
2018-07-26 17:38:24 +00:00
|
|
|
if trace {
|
|
|
|
fmt.Fprintf(w, "\tprintf(\"### call=%v errno=%%d\\n\", res == -1 ? errno : 0);\n", ci)
|
|
|
|
}
|
2015-12-23 12:38:31 +00:00
|
|
|
}
|
2017-12-15 12:42:28 +00:00
|
|
|
|
|
|
|
// Copyout.
|
2018-02-26 14:00:46 +00:00
|
|
|
if resCopyout || argCopyout {
|
2018-06-30 11:40:00 +00:00
|
|
|
if ctx.sysTarget.OS == "fuchsia" {
|
|
|
|
// On fuchsia we have real system calls that return ZX_OK on success,
|
|
|
|
// and libc calls that are casted to function returning long,
|
2018-06-29 08:47:42 +00:00
|
|
|
// as the result int -1 is returned as 0x00000000ffffffff rather than full -1.
|
2018-06-30 11:40:00 +00:00
|
|
|
if strings.HasPrefix(callName, "zx_") {
|
|
|
|
fmt.Fprintf(w, "\tif (res == ZX_OK)")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "\tif ((int)res != -1)")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "\tif (res != -1)")
|
2018-06-29 08:47:42 +00:00
|
|
|
}
|
2018-02-26 14:00:46 +00:00
|
|
|
copyoutMultiple := len(call.Copyout) > 1 || resCopyout && len(call.Copyout) > 0
|
|
|
|
if copyoutMultiple {
|
|
|
|
fmt.Fprintf(w, " {")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "\n")
|
|
|
|
if resCopyout {
|
|
|
|
fmt.Fprintf(w, "\t\tr[%v] = res;\n", call.Index)
|
|
|
|
}
|
|
|
|
for _, copyout := range call.Copyout {
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(w, "\t\tNONFAILING(r[%v] = *(uint%v*)0x%x);\n",
|
2018-02-26 14:00:46 +00:00
|
|
|
copyout.Index, copyout.Size*8, copyout.Addr)
|
|
|
|
}
|
|
|
|
if copyoutMultiple {
|
|
|
|
fmt.Fprintf(w, "\t}\n")
|
|
|
|
}
|
2017-12-16 11:38:49 +00:00
|
|
|
}
|
2017-12-15 12:42:28 +00:00
|
|
|
calls = append(calls, w.String())
|
2015-12-23 12:38:31 +00:00
|
|
|
}
|
2018-02-26 14:00:46 +00:00
|
|
|
return calls, p.Vars
|
2015-12-23 12:38:31 +00:00
|
|
|
}
|
2017-12-17 09:55:35 +00:00
|
|
|
|
2018-05-04 12:24:51 +00:00
|
|
|
func (ctx *context) generateCsumInet(w *bytes.Buffer, addr uint64, arg prog.ExecArgCsum, csumSeq int) {
|
|
|
|
fmt.Fprintf(w, "\tstruct csum_inet csum_%d;\n", csumSeq)
|
|
|
|
fmt.Fprintf(w, "\tcsum_inet_init(&csum_%d);\n", csumSeq)
|
|
|
|
for i, chunk := range arg.Chunks {
|
|
|
|
switch chunk.Kind {
|
|
|
|
case prog.ExecArgCsumChunkData:
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(w, "\tNONFAILING(csum_inet_update(&csum_%d, (const uint8*)0x%x, %d));\n",
|
2018-05-04 12:24:51 +00:00
|
|
|
csumSeq, chunk.Value, chunk.Size)
|
|
|
|
case prog.ExecArgCsumChunkConst:
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(w, "\tuint%d csum_%d_chunk_%d = 0x%x;\n",
|
2018-05-04 12:24:51 +00:00
|
|
|
chunk.Size*8, csumSeq, i, chunk.Value)
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(w, "\tcsum_inet_update(&csum_%d, (const uint8*)&csum_%d_chunk_%d, %d);\n",
|
2018-05-04 12:24:51 +00:00
|
|
|
csumSeq, csumSeq, i, chunk.Size)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown checksum chunk kind %v", chunk.Kind))
|
|
|
|
}
|
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(w, "\tNONFAILING(*(uint16*)0x%x = csum_inet_digest(&csum_%d));\n",
|
2018-05-04 12:24:51 +00:00
|
|
|
addr, csumSeq)
|
|
|
|
}
|
|
|
|
|
2018-07-07 18:07:30 +00:00
|
|
|
func (ctx *context) copyin(w *bytes.Buffer, csumSeq *int, copyin prog.ExecCopyin) {
|
|
|
|
switch arg := copyin.Arg.(type) {
|
|
|
|
case prog.ExecArgConst:
|
|
|
|
if arg.BitfieldOffset == 0 && arg.BitfieldLength == 0 {
|
|
|
|
ctx.copyinVal(w, copyin.Addr, arg.Size, ctx.constArgToStr(arg), arg.Format)
|
|
|
|
} else {
|
|
|
|
if arg.Format != prog.FormatNative && arg.Format != prog.FormatBigEndian {
|
|
|
|
panic("bitfield+string format")
|
|
|
|
}
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(w, "\tNONFAILING(STORE_BY_BITMASK(uint%v, 0x%x, %v, %v, %v));\n",
|
2018-07-07 18:07:30 +00:00
|
|
|
arg.Size*8, copyin.Addr, ctx.constArgToStr(arg),
|
|
|
|
arg.BitfieldOffset, arg.BitfieldLength)
|
|
|
|
}
|
|
|
|
case prog.ExecArgResult:
|
|
|
|
ctx.copyinVal(w, copyin.Addr, arg.Size, ctx.resultArgToStr(arg), arg.Format)
|
|
|
|
case prog.ExecArgData:
|
|
|
|
fmt.Fprintf(w, "\tNONFAILING(memcpy((void*)0x%x, \"%s\", %v));\n",
|
|
|
|
copyin.Addr, toCString(arg.Data), len(arg.Data))
|
|
|
|
case prog.ExecArgCsum:
|
|
|
|
switch arg.Kind {
|
|
|
|
case prog.ExecArgCsumInet:
|
|
|
|
*csumSeq++
|
|
|
|
ctx.generateCsumInet(w, copyin.Addr, arg, *csumSeq)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown csum kind %v", arg.Kind))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("bad argument type: %+v", arg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *context) copyinVal(w *bytes.Buffer, addr, size uint64, val string, bf prog.BinaryFormat) {
|
|
|
|
switch bf {
|
|
|
|
case prog.FormatNative, prog.FormatBigEndian:
|
2018-07-26 15:47:27 +00:00
|
|
|
fmt.Fprintf(w, "\tNONFAILING(*(uint%v*)0x%x = %v);\n", size*8, addr, val)
|
2018-07-07 18:07:30 +00:00
|
|
|
case prog.FormatStrDec:
|
|
|
|
if size != 20 {
|
|
|
|
panic("bad strdec size")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "\tNONFAILING(sprintf((char*)0x%x, \"%%020llu\", (long long)%v));\n", addr, val)
|
|
|
|
case prog.FormatStrHex:
|
|
|
|
if size != 18 {
|
|
|
|
panic("bad strdec size")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "\tNONFAILING(sprintf((char*)0x%x, \"0x%%016llx\", (long long)%v));\n", addr, val)
|
|
|
|
case prog.FormatStrOct:
|
|
|
|
if size != 23 {
|
|
|
|
panic("bad strdec size")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "\tNONFAILING(sprintf((char*)0x%x, \"%%023llo\", (long long)%v));\n", addr, val)
|
|
|
|
default:
|
|
|
|
panic("unknown binary format")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:47:04 +00:00
|
|
|
func (ctx *context) constArgToStr(arg prog.ExecArgConst) string {
|
|
|
|
mask := (uint64(1) << (arg.Size * 8)) - 1
|
2017-12-27 19:02:58 +00:00
|
|
|
v := arg.Value & mask
|
|
|
|
val := fmt.Sprintf("%v", v)
|
|
|
|
if v == ^uint64(0)&mask {
|
|
|
|
val = "-1"
|
|
|
|
} else if v >= 10 {
|
|
|
|
val = fmt.Sprintf("0x%x", v)
|
|
|
|
}
|
2017-12-22 10:47:04 +00:00
|
|
|
if ctx.opts.Procs > 1 && arg.PidStride != 0 {
|
2018-01-06 14:26:40 +00:00
|
|
|
val += fmt.Sprintf(" + procid*%v", arg.PidStride)
|
2017-12-22 10:47:04 +00:00
|
|
|
}
|
2018-07-07 18:07:30 +00:00
|
|
|
if arg.Format == prog.FormatBigEndian {
|
2017-12-22 10:47:04 +00:00
|
|
|
val = fmt.Sprintf("htobe%v(%v)", arg.Size*8, val)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *context) resultArgToStr(arg prog.ExecArgResult) string {
|
|
|
|
res := fmt.Sprintf("r[%v]", arg.Index)
|
|
|
|
if arg.DivOp != 0 {
|
|
|
|
res = fmt.Sprintf("%v/%v", res, arg.DivOp)
|
|
|
|
}
|
|
|
|
if arg.AddOp != 0 {
|
|
|
|
res = fmt.Sprintf("%v+%v", res, arg.AddOp)
|
|
|
|
}
|
2018-07-07 18:07:30 +00:00
|
|
|
if arg.Format == prog.FormatBigEndian {
|
|
|
|
res = fmt.Sprintf("htobe%v(%v)", arg.Size*8, res)
|
|
|
|
}
|
2017-12-22 10:47:04 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-07-20 18:26:05 +00:00
|
|
|
func (ctx *context) postProcess(result []byte) []byte {
|
|
|
|
// Remove NONFAILING, debug, fail, etc calls.
|
|
|
|
if !ctx.opts.HandleSegv {
|
|
|
|
result = regexp.MustCompile(`\t*NONFAILING\((.*)\);\n`).ReplaceAll(result, []byte("$1;\n"))
|
|
|
|
}
|
|
|
|
result = bytes.Replace(result, []byte("NORETURN"), nil, -1)
|
|
|
|
result = bytes.Replace(result, []byte("PRINTF"), nil, -1)
|
|
|
|
result = bytes.Replace(result, []byte("doexit("), []byte("exit("), -1)
|
|
|
|
result = regexp.MustCompile(`\t*debug\((.*\n)*?.*\);\n`).ReplaceAll(result, nil)
|
|
|
|
result = regexp.MustCompile(`\t*debug_dump_data\((.*\n)*?.*\);\n`).ReplaceAll(result, nil)
|
|
|
|
result = regexp.MustCompile(`\t*exitf\((.*\n)*?.*\);\n`).ReplaceAll(result, []byte("\texit(1);\n"))
|
|
|
|
result = regexp.MustCompile(`\t*fail\((.*\n)*?.*\);\n`).ReplaceAll(result, []byte("\texit(1);\n"))
|
|
|
|
result = regexp.MustCompile(`\t*error\((.*\n)*?.*\);\n`).ReplaceAll(result, []byte("\texit(1);\n"))
|
|
|
|
|
|
|
|
result = ctx.hoistIncludes(result)
|
|
|
|
result = ctx.removeEmptyLines(result)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// hoistIncludes moves all includes to the top, removes dups and sorts.
|
|
|
|
func (ctx *context) hoistIncludes(result []byte) []byte {
|
|
|
|
includesStart := bytes.Index(result, []byte("#include"))
|
|
|
|
if includesStart == -1 {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
includes := make(map[string]bool)
|
|
|
|
includeRe := regexp.MustCompile("#include <.*>\n")
|
|
|
|
for _, match := range includeRe.FindAll(result, -1) {
|
|
|
|
includes[string(match)] = true
|
|
|
|
}
|
|
|
|
result = includeRe.ReplaceAll(result, nil)
|
|
|
|
// Linux headers are broken, so we have to move all linux includes to the bottom.
|
|
|
|
var sorted, sortedLinux []string
|
|
|
|
for include := range includes {
|
|
|
|
if strings.Contains(include, "<linux/") {
|
|
|
|
sortedLinux = append(sortedLinux, include)
|
|
|
|
} else {
|
|
|
|
sorted = append(sorted, include)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(sorted)
|
|
|
|
sort.Strings(sortedLinux)
|
|
|
|
newResult := append([]byte{}, result[:includesStart]...)
|
|
|
|
newResult = append(newResult, strings.Join(sorted, "")...)
|
|
|
|
newResult = append(newResult, '\n')
|
|
|
|
newResult = append(newResult, strings.Join(sortedLinux, "")...)
|
|
|
|
newResult = append(newResult, result[includesStart:]...)
|
|
|
|
return newResult
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeEmptyLines removes duplicate new lines.
|
|
|
|
func (ctx *context) removeEmptyLines(result []byte) []byte {
|
|
|
|
for {
|
|
|
|
newResult := bytes.Replace(result, []byte{'\n', '\n', '\n'}, []byte{'\n', '\n'}, -1)
|
2018-07-26 15:47:27 +00:00
|
|
|
newResult = bytes.Replace(newResult, []byte{'\n', '\n', '\t'}, []byte{'\n', '\t'}, -1)
|
|
|
|
newResult = bytes.Replace(newResult, []byte{'\n', '\n', ' '}, []byte{'\n', ' '}, -1)
|
2018-07-20 18:26:05 +00:00
|
|
|
if len(newResult) == len(result) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
result = newResult
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 09:55:35 +00:00
|
|
|
func toCString(data []byte) []byte {
|
|
|
|
if len(data) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
readable := true
|
|
|
|
for i, v := range data {
|
|
|
|
// Allow 0 only as last byte.
|
|
|
|
if !isReadable(v) && (i != len(data)-1 || v != 0) {
|
|
|
|
readable = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !readable {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for _, v := range data {
|
|
|
|
buf.Write([]byte{'\\', 'x', toHex(v >> 4), toHex(v << 4 >> 4)})
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
if data[len(data)-1] == 0 {
|
|
|
|
// Don't serialize last 0, C strings are 0-terminated anyway.
|
|
|
|
data = data[:len(data)-1]
|
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for _, v := range data {
|
|
|
|
switch v {
|
|
|
|
case '\t':
|
|
|
|
buf.Write([]byte{'\\', 't'})
|
|
|
|
case '\r':
|
|
|
|
buf.Write([]byte{'\\', 'r'})
|
|
|
|
case '\n':
|
|
|
|
buf.Write([]byte{'\\', 'n'})
|
|
|
|
case '\\':
|
|
|
|
buf.Write([]byte{'\\', '\\'})
|
2017-12-22 10:59:09 +00:00
|
|
|
case '"':
|
|
|
|
buf.Write([]byte{'\\', '"'})
|
2017-12-17 09:55:35 +00:00
|
|
|
default:
|
|
|
|
if v < 0x20 || v >= 0x7f {
|
|
|
|
panic("unexpected char during data serialization")
|
|
|
|
}
|
|
|
|
buf.WriteByte(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func isReadable(v byte) bool {
|
|
|
|
return v >= 0x20 && v < 0x7f || v == '\t' || v == '\r' || v == '\n'
|
|
|
|
}
|
|
|
|
|
|
|
|
func toHex(v byte) byte {
|
|
|
|
if v < 10 {
|
|
|
|
return '0' + v
|
|
|
|
}
|
|
|
|
return 'a' + v - 10
|
|
|
|
}
|