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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-12-23 12:38:31 +00:00
|
|
|
"flag"
|
2015-10-12 08:16:57 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2015-12-23 12:38:31 +00:00
|
|
|
"github.com/google/syzkaller/csource"
|
2015-10-12 08:16:57 +00:00
|
|
|
"github.com/google/syzkaller/prog"
|
|
|
|
)
|
|
|
|
|
2015-12-23 12:38:31 +00:00
|
|
|
var (
|
2017-05-18 12:54:02 +00:00
|
|
|
flagThreaded = flag.Bool("threaded", false, "create threaded program")
|
|
|
|
flagCollide = flag.Bool("collide", false, "create collide program")
|
|
|
|
flagRepeat = flag.Bool("repeat", false, "repeat program infinitely or not")
|
|
|
|
flagProcs = flag.Int("procs", 4, "number of parallel processes")
|
2017-05-18 15:03:02 +00:00
|
|
|
flagSandbox = flag.String("sandbox", "", "sandbox to use (none, setuid, namespace)")
|
2017-05-18 12:54:02 +00:00
|
|
|
flagProg = flag.String("prog", "", "file with program to convert (required)")
|
|
|
|
flagFaultCall = flag.Int("fault_call", -1, "inject fault into this call (0-based)")
|
|
|
|
flagFaultNth = flag.Int("fault_nth", 0, "inject fault on n-th operation (0-based)")
|
|
|
|
flagEnableTun = flag.Bool("tun", false, "set up TUN/TAP interface")
|
|
|
|
flagUseTmpDir = flag.Bool("tmpdir", false, "create a temporary dir and execute inside it")
|
|
|
|
flagHandleSegv = flag.Bool("segv", false, "catch and ignore SIGSEGV")
|
2017-05-18 16:17:16 +00:00
|
|
|
flagWaitRepeat = flag.Bool("waitrepeat", false, "wait for each repeat attempt")
|
2017-05-18 17:18:26 +00:00
|
|
|
flagDebug = flag.Bool("debug", false, "generate debug printfs")
|
2015-12-23 12:38:31 +00:00
|
|
|
)
|
|
|
|
|
2015-10-12 08:16:57 +00:00
|
|
|
func main() {
|
2015-12-23 12:38:31 +00:00
|
|
|
flag.Parse()
|
2016-10-18 19:07:40 +00:00
|
|
|
if *flagProg == "" {
|
|
|
|
flag.PrintDefaults()
|
2015-10-12 08:16:57 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-10-18 19:07:40 +00:00
|
|
|
data, err := ioutil.ReadFile(*flagProg)
|
2015-10-12 08:16:57 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to read prog file: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
p, err := prog.Deserialize(data)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to deserialize the program: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-12-23 12:38:31 +00:00
|
|
|
opts := csource.Options{
|
2017-05-18 12:54:02 +00:00
|
|
|
Threaded: *flagThreaded,
|
|
|
|
Collide: *flagCollide,
|
|
|
|
Repeat: *flagRepeat,
|
|
|
|
Procs: *flagProcs,
|
|
|
|
Sandbox: *flagSandbox,
|
|
|
|
Fault: *flagFaultCall >= 0,
|
|
|
|
FaultCall: *flagFaultCall,
|
|
|
|
FaultNth: *flagFaultNth,
|
|
|
|
EnableTun: *flagEnableTun,
|
|
|
|
UseTmpDir: *flagUseTmpDir,
|
|
|
|
HandleSegv: *flagHandleSegv,
|
2017-05-18 16:17:16 +00:00
|
|
|
WaitRepeat: *flagWaitRepeat,
|
2017-05-18 17:18:26 +00:00
|
|
|
Debug: *flagDebug,
|
2017-05-18 12:54:02 +00:00
|
|
|
Repro: false,
|
2016-10-18 19:07:40 +00:00
|
|
|
}
|
|
|
|
src, err := csource.Write(p, opts)
|
|
|
|
if err != nil {
|
2017-01-25 10:05:19 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "failed to generate C source: %v\n", err)
|
2016-10-18 19:07:40 +00:00
|
|
|
os.Exit(1)
|
2015-12-23 12:38:31 +00:00
|
|
|
}
|
2016-01-15 19:32:32 +00:00
|
|
|
if formatted, err := csource.Format(src); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
|
|
} else {
|
|
|
|
src = formatted
|
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
os.Stdout.Write(src)
|
|
|
|
}
|