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 <cwallez@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
diff --git a/src/dawn_wire/client/Buffer.cpp b/src/dawn_wire/client/Buffer.cpp
index 5fc7fbb..4515caf 100644
--- a/src/dawn_wire/client/Buffer.cpp
+++ b/src/dawn_wire/client/Buffer.cpp
@@ -26,7 +26,7 @@
bool mappable =
(descriptor->usage & (WGPUBufferUsage_MapRead | WGPUBufferUsage_MapWrite)) != 0 ||
descriptor->mappedAtCreation;
- if (mappable && descriptor->size > std::numeric_limits<size_t>::max()) {
+ if (mappable && descriptor->size >= std::numeric_limits<size_t>::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 8b948ae..1f0f31b 100644
--- a/src/tests/unittests/wire/WireBufferMappingTests.cpp
+++ b/src/tests/unittests/wire/WireBufferMappingTests.cpp
@@ -630,3 +630,47 @@
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<size_t>::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();
+ }
+}