1
0
mirror of https://github.com/golang/go synced 2024-11-26 03:07:57 -07:00

crypto/internal/cryptotest: add SkipTestAllocations

[                                                                     ]
[    It has been [ 0 ] days since Filippo broke a TestAllocations.    ]
[                                                                     ]

Concentrate all the skips in one place, so we don't have to re-discover
always the same ones via trial and error.

This might over-skip fixable allocations, but all these targets are not
fast anyway, so they are not worth going back for.

Removed the sysrand TestAllocations because it causes an import loop
with cryptotest and it's covered by TestAllocations in crypto/rand.

Change-Id: Icd40e97f9128e037f567147f8c9dafa758a47fac
Reviewed-on: https://go-review.googlesource.com/c/go/+/626438
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Daniel McCarney <daniel@binaryparadox.net>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Filippo Valsorda <filippo@golang.org>
This commit is contained in:
Filippo Valsorda 2024-11-08 14:41:06 +01:00 committed by Gopher Robot
parent cf531372b2
commit ecdc8c1b3f
12 changed files with 52 additions and 77 deletions

View File

@ -9,11 +9,10 @@ import (
"bytes"
"compress/gzip"
"crypto"
"crypto/internal/boring"
"crypto/internal/cryptotest"
"crypto/rand"
"crypto/sha512"
"encoding/hex"
"internal/testenv"
"log"
"os"
"strings"
@ -319,11 +318,7 @@ func TestMalleability(t *testing.T) {
}
func TestAllocations(t *testing.T) {
if boring.Enabled {
t.Skip("skipping allocations test with BoringCrypto")
}
testenv.SkipIfOptimizationOff(t)
cryptotest.SkipTestAllocations(t)
if allocs := testing.AllocsPerRun(100, func() {
seed := make([]byte, SeedSize)
message := []byte("Hello, world!")

View File

@ -0,0 +1,37 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cryptotest
import (
"crypto/internal/boring"
"internal/asan"
"internal/msan"
"internal/race"
"internal/testenv"
"runtime"
"testing"
)
// SkipTestAllocations skips the test if there are any factors that interfere
// with allocation optimizations.
func SkipTestAllocations(t *testing.T) {
// Go+BoringCrypto uses cgo.
if boring.Enabled {
t.Skip("skipping allocations test with BoringCrypto")
}
// The sanitizers sometimes cause allocations.
if race.Enabled || msan.Enabled || asan.Enabled {
t.Skip("skipping allocations test with sanitizers")
}
// The plan9 crypto/rand allocates.
if runtime.GOOS == "plan9" {
t.Skip("skipping allocations test on plan9")
}
// Some APIs rely on inliner and devirtualization to allocate on the stack.
testenv.SkipIfOptimizationOff(t)
}

View File

@ -5,9 +5,9 @@
package edwards25519
import (
"crypto/internal/cryptotest"
"crypto/internal/edwards25519/field"
"encoding/hex"
"internal/testenv"
"reflect"
"testing"
)
@ -280,8 +280,7 @@ func TestNonCanonicalPoints(t *testing.T) {
var testAllocationsSink byte
func TestAllocations(t *testing.T) {
testenv.SkipIfOptimizationOff(t)
cryptotest.SkipTestAllocations(t)
if allocs := testing.AllocsPerRun(100, func() {
p := NewIdentityPoint()
p.Add(p, NewGeneratorPoint())

View File

@ -12,7 +12,6 @@ import (
"encoding"
"encoding/hex"
"fmt"
"internal/testenv"
"io"
"math/rand"
"strings"
@ -370,7 +369,7 @@ func testClone(t *testing.T) {
var sink byte
func TestAllocations(t *testing.T) {
testenv.SkipIfOptimizationOff(t)
cryptotest.SkipTestAllocations(t)
t.Run("New", func(t *testing.T) {
if allocs := testing.AllocsPerRun(10, func() {
h := New256()

View File

@ -7,17 +7,16 @@ package nistec_test
import (
"bytes"
"crypto/elliptic"
"crypto/internal/cryptotest"
"crypto/internal/nistec"
"fmt"
"internal/testenv"
"math/big"
"math/rand"
"testing"
)
func TestAllocations(t *testing.T) {
testenv.SkipIfOptimizationOff(t)
cryptotest.SkipTestAllocations(t)
t.Run("P224", func(t *testing.T) {
if allocs := testing.AllocsPerRun(10, func() {
p := nistec.NewP224Point().SetGenerator()

View File

@ -7,9 +7,6 @@ package sysrand
import (
"bytes"
"compress/flate"
"internal/asan"
"internal/msan"
"internal/race"
"internal/testenv"
"os"
"runtime"
@ -72,27 +69,6 @@ func TestConcurrentRead(t *testing.T) {
wg.Wait()
}
var sink byte
func TestAllocations(t *testing.T) {
if race.Enabled || msan.Enabled || asan.Enabled {
t.Skip("urandomRead allocates under -race, -asan, and -msan")
}
if runtime.GOOS == "plan9" {
t.Skip("plan9 allocates")
}
testenv.SkipIfOptimizationOff(t)
n := int(testing.AllocsPerRun(10, func() {
buf := make([]byte, 32)
Read(buf)
sink ^= buf[0]
}))
if n > 0 {
t.Errorf("allocs = %d, want 0", n)
}
}
// TestNoUrandomFallback ensures the urandom fallback is not reached in
// normal operations.
func TestNoUrandomFallback(t *testing.T) {

View File

@ -225,6 +225,7 @@ func TestLargeHashes(t *testing.T) {
}
func TestAllocations(t *testing.T) {
cryptotest.SkipTestAllocations(t)
in := []byte("hello, world!")
out := make([]byte, 0, Size)
h := New()

View File

@ -7,15 +7,11 @@ package rand
import (
"bytes"
"compress/flate"
"crypto/internal/boring"
"crypto/internal/cryptotest"
"errors"
"internal/asan"
"internal/msan"
"internal/race"
"internal/testenv"
"io"
"os"
"runtime"
"sync"
"testing"
)
@ -157,18 +153,7 @@ func testConcurrentRead(t *testing.T, Read func([]byte) (int, error)) {
var sink byte
func TestAllocations(t *testing.T) {
if boring.Enabled {
// Might be fixable with https://go.dev/issue/56378.
t.Skip("boringcrypto allocates")
}
if race.Enabled || msan.Enabled || asan.Enabled {
t.Skip("urandomRead allocates under -race, -asan, and -msan")
}
if runtime.GOOS == "plan9" {
t.Skip("plan9 allocates")
}
testenv.SkipIfOptimizationOff(t)
cryptotest.SkipTestAllocations(t)
n := int(testing.AllocsPerRun(10, func() {
buf := make([]byte, 32)
Read(buf)

View File

@ -8,7 +8,7 @@ import (
"bufio"
"bytes"
"crypto"
"crypto/internal/boring"
"crypto/internal/cryptotest"
"crypto/rand"
. "crypto/rsa"
"crypto/sha1"
@ -17,7 +17,6 @@ import (
"encoding/pem"
"flag"
"fmt"
"internal/testenv"
"math/big"
"strings"
"testing"
@ -132,10 +131,7 @@ func testKeyBasics(t *testing.T, priv *PrivateKey) {
}
func TestAllocations(t *testing.T) {
if boring.Enabled {
t.Skip("skipping allocations test with BoringCrypto")
}
testenv.SkipIfOptimizationOff(t)
cryptotest.SkipTestAllocations(t)
m := []byte("Hello Gophers")
c, err := EncryptPKCS1v15(rand.Reader, &test2048Key.PublicKey, m)

View File

@ -231,9 +231,7 @@ func TestLargeHashes(t *testing.T) {
}
func TestAllocations(t *testing.T) {
if boring.Enabled {
t.Skip("BoringCrypto doesn't allocate the same way as stdlib")
}
cryptotest.SkipTestAllocations(t)
in := []byte("hello, world!")
out := make([]byte, 0, Size)
h := New()

View File

@ -8,12 +8,10 @@ package sha256
import (
"bytes"
"crypto/internal/boring"
"crypto/internal/cryptotest"
"encoding"
"fmt"
"hash"
"internal/testenv"
"io"
"testing"
)
@ -298,10 +296,7 @@ func TestLargeHashes(t *testing.T) {
}
func TestAllocations(t *testing.T) {
testenv.SkipIfOptimizationOff(t)
if boring.Enabled {
t.Skip("BoringCrypto doesn't allocate the same way as stdlib")
}
cryptotest.SkipTestAllocations(t)
if n := testing.AllocsPerRun(10, func() {
in := []byte("hello, world!")
out := make([]byte, 0, Size)

View File

@ -8,13 +8,11 @@ package sha512
import (
"bytes"
"crypto/internal/boring"
"crypto/internal/cryptotest"
"encoding"
"encoding/hex"
"fmt"
"hash"
"internal/testenv"
"io"
"testing"
)
@ -903,10 +901,7 @@ func TestLargeHashes(t *testing.T) {
}
func TestAllocations(t *testing.T) {
testenv.SkipIfOptimizationOff(t)
if boring.Enabled {
t.Skip("BoringCrypto doesn't allocate the same way as stdlib")
}
cryptotest.SkipTestAllocations(t)
if n := testing.AllocsPerRun(10, func() {
in := []byte("hello, world!")
out := make([]byte, 0, Size)