Fix integer overflow in StationTessellatorImpl buffer growth#262
Open
matthewperiut wants to merge 1 commit into
Open
Fix integer overflow in StationTessellatorImpl buffer growth#262matthewperiut wants to merge 1 commit into
matthewperiut wants to merge 1 commit into
Conversation
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.
Fix integer overflow in StationTessellatorImpl buffer growth
Symptom
After a long play session, the Arsenic tessellator crashes while rendering GUI items
(e.g. items in the inventory/hotbar) with:
Root cause
StationTessellatorImpl.ensureBufferCapacitygrows the tessellator buffer bydoubling
bufferSize(anint) every time the buffer fills, and never shrinks itback. Vanilla
Tessellator.reset()resetsvertexCount/bufferPositionbut does NOTreset
bufferSize, so the doubled size accumulates across the whole session.The buffer normally never grows, because vanilla
Tessellator.vertex()auto-flushes(
draw()) when it nears full. Arsenic's direct-writequad()path bypasses thatauto-flush and instead calls
ensureBufferCapacity(48), which grows the buffer ratherthan flushing it. Over a long session the buffer therefore doubles repeatedly:
GlAllocationUtils.allocateByteBuffer(-2147483648)then callsByteBuffer.allocateDirect(MIN_VALUE), which throwscapacity < 0. (A latent ~2 GBArrays.copyOf(buffer, bufferSize)allocation sits one line earlier as well.)Fix
Minimal, contained change in
ensureBufferCapacity:bufferSizeat a sane maximum (MAX_BUFFER_SIZE = 1 << 28ints = a 1 GiB directbuffer) so growth is bounded and
bufferSize * 4can never overflow a signed int.The base size is 2^21, so this still permits several doublings for any plausible load.
doubling into an invalid (negative) capacity.
longbefore the allocation, so even at thecap the computed capacity stays a correct positive
int.The diff touches only
StationTessellatorImpl.javaand preserves the existing loggingand growth behaviour up to the cap.
Verification
2^29 and
bufferSize * 4 == 2^31, which overflows to Integer.MIN_VALUE -- matching thereported crash capacity exactly. With the cap, bufferSize stops at 2^28 and the byte
count tops out at 2^30 (a valid positive int), so the overflow can no longer occur.
./gradlew :station-renderer-api-v0:build-> BUILD SUCCESSFUL (only the project'spre-existing -source 17 / deprecation warnings, none from this change).