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>
This commit is contained in:
Corentin Wallez 2020-10-07 15:03:33 +00:00 committed by Commit Bot service account
parent 012ca84940
commit 3066cd3387
2 changed files with 45 additions and 1 deletions

View File

@ -26,7 +26,7 @@ namespace dawn_wire { namespace client {
bool mappable = bool mappable =
(descriptor->usage & (WGPUBufferUsage_MapRead | WGPUBufferUsage_MapWrite)) != 0 || (descriptor->usage & (WGPUBufferUsage_MapRead | WGPUBufferUsage_MapWrite)) != 0 ||
descriptor->mappedAtCreation; 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"); device_->InjectError(WGPUErrorType_OutOfMemory, "Buffer is too large for map usage");
return device_->CreateErrorBuffer(); return device_->CreateErrorBuffer();
} }

View File

@ -630,3 +630,47 @@ TEST_F(WireBufferMappingTests, MappedAtCreationThenMapFailure) {
FlushClient(); 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();
}
}