-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowfuzz.go
103 lines (84 loc) · 2.25 KB
/
showfuzz.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
package showfuzz
import (
"go/ast"
"go/types"
"reflect"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const doc = "showfuzz is the tool that analyze functions can do fuzz test"
type Results struct {
Events []Event
}
type Event struct {
Name string
Args []TypeInfo
}
type TypeInfo struct {
TypName string
UnderlyingName string
IsByteArr bool
}
// Analyzer is checking the function whether do fuzz test
var Analyzer = &analysis.Analyzer{
Name: "showfuzz",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
ResultType: reflect.TypeOf(new(Results)),
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
}
results := &Results{}
if strings.HasSuffix(pass.Pkg.Name(), "_test") {
return results, nil
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
e := Event{}
switch n := n.(type) {
case *ast.FuncDecl:
if len(n.Type.Params.List) != 0 {
e.Name = n.Name.Name
for _, l := range n.Type.Params.List {
switch t := l.Type.(type) {
case *ast.ArrayType:
typ := pass.TypesInfo.TypeOf(t.Elt)
if !types.Identical(typ.Underlying(), types.Typ[types.Byte]) {
return
}
e.Args = append(e.Args, TypeInfo{typ.String(), typ.Underlying().String(), true})
case *ast.Ident:
typ := pass.TypesInfo.TypeOf(l.Type)
if !isFuzzable(typ.Underlying()) {
return
}
e.Args = append(e.Args, TypeInfo{typ.String(), typ.Underlying().String(), false})
}
}
pass.Reportf(n.Pos(), "%s can fuzz test", n.Name)
results.Events = append(results.Events, e)
}
}
})
return results, nil
}
func isFuzzable(typ types.Type) bool {
ns := []types.BasicKind{types.Int, types.Int8, types.Int16, types.Int32, types.Int64, types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64, types.Float32, types.Float64, types.Bool, types.String}
fuzzableTypes := make([]*types.Basic, len(ns))
for i, v := range ns {
fuzzableTypes[i] = types.Typ[v]
}
for _, v := range fuzzableTypes {
if types.Identical(typ, v) {
return true
}
}
return false
}