golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.91k stars 17.52k forks source link

x/tools/go/ssa/interp: panic: no code for function: time.runtimeNano on darwin #30881

Open ajalab opened 5 years ago

ajalab commented 5 years ago

What did you do?

main.go

package main

import (
    "go/types"
    "log"

    "golang.org/x/tools/go/packages"
    "golang.org/x/tools/go/ssa"
    "golang.org/x/tools/go/ssa/interp"
    "golang.org/x/tools/go/ssa/ssautil"
)

func main() {
    target := "./testdata"

    cfg := &packages.Config{
        Mode:  packages.LoadAllSyntax,
        Tests: false,
    }

    pkgs, err := packages.Load(cfg, target)
    if err != nil {
        log.Fatalf("failed to load package: %v", err)
        return
    }

    ssaProg, ssaPkgs := ssautil.AllPackages(pkgs, 0)
    ssaProg.Build()

    var mainPkg *ssa.Package
    for _, ssaPkg := range ssaPkgs {
        if ssaPkg.Pkg.Name() == "main" {
            mainPkg = ssaPkg
        }
    }

    mode := interp.EnableTracing
    interp.Interpret(mainPkg, mode, &types.StdSizes{WordSize: 8, MaxAlign: 8}, "main", []string{})
}

testdata/t.go

package main

import "fmt"

func main() {
    fmt.Println("hello")
}

go.mod

module github.com/ajalab/ssainterp

go 1.12

require golang.org/x/tools v0.0.0-20190315214010-f0bfdbff1f9c

What did you expect to see?

The program prints "hello" to stdout.

What did you see instead?

The program panics.

Entering time.runtimeNano at /usr/local/go/src/time/time.go:1076:6.
Leaving time.runtimeNano, resuming time.init at /usr/local/go/src/time/time.go:1084:34.
Panicking: string no code for function: time.runtimeNano.
Leaving time.init, resuming internal/poll.init.
Panicking: string no code for function: time.runtimeNano.
Leaving internal/poll.init, resuming os.init.
Panicking: string no code for function: time.runtimeNano.
Leaving os.init, resuming fmt.init.
Panicking: string no code for function: time.runtimeNano.
Leaving fmt.init, resuming github.com/ajalab/ssainterp/testdata.init.
Panicking: string no code for function: time.runtimeNano.
Leaving github.com/ajalab/ssainterp/testdata.init.
panic: no code for function: time.runtimeNano

Does this issue reproduce with the latest release (go1.12.1)?

yes

System details

go version go1.12.1 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/koki/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/koki/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/koki/prj/ssainterp/go.mod"
GOROOT/bin/go version: go version go1.12.1 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.12.1
uname -v: Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; root:xnu-4903.241.1~1/RELEASE_X86_64
ProductName:    Mac OS X
ProductVersion: 10.14.3
BuildVersion:   18D42
lldb --version: lldb-1000.0.38.2
  Swift-4.2
ajalab commented 5 years ago

I didn't see the error in the previous Go version.

It seems that we have to add more external functions in externals in external/darwin.go. I confirmed that adding emulated functions for time.runtimeNano, runtime.envKeyEqual and internal/syscall/unix.fcntl prevents the panic caused in the above code, but I am not sure that all the causes are these three functions or this situation will happen in other OSs.

agnivade commented 5 years ago

@randall77 for darwin. @alandonovan for go/ssa

alandonovan commented 5 years ago

I finally got around to doing what I've been putting off for years: https://go-review.googlesource.com/c/tools/+/168898