Add validation pass for conversion instructions

The pass checks correctness of operands of instruction in opcode range
OpConvertFToU - OpBitset.

Disabled invalid tests

Disabled UConvert validation until Vulkan CTS can catch up.

Add validate_conversion to Android.mk

Also remove duplicate entry in CMakeLists.txt.
This commit is contained in:
Andrey Tuganov 2017-09-28 14:53:24 -04:00 committed by David Neto
parent bb7802b18c
commit 39e25fd8ab
10 changed files with 1544 additions and 4 deletions

View File

@ -35,8 +35,9 @@ SPVTOOLS_SRC_FILES := \
source/validate.cpp \
source/validate_arithmetics.cpp \
source/validate_bitwise.cpp \
source/validate_cfg.cpp \
source/validate_capability.cpp \
source/validate_cfg.cpp \
source/validate_conversion.cpp \
source/validate_datarules.cpp \
source/validate_decorations.cpp \
source/validate_id.cpp \

View File

@ -255,8 +255,9 @@ set(SPIRV_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/validate.cpp
${CMAKE_CURRENT_SOURCE_DIR}/validate_arithmetics.cpp
${CMAKE_CURRENT_SOURCE_DIR}/validate_bitwise.cpp
${CMAKE_CURRENT_SOURCE_DIR}/validate_cfg.cpp
${CMAKE_CURRENT_SOURCE_DIR}/validate_capability.cpp
${CMAKE_CURRENT_SOURCE_DIR}/validate_cfg.cpp
${CMAKE_CURRENT_SOURCE_DIR}/validate_conversion.cpp
${CMAKE_CURRENT_SOURCE_DIR}/validate_datarules.cpp
${CMAKE_CURRENT_SOURCE_DIR}/validate_decorations.cpp
${CMAKE_CURRENT_SOURCE_DIR}/validate_id.cpp

View File

@ -655,4 +655,34 @@ bool ValidationState_t::GetStructMemberTypes(
return true;
}
bool ValidationState_t::IsPointerType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
return inst->opcode() == SpvOpTypePointer;
}
bool ValidationState_t::GetPointerTypeInfo(
uint32_t id, uint32_t* data_type, uint32_t* storage_class) const {
if (!id)
return false;
const Instruction* inst = FindDef(id);
assert(inst);
if (inst->opcode() != SpvOpTypePointer)
return false;
*storage_class = inst->word(2);
*data_type = inst->word(3);
return true;
}
uint32_t ValidationState_t::GetOperandTypeId(
const spv_parsed_instruction_t* inst,
size_t operand_index) const {
assert(operand_index < inst->num_operands);
const spv_parsed_operand_t& operand = inst->operands[operand_index];
assert(operand.num_words == 1);
return GetTypeId(inst->words[operand.offset]);
}
} /// namespace libspirv

View File

@ -378,10 +378,21 @@ class ValidationState_t {
bool IsSignedIntVectorType(uint32_t id) const;
bool IsBoolScalarType(uint32_t id) const;
bool IsBoolVectorType(uint32_t id) const;
bool IsPointerType(uint32_t id) const;
// Returns type_id if id has type or zero otherwise.
uint32_t GetTypeId(uint32_t id) const;
// Returns type_id for given id operand if it has a type or zero otherwise.
// |operand_index| is expected to be pointing towards an operand which is an
// id.
uint32_t GetOperandTypeId(const spv_parsed_instruction_t* inst,
size_t operand_index) const;
// Provides information on pointer type. Returns false iff not pointer type.
bool GetPointerTypeInfo(
uint32_t id, uint32_t* data_type, uint32_t* storage_class) const;
private:
ValidationState_t(const ValidationState_t&);

View File

@ -181,6 +181,7 @@ spv_result_t ProcessInstruction(void* user_data,
if (auto error = InstructionPass(_, inst)) return error;
if (auto error = TypeUniquePass(_, inst)) return error;
if (auto error = ArithmeticsPass(_, inst)) return error;
if (auto error = ConversionPass(_, inst)) return error;
if (auto error = LogicalsPass(_, inst)) return error;
if (auto error = BitwisePass(_, inst)) return error;

View File

@ -115,6 +115,10 @@ spv_result_t TypeUniquePass(ValidationState_t& _,
spv_result_t ArithmeticsPass(ValidationState_t& _,
const spv_parsed_instruction_t* inst);
/// Validates correctness of conversion instructions.
spv_result_t ConversionPass(ValidationState_t& _,
const spv_parsed_instruction_t* inst);
/// Validates correctness of logical instructions.
spv_result_t LogicalsPass(ValidationState_t& _,
const spv_parsed_instruction_t* inst);

View File

@ -0,0 +1,419 @@
// Copyright (c) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Validates correctness of conversion instructions.
#include "validate.h"
#include "diagnostic.h"
#include "opcode.h"
#include "val/instruction.h"
#include "val/validation_state.h"
namespace libspirv {
// Validates correctness of conversion instructions.
spv_result_t ConversionPass(ValidationState_t& _,
const spv_parsed_instruction_t* inst) {
const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
const uint32_t result_type = inst->type_id;
switch (opcode) {
case SpvOpConvertFToU: {
if (!_.IsUnsignedIntScalarType(result_type) &&
!_.IsUnsignedIntVectorType(result_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected unsigned int scalar or vector type as Result Type: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (!input_type || (!_.IsFloatScalarType(input_type) &&
!_.IsFloatVectorType(input_type)))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be float scalar or vector: "
<< spvOpcodeString(opcode);
if (_.GetDimension(result_type) != _.GetDimension(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have the same dimension as Result Type: "
<< spvOpcodeString(opcode);
break;
}
case SpvOpConvertFToS: {
if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected int scalar or vector type as Result Type: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (!input_type || (!_.IsFloatScalarType(input_type) &&
!_.IsFloatVectorType(input_type)))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be float scalar or vector: "
<< spvOpcodeString(opcode);
if (_.GetDimension(result_type) != _.GetDimension(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have the same dimension as Result Type: "
<< spvOpcodeString(opcode);
break;
}
case SpvOpConvertSToF:
case SpvOpConvertUToF: {
if (!_.IsFloatScalarType(result_type) &&
!_.IsFloatVectorType(result_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected float scalar or vector type as Result Type: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (!input_type || (!_.IsIntScalarType(input_type) &&
!_.IsIntVectorType(input_type)))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be int scalar or vector: "
<< spvOpcodeString(opcode);
if (_.GetDimension(result_type) != _.GetDimension(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have the same dimension as Result Type: "
<< spvOpcodeString(opcode);
break;
}
#if 0
// TODO(atgoo@github.com) Reenable this once VulkanCTS can pass this test.
case SpvOpUConvert: {
if (!_.IsUnsignedIntScalarType(result_type) &&
!_.IsUnsignedIntVectorType(result_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected unsigned int scalar or vector type as Result Type: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (!input_type || (!_.IsIntScalarType(input_type) &&
!_.IsIntVectorType(input_type)))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be int scalar or vector: "
<< spvOpcodeString(opcode);
if (_.GetDimension(result_type) != _.GetDimension(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have the same dimension as Result Type: "
<< spvOpcodeString(opcode);
if (_.GetBitWidth(result_type) == _.GetBitWidth(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have different bit width from Result Type: "
<< spvOpcodeString(opcode);
break;
}
#endif
case SpvOpSConvert: {
if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected int scalar or vector type as Result Type: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (!input_type || (!_.IsIntScalarType(input_type) &&
!_.IsIntVectorType(input_type)))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be int scalar or vector: "
<< spvOpcodeString(opcode);
if (_.GetDimension(result_type) != _.GetDimension(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have the same dimension as Result Type: "
<< spvOpcodeString(opcode);
if (_.GetBitWidth(result_type) == _.GetBitWidth(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have different bit width from Result Type: "
<< spvOpcodeString(opcode);
break;
}
case SpvOpFConvert: {
if (!_.IsFloatScalarType(result_type) &&
!_.IsFloatVectorType(result_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected float scalar or vector type as Result Type: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (!input_type || (!_.IsFloatScalarType(input_type) &&
!_.IsFloatVectorType(input_type)))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be float scalar or vector: "
<< spvOpcodeString(opcode);
if (_.GetDimension(result_type) != _.GetDimension(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have the same dimension as Result Type: "
<< spvOpcodeString(opcode);
if (_.GetBitWidth(result_type) == _.GetBitWidth(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have different bit width from Result Type: "
<< spvOpcodeString(opcode);
break;
}
case SpvOpQuantizeToF16: {
if ((!_.IsFloatScalarType(result_type) &&
!_.IsFloatVectorType(result_type)) ||
_.GetBitWidth(result_type) != 32)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected 32-bit float scalar or vector type as Result Type: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (input_type != result_type)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input type to be equal to Result Type: "
<< spvOpcodeString(opcode);
break;
}
case SpvOpConvertPtrToU: {
if (!_.IsUnsignedIntScalarType(result_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected unsigned int scalar type as Result Type: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (!_.IsPointerType(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be a pointer: " << spvOpcodeString(opcode);
break;
}
case SpvOpSatConvertSToU:
case SpvOpSatConvertUToS: {
if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected int scalar or vector type as Result Type: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (!input_type || (!_.IsIntScalarType(input_type) &&
!_.IsIntVectorType(input_type)))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected int scalar or vector as input: "
<< spvOpcodeString(opcode);
if (_.GetDimension(result_type) != _.GetDimension(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have the same dimension as Result Type: "
<< spvOpcodeString(opcode);
break;
}
case SpvOpConvertUToPtr: {
if (!_.IsPointerType(result_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected Result Type to be a pointer: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (!_.IsIntScalarType(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected int scalar as input: " << spvOpcodeString(opcode);
break;
}
case SpvOpPtrCastToGeneric: {
uint32_t result_storage_class = 0;
uint32_t result_data_type = 0;
if (!_.GetPointerTypeInfo(result_type, &result_data_type,
&result_storage_class))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected Result Type to be a pointer: "
<< spvOpcodeString(opcode);
if (result_storage_class != SpvStorageClassGeneric)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected Result Type to have storage class Generic: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
uint32_t input_storage_class = 0;
uint32_t input_data_type = 0;
if (!_.GetPointerTypeInfo(input_type, &input_data_type,
&input_storage_class))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be a pointer: "
<< spvOpcodeString(opcode);
if (input_storage_class != SpvStorageClassWorkgroup &&
input_storage_class != SpvStorageClassCrossWorkgroup &&
input_storage_class != SpvStorageClassFunction)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have storage class Workgroup, "
<< "CrossWorkgroup or Function: "
<< spvOpcodeString(opcode);
if (result_data_type != input_data_type)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input and Result Type to point to the same type: "
<< spvOpcodeString(opcode);
break;
}
case SpvOpGenericCastToPtr: {
uint32_t result_storage_class = 0;
uint32_t result_data_type = 0;
if (!_.GetPointerTypeInfo(result_type, &result_data_type,
&result_storage_class))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected Result Type to be a pointer: "
<< spvOpcodeString(opcode);
if (result_storage_class != SpvStorageClassWorkgroup &&
result_storage_class != SpvStorageClassCrossWorkgroup &&
result_storage_class != SpvStorageClassFunction)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected Result Type to have storage class Workgroup, "
<< "CrossWorkgroup or Function: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
uint32_t input_storage_class = 0;
uint32_t input_data_type = 0;
if (!_.GetPointerTypeInfo(input_type, &input_data_type,
&input_storage_class))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be a pointer: "
<< spvOpcodeString(opcode);
if (input_storage_class != SpvStorageClassGeneric)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have storage class Generic: "
<< spvOpcodeString(opcode);
if (result_data_type != input_data_type)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input and Result Type to point to the same type: "
<< spvOpcodeString(opcode);
break;
}
case SpvOpGenericCastToPtrExplicit: {
uint32_t result_storage_class = 0;
uint32_t result_data_type = 0;
if (!_.GetPointerTypeInfo(result_type, &result_data_type,
&result_storage_class))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected Result Type to be a pointer: "
<< spvOpcodeString(opcode);
const uint32_t target_storage_class = inst->words[4];
if (result_storage_class != target_storage_class)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected Result Type to be of target storage class: "
<< spvOpcodeString(opcode);
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
uint32_t input_storage_class = 0;
uint32_t input_data_type = 0;
if (!_.GetPointerTypeInfo(input_type, &input_data_type,
&input_storage_class))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be a pointer: "
<< spvOpcodeString(opcode);
if (input_storage_class != SpvStorageClassGeneric)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have storage class Generic: "
<< spvOpcodeString(opcode);
if (result_data_type != input_data_type)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input and Result Type to point to the same type: "
<< spvOpcodeString(opcode);
if (target_storage_class != SpvStorageClassWorkgroup &&
target_storage_class != SpvStorageClassCrossWorkgroup &&
target_storage_class != SpvStorageClassFunction)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected target storage class to be Workgroup, "
<< "CrossWorkgroup or Function: "
<< spvOpcodeString(opcode);
break;
}
case SpvOpBitcast: {
const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (!input_type)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have a type: " << spvOpcodeString(opcode);
const bool result_is_pointer = _.IsPointerType(result_type);
const bool result_is_int_scalar = _.IsIntScalarType(result_type);
const bool input_is_pointer = _.IsPointerType(input_type);
const bool input_is_int_scalar = _.IsIntScalarType(input_type);
if (!result_is_pointer && !result_is_int_scalar &&
!_.IsIntVectorType(result_type) &&
!_.IsFloatScalarType(result_type) &&
!_.IsFloatVectorType(result_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected Result Type to be a pointer or int or float vector "
<< "or scalar type: " << spvOpcodeString(opcode);
if (!input_is_pointer && !input_is_int_scalar &&
!_.IsIntVectorType(input_type) &&
!_.IsFloatScalarType(input_type) &&
!_.IsFloatVectorType(input_type))
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be a pointer or int or float vector "
<< "or scalar: " << spvOpcodeString(opcode);
if (result_is_pointer && !input_is_pointer && !input_is_int_scalar)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to be a pointer or int scalar if Result Type "
<< "is pointer: " << spvOpcodeString(opcode);
if (input_is_pointer && !result_is_pointer && !result_is_int_scalar)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Pointer can only be converted to another pointer or int "
<< "scalar: " << spvOpcodeString(opcode);
if (!result_is_pointer && !input_is_pointer) {
const uint32_t result_size =
_.GetBitWidth(result_type) * _.GetDimension(result_type);
const uint32_t input_size =
_.GetBitWidth(input_type) * _.GetDimension(input_type);
if (result_size != input_size)
return _.diag(SPV_ERROR_INVALID_DATA)
<< "Expected input to have the same total bit width as "
<< "Result Type: " << spvOpcodeString(opcode);
}
break;
}
default:
break;
}
return SPV_SUCCESS;
}
} // namespace libspirv

View File

@ -80,6 +80,12 @@ add_spvtools_unittest(TARGET val_arithmetics
LIBS ${SPIRV_TOOLS}
)
add_spvtools_unittest(TARGET val_conversion
SRCS val_conversion_test.cpp
${VAL_TEST_COMMON_SRCS}
LIBS ${SPIRV_TOOLS}
)
add_spvtools_unittest(TARGET val_logicals
SRCS val_logicals_test.cpp
${VAL_TEST_COMMON_SRCS}

File diff suppressed because it is too large Load Diff

View File

@ -2005,7 +2005,8 @@ TEST_F(ValidateIdWithMessage, OpLoadPointerBad) {
HasSubstr("ID 8 has not been defined"));
}
TEST_F(ValidateIdWithMessage, OpLoadLogicalPointerBad) {
// Disabled as bitcasting type to object is now not valid.
TEST_F(ValidateIdWithMessage, DISABLED_OpLoadLogicalPointerBad) {
string spirv = kGLSL450MemoryModel + R"(
%1 = OpTypeVoid
%2 = OpTypeInt 32 0
@ -2065,7 +2066,8 @@ TEST_F(ValidateIdWithMessage, OpStorePointerBad) {
HasSubstr("OpStore Pointer <id> '3' is not a logical pointer."));
}
TEST_F(ValidateIdWithMessage, OpStoreLogicalPointerBad) {
// Disabled as bitcasting type to object is now not valid.
TEST_F(ValidateIdWithMessage, DISABLED_OpStoreLogicalPointerBad) {
string spirv = kGLSL450MemoryModel + R"(
%1 = OpTypeVoid
%2 = OpTypeInt 32 0