1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package copylock defines an Analyzer that checks for locks
6 // erroneously passed by value.
16 "golang.org/x/tools/go/analysis"
17 "golang.org/x/tools/go/analysis/passes/inspect"
18 "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
19 "golang.org/x/tools/go/ast/inspector"
22 const Doc = `check for locks erroneously passed by value
24 Inadvertently copying a value containing a lock, such as sync.Mutex or
25 sync.WaitGroup, may cause both copies to malfunction. Generally such
26 values should be referred to through a pointer.`
28 var Analyzer = &analysis.Analyzer{
31 Requires: []*analysis.Analyzer{inspect.Analyzer},
32 RunDespiteErrors: true,
36 func run(pass *analysis.Pass) (interface{}, error) {
37 inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
39 nodeFilter := []ast.Node{
40 (*ast.AssignStmt)(nil),
42 (*ast.CompositeLit)(nil),
46 (*ast.RangeStmt)(nil),
47 (*ast.ReturnStmt)(nil),
49 inspect.Preorder(nodeFilter, func(node ast.Node) {
50 switch node := node.(type) {
52 checkCopyLocksRange(pass, node)
54 checkCopyLocksFunc(pass, node.Name.Name, node.Recv, node.Type)
56 checkCopyLocksFunc(pass, "func", nil, node.Type)
58 checkCopyLocksCallExpr(pass, node)
60 checkCopyLocksAssign(pass, node)
62 checkCopyLocksGenDecl(pass, node)
63 case *ast.CompositeLit:
64 checkCopyLocksCompositeLit(pass, node)
66 checkCopyLocksReturnStmt(pass, node)
72 // checkCopyLocksAssign checks whether an assignment
74 func checkCopyLocksAssign(pass *analysis.Pass, as *ast.AssignStmt) {
75 for i, x := range as.Rhs {
76 if path := lockPathRhs(pass, x); path != nil {
77 pass.ReportRangef(x, "assignment copies lock value to %v: %v", analysisutil.Format(pass.Fset, as.Lhs[i]), path)
82 // checkCopyLocksGenDecl checks whether lock is copied
83 // in variable declaration.
84 func checkCopyLocksGenDecl(pass *analysis.Pass, gd *ast.GenDecl) {
85 if gd.Tok != token.VAR {
88 for _, spec := range gd.Specs {
89 valueSpec := spec.(*ast.ValueSpec)
90 for i, x := range valueSpec.Values {
91 if path := lockPathRhs(pass, x); path != nil {
92 pass.ReportRangef(x, "variable declaration copies lock value to %v: %v", valueSpec.Names[i].Name, path)
98 // checkCopyLocksCompositeLit detects lock copy inside a composite literal
99 func checkCopyLocksCompositeLit(pass *analysis.Pass, cl *ast.CompositeLit) {
100 for _, x := range cl.Elts {
101 if node, ok := x.(*ast.KeyValueExpr); ok {
104 if path := lockPathRhs(pass, x); path != nil {
105 pass.ReportRangef(x, "literal copies lock value from %v: %v", analysisutil.Format(pass.Fset, x), path)
110 // checkCopyLocksReturnStmt detects lock copy in return statement
111 func checkCopyLocksReturnStmt(pass *analysis.Pass, rs *ast.ReturnStmt) {
112 for _, x := range rs.Results {
113 if path := lockPathRhs(pass, x); path != nil {
114 pass.ReportRangef(x, "return copies lock value: %v", path)
119 // checkCopyLocksCallExpr detects lock copy in the arguments to a function call
120 func checkCopyLocksCallExpr(pass *analysis.Pass, ce *ast.CallExpr) {
122 switch fun := ce.Fun.(type) {
125 case *ast.SelectorExpr:
128 if fun, ok := pass.TypesInfo.Uses[id].(*types.Builtin); ok {
130 case "new", "len", "cap", "Sizeof":
134 for _, x := range ce.Args {
135 if path := lockPathRhs(pass, x); path != nil {
136 pass.ReportRangef(x, "call of %s copies lock value: %v", analysisutil.Format(pass.Fset, ce.Fun), path)
141 // checkCopyLocksFunc checks whether a function might
142 // inadvertently copy a lock, by checking whether
143 // its receiver, parameters, or return values
145 func checkCopyLocksFunc(pass *analysis.Pass, name string, recv *ast.FieldList, typ *ast.FuncType) {
146 if recv != nil && len(recv.List) > 0 {
147 expr := recv.List[0].Type
148 if path := lockPath(pass.Pkg, pass.TypesInfo.Types[expr].Type); path != nil {
149 pass.ReportRangef(expr, "%s passes lock by value: %v", name, path)
153 if typ.Params != nil {
154 for _, field := range typ.Params.List {
156 if path := lockPath(pass.Pkg, pass.TypesInfo.Types[expr].Type); path != nil {
157 pass.ReportRangef(expr, "%s passes lock by value: %v", name, path)
162 // Don't check typ.Results. If T has a Lock field it's OK to write
164 // because that is returning the zero value. Leave result checking
165 // to the return statement.
168 // checkCopyLocksRange checks whether a range statement
169 // might inadvertently copy a lock by checking whether
170 // any of the range variables are locks.
171 func checkCopyLocksRange(pass *analysis.Pass, r *ast.RangeStmt) {
172 checkCopyLocksRangeVar(pass, r.Tok, r.Key)
173 checkCopyLocksRangeVar(pass, r.Tok, r.Value)
176 func checkCopyLocksRangeVar(pass *analysis.Pass, rtok token.Token, e ast.Expr) {
180 id, isId := e.(*ast.Ident)
181 if isId && id.Name == "_" {
186 if rtok == token.DEFINE {
190 obj := pass.TypesInfo.Defs[id]
196 typ = pass.TypesInfo.Types[e].Type
202 if path := lockPath(pass.Pkg, typ); path != nil {
203 pass.Reportf(e.Pos(), "range var %s copies lock: %v", analysisutil.Format(pass.Fset, e), path)
207 type typePath []types.Type
209 // String pretty-prints a typePath.
210 func (path typePath) String() string {
213 for i := range path {
215 fmt.Fprint(&buf, " contains ")
217 // The human-readable path is in reverse order, outermost to innermost.
218 fmt.Fprint(&buf, path[n-i-1].String())
223 func lockPathRhs(pass *analysis.Pass, x ast.Expr) typePath {
224 if _, ok := x.(*ast.CompositeLit); ok {
227 if _, ok := x.(*ast.CallExpr); ok {
228 // A call may return a zero value.
231 if star, ok := x.(*ast.StarExpr); ok {
232 if _, ok := star.X.(*ast.CallExpr); ok {
233 // A call may return a pointer to a zero value.
237 return lockPath(pass.Pkg, pass.TypesInfo.Types[x].Type)
240 // lockPath returns a typePath describing the location of a lock value
241 // contained in typ. If there is no contained lock, it returns nil.
242 func lockPath(tpkg *types.Package, typ types.Type) typePath {
248 atyp, ok := typ.Underlying().(*types.Array)
255 // We're only interested in the case in which the underlying
256 // type is a struct. (Interfaces and pointers are safe to copy.)
257 styp, ok := typ.Underlying().(*types.Struct)
262 // We're looking for cases in which a pointer to this type
263 // is a sync.Locker, but a value is not. This differentiates
264 // embedded interfaces from embedded values.
265 if types.Implements(types.NewPointer(typ), lockerType) && !types.Implements(typ, lockerType) {
266 return []types.Type{typ}
269 // In go1.10, sync.noCopy did not implement Locker.
270 // (The Unlock method was added only in CL 121876.)
271 // TODO(adonovan): remove workaround when we drop go1.10.
272 if named, ok := typ.(*types.Named); ok &&
273 named.Obj().Name() == "noCopy" &&
274 named.Obj().Pkg().Path() == "sync" {
275 return []types.Type{typ}
278 nfields := styp.NumFields()
279 for i := 0; i < nfields; i++ {
280 ftyp := styp.Field(i).Type()
281 subpath := lockPath(tpkg, ftyp)
283 return append(subpath, typ)
290 var lockerType *types.Interface
292 // Construct a sync.Locker interface type.
294 nullary := types.NewSignature(nil, nil, nil, false) // func()
295 methods := []*types.Func{
296 types.NewFunc(token.NoPos, nil, "Lock", nullary),
297 types.NewFunc(token.NoPos, nil, "Unlock", nullary),
299 lockerType = types.NewInterface(methods, nil).Complete()