| 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 | // Package copylock defines an Analyzer that checks for locks |
| 6 | // erroneously passed by value. |
| 7 | package copylock |
| 8 | |
| 9 | import ( |
| 10 | "bytes" |
| 11 | "fmt" |
| 12 | "go/ast" |
| 13 | "go/token" |
| 14 | "go/types" |
| 15 | |
| 16 | "golang.org/x/tools/go/analysis" |
| 17 | "golang.org/x/tools/go/analysis/passes/inspect" |
| 18 | "golang.org/x/tools/go/ast/inspector" |
| 19 | "golang.org/x/tools/internal/astutil" |
| 20 | "golang.org/x/tools/internal/typeparams" |
| 21 | "golang.org/x/tools/internal/typesinternal" |
| 22 | "golang.org/x/tools/internal/versions" |
| 23 | ) |
| 24 | |
| 25 | const Doc = `check for locks erroneously passed by value |
| 26 | |
| 27 | Inadvertently copying a value containing a lock, such as sync.Mutex or |
| 28 | sync.WaitGroup, may cause both copies to malfunction. Generally such |
| 29 | values should be referred to through a pointer.` |
| 30 | |
| 31 | var Analyzer = &analysis.Analyzer{ |
| 32 | Name: "copylocks", |
| 33 | Doc: Doc, |
| 34 | URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/copylock", |
| 35 | Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 36 | RunDespiteErrors: true, |
| 37 | Run: run, |
| 38 | } |
| 39 | |
| 40 | func run(pass *analysis.Pass) (any, error) { |
| 41 | inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 42 | |
| 43 | var goversion string // effective file version ("" => unknown) |
| 44 | nodeFilter := []ast.Node{ |
| 45 | (*ast.AssignStmt)(nil), |
| 46 | (*ast.CallExpr)(nil), |
| 47 | (*ast.CompositeLit)(nil), |
| 48 | (*ast.File)(nil), |
| 49 | (*ast.FuncDecl)(nil), |
| 50 | (*ast.FuncLit)(nil), |
| 51 | (*ast.GenDecl)(nil), |
| 52 | (*ast.RangeStmt)(nil), |
| 53 | (*ast.ReturnStmt)(nil), |
| 54 | } |
| 55 | inspect.WithStack(nodeFilter, func(node ast.Node, push bool, stack []ast.Node) bool { |
| 56 | if !push { |
| 57 | return false |
| 58 | } |
| 59 | switch node := node.(type) { |
| 60 | case *ast.File: |
| 61 | goversion = versions.FileVersion(pass.TypesInfo, node) |
| 62 | case *ast.RangeStmt: |
| 63 | checkCopyLocksRange(pass, node) |
| 64 | case *ast.FuncDecl: |
| 65 | checkCopyLocksFunc(pass, node.Name.Name, node.Recv, node.Type) |
| 66 | case *ast.FuncLit: |
| 67 | checkCopyLocksFunc(pass, "func", nil, node.Type) |
| 68 | case *ast.CallExpr: |
| 69 | checkCopyLocksCallExpr(pass, node) |
| 70 | case *ast.AssignStmt: |
| 71 | checkCopyLocksAssign(pass, node, goversion, parent(stack)) |
| 72 | case *ast.GenDecl: |
| 73 | checkCopyLocksGenDecl(pass, node) |
| 74 | case *ast.CompositeLit: |
| 75 | checkCopyLocksCompositeLit(pass, node) |
| 76 | case *ast.ReturnStmt: |
| 77 | checkCopyLocksReturnStmt(pass, node) |
| 78 | } |
| 79 | return true |
| 80 | }) |
| 81 | return nil, nil |
| 82 | } |
| 83 | |
| 84 | // checkCopyLocksAssign checks whether an assignment |
| 85 | // copies a lock. |
| 86 | func checkCopyLocksAssign(pass *analysis.Pass, assign *ast.AssignStmt, goversion string, parent ast.Node) { |
| 87 | lhs := assign.Lhs |
| 88 | for i, x := range assign.Rhs { |
| 89 | if path := lockPathRhs(pass, x); path != nil { |
| 90 | pass.ReportRangef(x, "assignment copies lock value to %v: %v", astutil.Format(pass.Fset, assign.Lhs[i]), path) |
| 91 | lhs = nil // An lhs has been reported. We prefer the assignment warning and do not report twice. |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // After GoVersion 1.22, loop variables are implicitly copied on each iteration. |
| 96 | // So a for statement may inadvertently copy a lock when any of the |
| 97 | // iteration variables contain locks. |
| 98 | if assign.Tok == token.DEFINE && versions.AtLeast(goversion, versions.Go1_22) { |
| 99 | if parent, _ := parent.(*ast.ForStmt); parent != nil && parent.Init == assign { |
| 100 | for _, l := range lhs { |
| 101 | if id, ok := l.(*ast.Ident); ok && id.Name != "_" { |
| 102 | if obj := pass.TypesInfo.Defs[id]; obj != nil && obj.Type() != nil { |
| 103 | if path := lockPath(pass.Pkg, obj.Type(), nil); path != nil { |
| 104 | pass.ReportRangef(l, "for loop iteration copies lock value to %v: %v", astutil.Format(pass.Fset, l), path) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // checkCopyLocksGenDecl checks whether lock is copied |
| 114 | // in variable declaration. |
| 115 | func checkCopyLocksGenDecl(pass *analysis.Pass, gd *ast.GenDecl) { |
| 116 | if gd.Tok != token.VAR { |
| 117 | return |
| 118 | } |
| 119 | for _, spec := range gd.Specs { |
| 120 | valueSpec := spec.(*ast.ValueSpec) |
| 121 | for i, x := range valueSpec.Values { |
| 122 | if path := lockPathRhs(pass, x); path != nil { |
| 123 | pass.ReportRangef(x, "variable declaration copies lock value to %v: %v", valueSpec.Names[i].Name, path) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // checkCopyLocksCompositeLit detects lock copy inside a composite literal |
| 130 | func checkCopyLocksCompositeLit(pass *analysis.Pass, cl *ast.CompositeLit) { |
| 131 | for _, x := range cl.Elts { |
| 132 | if node, ok := x.(*ast.KeyValueExpr); ok { |
| 133 | x = node.Value |
| 134 | } |
| 135 | if path := lockPathRhs(pass, x); path != nil { |
| 136 | pass.ReportRangef(x, "literal copies lock value from %v: %v", astutil.Format(pass.Fset, x), path) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // checkCopyLocksReturnStmt detects lock copy in return statement |
| 142 | func checkCopyLocksReturnStmt(pass *analysis.Pass, rs *ast.ReturnStmt) { |
| 143 | for _, x := range rs.Results { |
| 144 | if path := lockPathRhs(pass, x); path != nil { |
| 145 | pass.ReportRangef(x, "return copies lock value: %v", path) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // checkCopyLocksCallExpr detects lock copy in the arguments to a function call |
| 151 | func checkCopyLocksCallExpr(pass *analysis.Pass, ce *ast.CallExpr) { |
| 152 | var id *ast.Ident |
| 153 | switch fun := ce.Fun.(type) { |
| 154 | case *ast.Ident: |
| 155 | id = fun |
| 156 | case *ast.SelectorExpr: |
| 157 | id = fun.Sel |
| 158 | } |
| 159 | if fun, ok := pass.TypesInfo.Uses[id].(*types.Builtin); ok { |
| 160 | switch fun.Name() { |
| 161 | case "len", "cap", "Sizeof", "Offsetof", "Alignof": |
| 162 | // The argument of this operation is used only |
| 163 | // for its type (e.g. len(array)), or the operation |
| 164 | // does not copy a lock (e.g. len(slice)). |
| 165 | return |
| 166 | } |
| 167 | } |
| 168 | for _, x := range ce.Args { |
| 169 | if path := lockPathRhs(pass, x); path != nil { |
| 170 | pass.ReportRangef(x, "call of %s copies lock value: %v", astutil.Format(pass.Fset, ce.Fun), path) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // checkCopyLocksFunc checks whether a function might |
| 176 | // inadvertently copy a lock, by checking whether |
| 177 | // its receiver, parameters, or return values |
| 178 | // are locks. |
| 179 | func checkCopyLocksFunc(pass *analysis.Pass, name string, recv *ast.FieldList, typ *ast.FuncType) { |
| 180 | if recv != nil && len(recv.List) > 0 { |
| 181 | expr := recv.List[0].Type |
| 182 | if path := lockPath(pass.Pkg, pass.TypesInfo.Types[expr].Type, nil); path != nil { |
| 183 | pass.ReportRangef(expr, "%s passes lock by value: %v", name, path) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if typ.Params != nil { |
| 188 | for _, field := range typ.Params.List { |
| 189 | expr := field.Type |
| 190 | if path := lockPath(pass.Pkg, pass.TypesInfo.Types[expr].Type, nil); path != nil { |
| 191 | pass.ReportRangef(expr, "%s passes lock by value: %v", name, path) |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Don't check typ.Results. If T has a Lock field it's OK to write |
| 197 | // return T{} |
| 198 | // because that is returning the zero value. Leave result checking |
| 199 | // to the return statement. |
| 200 | } |
| 201 | |
| 202 | // checkCopyLocksRange checks whether a range statement |
| 203 | // might inadvertently copy a lock by checking whether |
| 204 | // any of the range variables are locks. |
| 205 | func checkCopyLocksRange(pass *analysis.Pass, r *ast.RangeStmt) { |
| 206 | checkCopyLocksRangeVar(pass, r.Tok, r.Key) |
| 207 | checkCopyLocksRangeVar(pass, r.Tok, r.Value) |
| 208 | } |
| 209 | |
| 210 | func checkCopyLocksRangeVar(pass *analysis.Pass, rtok token.Token, e ast.Expr) { |
| 211 | if e == nil { |
| 212 | return |
| 213 | } |
| 214 | id, isId := e.(*ast.Ident) |
| 215 | if isId && id.Name == "_" { |
| 216 | return |
| 217 | } |
| 218 | |
| 219 | var typ types.Type |
| 220 | if rtok == token.DEFINE { |
| 221 | if !isId { |
| 222 | return |
| 223 | } |
| 224 | obj := pass.TypesInfo.Defs[id] |
| 225 | if obj == nil { |
| 226 | return |
| 227 | } |
| 228 | typ = obj.Type() |
| 229 | } else { |
| 230 | typ = pass.TypesInfo.Types[e].Type |
| 231 | } |
| 232 | |
| 233 | if typ == nil { |
| 234 | return |
| 235 | } |
| 236 | if path := lockPath(pass.Pkg, typ, nil); path != nil { |
| 237 | pass.Reportf(e.Pos(), "range var %s copies lock: %v", astutil.Format(pass.Fset, e), path) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | type typePath []string |
| 242 | |
| 243 | // String pretty-prints a typePath. |
| 244 | func (path typePath) String() string { |
| 245 | n := len(path) |
| 246 | var buf bytes.Buffer |
| 247 | for i := range path { |
| 248 | if i > 0 { |
| 249 | fmt.Fprint(&buf, " contains ") |
| 250 | } |
| 251 | // The human-readable path is in reverse order, outermost to innermost. |
| 252 | fmt.Fprint(&buf, path[n-i-1]) |
| 253 | } |
| 254 | return buf.String() |
| 255 | } |
| 256 | |
| 257 | func lockPathRhs(pass *analysis.Pass, x ast.Expr) typePath { |
| 258 | x = ast.Unparen(x) // ignore parens on rhs |
| 259 | |
| 260 | if _, ok := x.(*ast.CompositeLit); ok { |
| 261 | return nil |
| 262 | } |
| 263 | if _, ok := x.(*ast.CallExpr); ok { |
| 264 | // A call may return a zero value. |
| 265 | return nil |
| 266 | } |
| 267 | if star, ok := x.(*ast.StarExpr); ok { |
| 268 | if _, ok := ast.Unparen(star.X).(*ast.CallExpr); ok { |
| 269 | // A call may return a pointer to a zero value. |
| 270 | return nil |
| 271 | } |
| 272 | } |
| 273 | if tv, ok := pass.TypesInfo.Types[x]; ok && tv.IsValue() { |
| 274 | return lockPath(pass.Pkg, tv.Type, nil) |
| 275 | } |
| 276 | return nil |
| 277 | } |
| 278 | |
| 279 | // lockPath returns a typePath describing the location of a lock value |
| 280 | // contained in typ. If there is no contained lock, it returns nil. |
| 281 | // |
| 282 | // The seen map is used to short-circuit infinite recursion due to type cycles. |
| 283 | func lockPath(tpkg *types.Package, typ types.Type, seen map[types.Type]bool) typePath { |
| 284 | if typ == nil || seen[typ] { |
| 285 | return nil |
| 286 | } |
| 287 | if seen == nil { |
| 288 | seen = make(map[types.Type]bool) |
| 289 | } |
| 290 | seen[typ] = true |
| 291 | |
| 292 | if tpar, ok := types.Unalias(typ).(*types.TypeParam); ok { |
| 293 | terms, err := typeparams.StructuralTerms(tpar) |
| 294 | if err != nil { |
| 295 | return nil // invalid type |
| 296 | } |
| 297 | for _, term := range terms { |
| 298 | subpath := lockPath(tpkg, term.Type(), seen) |
| 299 | if len(subpath) > 0 { |
| 300 | if term.Tilde() { |
| 301 | // Prepend a tilde to our lock path entry to clarify the resulting |
| 302 | // diagnostic message. Consider the following example: |
| 303 | // |
| 304 | // func _[Mutex interface{ ~sync.Mutex; M() }](m Mutex) {} |
| 305 | // |
| 306 | // Here the naive error message will be something like "passes lock |
| 307 | // by value: Mutex contains sync.Mutex". This is misleading because |
| 308 | // the local type parameter doesn't actually contain sync.Mutex, |
| 309 | // which lacks the M method. |
| 310 | // |
| 311 | // With tilde, it is clearer that the containment is via an |
| 312 | // approximation element. |
| 313 | subpath[len(subpath)-1] = "~" + subpath[len(subpath)-1] |
| 314 | } |
| 315 | return append(subpath, typ.String()) |
| 316 | } |
| 317 | } |
| 318 | return nil |
| 319 | } |
| 320 | |
| 321 | for { |
| 322 | atyp, ok := typ.Underlying().(*types.Array) |
| 323 | if !ok { |
| 324 | break |
| 325 | } |
| 326 | typ = atyp.Elem() |
| 327 | } |
| 328 | |
| 329 | ttyp, ok := typ.Underlying().(*types.Tuple) |
| 330 | if ok { |
| 331 | for v := range ttyp.Variables() { |
| 332 | subpath := lockPath(tpkg, v.Type(), seen) |
| 333 | if subpath != nil { |
| 334 | return append(subpath, typ.String()) |
| 335 | } |
| 336 | } |
| 337 | return nil |
| 338 | } |
| 339 | |
| 340 | // We're only interested in the case in which the underlying |
| 341 | // type is a struct. (Interfaces and pointers are safe to copy.) |
| 342 | styp, ok := typ.Underlying().(*types.Struct) |
| 343 | if !ok { |
| 344 | return nil |
| 345 | } |
| 346 | |
| 347 | // We're looking for cases in which a pointer to this type |
| 348 | // is a sync.Locker, but a value is not. This differentiates |
| 349 | // embedded interfaces from embedded values. |
| 350 | if types.Implements(types.NewPointer(typ), lockerType) && !types.Implements(typ, lockerType) { |
| 351 | return []string{typ.String()} |
| 352 | } |
| 353 | |
| 354 | // In go1.10, sync.noCopy did not implement Locker. |
| 355 | // (The Unlock method was added only in CL 121876.) |
| 356 | // TODO(adonovan): remove workaround when we drop go1.10. |
| 357 | if typesinternal.IsTypeNamed(typ, "sync", "noCopy") { |
| 358 | return []string{typ.String()} |
| 359 | } |
| 360 | |
| 361 | nfields := styp.NumFields() |
| 362 | for i := range nfields { |
| 363 | ftyp := styp.Field(i).Type() |
| 364 | subpath := lockPath(tpkg, ftyp, seen) |
| 365 | if subpath != nil { |
| 366 | return append(subpath, typ.String()) |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return nil |
| 371 | } |
| 372 | |
| 373 | // parent returns the second from the last node on stack if it exists. |
| 374 | func parent(stack []ast.Node) ast.Node { |
| 375 | if len(stack) >= 2 { |
| 376 | return stack[len(stack)-2] |
| 377 | } |
| 378 | return nil |
| 379 | } |
| 380 | |
| 381 | var lockerType *types.Interface |
| 382 | |
| 383 | // Construct a sync.Locker interface type. |
| 384 | func init() { |
| 385 | nullary := types.NewSignatureType(nil, nil, nil, nil, nil, false) // func() |
| 386 | methods := []*types.Func{ |
| 387 | types.NewFunc(token.NoPos, nil, "Lock", nullary), |
| 388 | types.NewFunc(token.NoPos, nil, "Unlock", nullary), |
| 389 | } |
| 390 | lockerType = types.NewInterface(methods, nil).Complete() |
| 391 | } |