Validate variable initializer type (#2668)

Fixes #249

* The pointed to type of Result Type must match the initializer type
* Had to update some opt tests to be valid
This commit is contained in:
alan-baker 2019-06-15 00:34:18 -04:00 committed by David Neto
parent 9477c91dec
commit 59983a6010
4 changed files with 30 additions and 2 deletions

View File

@ -422,6 +422,11 @@ spv_result_t ValidateVariable(ValidationState_t& _, const Instruction* inst) {
<< "OpVariable Initializer <id> '" << _.getIdName(initializer_id) << "OpVariable Initializer <id> '" << _.getIdName(initializer_id)
<< "' is not a constant or module-scope variable."; << "' is not a constant or module-scope variable.";
} }
if (initializer->type_id() != result_type->GetOperandAs<uint32_t>(2u)) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "Initializer type must match the type pointed to by the Result "
"Type";
}
} }
const auto storage_class = const auto storage_class =

View File

@ -3811,7 +3811,7 @@ OpName %output "output"
%6 = OpTypeFunction %void %6 = OpTypeFunction %void
%float = OpTypeFloat 32 %float = OpTypeFloat 32
%_ptr_Private_float = OpTypePointer Private %float %_ptr_Private_float = OpTypePointer Private %float
%initializer = OpVariable %_ptr_Private_float Private %initializer = OpConstant %float 0
%live = OpVariable %_ptr_Private_float Private %initializer %live = OpVariable %_ptr_Private_float Private %initializer
%_ptr_Output_float = OpTypePointer Output %float %_ptr_Output_float = OpTypePointer Output %float
%output = OpVariable %_ptr_Output_float Output %output = OpVariable %_ptr_Output_float Output

View File

@ -217,7 +217,7 @@ OpName %initializer "initializer"
%6 = OpTypeFunction %void %6 = OpTypeFunction %void
%float = OpTypeFloat 32 %float = OpTypeFloat 32
%_ptr_Private_float = OpTypePointer Private %float %_ptr_Private_float = OpTypePointer Private %float
%initializer = OpVariable %_ptr_Private_float Private %initializer = OpConstant %float 0
%live = OpVariable %_ptr_Private_float Private %initializer %live = OpVariable %_ptr_Private_float Private %initializer
%main = OpFunction %void None %6 %main = OpFunction %void None %6
%9 = OpLabel %9 = OpLabel

View File

@ -3543,6 +3543,29 @@ OpFunctionEnd
INSTANTIATE_TEST_SUITE_P(PointerComparisons, ValidatePointerComparisons, INSTANTIATE_TEST_SUITE_P(PointerComparisons, ValidatePointerComparisons,
Values("OpPtrEqual", "OpPtrNotEqual", "OpPtrDiff")); Values("OpPtrEqual", "OpPtrNotEqual", "OpPtrDiff"));
TEST_F(ValidateMemory, VariableInitializerWrongType) {
const std::string spirv = R"(
OpCapability Shader
OpCapability Linkage
OpCapability VariablePointersStorageBuffer
OpMemoryModel Logical GLSL450
%void = OpTypeVoid
%int = OpTypeInt 32 0
%float = OpTypeFloat 32
%ptr_wg_int = OpTypePointer Workgroup %int
%ptr_wg_float = OpTypePointer Workgroup %int
%wg_var = OpVariable %ptr_wg_int Workgroup
%ptr_private_wg_float = OpTypePointer Private %ptr_wg_float
%priv_var = OpVariable %ptr_private_wg_float Private %wg_var
)";
CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Initializer type must match the type pointed to by "
"the Result Type"));
}
} // namespace } // namespace
} // namespace val } // namespace val
} // namespace spvtools } // namespace spvtools