Skip to content

Bad runtime.Stack() content in defer on Linux amd64 #47053

Closed
@maxatome

Description

@maxatome

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

$ go version
go version devel go1.17-912f075 Fri Jul 2 21:06:08 2021 +0000 linux/amd64

Does this issue reproduce with the latest release?

No, only with tip on Linux amd64 (Debian 10.9).

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

Linux x86_64 (Debian 10.9)

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/root/sdk/gotip"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/root/sdk/gotip/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel go1.17-912f075 Fri Jul 2 21:06:08 2021 +0000"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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-build680001886=/tmp/go-build -gno-record-gcc-switches"

What did you do?

x.go file:

package main

import (
	"fmt"
	"runtime"
)

func dumpStack() {
	var buf [8192]byte
	n := runtime.Stack(buf[:], false)
	fmt.Print(string(buf[:n]))
}

func setVal(i int64) {
	defer dumpStack()
}

func main() {
	setVal(1)
}

then go run x.go

What did you expect to see?

go 1.16.5 prints:

goroutine 1 [running]:
main.dumpStack()
	/home/foo/gotest/x.go:10 +0x5b
main.setVal(0x1)
	/home/foo/gotest/x.go:16 +0x45
main.main()
	/home/foo/gotest/x.go:19 +0x2a

What did you see instead?

go1.17-912f075047 prints:

goroutine 1 [running]:
main.dumpStack()
	/home/foo/gotest/x.go:10 +0x39
main.setVal(0xc0000001a0)
	/home/foo/gotest/x.go:16 +0x3b
main.main()
	/home/foo/gotest/x.go:19 +0x1e

so main.setVal(0xc0000001a0) instead of main.setVal(0x1).

The same go1.17-912f075047 on FreeBSD 13.0 amd64 works correctly, so this bug seems to be related to Linux, at least on amd64.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions