Change validator boolean tests to avoid asserts (#4503)

Fixes https://crbug.com/38102

* Check for valid instructions in many boolean tests instead of
  asserting in ValidationState_t
This commit is contained in:
alan-baker 2021-09-12 19:14:33 -04:00 committed by GitHub
parent 912460e46a
commit 4f4f76037c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 25 deletions

View File

@ -732,19 +732,19 @@ uint32_t ValidationState_t::GetBitWidth(uint32_t id) const {
bool ValidationState_t::IsVoidType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
return inst->opcode() == SpvOpTypeVoid;
return inst && inst->opcode() == SpvOpTypeVoid;
}
bool ValidationState_t::IsFloatScalarType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
return inst->opcode() == SpvOpTypeFloat;
return inst && inst->opcode() == SpvOpTypeFloat;
}
bool ValidationState_t::IsFloatVectorType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
if (!inst) {
return false;
}
if (inst->opcode() == SpvOpTypeVector) {
return IsFloatScalarType(GetComponentType(id));
@ -755,7 +755,9 @@ bool ValidationState_t::IsFloatVectorType(uint32_t id) const {
bool ValidationState_t::IsFloatScalarOrVectorType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
if (!inst) {
return false;
}
if (inst->opcode() == SpvOpTypeFloat) {
return true;
@ -770,13 +772,14 @@ bool ValidationState_t::IsFloatScalarOrVectorType(uint32_t id) const {
bool ValidationState_t::IsIntScalarType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
return inst->opcode() == SpvOpTypeInt;
return inst && inst->opcode() == SpvOpTypeInt;
}
bool ValidationState_t::IsIntVectorType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
if (!inst) {
return false;
}
if (inst->opcode() == SpvOpTypeVector) {
return IsIntScalarType(GetComponentType(id));
@ -787,7 +790,9 @@ bool ValidationState_t::IsIntVectorType(uint32_t id) const {
bool ValidationState_t::IsIntScalarOrVectorType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
if (!inst) {
return false;
}
if (inst->opcode() == SpvOpTypeInt) {
return true;
@ -802,13 +807,14 @@ bool ValidationState_t::IsIntScalarOrVectorType(uint32_t id) const {
bool ValidationState_t::IsUnsignedIntScalarType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
return inst->opcode() == SpvOpTypeInt && inst->word(3) == 0;
return inst && inst->opcode() == SpvOpTypeInt && inst->word(3) == 0;
}
bool ValidationState_t::IsUnsignedIntVectorType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
if (!inst) {
return false;
}
if (inst->opcode() == SpvOpTypeVector) {
return IsUnsignedIntScalarType(GetComponentType(id));
@ -819,13 +825,14 @@ bool ValidationState_t::IsUnsignedIntVectorType(uint32_t id) const {
bool ValidationState_t::IsSignedIntScalarType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
return inst->opcode() == SpvOpTypeInt && inst->word(3) == 1;
return inst && inst->opcode() == SpvOpTypeInt && inst->word(3) == 1;
}
bool ValidationState_t::IsSignedIntVectorType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
if (!inst) {
return false;
}
if (inst->opcode() == SpvOpTypeVector) {
return IsSignedIntScalarType(GetComponentType(id));
@ -836,13 +843,14 @@ bool ValidationState_t::IsSignedIntVectorType(uint32_t id) const {
bool ValidationState_t::IsBoolScalarType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
return inst->opcode() == SpvOpTypeBool;
return inst && inst->opcode() == SpvOpTypeBool;
}
bool ValidationState_t::IsBoolVectorType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
if (!inst) {
return false;
}
if (inst->opcode() == SpvOpTypeVector) {
return IsBoolScalarType(GetComponentType(id));
@ -853,7 +861,9 @@ bool ValidationState_t::IsBoolVectorType(uint32_t id) const {
bool ValidationState_t::IsBoolScalarOrVectorType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
if (!inst) {
return false;
}
if (inst->opcode() == SpvOpTypeBool) {
return true;
@ -868,7 +878,9 @@ bool ValidationState_t::IsBoolScalarOrVectorType(uint32_t id) const {
bool ValidationState_t::IsFloatMatrixType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
if (!inst) {
return false;
}
if (inst->opcode() == SpvOpTypeMatrix) {
return IsFloatScalarType(GetComponentType(id));
@ -923,8 +935,7 @@ bool ValidationState_t::GetStructMemberTypes(
bool ValidationState_t::IsPointerType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
return inst->opcode() == SpvOpTypePointer;
return inst && inst->opcode() == SpvOpTypePointer;
}
bool ValidationState_t::GetPointerTypeInfo(uint32_t id, uint32_t* data_type,
@ -942,8 +953,7 @@ bool ValidationState_t::GetPointerTypeInfo(uint32_t id, uint32_t* data_type,
bool ValidationState_t::IsCooperativeMatrixType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
return inst->opcode() == SpvOpTypeCooperativeMatrixNV;
return inst && inst->opcode() == SpvOpTypeCooperativeMatrixNV;
}
bool ValidationState_t::IsFloatCooperativeMatrixType(uint32_t id) const {

View File

@ -1580,6 +1580,19 @@ TEST_F(ValidateExtInst, GlslStd450LdexpExpWrongSize) {
"number as Result Type"));
}
TEST_F(ValidateExtInst, GlslStd450LdexpExpNoType) {
const std::string body = R"(
%val1 = OpExtInst %f32 %extinst Ldexp %f32_1 %main_entry
)";
CompileSuccessfully(GenerateShaderCode(body));
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("GLSL.std.450 Ldexp: "
"expected operand Exp to be a 32-bit int scalar "
"or vector type"));
}
TEST_F(ValidateExtInst, GlslStd450FrexpStructSuccess) {
const std::string body = R"(
%val1 = OpExtInst %struct_f32_u32 %extinst FrexpStruct %f32_h