Repository navigation
Fix locking in fallback atomics - #233
Open
andreas-karlsson wants to merge 5 commits into
Open
andreas-karlsson wants to merge 5 commits into
andreas-karlsson wants to merge 5 commits into
Conversation
This branch has not been deployed
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.
Follow-up to #224. While looking at the
ByteArrayMemoryfallback paths on JDK 25 I found three bugs in how atomics lock. Each bug has a new test inMemoryTestthat fails before the fix.Atomics on non-shared memory trapped.
waitStateswas only created for shared memories, so the lock-based fallback threw an NPE that surfaced as an out-of-bounds trap. Atomics are valid on non-shared memories, so the map is now always created. I chose that over skipping the lock so that atomics never silently stop being atomic. Note: for consistency the same fix is applied toByteBufferMemory.Overlapping atomics of different sizes didn't exclude each other. The lock was keyed by the exact address, so e.g. an
i32add at 0 and ani16add at 2 took different locks and could lose updates. Both memories now lock the containing 8-byte word. Wasm atomics are naturally aligned and at most 8 bytes wide, so overlapping accesses always share a lock. This also affectsByteBufferMemoryon every JDK, since it always uses the lock-based default atomics.Lock-free and locked atomics were mixed. On JDK 25 the byte-array view handles lose their atomic modes, but byte atomics, which use an array element handle, keep them. Byte ops then ran lock-free while wider overlapping ops locked, so the locked read-modify-write could overwrite the byte update. A single
LOCK_FREEflag now decides for all sizes and all atomic accesses (reads, writes and read-modify-write): either everything uses hardware atomics or everything locks. On JDK 21 nothing changes. On JDK 25 byte atomics now lock too, which costs some speed but is correct.