From 3f1d02b23f99beaf2bf3b06c11642e56578b12ee Mon Sep 17 00:00:00 2001 From: Jiaheng Hu Date: Sat, 15 Aug 2020 00:24:07 +0000 Subject: syz-manager: add test file as corpus This commit enables the syz-manager to add unit test files as corpus to accelerate fuzzing. The syz-ci would copy unit tests into the worker/seeds folder for each manager process, and the manager would add those tests as seed into the corpus. --- pkg/db/db.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'pkg/db') diff --git a/pkg/db/db.go b/pkg/db/db.go index 17723b224..7961c0797 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -17,6 +17,7 @@ import ( "io" "io/ioutil" "os" + "path/filepath" "github.com/google/syzkaller/pkg/hash" "github.com/google/syzkaller/pkg/log" @@ -68,6 +69,41 @@ func (db *DB) Save(key string, val []byte, seq uint64) { db.uncompacted++ } +// Load the test progs in the given directory store them inside db. +func (db *DB) LoadTestAsSeed(target *prog.Target, seedsDir string) { + var files []string + err := filepath.Walk(seedsDir, func(path string, info os.FileInfo, err error) error { + if !info.IsDir() { + files = append(files, path) + } + return nil + }) + if err != nil { + panic(err) + } + progEntries := loadPrograms(target, files) + if len(progEntries) == 0 { + return + } + for _, progEntry := range progEntries { + prog := progEntry.P.Serialize() + sig := hash.String(prog) + db.Save(sig, prog, 0) + } +} + +func loadPrograms(target *prog.Target, files []string) []*prog.LogEntry { + var entries []*prog.LogEntry + for _, fn := range files { + data, err := ioutil.ReadFile(fn) + if err != nil { + log.Fatalf("failed to read log file: %v", err) + } + entries = append(entries, target.ParseLog(data)...) + } + return entries +} + func (db *DB) Delete(key string) { if _, ok := db.Records[key]; !ok { return -- cgit mrf-deployment