github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/tools/syz-upgrade/upgrade.go (about) 1 // Copyright 2015 syzkaller project authors. All rights reserved. 2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4 // upgrade upgrades corpus from an old format to a new format. 5 // Upgrade is not fully automatic. You need to update prog.Serialize. 6 // Run the tool. Then update prog.Deserialize. And run the tool again that 7 // the corpus is not changed this time. 8 package main 9 10 import ( 11 "bytes" 12 "crypto/sha1" 13 "encoding/hex" 14 "fmt" 15 "os" 16 "path/filepath" 17 "runtime" 18 19 "github.com/google/syzkaller/pkg/osutil" 20 "github.com/google/syzkaller/prog" 21 _ "github.com/google/syzkaller/sys" 22 ) 23 24 func main() { 25 if len(os.Args) != 2 { 26 fatalf("usage: syz-upgrade corpus_dir") 27 } 28 files, err := os.ReadDir(os.Args[1]) 29 if err != nil { 30 fatalf("failed to read corpus dir: %v", err) 31 } 32 target, err := prog.GetTarget(runtime.GOOS, runtime.GOARCH) 33 if err != nil { 34 fatalf("%v", err) 35 } 36 for _, f := range files { 37 fname := filepath.Join(os.Args[1], f.Name()) 38 data, err := os.ReadFile(fname) 39 if err != nil { 40 fatalf("failed to read program: %v", err) 41 } 42 p, err := target.Deserialize(data, prog.NonStrict) 43 if err != nil { 44 fatalf("failed to deserialize program: %v", err) 45 } 46 data1 := p.Serialize() 47 if bytes.Equal(data, data1) { 48 continue 49 } 50 fmt.Printf("upgrading:\n%s\nto:\n%s\n\n", data, data1) 51 hash := sha1.Sum(data1) 52 fname1 := filepath.Join(os.Args[1], hex.EncodeToString(hash[:])) 53 if err := osutil.WriteFile(fname1, data1); err != nil { 54 fatalf("failed to write program: %v", err) 55 } 56 if err := os.Remove(fname); err != nil { 57 fatalf("failed to remove program: %v", err) 58 } 59 } 60 } 61 62 func fatalf(msg string, args ...interface{}) { 63 fmt.Fprintf(os.Stderr, msg+"\n", args...) 64 os.Exit(1) 65 }