Unverified Commit 844485ec authored by Preston Van Loon's avatar Preston Van Loon Committed by GitHub

consensus/ethash: fix usage of *reflect.SliceHeader (#21372)

* consensus/ethash: only use *reflect.SliceHeader, not reflect.SliceHeader. See comment here: https://github.com/golang/go/issues/40397\#issuecomment-663748689

* consensus/ethash: pr feedback from @mdempsky, makes a copy of dest such that is not mutated

* consensus/ethash: remove noop assign

* consensus/ethash: apply same fix to another location
Co-authored-by: 's avatarPéter Szilágyi <peterke@gmail.com>
Co-authored-by: 's avatarMartin Holst Swende <martin@swende.se>
parent 1ea75379
...@@ -151,10 +151,12 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) { ...@@ -151,10 +151,12 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) {
logFn("Generated ethash verification cache", "elapsed", common.PrettyDuration(elapsed)) logFn("Generated ethash verification cache", "elapsed", common.PrettyDuration(elapsed))
}() }()
// Convert our destination slice to a byte buffer // Convert our destination slice to a byte buffer
header := *(*reflect.SliceHeader)(unsafe.Pointer(&dest)) var cache []byte
header.Len *= 4 cacheHdr := (*reflect.SliceHeader)(unsafe.Pointer(&cache))
header.Cap *= 4 dstHdr := (*reflect.SliceHeader)(unsafe.Pointer(&dest))
cache := *(*[]byte)(unsafe.Pointer(&header)) cacheHdr.Data = dstHdr.Data
cacheHdr.Len = dstHdr.Len * 4
cacheHdr.Cap = dstHdr.Cap * 4
// Calculate the number of theoretical rows (we'll store in one buffer nonetheless) // Calculate the number of theoretical rows (we'll store in one buffer nonetheless)
size := uint64(len(cache)) size := uint64(len(cache))
...@@ -283,10 +285,12 @@ func generateDataset(dest []uint32, epoch uint64, cache []uint32) { ...@@ -283,10 +285,12 @@ func generateDataset(dest []uint32, epoch uint64, cache []uint32) {
swapped := !isLittleEndian() swapped := !isLittleEndian()
// Convert our destination slice to a byte buffer // Convert our destination slice to a byte buffer
header := *(*reflect.SliceHeader)(unsafe.Pointer(&dest)) var dataset []byte
header.Len *= 4 datasetHdr := (*reflect.SliceHeader)(unsafe.Pointer(&dataset))
header.Cap *= 4 destHdr := (*reflect.SliceHeader)(unsafe.Pointer(&dest))
dataset := *(*[]byte)(unsafe.Pointer(&header)) 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 // Generate the dataset on many goroutines since it takes a while
threads := runtime.NumCPU() threads := runtime.NumCPU()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment