diff --git a/src/dawn_wire/client/Buffer.cpp b/src/dawn_wire/client/Buffer.cpp index 5fc7fbb20c..4515caf0ed 100644 --- a/src/dawn_wire/client/Buffer.cpp +++ b/src/dawn_wire/client/Buffer.cpp @@ -26,7 +26,7 @@ namespace dawn_wire { namespace client { bool mappable = (descriptor->usage & (WGPUBufferUsage_MapRead | WGPUBufferUsage_MapWrite)) != 0 || descriptor->mappedAtCreation; - if (mappable && descriptor->size > std::numeric_limits::max()) { + if (mappable && descriptor->size >= std::numeric_limits::max()) { device_->InjectError(WGPUErrorType_OutOfMemory, "Buffer is too large for map usage"); return device_->CreateErrorBuffer(); } diff --git a/src/tests/unittests/wire/WireBufferMappingTests.cpp b/src/tests/unittests/wire/WireBufferMappingTests.cpp index 8b948aef79..1f0f31ba4b 100644 --- a/src/tests/unittests/wire/WireBufferMappingTests.cpp +++ b/src/tests/unittests/wire/WireBufferMappingTests.cpp @@ -630,3 +630,47 @@ TEST_F(WireBufferMappingTests, MappedAtCreationThenMapFailure) { FlushClient(); } + +// Check that trying to create a buffer of size MAX_SIZE_T is an error handling in the client and +// never gets to the server-side. +TEST_F(WireBufferMappingTests, MaxSizeMappableBufferOOMDirectly) { + size_t kOOMSize = std::numeric_limits::max(); + WGPUBuffer apiBuffer = api.GetNewBuffer(); + + // Check for CreateBufferMapped. + { + WGPUBufferDescriptor descriptor = {}; + descriptor.usage = WGPUBufferUsage_CopySrc; + descriptor.size = kOOMSize; + descriptor.mappedAtCreation = true; + + wgpuDeviceCreateBuffer(device, &descriptor); + EXPECT_CALL(api, DeviceInjectError(apiDevice, WGPUErrorType_OutOfMemory, _)); + EXPECT_CALL(api, DeviceCreateErrorBuffer(apiDevice)).WillOnce(Return(apiBuffer)); + FlushClient(); + } + + // Check for MapRead usage. + { + WGPUBufferDescriptor descriptor = {}; + descriptor.usage = WGPUBufferUsage_MapRead; + descriptor.size = kOOMSize; + + wgpuDeviceCreateBuffer(device, &descriptor); + EXPECT_CALL(api, DeviceInjectError(apiDevice, WGPUErrorType_OutOfMemory, _)); + EXPECT_CALL(api, DeviceCreateErrorBuffer(apiDevice)).WillOnce(Return(apiBuffer)); + FlushClient(); + } + + // Check for MapWrite usage. + { + WGPUBufferDescriptor descriptor = {}; + descriptor.usage = WGPUBufferUsage_MapWrite; + descriptor.size = kOOMSize; + + wgpuDeviceCreateBuffer(device, &descriptor); + EXPECT_CALL(api, DeviceInjectError(apiDevice, WGPUErrorType_OutOfMemory, _)); + EXPECT_CALL(api, DeviceCreateErrorBuffer(apiDevice)).WillOnce(Return(apiBuffer)); + FlushClient(); + } +}