Skip to content

Protect cached threadlocal in recursive hash#7901

Merged
headius merged 3 commits intojruby:masterfrom
headius:protect_cached_threadlocal_in_recursive_hash
Aug 22, 2023
Merged

Protect cached threadlocal in recursive hash#7901
headius merged 3 commits intojruby:masterfrom
headius:protect_cached_threadlocal_in_recursive_hash

Conversation

@headius
Copy link
Member

@headius headius commented Aug 22, 2023

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.

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.
@headius headius force-pushed the protect_cached_threadlocal_in_recursive_hash branch from 423a64e to 57e9b7e Compare August 22, 2023 22:20
@headius headius merged commit b4a6a9d into jruby:master Aug 22, 2023
@headius headius deleted the protect_cached_threadlocal_in_recursive_hash branch August 22, 2023 22:40
@headius headius linked an issue Aug 22, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recursive hashing corrupts shared hash buffer

1 participant