Protect cached threadlocal in recursive hash#7901
Merged
headius merged 3 commits intojruby:masterfrom Aug 22, 2023
Merged
Conversation
The optimization previously introduced here caches the byte[16] as a thread-local value, but when recursively calling Hash#hash it will be overwritten by downstream usage. This modification avoids the hash being overwritten before it can be used to calculate the aggregate hash.
The original optimization here moved the allocation of the buffer byte[16] into a thread-local to reuse it, but it kept the original inline modification of that buffer via ByteBuffer.putLong. This caused recursive Hash#hash calls to overwrite the buffer mid-hash, leading to issues like jruby#7866 where a lazily-calculated recursive hash produces a bogus value the first time through. The change here avoids accessing and modifying the shared buffer until after any recursive hash calls have completed.
423a64e to
57e9b7e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A change introduced in 60ae8ba caused the once-transient buffer to be shared during recursive hash calculation, corrupting the results. This led to #7866.
The PR here fixes that by avoiding the shared buffer until all recursive hash calls have completed.
An additional small fix avoids allocating the long[] carrier object for hashes of size zero. Caching this in a thread-local would probably be worthwhile as well, but I was unsure how to do it safely.