Repository navigation
Keep the original allocation valid when realloc wrapping fails - #5
Merged
riccardobl merged 1 commit intoOct 5, 2026
Conversation
riccardobl
marked this pull request as ready for review
October 5, 2026 20:07
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.
Problem
High-level
SaferAlloc.reallocresizes native memory before creating the replacement JavaByteBuffer. If wrapper creation throwsOutOfMemoryError, the original allocation may already have moved or been freed. This violates the documented guarantee that the original remains valid when realloc throws.Change
Add a package-private JNI entry point for the high-level API. It allocates a replacement, creates its Java wrapper, copies
min(old capacity, new size)bytes, and only then frees the original. Wrapper failure releases the replacement and preserves the original. Negative sizes and heap buffers are rejected before mutation; null input and zero size are covered explicitly. Raw native realloc and exported function pointers retain their existing behavior.Deliberate tradeoff
The high-level API now copies and temporarily holds both native allocations, including for shrink or same-size calls. It gives up in-place resizing to maintain the exception-safety guarantee. README documents this cost, ownership requirements, null/zero behavior, and position/limit semantics.
Regression evidence
With the original master 6439892 and a warmed realloc path, the isolated 16 MiB heap regression fails safely:
native accounting changed after wrapper failure: 16 -> 4096. It checks accounting before dereferencing/freeing the original. With the fix, the original address, position, limit, contents, writeability and eventual free all remain valid, and accounting returns to baseline.Validation
-Xcheck:jnigit diff --checkpassedLimits
Full Gradle/CMake and non-Linux platforms were not run locally. Java
--release 8platform data is unavailable in the cloud runtime, so source/target compilation is not equivalent to a complete JDK 8 compatibility check. No mobile-device runtime validation. This independent PR does not include the new-allocation wrapper leak fix or Darwin alias fix.