diff options
author | Russ Cox <rsc@golang.org> | 2022-04-27 09:02:53 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2022-04-29 14:23:31 +0000 |
commit | a840bf871e005d948ba6442948997eb3ef2e3c7f (patch) | |
tree | 36868840f2743fad398c8737844a9cc8c751b2f9 /src/crypto/internal/boring/cache_test.go | |
parent | 0184fe5ece4f84fda9db04d2472b76efcaa8ef55 (diff) | |
download | go-a840bf871e005d948ba6442948997eb3ef2e3c7f.tar.gz go-a840bf871e005d948ba6442948997eb3ef2e3c7f.zip |
[dev.boringcrypto] crypto/internal/boring: add GC-aware cache
In the original BoringCrypto port, ecdsa and rsa's public and private
keys added a 'boring unsafe.Pointer' field to cache the BoringCrypto
form of the key. This led to problems with code that “knew” the layout
of those structs and in particular that they had no unexported fields.
In response, as an awful kludge, I changed the compiler to pretend
that field did not exist when laying out reflect data. Because we want
to merge BoringCrypto in the main tree, we need a different solution.
The different solution is this CL's boring.Cache, which is a
concurrent, GC-aware map from unsafe.Pointer to unsafe.Pointer (if
generics were farther along we could use them nicely here, but I am
afraid of breaking tools that aren't ready to see generics in the
standard library yet).
More complex approaches are possible, but a simple, fixed-size hash
table is easy to make concurrent and should be fine.
For #51940.
Change-Id: I44062a8defbd87b705a787cffc64c6a9d0132785
Reviewed-on: https://go-review.googlesource.com/c/go/+/395882
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/crypto/internal/boring/cache_test.go')
-rw-r--r-- | src/crypto/internal/boring/cache_test.go | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/crypto/internal/boring/cache_test.go b/src/crypto/internal/boring/cache_test.go new file mode 100644 index 0000000000..050ba457b2 --- /dev/null +++ b/src/crypto/internal/boring/cache_test.go @@ -0,0 +1,85 @@ +// Copyright 2022 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. + +//go:build boringcrypto + +package boring + +import ( + "fmt" + "runtime" + "testing" + "unsafe" +) + +var registeredCache Cache + +func init() { + registeredCache.Register() +} + +func TestCache(t *testing.T) { + // Use unregistered cache for functionality tests, + // to keep the runtime from clearing behind our backs. + c := new(Cache) + + // Create many entries. + seq := 0 + next := func() unsafe.Pointer { + x := new(int) + *x = seq + seq++ + return unsafe.Pointer(x) + } + m := make(map[unsafe.Pointer]unsafe.Pointer) + for i := 0; i < 10000; i++ { + k := next() + v := next() + m[k] = v + c.Put(k, v) + } + + // Overwrite a random 20% of those. + n := 0 + for k := range m { + v := next() + m[k] = v + c.Put(k, v) + if n++; n >= 2000 { + break + } + } + + // Check results. + str := func(p unsafe.Pointer) string { + if p == nil { + return "nil" + } + return fmt.Sprint(*(*int)(p)) + } + for k, v := range m { + if cv := c.Get(k); cv != v { + t.Fatalf("c.Get(%v) = %v, want %v", str(k), str(cv), str(v)) + } + } + + c.Clear() + for k := range m { + if cv := c.Get(k); cv != nil { + t.Fatalf("after Clear, c.Get(%v) = %v, want nil", str(k), str(cv)) + } + } + + // Check that registered cache is cleared at GC. + c = ®isteredCache + for k, v := range m { + c.Put(k, v) + } + runtime.GC() + for k := range m { + if cv := c.Get(k); cv != nil { + t.Fatalf("after Clear, c.Get(%v) = %v, want nil", str(k), str(cv)) + } + } +} |