From 0b5470f31d8d8cdd49e5754fd1cb5ce391f77bd6 Mon Sep 17 00:00:00 2001 From: Roland Shoemaker Date: Mon, 26 Apr 2021 13:00:15 -0700 Subject: [PATCH] [dev.fuzz] internal/fuzz: allow setting pcg seed via GODEBUG Format is "fuzzseed=123". Change-Id: Idb314270c8fd4307149c8503e13424b653ec4b0a Reviewed-on: https://go-review.googlesource.com/c/go/+/313651 Trust: Roland Shoemaker Trust: Katie Hockman Run-TryBot: Roland Shoemaker Run-TryBot: Katie Hockman TryBot-Result: Go Bot Reviewed-by: Katie Hockman --- src/internal/fuzz/pcg.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/internal/fuzz/pcg.go b/src/internal/fuzz/pcg.go index 5f0c1c39f6..18e553bc94 100644 --- a/src/internal/fuzz/pcg.go +++ b/src/internal/fuzz/pcg.go @@ -6,6 +6,9 @@ package fuzz import ( "math/bits" + "os" + "strconv" + "strings" "sync/atomic" "time" ) @@ -28,10 +31,27 @@ type pcgRand struct { inc uint64 } +func godebugSeed() *int { + debug := strings.Split(os.Getenv("GODEBUG"), ",") + for _, f := range debug { + if strings.HasPrefix(f, "fuzzseed=") { + seed, err := strconv.Atoi(strings.TrimPrefix(f, "fuzzseed=")) + if err != nil { + panic("malformed fuzzseed") + } + return &seed + } + } + return nil +} + // newPcgRand generates a new, seeded Rand, ready for use. func newPcgRand() *pcgRand { r := new(pcgRand) now := uint64(time.Now().UnixNano()) + if seed := godebugSeed(); seed != nil { + now = uint64(*seed) + } inc := atomic.AddUint64(&globalInc, 1) r.state = now r.inc = (inc << 1) | 1 -- 2.48.1