github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/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, ok := spec.(*ast.ValueSpec) 65 if !ok { 66 continue 67 } 68 for i, x := range valueSpec.Values { 69 if path := lockPathRhs(f, x); path != nil { 70 f.Badf(x.Pos(), "variable declaration copies lock value to %v: %v", valueSpec.Names[i].Name, path) 71 } 72 } 73 } 74 } 75 76 // checkCopyLocksCompositeLit detects lock copy inside a composite literal 77 func checkCopyLocksCompositeLit(f *File, cl *ast.CompositeLit) { 78 for _, x := range cl.Elts { 79 if node, ok := x.(*ast.KeyValueExpr); ok { 80 x = node.Value 81 } 82 if path := lockPathRhs(f, x); path != nil { 83 f.Badf(x.Pos(), "literal copies lock value from %v: %v", f.gofmt(x), path) 84 } 85 } 86 } 87 88 // checkCopyLocksReturnStmt detects lock copy in return statement 89 func checkCopyLocksReturnStmt(f *File, rs *ast.ReturnStmt) { 90 for _, x := range rs.Results { 91 if path := lockPathRhs(f, x); path != nil { 92 f.Badf(x.Pos(), "return copies lock value: %v", path) 93 } 94 } 95 } 96 97 // checkCopyLocksCallExpr detects lock copy in the arguments to a function call 98 func checkCopyLocksCallExpr(f *File, ce *ast.CallExpr) { 99 if id, ok := ce.Fun.(*ast.Ident); ok && id.Name == "new" && f.pkg.types[id].IsBuiltin() { 100 // Skip 'new(Type)' for built-in 'new' 101 return 102 } 103 for _, x := range ce.Args { 104 if path := lockPathRhs(f, x); path != nil { 105 f.Badf(x.Pos(), "function call copies lock value: %v", path) 106 } 107 } 108 } 109 110 // checkCopyLocksFunc checks whether a function might 111 // inadvertently copy a lock, by checking whether 112 // its receiver, parameters, or return values 113 // are locks. 114 func checkCopyLocksFunc(f *File, name string, recv *ast.FieldList, typ *ast.FuncType) { 115 if recv != nil && len(recv.List) > 0 { 116 expr := recv.List[0].Type 117 if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil { 118 f.Badf(expr.Pos(), "%s passes lock by value: %v", name, path) 119 } 120 } 121 122 if typ.Params != nil { 123 for _, field := range typ.Params.List { 124 expr := field.Type 125 if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil { 126 f.Badf(expr.Pos(), "%s passes lock by value: %v", name, path) 127 } 128 } 129 } 130 131 // Don't check typ.Results. If T has a Lock field it's OK to write 132 // return T{} 133 // because that is returning the zero value. Leave result checking 134 // to the return statement. 135 } 136 137 // checkCopyLocksRange checks whether a range statement 138 // might inadvertently copy a lock by checking whether 139 // any of the range variables are locks. 140 func checkCopyLocksRange(f *File, r *ast.RangeStmt) { 141 checkCopyLocksRangeVar(f, r.Tok, r.Key) 142 checkCopyLocksRangeVar(f, r.Tok, r.Value) 143 } 144 145 func checkCopyLocksRangeVar(f *File, rtok token.Token, e ast.Expr) { 146 if e == nil { 147 return 148 } 149 id, isId := e.(*ast.Ident) 150 if isId && id.Name == "_" { 151 return 152 } 153 154 var typ types.Type 155 if rtok == token.DEFINE { 156 if !isId { 157 return 158 } 159 obj := f.pkg.defs[id] 160 if obj == nil { 161 return 162 } 163 typ = obj.Type() 164 } else { 165 typ = f.pkg.types[e].Type 166 } 167 168 if typ == nil { 169 return 170 } 171 if path := lockPath(f.pkg.typesPkg, typ); path != nil { 172 f.Badf(e.Pos(), "range var %s copies lock: %v", f.gofmt(e), path) 173 } 174 } 175 176 type typePath []types.Type 177 178 // String pretty-prints a typePath. 179 func (path typePath) String() string { 180 n := len(path) 181 var buf bytes.Buffer 182 for i := range path { 183 if i > 0 { 184 fmt.Fprint(&buf, " contains ") 185 } 186 // The human-readable path is in reverse order, outermost to innermost. 187 fmt.Fprint(&buf, path[n-i-1].String()) 188 } 189 return buf.String() 190 } 191 192 func lockPathRhs(f *File, x ast.Expr) typePath { 193 if _, ok := x.(*ast.CompositeLit); ok { 194 return nil 195 } 196 if _, ok := x.(*ast.CallExpr); ok { 197 // A call may return a zero value. 198 return nil 199 } 200 if star, ok := x.(*ast.StarExpr); ok { 201 if _, ok := star.X.(*ast.CallExpr); ok { 202 // A call may return a pointer to a zero value. 203 return nil 204 } 205 } 206 return lockPath(f.pkg.typesPkg, f.pkg.types[x].Type) 207 } 208 209 // lockPath returns a typePath describing the location of a lock value 210 // contained in typ. If there is no contained lock, it returns nil. 211 func lockPath(tpkg *types.Package, typ types.Type) typePath { 212 if typ == nil { 213 return nil 214 } 215 216 for { 217 atyp, ok := typ.Underlying().(*types.Array) 218 if !ok { 219 break 220 } 221 typ = atyp.Elem() 222 } 223 224 // We're only interested in the case in which the underlying 225 // type is a struct. (Interfaces and pointers are safe to copy.) 226 styp, ok := typ.Underlying().(*types.Struct) 227 if !ok { 228 return nil 229 } 230 231 // We're looking for cases in which a reference to this type 232 // can be locked, but a value cannot. This differentiates 233 // embedded interfaces from embedded values. 234 if plock := types.NewMethodSet(types.NewPointer(typ)).Lookup(tpkg, "Lock"); plock != nil { 235 if lock := types.NewMethodSet(typ).Lookup(tpkg, "Lock"); lock == nil { 236 return []types.Type{typ} 237 } 238 } 239 240 nfields := styp.NumFields() 241 for i := 0; i < nfields; i++ { 242 ftyp := styp.Field(i).Type() 243 subpath := lockPath(tpkg, ftyp) 244 if subpath != nil { 245 return append(subpath, typ) 246 } 247 } 248 249 return nil 250 }