Pass the buffer size into DeserializeWGPUDeviceProperties

This allows deserialization to fail if the buffer is not large enough.
Before, we simply assumed the buffer was at least the size of
WGPUDeviceProperties.

Bug: none
Change-Id: I24e1f84c583f48d4e32c35276e5508e257e9f530
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/39861
Auto-Submit: Austin Eng <enga@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
This commit is contained in:
Austin Eng 2021-02-02 04:20:09 +00:00 committed by Commit Bot service account
parent 387a597154
commit 9a2174a37c
3 changed files with 28 additions and 6 deletions

View File

@ -687,16 +687,20 @@ namespace dawn_wire {
} }
bool DeserializeWGPUDeviceProperties(WGPUDeviceProperties* deviceProperties, bool DeserializeWGPUDeviceProperties(WGPUDeviceProperties* deviceProperties,
const volatile char* deserializeBuffer) { const volatile char* deserializeBuffer,
size_t devicePropertiesSize = SerializedWGPUDevicePropertiesSize(deviceProperties); size_t deserializeBufferSize) {
if (deserializeBufferSize == 0) {
// TODO(enga): Remove this after updating Chromium.
deserializeBufferSize = SerializedWGPUDevicePropertiesSize(deviceProperties);
}
const volatile WGPUDevicePropertiesTransfer* transfer = nullptr; const volatile WGPUDevicePropertiesTransfer* transfer = nullptr;
if (GetPtrFromBuffer(&deserializeBuffer, &devicePropertiesSize, 1, &transfer) != if (GetPtrFromBuffer(&deserializeBuffer, &deserializeBufferSize, 1, &transfer) !=
DeserializeResult::Success) { DeserializeResult::Success) {
return false; return false;
} }
return WGPUDevicePropertiesDeserialize(deviceProperties, transfer, &deserializeBuffer, return WGPUDevicePropertiesDeserialize(deviceProperties, transfer, &deserializeBuffer,
&devicePropertiesSize, &deserializeBufferSize,
nullptr) == DeserializeResult::Success; nullptr) == DeserializeResult::Success;
} }

View File

@ -49,8 +49,10 @@ namespace dawn_wire {
const WGPUDeviceProperties* deviceProperties, const WGPUDeviceProperties* deviceProperties,
char* serializeBuffer); char* serializeBuffer);
// TODO(enga): Remove the default value after updating Chromium.
DAWN_WIRE_EXPORT bool DeserializeWGPUDeviceProperties(WGPUDeviceProperties* deviceProperties, DAWN_WIRE_EXPORT bool DeserializeWGPUDeviceProperties(WGPUDeviceProperties* deviceProperties,
const volatile char* deserializeBuffer); const volatile char* deserializeBuffer,
size_t deserializeBufferSize = 0);
} // namespace dawn_wire } // namespace dawn_wire

View File

@ -34,8 +34,24 @@ TEST_F(WireWGPUDevicePropertiesTests, SerializeWGPUDeviceProperties) {
dawn_wire::SerializeWGPUDeviceProperties(&sentWGPUDeviceProperties, buffer.data()); dawn_wire::SerializeWGPUDeviceProperties(&sentWGPUDeviceProperties, buffer.data());
WGPUDeviceProperties receivedWGPUDeviceProperties; WGPUDeviceProperties receivedWGPUDeviceProperties;
dawn_wire::DeserializeWGPUDeviceProperties(&receivedWGPUDeviceProperties, buffer.data()); ASSERT_TRUE(dawn_wire::DeserializeWGPUDeviceProperties(&receivedWGPUDeviceProperties,
buffer.data(), buffer.size()));
ASSERT_TRUE(receivedWGPUDeviceProperties.textureCompressionBC); ASSERT_TRUE(receivedWGPUDeviceProperties.textureCompressionBC);
ASSERT_FALSE(receivedWGPUDeviceProperties.pipelineStatisticsQuery); ASSERT_FALSE(receivedWGPUDeviceProperties.pipelineStatisticsQuery);
ASSERT_TRUE(receivedWGPUDeviceProperties.timestampQuery); ASSERT_TRUE(receivedWGPUDeviceProperties.timestampQuery);
} }
// Test that deserialization if the buffer is just one byte too small fails.
TEST_F(WireWGPUDevicePropertiesTests, DeserializeBufferTooSmall) {
WGPUDeviceProperties sentWGPUDeviceProperties = {};
size_t sentWGPUDevicePropertiesSize =
dawn_wire::SerializedWGPUDevicePropertiesSize(&sentWGPUDeviceProperties);
std::vector<char> buffer;
buffer.resize(sentWGPUDevicePropertiesSize);
dawn_wire::SerializeWGPUDeviceProperties(&sentWGPUDeviceProperties, buffer.data());
WGPUDeviceProperties receivedWGPUDeviceProperties;
ASSERT_FALSE(dawn_wire::DeserializeWGPUDeviceProperties(&receivedWGPUDeviceProperties,
buffer.data(), buffer.size() - 1));
}