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/pointer: Pointer MayAlias reports false, when it should report true #46280

Closed
chabbimilind opened this issue May 20, 2021 · 3 comments
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) 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

@chabbimilind
Copy link

chabbimilind commented May 20, 2021

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

$ go version
1.15.5

Does this issue reproduce with the latest release?

Did not check, but there were no fixes in the package addressing this issue.

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

go env Output
$ go env
go version go1.15.5 darwin/amd64
milind-C02X40F1HX8F:alias milind$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/milind/Library/Caches/go-build"
GOENV="/Users/milind/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/milind/gocode/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/milind/gocode"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/xr/hsc7mk1n5glg6vfqb4z5nlkh0000gn/T/go-build659129965=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I have the following code (you need https://github.com/uber-go/tally/ ) named bug.go in that attached repro.zip, which contains all files needed for reproducing.

package main

import (
        "fmt"
        "unsafe"
        tally "github.com/uber-go/tally")
)

func main() {
        x := tally.Scope.Counter
        lst := []unsafe.Pointer{(unsafe.Pointer)(&x)}
        fmt.Printf("&v", lst)
}

Scope is an interface in tally and Counter function looks like this

func (s *scope) Counter(name string) Counter {
	name = s.sanitizer.Name(name)
	if c, ok := s.counter(name); ok {
		return c
	}
	s.cm.Lock()
	defer s.cm.Unlock()
 ...

The reason to take the address of tally.Scope.Counter in main is only to make it appear as if the Counter function inside tally is reachable.

I have written a pointer analysis driver (pointsto_bug.go) (feed the above file to this driver) that is trying to check if the two instances of s.cm used inside Counter at Lock() and Unlock() "May Alias".

The driver collects all values on which the program may be calling Lock/Unlock/Rlock/RUnlock to feed to the Analyze() function of the pointer package.
Additionally, the two sync.Mutex values, on which Lock and Unlock are called, are also remembered (in p1 and p2).
Then, in the Result provided by Analyze(), I query if p1.MayAlias(p2).

However, the pointer analysis reports “false”.

What did you expect to see?

The expectation is that the MayAlias report true, in fact here they are the same objects.

What did you see instead?

MayAlias reports false

Addendum

Instead of using the large tally library, if you either create a small Scope interface inside themain package (example nobug.go)

, the function Counter is not listed in the set of ssautil.AllFunctions(prog). The same holds true if I carve out a small package with just a single Scope interface and a Counter function implementation.
repro.zip

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

CC @golang/tools-team via owners.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 21, 2021
@timothy-king timothy-king added the Analysis Issues related to static analysis (vet, x/tools/go/analysis) label Jun 28, 2022
@timothy-king
Copy link
Contributor

In the small example in the Addendum, ssautil.AllFunctions(prog) can safely eliminate the type scope as not being reachable using a linker style argument. So no real surprises that (*scope).Counter() is skipped.

I'll try to investigate the other examples.

@adonovan
Copy link
Member

x/tools/go/pointer was deleted; see #59676.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) 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

5 participants