Fix integer overflow in Vulkan/Metal buffer size and pitch calculations#106
Open
mohammadmseet-hue wants to merge 1 commit intogoogle:mainfrom
Open
Conversation
The Terracotta initiative (commits 88ff9f4, a7d24e6) fixed integer overflow in D3D11 backend buffer allocation and Metal sizeInBytes calculation, but several equivalent overflow patterns in the Vulkan and Metal backends were missed. When processing large textures (e.g. RGBA32F at GL_MAX_TEXTURE_SIZE=16384), multiplication of pixelBytes * width * height exceeds UINT32_MAX, wrapping to a small value. This causes undersized buffer allocation followed by writes of the full image data, resulting in heap buffer overflow. Fixed locations: - vk_helpers.cpp readPixelsImpl: allocationSize computed in 32-bit - TextureVk.cpp reformatStagedUpdate: GLuint pitch variables overflow - SurfaceVk.cpp: GLuint rowStride overflow before VkDeviceSize widening - UtilsVk.cpp: GLuint sliceTexels/sliceSize/texBufferSize chain overflow - FrameBufferMtl.mm: uint32_t bytesPerRow not fixed by Terracotta All fixed by widening to size_t/VkDeviceSize before multiplication.
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.
Summary
The Terracotta initiative (commits 88ff9f4, a7d24e6) fixed integer overflow in D3D11 backend buffer allocation and Metal
sizeInBytescalculation. However, several equivalent overflow patterns in the Vulkan and Metal backends were missed.When processing large textures (e.g. RGBA32F at
GL_MAX_TEXTURE_SIZE=16384), multiplication ofpixelBytes * width * heightexceedsUINT32_MAX, wrapping to a small value. This causes undersized buffer allocation followed by writes of the full image data.Affected Code
vk_helpers.cpp:11538allocationSizereadFormat->pixelBytes * area.width * area.heightin 32-bitTextureVk.cpp:3176-3183srcDataRowPitch,dstDataRowPitchetc.GLuintpitch calculations overflowSurfaceVk.cpp:286rowStrideGLuintoverflow beforeVkDeviceSizewideningUtilsVk.cpp:4287-4290sliceTexels,sliceSize,texBufferSizeGLuintoverflowsFrameBufferMtl.mm:95bytesPerRowuint32_tnot fixed by Terracotta (onlysizeInByteswas fixed)Fix
Widen all affected variables to
size_torVkDeviceSizewith explicitstatic_cast<size_t>()before multiplication, matching the approach used in the Terracotta D3D11 fix.Testing
Existing tests continue to pass. The overflow is triggered by large textures (e.g. RGBA32F 16384x16384) which are within
GL_MAX_TEXTURE_SIZEon most desktop and mobile Vulkan GPUs.