From d36740509fa6daa1200ee47f6dbbed66df652aeb Mon Sep 17 00:00:00 2001 From: James Price Date: Mon, 24 Apr 2023 19:48:23 +0000 Subject: [PATCH] spirv-reader: Error on multiple Position built-ins Produce a meaningful error instead of just crashing. Bug: oss-fuzz:55170 Change-Id: I09d94a910835839ce9407849446cf2928231a114 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/128540 Kokoro: Kokoro Commit-Queue: James Price Reviewed-by: David Neto --- src/tint/reader/spirv/parser_impl.cc | 6 ++++ .../spirv/parser_impl_module_var_test.cc | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/tint/reader/spirv/parser_impl.cc b/src/tint/reader/spirv/parser_impl.cc index 9f5afdbb5c..3c84ba9fa6 100644 --- a/src/tint/reader/spirv/parser_impl.cc +++ b/src/tint/reader/spirv/parser_impl.cc @@ -1438,6 +1438,12 @@ bool ParserImpl::EmitModuleScopeVariables() { if ((type_id == builtin_position_.pointer_type_id) && ((spirv_storage_class == spv::StorageClass::Input) || (spirv_storage_class == spv::StorageClass::Output))) { + // TODO(crbug.com/tint/103): Support modules that contain multiple Position built-ins. + if (builtin_position_.per_vertex_var_id != 0) { + return Fail() + << "unsupported: multiple Position built-in variables in the same module"; + } + // Skip emitting gl_PerVertex. builtin_position_.per_vertex_var_id = var.result_id(); builtin_position_.per_vertex_var_init_id = diff --git a/src/tint/reader/spirv/parser_impl_module_var_test.cc b/src/tint/reader/spirv/parser_impl_module_var_test.cc index c731fbfee4..2ae0435473 100644 --- a/src/tint/reader/spirv/parser_impl_module_var_test.cc +++ b/src/tint/reader/spirv/parser_impl_module_var_test.cc @@ -4132,6 +4132,35 @@ fn main() -> main_out { EXPECT_EQ(got, expected) << got; } +TEST_F(SpvModuleScopeVarParserTest, BuiltinPosition_MultiplePerVertexVariables) { + // This is not currently supported, so just make sure we produce a meaningful error instead of + // crashing. + const std::string assembly = R"( + OpCapability Shader + OpMemoryModel Logical Simple + OpEntryPoint Vertex %main "main" %1 + OpDecorate %struct Block + OpMemberDecorate %struct 0 BuiltIn Position + %void = OpTypeVoid + %voidfn = OpTypeFunction %void + %f32 = OpTypeFloat 32 + %vec4f = OpTypeVector %f32 4 + %struct = OpTypeStruct %vec4f + %struct_out_ptr = OpTypePointer Output %struct + %1 = OpVariable %struct_out_ptr Output + %2 = OpVariable %struct_out_ptr Output + %main = OpFunction %void None %voidfn + %entry = OpLabel + OpReturn + OpFunctionEnd +)"; + auto p = parser(test::Assemble(assembly)); + + EXPECT_FALSE(p->Parse()); + EXPECT_FALSE(p->success()); + EXPECT_EQ(p->error(), "unsupported: multiple Position built-in variables in the same module"); +} + TEST_F(SpvModuleScopeVarParserTest, Input_FlattenArray_OneLevel) { const std::string assembly = R"( OpCapability Shader