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

crypto: avoid escaping Hash.Sum on generic architectures

For architectures without a specialized implementation (e.g. arm),
the generic implementation allocates because it does:

	var block = blockGeneric

which causes the compiler to give up trying to analyze block
even though it is technically only ever one implementation.
Instead of a variable, declare a function that wraps blockGeneric.

We apply this fix to md5, sha1, and sha256,
while sha512 already had the equivalent change.
We add a test to all hashing packages to ensure no allocations.

Credit goes to Cuong Manh Le for more specifically identifying
the problem and Keith Randal for suggesting a concrete solution.

Fixes #48055

Change-Id: I1a6a2e028038e051c83fd72b10a8bf4d210df57d
Reviewed-on: https://go-review.googlesource.com/c/go/+/346209
Trust: Joe Tsai <joetsai@digital-static.net>
Run-TryBot: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Joe Tsai 2021-08-30 10:25:11 -07:00
parent 335e72bcb6
commit 5961134fa5
7 changed files with 65 additions and 3 deletions

View File

@ -211,6 +211,20 @@ func TestLargeHashes(t *testing.T) {
}
}
func TestAllocations(t *testing.T) {
in := []byte("hello, world!")
out := make([]byte, 0, Size)
h := New()
n := int(testing.AllocsPerRun(10, func() {
h.Reset()
h.Write(in)
out = h.Sum(out[:0])
}))
if n > 0 {
t.Errorf("allocs = %d, want 0", n)
}
}
var bench = New()
var buf = make([]byte, 1024*1024*8+1)
var sum = make([]byte, bench.Size())

View File

@ -9,4 +9,6 @@ package md5
const haveAsm = false
var block = blockGeneric
func block(dig *digest, p []byte) {
blockGeneric(dig, p)
}

View File

@ -210,6 +210,20 @@ func TestLargeHashes(t *testing.T) {
}
}
func TestAllocations(t *testing.T) {
in := []byte("hello, world!")
out := make([]byte, 0, Size)
h := New()
n := int(testing.AllocsPerRun(10, func() {
h.Reset()
h.Write(in)
out = h.Sum(out[:0])
}))
if n > 0 {
t.Errorf("allocs = %d, want 0", n)
}
}
var bench = New()
var buf = make([]byte, 8192)

View File

@ -7,4 +7,6 @@
package sha1
var block = blockGeneric
func block(dig *digest, p []byte) {
blockGeneric(dig, p)
}

View File

@ -289,6 +289,20 @@ func TestLargeHashes(t *testing.T) {
}
}
func TestAllocations(t *testing.T) {
in := []byte("hello, world!")
out := make([]byte, 0, Size)
h := New()
n := int(testing.AllocsPerRun(10, func() {
h.Reset()
h.Write(in)
out = h.Sum(out[:0])
}))
if n > 0 {
t.Errorf("allocs = %d, want 0", n)
}
}
var bench = New()
var buf = make([]byte, 8192)

View File

@ -7,4 +7,6 @@
package sha256
var block = blockGeneric
func block(dig *digest, p []byte) {
blockGeneric(dig, p)
}

View File

@ -888,6 +888,20 @@ func TestLargeHashes(t *testing.T) {
}
}
func TestAllocations(t *testing.T) {
in := []byte("hello, world!")
out := make([]byte, 0, Size)
h := New()
n := int(testing.AllocsPerRun(10, func() {
h.Reset()
h.Write(in)
out = h.Sum(out[:0])
}))
if n > 0 {
t.Errorf("allocs = %d, want 0", n)
}
}
var bench = New()
var buf = make([]byte, 8192)