forked from jordanlewis/gcassert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcassert.go
345 lines (313 loc) · 8.81 KB
/
gcassert.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package gcassert
import (
"bufio"
"errors"
"fmt"
"go/ast"
"go/printer"
"go/token"
"go/types"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"golang.org/x/tools/go/packages"
)
type assertDirective int
const (
noDirective assertDirective = iota
inline
bce
noescape
)
func stringToDirective(s string) (assertDirective, error) {
switch s {
case "inline":
return inline, nil
case "bce":
return bce, nil
case "noescape":
return noescape, nil
}
return noDirective, errors.New(fmt.Sprintf("no such directive %s", s))
}
type lineInfo struct {
n ast.Node
directives []assertDirective
// passedDirective is a map from index into the directives slice to a
// boolean that says whether or not the directive succeeded, in the case
// of directives like inlining that have compiler output if they passed.
// For directives like bce that have compiler output if they failed, there's
// no entry in this map.
passedDirective map[int]bool
}
var gcAssertRegex = regexp.MustCompile(`// ?gcassert:([\w,]+)`)
type assertVisitor struct {
commentMap ast.CommentMap
// directiveMap is a map from line number in the source file to the AST node
// that the line number corresponded to, as well as any directives that we
// parsed.
directiveMap map[int]lineInfo
// mustInlineFuncs is a set of types.Objects that represent FuncDecls of
// some kind that were marked with //gcassert:inline by the user.
mustInlineFuncs map[types.Object]struct{}
fileSet *token.FileSet
p *packages.Package
}
func newAssertVisitor(
commentMap ast.CommentMap,
fileSet *token.FileSet,
p *packages.Package,
mustInlineFuncs map[types.Object]struct{},
) assertVisitor {
return assertVisitor{
commentMap: commentMap,
fileSet: fileSet,
directiveMap: make(map[int]lineInfo),
mustInlineFuncs: mustInlineFuncs,
p: p,
}
}
func (v assertVisitor) Visit(node ast.Node) (w ast.Visitor) {
if node == nil {
return w
}
pos := node.Pos()
lineNumber := v.fileSet.Position(pos).Line
m := v.commentMap[node]
for _, g := range m {
COMMENT_LIST:
for _, c := range g.List {
matches := gcAssertRegex.FindStringSubmatch(c.Text)
if len(matches) == 0 {
continue
}
// The 0th match is the whole string, and the 1st match is the
// gcassert directive(s).
directiveStrings := strings.Split(matches[1], ",")
lineInfo := v.directiveMap[lineNumber]
lineInfo.n = node
for _, s := range directiveStrings {
directive, err := stringToDirective(s)
if err != nil {
continue
}
if directive == inline {
switch n := node.(type) {
case *ast.FuncDecl:
// Add the Object that this FuncDecl's ident is connected
// to to our map of must-inline functions.
obj := v.p.TypesInfo.Defs[n.Name]
if obj != nil {
v.mustInlineFuncs[obj] = struct{}{}
}
continue COMMENT_LIST
}
}
lineInfo.directives = append(lineInfo.directives, directive)
}
v.directiveMap[lineNumber] = lineInfo
}
}
return v
}
// GCAssert searches through the packages at the input path and writes failures
// to comply with //gcassert directives to the given io.Writer.
func GCAssert(w io.Writer, paths ...string) error {
fileSet := token.NewFileSet()
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedSyntax | packages.NeedCompiledGoFiles |
packages.NeedTypesInfo | packages.NeedTypes,
Fset: fileSet,
}, paths...)
directiveMap, err := parseDirectives(pkgs, fileSet)
if err != nil {
return err
}
// Next: invoke Go compiler with -m flags to get the compiler to print
// its optimization decisions.
args := []string{"build", "-gcflags=-m -m -d=ssa/check_bce/debug=1"}
for i := range paths {
args = append(args, "./"+paths[i])
}
cmd := exec.Command("go", args...)
cwd, err := os.Getwd()
if err != nil {
return err
}
cmd.Dir = cwd
pr, pw := io.Pipe()
cmd.Stdout = pw
cmd.Stderr = pw
cmdErr := make(chan error, 1)
go func() {
cmdErr <- cmd.Run()
pw.Close()
}()
scanner := bufio.NewScanner(pr)
optInfo := regexp.MustCompile(`([\.\/\w]+):(\d+):\d+: (.*)`)
boundsCheck := "Found IsInBounds"
sliceBoundsCheck := "Found SliceIsInBounds"
for scanner.Scan() {
line := scanner.Text()
matches := optInfo.FindStringSubmatch(line)
if len(matches) != 0 {
path := matches[1]
lineNo, err := strconv.Atoi(matches[2])
if err != nil {
return err
}
message := matches[3]
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
if lineToDirectives := directiveMap[absPath]; lineToDirectives != nil {
info := lineToDirectives[lineNo]
if len(info.directives) > 0 {
if info.passedDirective == nil {
info.passedDirective = make(map[int]bool)
lineToDirectives[lineNo] = info
}
}
for i, d := range info.directives {
switch d {
case bce:
if message == boundsCheck || message == sliceBoundsCheck {
// Error! We found a bounds check where the user expected
// there to be none.
// Print out the user's code lineNo that failed the assertion,
// the assertion itself, and the compiler output that
// proved that the assertion failed.
if err := printAssertionFailure(cwd, fileSet, info, w, message); err != nil {
return err
}
}
case inline:
if strings.HasPrefix(message, "inlining call to") {
info.passedDirective[i] = true
}
case noescape:
if strings.HasSuffix(message, "escapes to heap:") {
if err := printAssertionFailure(cwd, fileSet, info, w, message); err != nil {
return err
}
}
}
}
}
}
}
keys := make([]string, 0, len(directiveMap))
for k := range directiveMap {
keys = append(keys, k)
}
sort.Strings(keys)
var lines []int
for _, k := range keys {
lines = lines[:0]
lineToDirectives := directiveMap[k]
for line := range lineToDirectives {
lines = append(lines, line)
}
sort.Ints(lines)
for _, line := range lines {
info := lineToDirectives[line]
for i, d := range info.directives {
// An inlining directive passes if it has compiler output. For
// each inlining directive, check if there was matching compiler
// output and fail if not.
if d == inline {
if !info.passedDirective[i] {
if err := printAssertionFailure(
cwd, fileSet, info, w, "call was not inlined"); err != nil {
return err
}
}
}
}
}
}
return nil
}
func printAssertionFailure(cwd string, fileSet *token.FileSet, info lineInfo, w io.Writer, message string) error {
var buf strings.Builder
_ = printer.Fprint(&buf, fileSet, info.n)
pos := fileSet.Position(info.n.Pos())
relPath, err := filepath.Rel(cwd, pos.Filename)
if err != nil {
return err
}
fmt.Fprintf(w, "%s:%d:\t%s: %s\n", relPath, pos.Line, buf.String(), message)
return nil
}
type directiveMap map[string]map[int]lineInfo
func parseDirectives(pkgs []*packages.Package, fileSet *token.FileSet) (directiveMap, error) {
fileDirectiveMap := make(directiveMap)
mustInlineFuncs := make(map[types.Object]struct{})
for _, pkg := range pkgs {
for i, file := range pkg.Syntax {
commentMap := ast.NewCommentMap(fileSet, file, file.Comments)
v := newAssertVisitor(commentMap, fileSet, pkg, mustInlineFuncs)
// First: find all lines of code annotated with our gcassert directives.
ast.Walk(v, file)
file := pkg.CompiledGoFiles[i]
if len(v.directiveMap) > 0 {
fileDirectiveMap[file] = v.directiveMap
}
}
}
// Do another pass to find all callsites of funcs marked with inline.
for _, pkg := range pkgs {
for i, file := range pkg.Syntax {
v := &inlinedDeclVisitor{assertVisitor: newAssertVisitor(nil, fileSet, pkg, mustInlineFuncs)}
filePath := pkg.CompiledGoFiles[i]
v.directiveMap = fileDirectiveMap[filePath]
if v.directiveMap == nil {
v.directiveMap = make(map[int]lineInfo)
}
ast.Walk(v, file)
if len(v.directiveMap) > 0 {
fileDirectiveMap[filePath] = v.directiveMap
}
}
}
return fileDirectiveMap, nil
}
type inlinedDeclVisitor struct {
assertVisitor
}
func (v *inlinedDeclVisitor) Visit(node ast.Node) (w ast.Visitor) {
if node == nil {
return w
}
pos := node.Pos()
lineNumber := v.fileSet.Position(pos).Line
// Search for all func callsites of functions that were marked with
// gcassert:inline and add inline directives to those callsites.
switch n := node.(type) {
case *ast.CallExpr:
var obj types.Object
switch n := n.Fun.(type) {
case *ast.Ident:
obj = v.p.TypesInfo.Uses[n]
case *ast.SelectorExpr:
sel := v.p.TypesInfo.Selections[n]
if sel == nil {
break
}
obj = sel.Obj()
}
if _, ok := v.mustInlineFuncs[obj]; ok {
lineInfo := v.directiveMap[lineNumber]
lineInfo.n = node
lineInfo.directives = append(lineInfo.directives, inline)
v.directiveMap[lineNumber] = lineInfo
}
}
return v
}