Limit Vulkan memory allocation size to 1 GiB Memory offset calculations in 32-bit SIMD elements limit us to addressing at most 4 GiB. Signed arithmetic further restricts it to 2 GiB. The legacy OpenGL ES implementation limits image allocations to 1 GiB, to discourage excessive memory usage which wouldn't typically succeed on other (mobile) implementations anyway. So until we have a strong use case which requires more, let's impose the same limit. Bug: b/146515574 Change-Id: Iaa7bd22789fd20b4e7975d3d2ffee2a8f90b006a Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/39828 Presubmit-Ready: Nicolas Capens <nicolascapens@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Reviewed-by: Alexis Hétu <sugoi@google.com> Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Vulkan/VkConfig.h b/src/Vulkan/VkConfig.h index 1a39e0d..85b36e4 100644 --- a/src/Vulkan/VkConfig.h +++ b/src/Vulkan/VkConfig.h
@@ -88,4 +88,10 @@ #define SWIFTSHADER_EXTERNAL_SEMAPHORE_OPAQUE_FD 1 #endif +constexpr VkDeviceSize MAX_MEMORY_ALLOCATION_SIZE = 0x40000000ull; // 0x40000000 = 1 GiB + +// Memory offset calculations in 32-bit SIMD elements limit us to addressing at most 4 GiB. +// Signed arithmetic further restricts it to 2 GiB. +static_assert(MAX_MEMORY_ALLOCATION_SIZE <= 0x80000000ull, "maxMemoryAllocationSize must not exceed 2 GiB"); + #endif // VK_CONFIG_HPP_
diff --git a/src/Vulkan/VkDeviceMemory.cpp b/src/Vulkan/VkDeviceMemory.cpp index 6eff9e6..0e84d74 100644 --- a/src/Vulkan/VkDeviceMemory.cpp +++ b/src/Vulkan/VkDeviceMemory.cpp
@@ -100,7 +100,9 @@ { void *buffer = vk::allocate(size, REQUIRED_MEMORY_ALIGNMENT, DEVICE_MEMORY); if(!buffer) + { return VK_ERROR_OUT_OF_DEVICE_MEMORY; + } *pBuffer = buffer; return VK_SUCCESS; @@ -173,6 +175,11 @@ VkResult DeviceMemory::allocate() { + if(size > MAX_MEMORY_ALLOCATION_SIZE) + { + return VK_ERROR_OUT_OF_DEVICE_MEMORY; + } + VkResult result = VK_SUCCESS; if(!buffer) {
diff --git a/src/Vulkan/VkPhysicalDevice.cpp b/src/Vulkan/VkPhysicalDevice.cpp index 2635a57..27fb17a 100644 --- a/src/Vulkan/VkPhysicalDevice.cpp +++ b/src/Vulkan/VkPhysicalDevice.cpp
@@ -326,7 +326,7 @@ void PhysicalDevice::getProperties(VkPhysicalDeviceMaintenance3Properties *properties) const { - properties->maxMemoryAllocationSize = 1u << 31; + properties->maxMemoryAllocationSize = MAX_MEMORY_ALLOCATION_SIZE; properties->maxPerSetDescriptors = 1024; }