From 3066cd338738c5d5f5a64b5ac009fd88f6766801 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Wed, 7 Oct 2020 15:03:33 +0000 Subject: [PATCH] dawn_wire: Guarantee OOM on MAX_SIZE_T sized buffers. This can be used to simplify some logic in Blink. Bug: dawn:445 Change-Id: I9859c51bc95f564847035533426675188eb8ef99 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/29362 Commit-Queue: Corentin Wallez Reviewed-by: Stephen White Reviewed-by: Austin Eng --- src/dawn_wire/client/Buffer.cpp | 2 +- .../unittests/wire/WireBufferMappingTests.cpp | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) 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(); + } +}