2015-06-04 13:54:58 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// This file contains the code to check that locks are not passed by value.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2015-06-04 14:07:22 -06:00
|
|
|
"go/types"
|
2015-06-04 13:54:58 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register("copylocks",
|
|
|
|
"check that locks are not passed by value",
|
|
|
|
checkCopyLocks,
|
2016-03-18 03:25:04 -06:00
|
|
|
funcDecl, rangeStmt, funcLit, callExpr, assignStmt, genDecl, compositeLit, returnStmt)
|
2015-06-04 13:54:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkCopyLocks checks whether node might
|
|
|
|
// inadvertently copy a lock.
|
|
|
|
func checkCopyLocks(f *File, node ast.Node) {
|
|
|
|
switch node := node.(type) {
|
|
|
|
case *ast.RangeStmt:
|
|
|
|
checkCopyLocksRange(f, node)
|
|
|
|
case *ast.FuncDecl:
|
2015-06-12 20:49:45 -06:00
|
|
|
checkCopyLocksFunc(f, node.Name.Name, node.Recv, node.Type)
|
|
|
|
case *ast.FuncLit:
|
|
|
|
checkCopyLocksFunc(f, "func", nil, node.Type)
|
2016-03-18 03:25:04 -06:00
|
|
|
case *ast.CallExpr:
|
|
|
|
checkCopyLocksCallExpr(f, node)
|
2015-08-15 18:50:17 -06:00
|
|
|
case *ast.AssignStmt:
|
|
|
|
checkCopyLocksAssign(f, node)
|
2016-03-05 17:21:08 -07:00
|
|
|
case *ast.GenDecl:
|
|
|
|
checkCopyLocksGenDecl(f, node)
|
|
|
|
case *ast.CompositeLit:
|
2016-03-18 03:25:04 -06:00
|
|
|
checkCopyLocksCompositeLit(f, node)
|
|
|
|
case *ast.ReturnStmt:
|
|
|
|
checkCopyLocksReturnStmt(f, node)
|
2015-08-15 18:50:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkCopyLocksAssign checks whether an assignment
|
|
|
|
// copies a lock.
|
|
|
|
func checkCopyLocksAssign(f *File, as *ast.AssignStmt) {
|
2016-03-05 17:21:08 -07:00
|
|
|
for i, x := range as.Rhs {
|
|
|
|
if path := lockPathRhs(f, x); path != nil {
|
|
|
|
f.Badf(x.Pos(), "assignment copies lock value to %v: %v", f.gofmt(as.Lhs[i]), path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkCopyLocksGenDecl checks whether lock is copied
|
|
|
|
// in variable declaration.
|
|
|
|
func checkCopyLocksGenDecl(f *File, gd *ast.GenDecl) {
|
|
|
|
if gd.Tok != token.VAR {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, spec := range gd.Specs {
|
|
|
|
valueSpec := spec.(*ast.ValueSpec)
|
|
|
|
for i, x := range valueSpec.Values {
|
|
|
|
if path := lockPathRhs(f, x); path != nil {
|
|
|
|
f.Badf(x.Pos(), "variable declaration copies lock value to %v: %v", valueSpec.Names[i].Name, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 03:25:04 -06:00
|
|
|
// checkCopyLocksCompositeLit detects lock copy inside a composite literal
|
|
|
|
func checkCopyLocksCompositeLit(f *File, cl *ast.CompositeLit) {
|
2016-03-05 17:21:08 -07:00
|
|
|
for _, x := range cl.Elts {
|
|
|
|
if node, ok := x.(*ast.KeyValueExpr); ok {
|
|
|
|
x = node.Value
|
|
|
|
}
|
|
|
|
if path := lockPathRhs(f, x); path != nil {
|
|
|
|
f.Badf(x.Pos(), "literal copies lock value from %v: %v", f.gofmt(x), path)
|
2015-08-15 18:50:17 -06:00
|
|
|
}
|
2015-06-04 13:54:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 03:25:04 -06:00
|
|
|
// checkCopyLocksReturnStmt detects lock copy in return statement
|
|
|
|
func checkCopyLocksReturnStmt(f *File, rs *ast.ReturnStmt) {
|
|
|
|
for _, x := range rs.Results {
|
|
|
|
if path := lockPathRhs(f, x); path != nil {
|
|
|
|
f.Badf(x.Pos(), "return copies lock value: %v", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 12:08:15 -06:00
|
|
|
// checkCopyLocksCallExpr detects lock copy in the arguments to a function call
|
2016-03-18 03:25:04 -06:00
|
|
|
func checkCopyLocksCallExpr(f *File, ce *ast.CallExpr) {
|
2016-03-28 12:08:15 -06:00
|
|
|
if id, ok := ce.Fun.(*ast.Ident); ok && id.Name == "new" && f.pkg.types[id].IsBuiltin() {
|
|
|
|
// Skip 'new(Type)' for built-in 'new'
|
|
|
|
return
|
|
|
|
}
|
2016-03-18 03:25:04 -06:00
|
|
|
for _, x := range ce.Args {
|
|
|
|
if path := lockPathRhs(f, x); path != nil {
|
|
|
|
f.Badf(x.Pos(), "function call copies lock value: %v", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-04 13:54:58 -06:00
|
|
|
// checkCopyLocksFunc checks whether a function might
|
|
|
|
// inadvertently copy a lock, by checking whether
|
|
|
|
// its receiver, parameters, or return values
|
|
|
|
// are locks.
|
2015-06-12 20:49:45 -06:00
|
|
|
func checkCopyLocksFunc(f *File, name string, recv *ast.FieldList, typ *ast.FuncType) {
|
|
|
|
if recv != nil && len(recv.List) > 0 {
|
|
|
|
expr := recv.List[0].Type
|
2015-06-04 13:54:58 -06:00
|
|
|
if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil {
|
2015-08-15 18:50:17 -06:00
|
|
|
f.Badf(expr.Pos(), "%s passes lock by value: %v", name, path)
|
2015-06-04 13:54:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-12 20:49:45 -06:00
|
|
|
if typ.Params != nil {
|
|
|
|
for _, field := range typ.Params.List {
|
2015-06-04 13:54:58 -06:00
|
|
|
expr := field.Type
|
|
|
|
if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil {
|
2015-08-15 18:50:17 -06:00
|
|
|
f.Badf(expr.Pos(), "%s passes lock by value: %v", name, path)
|
2015-06-04 13:54:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 14:52:26 -06:00
|
|
|
// Don't check typ.Results. If T has a Lock field it's OK to write
|
|
|
|
// return T{}
|
|
|
|
// because that is returning the zero value. Leave result checking
|
|
|
|
// to the return statement.
|
2015-06-04 13:54:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkCopyLocksRange checks whether a range statement
|
|
|
|
// might inadvertently copy a lock by checking whether
|
|
|
|
// any of the range variables are locks.
|
|
|
|
func checkCopyLocksRange(f *File, r *ast.RangeStmt) {
|
|
|
|
checkCopyLocksRangeVar(f, r.Tok, r.Key)
|
|
|
|
checkCopyLocksRangeVar(f, r.Tok, r.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCopyLocksRangeVar(f *File, rtok token.Token, e ast.Expr) {
|
|
|
|
if e == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id, isId := e.(*ast.Ident)
|
|
|
|
if isId && id.Name == "_" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var typ types.Type
|
|
|
|
if rtok == token.DEFINE {
|
|
|
|
if !isId {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obj := f.pkg.defs[id]
|
|
|
|
if obj == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
typ = obj.Type()
|
|
|
|
} else {
|
|
|
|
typ = f.pkg.types[e].Type
|
|
|
|
}
|
|
|
|
|
|
|
|
if typ == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if path := lockPath(f.pkg.typesPkg, typ); path != nil {
|
2015-08-15 18:50:17 -06:00
|
|
|
f.Badf(e.Pos(), "range var %s copies lock: %v", f.gofmt(e), path)
|
2015-06-04 13:54:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type typePath []types.Type
|
|
|
|
|
|
|
|
// String pretty-prints a typePath.
|
|
|
|
func (path typePath) String() string {
|
|
|
|
n := len(path)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for i := range path {
|
|
|
|
if i > 0 {
|
|
|
|
fmt.Fprint(&buf, " contains ")
|
|
|
|
}
|
|
|
|
// The human-readable path is in reverse order, outermost to innermost.
|
|
|
|
fmt.Fprint(&buf, path[n-i-1].String())
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2016-03-05 17:21:08 -07:00
|
|
|
func lockPathRhs(f *File, x ast.Expr) typePath {
|
|
|
|
if _, ok := x.(*ast.CompositeLit); ok {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-01 14:52:26 -06:00
|
|
|
if _, ok := x.(*ast.CallExpr); ok {
|
|
|
|
// A call may return a zero value.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if star, ok := x.(*ast.StarExpr); ok {
|
|
|
|
if _, ok := star.X.(*ast.CallExpr); ok {
|
|
|
|
// A call may return a pointer to a zero value.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-03-05 17:21:08 -07:00
|
|
|
return lockPath(f.pkg.typesPkg, f.pkg.types[x].Type)
|
|
|
|
}
|
|
|
|
|
2015-06-04 13:54:58 -06:00
|
|
|
// lockPath returns a typePath describing the location of a lock value
|
|
|
|
// contained in typ. If there is no contained lock, it returns nil.
|
|
|
|
func lockPath(tpkg *types.Package, typ types.Type) typePath {
|
|
|
|
if typ == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're only interested in the case in which the underlying
|
|
|
|
// type is a struct. (Interfaces and pointers are safe to copy.)
|
|
|
|
styp, ok := typ.Underlying().(*types.Struct)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're looking for cases in which a reference to this type
|
|
|
|
// can be locked, but a value cannot. This differentiates
|
|
|
|
// embedded interfaces from embedded values.
|
|
|
|
if plock := types.NewMethodSet(types.NewPointer(typ)).Lookup(tpkg, "Lock"); plock != nil {
|
|
|
|
if lock := types.NewMethodSet(typ).Lookup(tpkg, "Lock"); lock == nil {
|
|
|
|
return []types.Type{typ}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nfields := styp.NumFields()
|
|
|
|
for i := 0; i < nfields; i++ {
|
|
|
|
ftyp := styp.Field(i).Type()
|
|
|
|
subpath := lockPath(tpkg, ftyp)
|
|
|
|
if subpath != nil {
|
|
|
|
return append(subpath, typ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|