Skip to content

runtime: work.cycles should consider integer overflow #42664

Open
@qingyunha

Description

@qingyunha

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Triage Backlog

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions