]> Cypherpunks.ru repositories - gostls13.git/commitdiff
net: remove dependency on math/rand
authorRuss Cox <rsc@golang.org>
Tue, 7 Jul 2020 13:07:16 +0000 (09:07 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 12 Oct 2020 16:30:11 +0000 (16:30 +0000)
Like we did for sync, let the runtime give net random numbers,
to avoid forcing an import of math/rand for DNS.

Change-Id: Iab3e64121d687d288a3961a8ccbcebe589047253
Reviewed-on: https://go-review.googlesource.com/c/go/+/241258
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/go/build/deps_test.go
src/net/dnsclient.go
src/net/dnsclient_test.go
src/net/dnsclient_unix.go
src/runtime/stubs.go

index 42382d583c0eb9a19984563ec24aa4f4acc702ea..ec2a2f93283eef503e1552314c63f1c675071c44 100644 (file)
@@ -318,7 +318,6 @@ var depsRules = `
        # so large dependencies must be kept out.
        # This is a long-looking list but most of these
        # are small with few dependencies.
-       # math/rand should probably be removed at some point.
        CGO,
        golang.org/x/net/dns/dnsmessage,
        golang.org/x/net/lif,
@@ -327,11 +326,11 @@ var depsRules = `
        internal/poll,
        internal/singleflight,
        internal/race,
-       math/rand,
        os
        < net;
 
        fmt, unicode !< net;
+       math/rand !< net; # net uses runtime instead
 
        # NET is net plus net-helper packages.
        FMT, net
@@ -479,7 +478,7 @@ var depsRules = `
        CGO, OS, fmt
        < os/signal/internal/pty;
 
-       NET, testing
+       NET, testing, math/rand
        < golang.org/x/net/nettest;
 
        FMT, container/heap, math/rand
index b5bb3a4d11dee557fea2aece3c99ad8e631f1769..e9c73845d78d7eee4bacc8645ec33685f70cc4cb 100644 (file)
@@ -5,12 +5,25 @@
 package net
 
 import (
-       "math/rand"
        "sort"
 
        "golang.org/x/net/dns/dnsmessage"
 )
 
+// provided by runtime
+func fastrand() uint32
+
+func randInt() int {
+       x, y := fastrand(), fastrand()    // 32-bit halves
+       u := uint(x)<<31 ^ uint(int32(y)) // full uint, even on 64-bit systems; avoid 32-bit shift on 32-bit systems
+       i := int(u >> 1)                  // clear sign bit, even on 32-bit systems
+       return i
+}
+
+func randIntn(n int) int {
+       return randInt() % n
+}
+
 // reverseaddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
 // address addr suitable for rDNS (PTR) record lookup or an error if it fails
 // to parse the IP address.
@@ -162,7 +175,7 @@ func (addrs byPriorityWeight) shuffleByWeight() {
        }
        for sum > 0 && len(addrs) > 1 {
                s := 0
-               n := rand.Intn(sum)
+               n := randIntn(sum)
                for i := range addrs {
                        s += int(addrs[i].Weight)
                        if s > n {
@@ -206,7 +219,7 @@ func (s byPref) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
 // sort reorders MX records as specified in RFC 5321.
 func (s byPref) sort() {
        for i := range s {
-               j := rand.Intn(i + 1)
+               j := randIntn(i + 1)
                s[i], s[j] = s[j], s[i]
        }
        sort.Sort(s)
index f3ed62db36a5c2907f16f9f83325227757130f07..24cd69e13be25d932760cdd4e195c46625dd004f 100644 (file)
@@ -5,7 +5,6 @@
 package net
 
 import (
-       "math/rand"
        "testing"
 )
 
@@ -17,7 +16,7 @@ func checkDistribution(t *testing.T, data []*SRV, margin float64) {
 
        results := make(map[string]int)
 
-       count := 1000
+       count := 10000
        for j := 0; j < count; j++ {
                d := make([]*SRV, len(data))
                copy(d, data)
@@ -39,7 +38,6 @@ func checkDistribution(t *testing.T, data []*SRV, margin float64) {
 }
 
 func testUniformity(t *testing.T, size int, margin float64) {
-       rand.Seed(1)
        data := make([]*SRV, size)
        for i := 0; i < size; i++ {
                data[i] = &SRV{Target: string('a' + rune(i)), Weight: 1}
@@ -55,7 +53,6 @@ func TestDNSSRVUniformity(t *testing.T) {
 }
 
 func testWeighting(t *testing.T, margin float64) {
-       rand.Seed(1)
        data := []*SRV{
                {Target: "a", Weight: 60},
                {Target: "b", Weight: 30},
index 8dd32ccc7b50beb4aa3b3c7ebf40bd7e944e7ecb..d7db0c8133ebc814eb2ed4d7c4a002b0e49eaa97 100644 (file)
@@ -18,7 +18,6 @@ import (
        "context"
        "errors"
        "io"
-       "math/rand"
        "os"
        "sync"
        "time"
@@ -47,7 +46,7 @@ var (
 )
 
 func newRequest(q dnsmessage.Question) (id uint16, udpReq, tcpReq []byte, err error) {
-       id = uint16(rand.Int()) ^ uint16(time.Now().UnixNano())
+       id = uint16(randInt())
        b := dnsmessage.NewBuilder(make([]byte, 2, 514), dnsmessage.Header{ID: id, RecursionDesired: true})
        b.EnableCompression()
        if err := b.StartQuestions(); err != nil {
index bd2514e8628b1bdd2ba686eedbc723689d9a822c..6290142a41b330ad3435f11c7924a842f22481fa 100644 (file)
@@ -130,6 +130,9 @@ func fastrandn(n uint32) uint32 {
 //go:linkname sync_fastrand sync.fastrand
 func sync_fastrand() uint32 { return fastrand() }
 
+//go:linkname net_fastrand net.fastrand
+func net_fastrand() uint32 { return fastrand() }
+
 // in internal/bytealg/equal_*.s
 //go:noescape
 func memequal(a, b unsafe.Pointer, size uintptr) bool