From 89b8e238eb39d6c8bb637b8d1c6e0fad1f45e77f Mon Sep 17 00:00:00 2001 From: alan-baker <33432579+alan-baker@users.noreply.github.com> Date: Mon, 22 Oct 2018 08:47:56 -0400 Subject: [PATCH] Better checking of the index operand (#1992) Fixes https://crbug.com/897069 * Code previously assumed the index instruction had a type * Added a test to reproduce --- source/val/validate_composites.cpp | 4 ++-- test/val/val_composites_test.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/source/val/validate_composites.cpp b/source/val/validate_composites.cpp index 6be60261e..038873506 100644 --- a/source/val/validate_composites.cpp +++ b/source/val/validate_composites.cpp @@ -148,8 +148,8 @@ spv_result_t ValidateVectorExtractDynamic(ValidationState_t& _, << "Expected Vector component type to be equal to Result Type"; } - const uint32_t index_type = _.GetOperandTypeId(inst, 3); - if (!_.IsIntScalarType(index_type)) { + const auto index = _.FindDef(inst->GetOperandAs(3)); + if (!index || index->type_id() == 0 || !_.IsIntScalarType(index->type_id())) { return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Expected Index to be int scalar"; } diff --git a/test/val/val_composites_test.cpp b/test/val/val_composites_test.cpp index 5b8381590..24e765745 100644 --- a/test/val/val_composites_test.cpp +++ b/test/val/val_composites_test.cpp @@ -1469,6 +1469,30 @@ OpFunctionEnd EXPECT_EQ(SPV_SUCCESS, ValidateInstructions()); } +TEST_F(ValidateComposites, ExtractDynamicLabelIndex) { + const std::string spirv = R"( +OpCapability Shader +OpCapability Linkage +OpMemoryModel Logical GLSL450 +%void = OpTypeVoid +%float = OpTypeFloat 32 +%v4float = OpTypeVector %float 4 +%void_fn = OpTypeFunction %void +%float_0 = OpConstant %float 0 +%v4float_0 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0 +%func = OpFunction %void None %void_fn +%1 = OpLabel +%ex = OpVectorExtractDynamic %float %v4float_0 %1 +OpReturn +OpFunctionEnd +)"; + + CompileSuccessfully(spirv); + EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions()); + EXPECT_THAT(getDiagnosticString(), + HasSubstr("Expected Index to be int scalar")); +} + } // namespace } // namespace val } // namespace spvtools