Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/go/analysis/passes/copylock: does not detect tuple copies #45896

Closed
ajwans opened this issue May 1, 2021 · 7 comments
Closed

x/tools/go/analysis/passes/copylock: does not detect tuple copies #45896

ajwans opened this issue May 1, 2021 · 7 comments
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@ajwans
Copy link

ajwans commented May 1, 2021

What version of Go are you using (go version)?

$ go version
go version go1.15.2 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/gocode/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user/gocode"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/go/root"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/go/root/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/user/Src/go/src/cmd/vendor/golang.org/x/tools/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build923664395=/tmp/go-build -gno-record-gcc-switches"

What did you do?

# cat >problem.go <<EOF
package main

import (
    "sync"
)

type mu struct {
    m sync.Mutex
}

func main() {
    m := map[string]mu{}
    m["entry"] = mu{}

    _, _ = m["entry"]
}
EOF
# go vet problem.go
# echo $?
0

What did you expect to see?

An error indicating that it is a bad idea to copy a mutex.

What did you see instead?

Success

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label May 1, 2021
@gopherbot gopherbot added this to the Unreleased milestone May 1, 2021
@ajwans
Copy link
Author

ajwans commented May 1, 2021

I have an idea for a fix for this problem

diff --git a/go/analysis/passes/copylock/copylock.go b/go/analysis/passes/copylock/copylock.go
index c4ebf785..e856a14c 100644
--- a/go/analysis/passes/copylock/copylock.go
+++ b/go/analysis/passes/copylock/copylock.go
@@ -252,6 +252,17 @@ func lockPath(tpkg *types.Package, typ types.Type) typePath {
                typ = atyp.Elem()
        }
 
+       ttyp, ok := typ.Underlying().(*types.Tuple)
+       if ok {
+               for i := 0; i < ttyp.Len(); i++ {
+                       subpath := lockPath(tpkg, ttyp.At(i).Type())
+                       if subpath != nil {
+                               return append(subpath, typ)
+                       }
+               }
+               return nil
+       }
+

But I could use some help with directions on how to test the solution. I have stepped through it using dlv and it seems to do the right thing but some pointers for writing and running the unit tests would be helpful.

@cagedmantis cagedmantis changed the title x/tools/go/analysis/passes/copylock does not detect tuple copies x/tools/go/analysis/passes/copylock: does not detect tuple copies May 3, 2021
@stamblerre
Copy link
Contributor

stamblerre commented May 3, 2021

@ajwans: You should be able to run the unit tests by running the copylock_test in the copylock directory. The test cases themselves can be found here: https://github.com/golang/tools/tree/master/go/analysis/passes/copylock/testdata/src/a. Each want comment indicates a diagnostic that should be reported at the given line, so you can add your repro case to one of those files and add a // want diagnostic where you expect it to add a test case.

@timothy-king timothy-king added the Analysis Issues related to static analysis (vet, x/tools/go/analysis) label May 3, 2021
@timothy-king
Copy link
Contributor

It would be good to also test this change on functions returning multiple values. Function returns can be a bit tricky: https://github.com/golang/tools/blob/master/go/analysis/passes/copylock/copylock.go#L162-L165

Tests for these could go in:
https://github.com/golang/tools/blob/master/go/analysis/passes/copylock/testdata/src/a/copylock_func.go

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 4, 2021
@ajwans
Copy link
Author

ajwans commented May 30, 2021

It would be good to also test this change on functions returning multiple values. Function returns can be a bit tricky: https://github.com/golang/tools/blob/master/go/analysis/passes/copylock/copylock.go#L162-L165

Tests for these could go in:
https://github.com/golang/tools/blob/master/go/analysis/passes/copylock/testdata/src/a/copylock_func.go

I looked into making this suggested change. It looks like this case is already considered and handled. The comments at copylocks.go:162 & copylocks.go:228 indicate that it not an error because a function is allowed to return a T{} with an empty lock.

@ajwans
Copy link
Author

ajwans commented May 30, 2021

I created a PR for this golang/tools#319

@ZekeLu
Copy link
Contributor

ZekeLu commented May 11, 2022

golang/tools#319 has been merged and I think this issue can be closed. (the referenced issue in the Fixes line is Fixes #45896).

@timothy-king
Copy link
Contributor

Looks closed to me too. I believe the Fixes #45896 message was just mis formatted for the gopherbot on an x/tools change.

@golang golang locked and limited conversation to collaborators May 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

6 participants