From bb417ace110bc3e7be0a587ba77daae0cd227827 Mon Sep 17 00:00:00 2001 From: Brandon Jones Date: Sat, 2 Oct 2021 01:54:31 +0000 Subject: [PATCH] Validate index formats in SetIndexBuffer Fuzzer discovered that it was possible to send invalid IndexFormats to SetIndexBuffer and cause IndexFormatSize to hit an UNREACHABLE(). This validates that the passed format is part of the enum before doing any further operations on it. Bug: 1254571 Change-Id: I1e4075f8d26afdb8e4bcae3cfc72e6219ff28f78 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/65610 Commit-Queue: Brandon Jones Reviewed-by: Kai Ninomiya Reviewed-by: Austin Eng --- src/dawn_native/RenderEncoderBase.cpp | 2 ++ .../validation/IndexBufferValidationTests.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/dawn_native/RenderEncoderBase.cpp b/src/dawn_native/RenderEncoderBase.cpp index 993bc66abe..240fc29436 100644 --- a/src/dawn_native/RenderEncoderBase.cpp +++ b/src/dawn_native/RenderEncoderBase.cpp @@ -244,6 +244,8 @@ namespace dawn_native { DAWN_TRY(GetDevice()->ValidateObject(buffer)); DAWN_TRY(ValidateCanUseAs(buffer, wgpu::BufferUsage::Index)); + DAWN_TRY(ValidateIndexFormat(format)); + DAWN_INVALID_IF(format == wgpu::IndexFormat::Undefined, "Index format must be specified"); diff --git a/src/tests/unittests/validation/IndexBufferValidationTests.cpp b/src/tests/unittests/validation/IndexBufferValidationTests.cpp index d13bf33ee1..d1d62b31ce 100644 --- a/src/tests/unittests/validation/IndexBufferValidationTests.cpp +++ b/src/tests/unittests/validation/IndexBufferValidationTests.cpp @@ -58,6 +58,21 @@ TEST_F(IndexBufferValidationTest, UndefinedIndexFormat) { ASSERT_DEVICE_ERROR(encoder.Finish()); } +// Test that an invalid index format is disallowed. +TEST_F(IndexBufferValidationTest, InvalidIndexFormat) { + wgpu::BufferDescriptor bufferDesc; + bufferDesc.usage = wgpu::BufferUsage::Index; + bufferDesc.size = 256; + wgpu::Buffer buffer = device.CreateBuffer(&bufferDesc); + + DummyRenderPass renderPass(device); + wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); + wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass); + pass.SetIndexBuffer(buffer, static_cast(404)); + pass.EndPass(); + ASSERT_DEVICE_ERROR(encoder.Finish()); +} + // Test that for OOB validation of index buffer offset and size. TEST_F(IndexBufferValidationTest, IndexBufferOffsetOOBValidation) { wgpu::BufferDescriptor bufferDesc;