2018-12-06 15:25:37 +00:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2019-10-08 10:26:27 +00:00
|
|
|
// +build !codeanalysis
|
|
|
|
|
2018-12-06 15:25:37 +00:00
|
|
|
// syz-trace2syz converts strace traces to syzkaller programs.
|
|
|
|
//
|
|
|
|
// Simple usage:
|
|
|
|
// strace -o trace -a 1 -s 65500 -v -xx -f -Xraw ./a.out
|
|
|
|
// syz-trace2syz -file trace
|
|
|
|
// Intended for seed selection or debugging
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/google/syzkaller/pkg/db"
|
|
|
|
"github.com/google/syzkaller/pkg/log"
|
2018-12-07 09:43:27 +00:00
|
|
|
"github.com/google/syzkaller/pkg/osutil"
|
2018-12-06 15:25:37 +00:00
|
|
|
"github.com/google/syzkaller/prog"
|
|
|
|
_ "github.com/google/syzkaller/sys"
|
|
|
|
"github.com/google/syzkaller/tools/syz-trace2syz/proggen"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
flagFile = flag.String("file", "", "file to parse")
|
|
|
|
flagDir = flag.String("dir", "", "directory to parse")
|
|
|
|
flagDeserialize = flag.String("deserialize", "", "(Optional) directory to store deserialized programs")
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-12-06 15:49:37 +00:00
|
|
|
goos = "linux" // Target OS
|
|
|
|
arch = "amd64" // Target architecture
|
2018-12-06 15:25:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
target := initializeTarget(goos, arch)
|
|
|
|
progs := parseTraces(target)
|
|
|
|
log.Logf(0, "successfully converted traces; generating corpus.db")
|
|
|
|
pack(progs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initializeTarget(os, arch string) *prog.Target {
|
|
|
|
target, err := prog.GetTarget(os, arch)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to load target: %s", err)
|
|
|
|
}
|
|
|
|
target.ConstMap = make(map[string]uint64)
|
|
|
|
for _, c := range target.Consts {
|
|
|
|
target.ConstMap[c.Name] = c.Value
|
|
|
|
}
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTraces(target *prog.Target) []*prog.Prog {
|
|
|
|
var ret []*prog.Prog
|
|
|
|
var names []string
|
|
|
|
|
|
|
|
if *flagFile != "" {
|
|
|
|
names = append(names, *flagFile)
|
|
|
|
} else if *flagDir != "" {
|
|
|
|
names = getTraceFiles(*flagDir)
|
|
|
|
} else {
|
|
|
|
log.Fatalf("-file or -dir must be specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializeDir := *flagDeserialize
|
|
|
|
|
|
|
|
totalFiles := len(names)
|
2018-12-07 09:43:27 +00:00
|
|
|
log.Logf(0, "parsing %v traces", totalFiles)
|
2018-12-06 15:25:37 +00:00
|
|
|
for i, file := range names {
|
2018-12-07 09:43:27 +00:00
|
|
|
log.Logf(1, "parsing file %v/%v: %v", i+1, totalFiles, filepath.Base(names[i]))
|
2018-12-07 11:05:43 +00:00
|
|
|
progs, err := proggen.ParseFile(file, target)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%v", err)
|
|
|
|
}
|
2018-12-07 10:21:47 +00:00
|
|
|
ret = append(ret, progs...)
|
|
|
|
if deserializeDir != "" {
|
|
|
|
for i, p := range progs {
|
|
|
|
progName := filepath.Join(deserializeDir, filepath.Base(file)+strconv.Itoa(i))
|
|
|
|
if err := osutil.WriteFile(progName, p.Serialize()); err != nil {
|
|
|
|
log.Fatalf("failed to output file: %v", err)
|
|
|
|
}
|
2018-12-06 15:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTraceFiles(dir string) []string {
|
|
|
|
infos, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s", err)
|
|
|
|
|
|
|
|
}
|
2018-12-07 09:35:42 +00:00
|
|
|
var names []string
|
2018-12-06 15:25:37 +00:00
|
|
|
for _, info := range infos {
|
|
|
|
name := filepath.Join(dir, info.Name())
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
func pack(progs []*prog.Prog) {
|
2018-12-06 15:49:37 +00:00
|
|
|
var records []db.Record
|
|
|
|
for _, prog := range progs {
|
|
|
|
records = append(records, db.Record{Val: prog.Serialize()})
|
2018-12-06 15:25:37 +00:00
|
|
|
}
|
2018-12-06 15:49:37 +00:00
|
|
|
if err := db.Create("corpus.db", 0, records); err != nil {
|
|
|
|
log.Fatalf("%v", err)
|
2018-12-06 15:25:37 +00:00
|
|
|
}
|
|
|
|
log.Logf(0, "finished!")
|
|
|
|
}
|