-
Notifications
You must be signed in to change notification settings - Fork 11
/
file_walker.go
157 lines (137 loc) · 3.51 KB
/
file_walker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"bytes"
//"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"os"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
)
// rewriteTestingImports rewrites imports for:
// - all package files
// - the fuzzer
// - dependencies
//
// it rewrites "testing" => "github.com/AdamKorcz/go-118-fuzz-build/testing"
func rewriteTestingImports(pkgs []*packages.Package, fuzzName string) (string, []byte, error) {
var fuzzFilepath string
var originalFuzzContents []byte
originalFuzzContents = []byte("NONE")
// First find file with fuzz harness
for _, pkg := range pkgs {
for _, file := range pkg.GoFiles {
//fmt.Println(file)
err := rewriteTestingImport(file)
if err != nil {
panic(err)
}
}
}
// rewrite testing in imported packages
packages.Visit(pkgs, rewriteImportTesting, nil)
for _, pkg := range pkgs {
for _, file := range pkg.GoFiles {
fuzzFile, b, err := rewriteFuzzer(file, fuzzName)
if err != nil {
panic(err)
}
if fuzzFile != "" {
fuzzFilepath = fuzzFile
originalFuzzContents = b
}
}
}
return fuzzFilepath, originalFuzzContents, nil
}
func rewriteFuzzer(path, fuzzerName string) (originalPath string, originalFile []byte, err error) {
var fileHasOurHarness bool // to determine whether we should rewrite filename
fileHasOurHarness = false
var originalFuzzContents []byte
originalFuzzContents = []byte("NONE")
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
return "", originalFuzzContents, err
}
for _, decl := range f.Decls {
if _, ok := decl.(*ast.FuncDecl); ok {
if decl.(*ast.FuncDecl).Name.Name == fuzzerName {
fileHasOurHarness = true
}
}
}
if fileHasOurHarness {
originalFuzzContents, err = os.ReadFile(path)
if err != nil {
panic(err)
}
// Replace import path
astutil.DeleteImport(fset, f, "testing")
astutil.AddImport(fset, f, "github.com/AdamKorcz/go-118-fuzz-build/testing")
}
// Rewrite filename
if fileHasOurHarness {
var buf bytes.Buffer
printer.Fprint(&buf, fset, f)
newFile, err := os.Create(path + "_fuzz.go")
if err != nil {
panic(err)
}
defer newFile.Close()
newFile.Write(buf.Bytes())
return path, originalFuzzContents, nil
}
return "", originalFuzzContents, nil
}
// Rewrites testing import of a single path
func rewriteTestingImport(path string) error {
//fmt.Println("Rewriting ", path)
fsetCheck := token.NewFileSet()
fCheck, err := parser.ParseFile(fsetCheck, path, nil, parser.ImportsOnly)
if err != nil {
return err
}
// First check if the import already exists
// Return if it does.
for _, imp := range fCheck.Imports {
if imp.Path.Value == "github.com/AdamKorcz/go-118-fuzz-build/testing" {
return nil
}
}
// Replace import path
for _, imp := range fCheck.Imports {
if imp.Path.Value == "testing" {
imp.Path.Value = "github.com/AdamKorcz/go-118-fuzz-build/testing"
}
}
return nil
}
// Rewrites testing import of a package
func rewriteImportTesting(pkg *packages.Package) bool {
for _, file := range pkg.GoFiles {
err := rewriteTestingImport(file)
if err != nil {
panic(err)
}
}
return true
}
// Checks whether a fuzz test exists in a given file
func rewriteFuzzerImports(path, fuzzName string) error {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
return err
}
for _, decl := range f.Decls {
if _, ok := decl.(*ast.FuncDecl); ok {
if decl.(*ast.FuncDecl).Name.Name == fuzzName {
// First rewrite testing.F
}
}
}
return nil
}