forked from godror/godror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
197 lines (179 loc) · 4.17 KB
/
check.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
// Copyright 2021 The Godror Authors
//
//
// SPDX-License-Identifier: UPL-1.0 OR Apache-2.0
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"time"
//"github.com/kortschak/utter"
)
func main() {
if err := Main(); err != nil {
log.SetOutput(os.Stderr)
log.Fatalf("%+v", err)
}
}
type goList struct {
CgoFiles []string
}
func Main() error {
flagVerbose := flag.Bool("v", false, "verbose logging")
flag.Parse()
if !*flagVerbose {
log.SetOutput(ioutil.Discard)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
b, err := exec.CommandContext(ctx, "go", "list", "-json").Output()
if err != nil {
return err
}
var lst goList
if err = json.Unmarshal(b, &lst); err != nil {
return err
}
fset := token.NewFileSet()
for _, fn := range lst.CgoFiles {
fi, err := os.Stat(fn)
if err != nil {
return err
}
fset.AddFile(fn, -1, int(fi.Size()))
f, err := parser.ParseFile(fset, fn, nil, 0)
if err != nil {
return fmt.Errorf("parse %q: %w", fn, err)
}
if err = checkFile(fset, f); err != nil {
return err
}
}
return nil
}
func checkFile(fset *token.FileSet, f *ast.File) error {
var errs []error
for _, fun := range funcs(f.Decls) {
//utter.Dump(fun.Body.List)
fpos := fset.Position(fun.Pos())
prefix := fmt.Sprintf("%s[%s line %d]: ", fun.Name, fpos.Filename, fpos.Line)
ce := calls(fun.Body.List)
cgoc := cgoCalls(ce)
if len(cgoc) == 0 {
continue
}
getErrors := filterCalls(ce, func(se *ast.SelectorExpr) bool { return se.Sel.Name == "getError" })
if len(getErrors) == 0 {
log.Println(prefix, "no getError call")
continue
}
flot := firstLockOSThread(ce)
if flot >= 0 && flot < cgoc[0].Pos() {
log.Printf(prefix+" the first runtime.LockOSThread() call is on line %d, before the first cgo call (line %d)", fset.Position(flot).Line, fset.Position(cgoc[0].Pos()).Line)
continue
}
errs = append(errs, fmt.Errorf(
prefix+" has cgo calls (line %d), which is not protected with runtime.LockOSThread",
fset.Position(cgoc[0].Pos()).Line,
))
log.Println(errs[len(errs)-1])
}
if len(errs) == 0 {
return nil
}
return errs[0]
}
var _ = (ast.Visitor)((*funcDeclVisitor)(nil))
type funcDeclVisitor struct {
FuncDecls []*ast.FuncDecl
}
func (v *funcDeclVisitor) Visit(node ast.Node) ast.Visitor {
if node == nil {
return nil
}
if fd, ok := node.(*ast.FuncDecl); ok && fd.Body != nil && len(fd.Body.List) != 0 {
v.FuncDecls = append(v.FuncDecls, fd)
}
return v
}
func funcs(decls []ast.Decl) []*ast.FuncDecl {
var v funcDeclVisitor
for _, d := range decls {
ast.Walk(&v, d)
}
return v.FuncDecls
}
type callExprVisitor struct {
CallExprs []*ast.CallExpr
}
var _ = (ast.Visitor)((*callExprVisitor)(nil))
func (v *callExprVisitor) Visit(node ast.Node) ast.Visitor {
if node == nil {
return nil
}
switch x := node.(type) {
case *ast.ExprStmt:
if c, ok := x.X.(*ast.CallExpr); ok {
v.CallExprs = append(v.CallExprs, c)
}
case *ast.CallExpr:
v.CallExprs = append(v.CallExprs, x)
}
return v
}
func calls(stmts []ast.Stmt) []*ast.CallExpr {
var v callExprVisitor
for _, s := range stmts {
ast.Walk(&v, s)
}
return v.CallExprs
}
func filterCalls(ce []*ast.CallExpr, filter func(se *ast.SelectorExpr) bool) []*ast.CallExpr {
var cs []*ast.CallExpr
for _, c := range ce {
se, ok := c.Fun.(*ast.SelectorExpr)
if ok && filter(se) {
cs = append(cs, c)
}
}
return cs
}
func firstLockOSThread(ce []*ast.CallExpr) token.Pos {
for _, c := range filterCalls(ce, func(se *ast.SelectorExpr) bool {
if se.Sel.Name == "LockOSThread" {
if id, ok := se.X.(*ast.Ident); ok && id.Name == "runtime" {
return true
}
} else if se.Sel.Name == "checkExec" {
return true
}
return false
}) {
return c.Fun.(*ast.SelectorExpr).Sel.NamePos
}
return -1
}
func cgoCalls(ce []*ast.CallExpr) []*ast.CallExpr {
return filterCalls(ce, func(se *ast.SelectorExpr) bool {
if len(se.Sel.Name) < 3 {
return false
}
switch strings.ToLower(se.Sel.Name[:3]) {
case "oci", "dpi":
if id, ok := se.X.(*ast.Ident); ok && id.Name == "C" {
return true
}
}
return false
})
}