Skip to content

Commit

Permalink
Remove references to math/rand package's Read function.
Browse files Browse the repository at this point in the history
The helper function is deprecated. The package gvisor.dev/gvisor/pkg/rand
depends on crypto/rand which performs worse thatn math/rand, the changes
are fine since they are not at any gVisor's hot path.

The ultimate goal is to migrate math/rand to math/rand/v2.
  • Loading branch information
milantracy committed Oct 16, 2024
1 parent 7882783 commit 7cc1722
Show file tree
Hide file tree
Showing 30 changed files with 38 additions and 32 deletions.
1 change: 1 addition & 0 deletions pkg/aio/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ go_test(
library = ":aio",
deps = [
"//pkg/bitmap",
"//pkg/rand",
"@org_golang_x_sys//unix:go_default_library",
],
)
2 changes: 1 addition & 1 deletion pkg/aio/aio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package aio
import (
"bytes"
"io"
"math/rand"
"os"
"testing"

"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/bitmap"
"gvisor.dev/gvisor/pkg/rand"
)

func TestRead(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/buffer/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ go_test(
],
library = ":buffer",
deps = [
"//pkg/rand",
"//pkg/state",
"//pkg/tcpip/checksum",
"@com_github_google_go_cmp//cmp:go_default_library",
Expand Down
2 changes: 1 addition & 1 deletion pkg/buffer/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
"context"
"fmt"
"io"
"math/rand"
"slices"
"strings"
"testing"

"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/state"
"gvisor.dev/gvisor/pkg/tcpip/checksum"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/buffer/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package buffer

import (
"bytes"
"math/rand"
"testing"

"github.com/google/go-cmp/cmp"
"gvisor.dev/gvisor/pkg/rand"
)

func TestNewView(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/lisafs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ go_test(
library = ":lisafs",
deps = [
"//pkg/marshal",
"//pkg/rand",
"//pkg/sync",
"//pkg/unet",
"@org_golang_x_sys//unix:go_default_library",
Expand Down
2 changes: 1 addition & 1 deletion pkg/lisafs/sock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ package lisafs

import (
"bytes"
"math/rand"
"reflect"
"testing"

"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/marshal"
"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/unet"
)
Expand Down
1 change: 1 addition & 0 deletions pkg/lisafs/testsuite/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"//pkg/abi/linux",
"//pkg/context",
"//pkg/lisafs",
"//pkg/rand",
"//pkg/refs",
"//pkg/unet",
"@com_github_syndtr_gocapability//capability:go_default_library",
Expand Down
2 changes: 1 addition & 1 deletion pkg/lisafs/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package testsuite
import (
"bytes"
"fmt"
"math/rand"
"os"
"testing"
"time"
Expand All @@ -29,6 +28,7 @@ import (
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/lisafs"
"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/refs"
"gvisor.dev/gvisor/pkg/unet"
)
Expand Down
1 change: 1 addition & 0 deletions pkg/p9/p9test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ go_test(
deps = [
"//pkg/fd",
"//pkg/p9",
"//pkg/rand",
"//pkg/sync",
"@com_github_golang_mock//gomock:go_default_library",
"@org_golang_x_sys//unix:go_default_library",
Expand Down
3 changes: 2 additions & 1 deletion pkg/p9/p9test/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/fd"
"gvisor.dev/gvisor/pkg/p9"
gvisorrand "gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/sync"
)

Expand Down Expand Up @@ -2192,7 +2193,7 @@ func TestReadWriteConcurrent(t *testing.T) {

// Initialize random data for each instance.
for i := 0; i < instances; i++ {
if _, err := rand.Read(dataSets[i][:]); err != nil {
if _, err := gvisorrand.Read(dataSets[i][:]); err != nil {
t.Fatalf("error initializing dataSet#%d, got %v", i, err)
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/tcpip/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ go_library(
deps = [
"//pkg/atomicbitops",
"//pkg/buffer",
"//pkg/rand",
"//pkg/sync",
"//pkg/waiter",
"@org_golang_x_sys//unix:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions pkg/tcpip/link/fdbased/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ go_test(
library = ":fdbased",
deps = [
"//pkg/buffer",
"//pkg/rand",
"//pkg/refs",
"//pkg/tcpip",
"//pkg/tcpip/header",
Expand Down
2 changes: 1 addition & 1 deletion pkg/tcpip/link/fdbased/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package fdbased
import (
"bytes"
"fmt"
"math/rand"
"os"
"slices"
"testing"
Expand All @@ -30,6 +29,7 @@ import (
"github.com/google/go-cmp/cmp"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/buffer"
"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/refs"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
Expand Down
1 change: 1 addition & 0 deletions pkg/tcpip/network/internal/testutil/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
],
deps = [
"//pkg/buffer",
"//pkg/rand",
"//pkg/tcpip",
"//pkg/tcpip/checker",
"//pkg/tcpip/header",
Expand Down
2 changes: 1 addition & 1 deletion pkg/tcpip/network/internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package testutil

import (
"fmt"
"math/rand"
"testing"

"github.com/google/go-cmp/cmp"
"gvisor.dev/gvisor/pkg/buffer"
"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/checker"
"gvisor.dev/gvisor/pkg/tcpip/header"
Expand Down
2 changes: 1 addition & 1 deletion pkg/tcpip/tcpip.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ import (
"io"
"math"
"math/bits"
"math/rand"
"net"
"reflect"
"strconv"
"strings"
"time"

"gvisor.dev/gvisor/pkg/atomicbitops"
"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/waiter"
)
Expand Down
1 change: 1 addition & 0 deletions pkg/test/testutil/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
visibility = ["//:sandbox"],
deps = [
"//pkg/sentry/watchdog",
"//pkg/rand",
"//pkg/sync",
"//runsc/config",
"//runsc/flag",
Expand Down
19 changes: 2 additions & 17 deletions pkg/test/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"io"
"log"
"math"
"math/rand"
"net/http"
"os"
"os/exec"
Expand All @@ -40,6 +39,7 @@ import (
"github.com/cenkalti/backoff"
specs "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/sentry/watchdog"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/runsc/config"
Expand Down Expand Up @@ -352,28 +352,13 @@ func writeSpec(dir string, spec *specs.Spec) error {
return os.WriteFile(filepath.Join(dir, "config.json"), b, 0755)
}

// idRandomSrc is a pseudo random generator used to in RandomID.
var idRandomSrc = rand.New(rand.NewSource(time.Now().UnixNano()))

// idRandomSrcMtx is the mutex protecting idRandomSrc.Read from being used
// concurrently in different goroutines.
var idRandomSrcMtx sync.Mutex

// RandomID returns 20 random bytes following the given prefix.
func RandomID(prefix string) string {
// Read 20 random bytes.
b := make([]byte, 20)
// Rand.Read is not safe for concurrent use. Packetimpact tests can be run in
// parallel now, so we have to protect the Read with a mutex. Otherwise we'll
// run into name conflicts.
// https://golang.org/pkg/math/rand/#Rand.Read
idRandomSrcMtx.Lock()
// "[Read] always returns len(p) and a nil error." --godoc
if _, err := idRandomSrc.Read(b); err != nil {
idRandomSrcMtx.Unlock()
if _, err := rand.Read(b); err != nil {
panic("rand.Read failed: " + err.Error())
}
idRandomSrcMtx.Unlock()
if prefix != "" {
prefix = prefix + "-"
}
Expand Down
1 change: 1 addition & 0 deletions test/benchmarks/tcp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_binary(
],
visibility = ["//:sandbox"],
deps = [
"//pkg/rand",
"//pkg/tcpip",
"//pkg/tcpip/adapters/gonet",
"//pkg/tcpip/link/fdbased",
Expand Down
2 changes: 1 addition & 1 deletion test/benchmarks/tcp/tcp_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"io"
"log"
"math/rand"
"net"
"os"
"os/signal"
Expand All @@ -33,6 +32,7 @@ import (
"time"

"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
"gvisor.dev/gvisor/pkg/tcpip/link/fdbased"
Expand Down
1 change: 1 addition & 0 deletions test/cmd/test_app/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_binary(
"//test/syscalls/linux:__pkg__",
],
deps = [
"//pkg/rand",
"//pkg/test/testutil",
"//pkg/unet",
"//runsc/flag",
Expand Down
3 changes: 2 additions & 1 deletion test/cmd/test_app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

"github.com/google/subcommands"
"github.com/kr/pty"
gvisorrand "gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/test/testutil"
"gvisor.dev/gvisor/runsc/flag"
)
Expand Down Expand Up @@ -104,7 +105,7 @@ func (c *fsTreeCreator) Execute(ctx context.Context, f *flag.FlagSet, args ...an
}

data := make([]byte, fileSize)
rand.Read(data)
gvisorrand.Read(data)
for i := uint(0); i < depth; i++ {
for j := uint(0); j < numFilesPerLevel; j++ {
filePath := filepath.Join(curDir, fmt.Sprintf("file%d", j))
Expand Down
1 change: 1 addition & 0 deletions test/packetimpact/testbench/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ go_library(
"//pkg/binary",
"//pkg/buffer",
"//pkg/hostarch",
"//pkg/rand",
"//pkg/tcpip",
"//pkg/tcpip/checksum",
"//pkg/tcpip/header",
Expand Down
3 changes: 2 additions & 1 deletion test/packetimpact/testbench/testbench.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (
"encoding/json"
"flag"
"fmt"
"math/rand"
"net"
"testing"
"time"

"gvisor.dev/gvisor/pkg/rand"
)

var (
Expand Down
2 changes: 2 additions & 0 deletions test/packetimpact/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ packetimpact_testbench(
name = "ipv4_fragment_reassembly",
srcs = ["ipv4_fragment_reassembly_test.go"],
deps = [
"//pkg/rand",
"//pkg/tcpip/checksum",
"//pkg/tcpip/header",
"//test/packetimpact/testbench",
Expand All @@ -293,6 +294,7 @@ packetimpact_testbench(
name = "ipv6_fragment_reassembly",
srcs = ["ipv6_fragment_reassembly_test.go"],
deps = [
"//pkg/rand",
"//pkg/tcpip",
"//pkg/tcpip/checksum",
"//pkg/tcpip/header",
Expand Down
2 changes: 1 addition & 1 deletion test/packetimpact/tests/ipv4_fragment_reassembly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package ipv4_fragment_reassembly_test

import (
"flag"
"math/rand"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/tcpip/checksum"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/test/packetimpact/testbench"
Expand Down
2 changes: 1 addition & 1 deletion test/packetimpact/tests/ipv6_fragment_reassembly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package ipv6_fragment_reassembly_test

import (
"flag"
"math/rand"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/checksum"
"gvisor.dev/gvisor/pkg/tcpip/header"
Expand Down
3 changes: 3 additions & 0 deletions tools/go_marshal/analysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ go_library(
name = "analysis",
testonly = 1,
srcs = ["analysis_unsafe.go"],
deps = [
"//pkg/rand",
],
visibility = [
"//:sandbox",
],
Expand Down
3 changes: 2 additions & 1 deletion tools/go_marshal/analysis/analysis_unsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ package analysis

import (
"fmt"
"math/rand"
"reflect"
"testing"
"unsafe"

"gvisor.dev/gvisor/pkg/rand"
)

// RandomizeValue assigns random value(s) to an abitrary type. This is intended
Expand Down

0 comments on commit 7cc1722

Please sign in to comment.