]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.fuzz] internal/fuzz: allow setting pcg seed via GODEBUG
authorRoland Shoemaker <roland@golang.org>
Mon, 26 Apr 2021 20:00:15 +0000 (13:00 -0700)
committerRoland Shoemaker <roland@golang.org>
Tue, 27 Apr 2021 02:05:53 +0000 (02:05 +0000)
Format is "fuzzseed=123".

Change-Id: Idb314270c8fd4307149c8503e13424b653ec4b0a
Reviewed-on: https://go-review.googlesource.com/c/go/+/313651
Trust: Roland Shoemaker <roland@golang.org>
Trust: Katie Hockman <katie@golang.org>
Run-TryBot: Roland Shoemaker <roland@golang.org>
Run-TryBot: Katie Hockman <katie@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Katie Hockman <katie@golang.org>
src/internal/fuzz/pcg.go

index 5f0c1c39f65fa899355eafa20d6d87c25c69bff3..18e553bc94d5e246076f200119e7b040e89bee49 100644 (file)
@@ -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