From adb1bce3b168100ca8d09ccd3c1978bba09facb4 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Fri, 24 Jul 2020 15:18:44 -0700 Subject: [PATCH 1/4] consensus/ethash: only use *reflect.SliceHeader, not reflect.SliceHeader. See comment here: https://github.com/golang/go/issues/40397\#issuecomment-663748689 --- consensus/ethash/algorithm.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/consensus/ethash/algorithm.go b/consensus/ethash/algorithm.go index d6c871092ed3..8da2c099aeca 100644 --- a/consensus/ethash/algorithm.go +++ b/consensus/ethash/algorithm.go @@ -151,10 +151,10 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) { logFn("Generated ethash verification cache", "elapsed", common.PrettyDuration(elapsed)) }() // Convert our destination slice to a byte buffer - header := *(*reflect.SliceHeader)(unsafe.Pointer(&dest)) + header := (*reflect.SliceHeader)(unsafe.Pointer(&dest)) header.Len *= 4 header.Cap *= 4 - cache := *(*[]byte)(unsafe.Pointer(&header)) + cache := *(*[]byte)(unsafe.Pointer(header)) // Calculate the number of theoretical rows (we'll store in one buffer nonetheless) size := uint64(len(cache)) From f3b468f00e29cbbd124207c1b3fde03881518c69 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Fri, 24 Jul 2020 16:59:24 -0700 Subject: [PATCH 2/4] consensus/ethash: pr feedback from @mdempsky, makes a copy of dest such that is not mutated --- consensus/ethash/algorithm.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/consensus/ethash/algorithm.go b/consensus/ethash/algorithm.go index 8da2c099aeca..74f017c762fc 100644 --- a/consensus/ethash/algorithm.go +++ b/consensus/ethash/algorithm.go @@ -151,10 +151,13 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) { logFn("Generated ethash verification cache", "elapsed", common.PrettyDuration(elapsed)) }() // Convert our destination slice to a byte buffer - header := (*reflect.SliceHeader)(unsafe.Pointer(&dest)) - header.Len *= 4 - header.Cap *= 4 - cache := *(*[]byte)(unsafe.Pointer(header)) + var cache []byte + cacheHdr := (*reflect.SliceHeader)(unsafe.Pointer(&cache)) + dstHdr := (*reflect.SliceHeader)(unsafe.Pointer(&dest)) + cacheHdr.Data = dstHdr.Data + cacheHdr.Len = dstHdr.Len * 4 + cacheHdr.Cap = dstHdr.Cap * 4 + cache = *(*[]byte)(unsafe.Pointer(cacheHdr)) // Calculate the number of theoretical rows (we'll store in one buffer nonetheless) size := uint64(len(cache)) From c1bf6974395d9ae8a4a550d0bfd1b0a10d11cc4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Tue, 11 Aug 2020 12:02:15 +0300 Subject: [PATCH 3/4] consensus/ethash: remove noop assign --- consensus/ethash/algorithm.go | 1 - 1 file changed, 1 deletion(-) diff --git a/consensus/ethash/algorithm.go b/consensus/ethash/algorithm.go index 74f017c762fc..cc513ca24f52 100644 --- a/consensus/ethash/algorithm.go +++ b/consensus/ethash/algorithm.go @@ -157,7 +157,6 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) { cacheHdr.Data = dstHdr.Data cacheHdr.Len = dstHdr.Len * 4 cacheHdr.Cap = dstHdr.Cap * 4 - cache = *(*[]byte)(unsafe.Pointer(cacheHdr)) // Calculate the number of theoretical rows (we'll store in one buffer nonetheless) size := uint64(len(cache)) From dc96e4429dc30ab6f73bab4a36fa9df882c3d644 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 28 Sep 2020 14:53:39 +0200 Subject: [PATCH 4/4] consensus/ethash: apply same fix to another location --- consensus/ethash/algorithm.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/consensus/ethash/algorithm.go b/consensus/ethash/algorithm.go index cc513ca24f52..570c2e14ea65 100644 --- a/consensus/ethash/algorithm.go +++ b/consensus/ethash/algorithm.go @@ -285,10 +285,12 @@ func generateDataset(dest []uint32, epoch uint64, cache []uint32) { swapped := !isLittleEndian() // Convert our destination slice to a byte buffer - header := *(*reflect.SliceHeader)(unsafe.Pointer(&dest)) - header.Len *= 4 - header.Cap *= 4 - dataset := *(*[]byte)(unsafe.Pointer(&header)) + var dataset []byte + datasetHdr := (*reflect.SliceHeader)(unsafe.Pointer(&dataset)) + destHdr := (*reflect.SliceHeader)(unsafe.Pointer(&dest)) + datasetHdr.Data = destHdr.Data + datasetHdr.Len = destHdr.Len * 4 + datasetHdr.Cap = destHdr.Cap * 4 // Generate the dataset on many goroutines since it takes a while threads := runtime.NumCPU()