Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Nov 17, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions consensus/ethash/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These statements are going to modify the len and cap of dest, which I don't think you want.

There's also a use of reflect.SliceHeader in ethash/ethash.go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you recommend is the appropriate fix? I'm not familiar with this bit of code or this usage of reflect.SliceHeader

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var cache []byte
cacheHdr := (*reflect.SliceHeader)(unsafe.Pointer(&cache))
dstHdr := (*reflect.SliceHeader)(unsafe.Pointer(&dst))
cacheHdr.Data = dstHdr.Data
cacheHdr.Len = dstHdr.Len * 4
cacheHdr.Cap = dstHdr.Cap * 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))
Expand Down