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

runtime: work.cycles should consider integer overflow #42664

Open
qingyunha opened this issue Nov 17, 2020 · 3 comments
Open

runtime: work.cycles should consider integer overflow #42664

qingyunha opened this issue Nov 17, 2020 · 3 comments
Labels

Comments

@qingyunha
Copy link
Contributor

@qingyunha qingyunha commented Nov 17, 2020

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

$ go version
go version devel +13f59a65 Sat Nov 7 10:28:10 2020 +0800 linux/amd64

Does this issue reproduce with the latest release?

not test

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/taoqy/.cache/go-build"
GOENV="/home/taoqy/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/taoqy/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/taoqy/go"
GOPRIVATE=""
GOPROXY="https://goproxy.io"
GOROOT="/home/taoqy/cc/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/taoqy/cc/go/pkg/tool/linux_amd64"
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-build557590123=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
        "fmt"
        "runtime"
        _ "unsafe"
)

type Work struct {
        _ [400 - 96]byte

        // cycles is the number of completed GC cycles, where a GC
        // cycle is sweep termination, mark, mark termination, and
        // sweep. This differs from memstats.numgc, which is
        // incremented at mark termination.
        cycles uint32

        // Timing/utilization stats for this cycle.
        stwprocs, maxprocs                 int32
        tSweepTerm, tMark, tMarkTerm, tEnd int64 // nanotime() of phase start

        pauseNS    int64 // total STW time this cycle
        pauseStart int64 // nanotime() of last STW

        // debug.gctrace heap sizes for this cycle.
        heap0, heap1, heap2, heapGoal uint64
}

//go:linkname work runtime.work
var work Work

func main() {
        work.cycles = ^uint32(0) - 3
        runtime.GC()
        fmt.Println(work.cycles)
        runtime.GC()
        fmt.Println(work.cycles)
        runtime.GC()
        fmt.Println(work.cycles)
}

What did you expect to see?

exit normally.

What did you see instead?

4294967293
4294967294
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [wait for GC cycle]:
runtime.GC()
        /home/taoqy/cc/go/src/runtime/mgc.go:1162 +0xa5
main.main()
        /tmp/wgc/main2.go:41 +0x105
@ALTree
Copy link
Member

@ALTree ALTree commented Nov 17, 2020

That's a lot of GC cycles...

cc @aclements whether it's worth handling this.

@qingyunha
Copy link
Contributor Author

@qingyunha qingyunha commented Nov 17, 2020

Assuming gc happens every 2ms, it take (1 << 32) / 500 / (24 * 3600) = 99 days to overflow. it seems possible.

@martisch
Copy link
Contributor

@martisch martisch commented Nov 18, 2020

Looking at some prod data it seems a more realistic rate of magnitude of GC cycles may be 10 times per second @ 99.9 percentile?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.