diff options
Diffstat (limited to 'src/crypto/rsa/boring_test.go')
-rw-r--r-- | src/crypto/rsa/boring_test.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/crypto/rsa/boring_test.go b/src/crypto/rsa/boring_test.go index d6203c22a1..141075a58d 100644 --- a/src/crypto/rsa/boring_test.go +++ b/src/crypto/rsa/boring_test.go @@ -16,7 +16,10 @@ import ( "encoding/asn1" "encoding/hex" "reflect" + "runtime" + "runtime/debug" "sync" + "sync/atomic" "testing" "unsafe" ) @@ -289,3 +292,41 @@ func TestBoringRandDecryptOAEP(t *testing.T) { } r.checkOffset(256) } + +func TestBoringFinalizers(t *testing.T) { + if runtime.GOOS == "nacl" { + // Times out on nacl (without BoringCrypto) + // but not clear why - probably consuming rand.Reader too quickly + // and being throttled. Also doesn't really matter. + t.Skip("skipping on nacl") + } + + k := testKey(t) + + // Run test with GOGC=10, to make bug more likely. + // Without the KeepAlives, the loop usually dies after + // about 30 iterations. + defer debug.SetGCPercent(debug.SetGCPercent(10)) + for n := 0; n < 200; n++ { + // Clear the underlying BoringCrypto object. + atomic.StorePointer(&k.boring, nil) + + // Race to create the underlying BoringCrypto object. + // The ones that lose the race are prime candidates for + // being GC'ed too early if the finalizers are not being + // used correctly. + var wg sync.WaitGroup + for i := 0; i < 10; i++ { + wg.Add(1) + go func() { + defer wg.Done() + sum := make([]byte, 32) + _, err := SignPKCS1v15(rand.Reader, k, crypto.SHA256, sum) + if err != nil { + panic(err) // usually caused by memory corruption, so hard stop + } + }() + } + wg.Wait() + } +} |