Skip to content

math/rand: panic generating NormFloat64 #66086

@KodamaHQ

Description

@KodamaHQ

Go version

go 1.20.5

Output of go env in your module/workspace:

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/user/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/user/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/user/go/src/lava/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4204543259=/tmp/go-build -gno-record-gcc-switches"

What did you do?

firstly we generate a global random instance:

// rand wrapper for protocol structs which hosts the same seed for deterministic unified random distribution
// we set the seed once for the entire process.

var protocolRand *rand.Rand

func Initialized() bool {
	return protocolRand != nil
}

func InitRandomSeed() {
	seed := time.Now().UnixNano()
	protocolRand = rand.New(rand.NewSource(seed))
}

And later on we are running:

func pertrubWithNormalGaussian(orig, percentage float64) float64 {
	perturb := rand.NormFloat64() * percentage * orig
	return orig + perturb
} 

generating the float caused our application to panic, Important to note this is very rare we used this method probably millions of times before it crashed.

What did you see happen?

panic: runtime error: index out of range [-1]

goroutine 18216 [running]:
math/rand.(*rngSource).Uint64(...)
	math/rand/rng.go:249
math/rand.(*rngSource).Int63(0x4d7c6d00?)
	math/rand/rng.go:234 +0x92
math/rand.(*Rand).Int63(...)
	math/rand/rand.go:95
math/rand.(*Rand).Uint32(...)
	math/rand/rand.go:98
math/rand.(*Rand).NormFloat64(0xc0015f7a40)
	math/rand/normal.go:39 +0x35
github.com/lavanet/lava/utils/rand.NormFloat64(...)
	github.com/lavanet/lava/utils/rand/rand.go:71

What did you expect to see?

Getting a random float without a panic :)

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