-
Notifications
You must be signed in to change notification settings - Fork 10
/
randomizeCalls.go
140 lines (116 loc) · 2.99 KB
/
randomizeCalls.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
package main
import (
"errors"
"fmt"
"go/ast"
"go/token"
"math/rand"
"strings"
"unicode"
"golang.org/x/tools/go/ast/astutil"
)
func randomizeCalls(fset *token.FileSet, pkgs map[string]*ast.Package) map[string]string {
funcChangeHistory := make(map[string]string)
// randomize the top level functions
for _, pkg := range pkgs {
for _, fileast := range pkg.Files {
newRandomizeTop(pkg.Name, fileast, funcChangeHistory)
}
}
// randomize all top level function bodies
for _, pkg := range pkgs {
for _, fileast := range pkg.Files {
newRandomizeInner(pkg.Name, fileast, funcChangeHistory)
}
}
return funcChangeHistory
}
func newRandomizeInner(pkgName string, fileAst *ast.File, changeHistory map[string]string) {
astutil.Apply(fileAst, func(cr *astutil.Cursor) bool {
callExpr, ok := cr.Node().(*ast.CallExpr)
if !ok {
return true
}
switch fun := callExpr.Fun.(type) {
case *ast.SelectorExpr:
ident, ok := fun.X.(*ast.Ident)
if ok {
if rname, ok := changeHistory[fmt.Sprintf("%s.%s", ident.Name, fun.Sel.Name)]; ok {
fun.Sel = ast.NewIdent(rname)
}
}
case *ast.Ident:
if rname, ok := changeHistory[fmt.Sprintf("%s.%s", pkgName, fun.Name)]; ok {
fun.Name = rname
}
}
return true
}, nil)
}
func newRandomizeTop(pkgName string, fileAst *ast.File, changeHistory map[string]string) {
astutil.Apply(fileAst, func(cr *astutil.Cursor) bool {
funcDecl, ok := cr.Node().(*ast.FuncDecl)
if !ok {
return true
}
// Ignore functions with recievers
if funcDecl.Recv != nil {
return true
}
// Ignore main
if funcDecl.Name.String() == "main" && pkgName == "main" {
return true
}
// Ignore init function
if funcDecl.Name.String() == "init" {
return true
}
outname := fmt.Sprintf("%s.%s", pkgName, funcDecl.Name.String())
// if it already exists
if randomName, ok := changeHistory[outname]; ok {
funcDecl.Name = ast.NewIdent(randomName)
} else {
randomName := randStringRunes(32)
if isExportedFunction(string(funcDecl.Name.String())) {
randomName = strings.Title(randomName)
}
changeHistory[outname] = randomName
funcDecl.Name = ast.NewIdent(randomName)
}
return true
}, nil)
}
func extractRecvTypeFromFuncDecl(funcDecl *ast.FuncDecl) (string, error) {
typeIdent := ""
if funcDecl.Recv == nil {
return "", errors.New("no recieve on function")
}
for _, field := range funcDecl.Recv.List {
astutil.Apply(field.Type, func(cr *astutil.Cursor) bool {
ident, ok := cr.Node().(*ast.Ident)
if !ok {
return true
}
typeIdent = ident.Name
return false
}, nil)
}
if typeIdent == "" {
return "", errors.New("unable to extract type ident")
}
return typeIdent, nil
}
func isExportedFunction(funcName string) bool {
if len(funcName) == 0 {
panic("this should not happen")
return false
}
return unicode.IsUpper(rune(funcName[0]))
}
func randStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}