github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/cmd/vet/copylock.go (about) 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. 4 5 // This file contains the code to check that locks are not passed by value. 6 7 package main 8 9 import ( 10 "bytes" 11 "fmt" 12 "go/ast" 13 "go/token" 14 "go/types" 15 ) 16 17 func init() { 18 register("copylocks", 19 "check that locks are not passed by value", 20 checkCopyLocks, 21 funcDecl, rangeStmt, funcLit, callExpr, assignStmt, genDecl, compositeLit, returnStmt) 22 } 23 24 // checkCopyLocks checks whether node might 25 // inadvertently copy a lock. 26 func checkCopyLocks(f *File, node ast.Node) { 27 switch node := node.(type) { 28 case *ast.RangeStmt: 29 checkCopyLocksRange(f, node) 30 case *ast.FuncDecl: 31 checkCopyLocksFunc(f, node.Name.Name, node.Recv, node.Type) 32 case *ast.FuncLit: 33 checkCopyLocksFunc(f, "func", nil, node.Type) 34 case *ast.CallExpr: 35 checkCopyLocksCallExpr(f, node) 36 case *ast.AssignStmt: 37 checkCopyLocksAssign(f, node) 38 case *ast.GenDecl: 39 checkCopyLocksGenDecl(f, node) 40 case *ast.CompositeLit: 41 checkCopyLocksCompositeLit(f, node) 42 case *ast.ReturnStmt: 43 checkCopyLocksReturnStmt(f, node) 44 } 45 } 46 47 // checkCopyLocksAssign checks whether an assignment 48 // copies a lock. 49 func checkCopyLocksAssign(f *File, as *ast.AssignStmt) { 50 for i, x := range as.Rhs { 51 if path := lockPathRhs(f, x); path != nil { 52 f.Badf(x.Pos(), "assignment copies lock value to %v: %v", f.gofmt(as.Lhs[i]), path) 53 } 54 } 55 } 56 57 // checkCopyLocksGenDecl checks whether lock is copied 58 // in variable declaration. 59 func checkCopyLocksGenDecl(f *File, gd *ast.GenDecl) { 60 if gd.Tok != token.VAR { 61 return 62 } 63 for _, spec := range gd.Specs { 64 valueSpec := spec.(*ast.ValueSpec) 65 for i, x := range valueSpec.Values { 66 if path := lockPathRhs(f, x); path != nil { 67 f.Badf(x.Pos(), "variable declaration copies lock value to %v: %v", valueSpec.Names[i].Name, path) 68 } 69 } 70 } 71 } 72 73 // checkCopyLocksCompositeLit detects lock copy inside a composite literal 74 func checkCopyLocksCompositeLit(f *File, cl *ast.CompositeLit) { 75 for _, x := range cl.Elts { 76 if node, ok := x.(*ast.KeyValueExpr); ok { 77 x = node.Value 78 } 79 if path := lockPathRhs(f, x); path != nil { 80 f.Badf(x.Pos(), "literal copies lock value from %v: %v", f.gofmt(x), path) 81 } 82 } 83 } 84 85 // checkCopyLocksReturnStmt detects lock copy in return statement 86 func checkCopyLocksReturnStmt(f *File, rs *ast.ReturnStmt) { 87 for _, x := range rs.Results { 88 if path := lockPathRhs(f, x); path != nil { 89 f.Badf(x.Pos(), "return copies lock value: %v", path) 90 } 91 } 92 } 93 94 // checkCopyLocksCallExpr detects lock copy in the arguments to a function call 95 func checkCopyLocksCallExpr(f *File, ce *ast.CallExpr) { 96 if id, ok := ce.Fun.(*ast.Ident); ok && id.Name == "new" && f.pkg.types[id].IsBuiltin() { 97 // Skip 'new(Type)' for built-in 'new' 98 return 99 } 100 for _, x := range ce.Args { 101 if path := lockPathRhs(f, x); path != nil { 102 f.Badf(x.Pos(), "function call copies lock value: %v", path) 103 } 104 } 105 } 106 107 // checkCopyLocksFunc checks whether a function might 108 // inadvertently copy a lock, by checking whether 109 // its receiver, parameters, or return values 110 // are locks. 111 func checkCopyLocksFunc(f *File, name string, recv *ast.FieldList, typ *ast.FuncType) { 112 if recv != nil && len(recv.List) > 0 { 113 expr := recv.List[0].Type 114 if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil { 115 f.Badf(expr.Pos(), "%s passes lock by value: %v", name, path) 116 } 117 } 118 119 if typ.Params != nil { 120 for _, field := range typ.Params.List { 121 expr := field.Type 122 if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil { 123 f.Badf(expr.Pos(), "%s passes lock by value: %v", name, path) 124 } 125 } 126 } 127 128 if typ.Results != nil { 129 for _, field := range typ.Results.List { 130 expr := field.Type 131 if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil { 132 f.Badf(expr.Pos(), "%s returns lock by value: %v", name, path) 133 } 134 } 135 } 136 } 137 138 // checkCopyLocksRange checks whether a range statement 139 // might inadvertently copy a lock by checking whether 140 // any of the range variables are locks. 141 func checkCopyLocksRange(f *File, r *ast.RangeStmt) { 142 checkCopyLocksRangeVar(f, r.Tok, r.Key) 143 checkCopyLocksRangeVar(f, r.Tok, r.Value) 144 } 145 146 func checkCopyLocksRangeVar(f *File, rtok token.Token, e ast.Expr) { 147 if e == nil { 148 return 149 } 150 id, isId := e.(*ast.Ident) 151 if isId && id.Name == "_" { 152 return 153 } 154 155 var typ types.Type 156 if rtok == token.DEFINE { 157 if !isId { 158 return 159 } 160 obj := f.pkg.defs[id] 161 if obj == nil { 162 return 163 } 164 typ = obj.Type() 165 } else { 166 typ = f.pkg.types[e].Type 167 } 168 169 if typ == nil { 170 return 171 } 172 if path := lockPath(f.pkg.typesPkg, typ); path != nil { 173 f.Badf(e.Pos(), "range var %s copies lock: %v", f.gofmt(e), path) 174 } 175 } 176 177 type typePath []types.Type 178 179 // String pretty-prints a typePath. 180 func (path typePath) String() string { 181 n := len(path) 182 var buf bytes.Buffer 183 for i := range path { 184 if i > 0 { 185 fmt.Fprint(&buf, " contains ") 186 } 187 // The human-readable path is in reverse order, outermost to innermost. 188 fmt.Fprint(&buf, path[n-i-1].String()) 189 } 190 return buf.String() 191 } 192 193 func lockPathRhs(f *File, x ast.Expr) typePath { 194 if _, ok := x.(*ast.CompositeLit); ok { 195 return nil 196 } 197 return lockPath(f.pkg.typesPkg, f.pkg.types[x].Type) 198 } 199 200 // lockPath returns a typePath describing the location of a lock value 201 // contained in typ. If there is no contained lock, it returns nil. 202 func lockPath(tpkg *types.Package, typ types.Type) typePath { 203 if typ == nil { 204 return nil 205 } 206 207 // We're only interested in the case in which the underlying 208 // type is a struct. (Interfaces and pointers are safe to copy.) 209 styp, ok := typ.Underlying().(*types.Struct) 210 if !ok { 211 return nil 212 } 213 214 // We're looking for cases in which a reference to this type 215 // can be locked, but a value cannot. This differentiates 216 // embedded interfaces from embedded values. 217 if plock := types.NewMethodSet(types.NewPointer(typ)).Lookup(tpkg, "Lock"); plock != nil { 218 if lock := types.NewMethodSet(typ).Lookup(tpkg, "Lock"); lock == nil { 219 return []types.Type{typ} 220 } 221 } 222 223 nfields := styp.NumFields() 224 for i := 0; i < nfields; i++ { 225 ftyp := styp.Field(i).Type() 226 subpath := lockPath(tpkg, ftyp) 227 if subpath != nil { 228 return append(subpath, typ) 229 } 230 } 231 232 return nil 233 }