• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
8package main
9
10import (
11	"bytes"
12	"crypto/sha1"
13	"encoding/hex"
14	"fmt"
15	"io/ioutil"
16	"os"
17	"path/filepath"
18	"runtime"
19
20	"github.com/google/syzkaller/pkg/osutil"
21	"github.com/google/syzkaller/prog"
22	_ "github.com/google/syzkaller/sys"
23)
24
25func main() {
26	if len(os.Args) != 2 {
27		fatalf("usage: syz-upgrade corpus_dir")
28	}
29	files, err := ioutil.ReadDir(os.Args[1])
30	if err != nil {
31		fatalf("failed to read corpus dir: %v", err)
32	}
33	target, err := prog.GetTarget(runtime.GOOS, runtime.GOARCH)
34	if err != nil {
35		fatalf("%v", err)
36	}
37	for _, f := range files {
38		fname := filepath.Join(os.Args[1], f.Name())
39		data, err := ioutil.ReadFile(fname)
40		if err != nil {
41			fatalf("failed to read program: %v", err)
42		}
43		p, err := target.Deserialize(data)
44		if err != nil {
45			fatalf("failed to deserialize program: %v", err)
46		}
47		data1 := p.Serialize()
48		if bytes.Equal(data, data1) {
49			continue
50		}
51		fmt.Printf("upgrading:\n%s\nto:\n%s\n\n", data, data1)
52		hash := sha1.Sum(data1)
53		fname1 := filepath.Join(os.Args[1], hex.EncodeToString(hash[:]))
54		if err := osutil.WriteFile(fname1, data1); err != nil {
55			fatalf("failed to write program: %v", err)
56		}
57		if err := os.Remove(fname); err != nil {
58			fatalf("failed to remove program: %v", err)
59		}
60	}
61}
62
63func fatalf(msg string, args ...interface{}) {
64	fmt.Fprintf(os.Stderr, msg+"\n", args...)
65	os.Exit(1)
66}
67