2016-01-14 04:25:11 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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
|
2016-01-14 04:25:11 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-01-14 04:25:11 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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.
|
2016-01-14 04:25:11 +00:00
|
|
|
|
|
|
|
// Performs validation on instructions that appear inside of a SPIR-V block.
|
|
|
|
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/val/validate.h"
|
2016-06-02 22:51:05 +00:00
|
|
|
|
2016-12-04 15:48:26 +00:00
|
|
|
#include <algorithm>
|
2016-01-23 19:14:32 +00:00
|
|
|
#include <cassert>
|
2016-06-02 22:51:05 +00:00
|
|
|
|
2016-01-31 04:32:09 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/binary.h"
|
|
|
|
#include "source/diagnostic.h"
|
|
|
|
#include "source/enum_set.h"
|
|
|
|
#include "source/enum_string_mapping.h"
|
|
|
|
#include "source/extensions.h"
|
|
|
|
#include "source/opcode.h"
|
|
|
|
#include "source/operand.h"
|
|
|
|
#include "source/spirv_constant.h"
|
|
|
|
#include "source/spirv_definition.h"
|
|
|
|
#include "source/spirv_target_env.h"
|
|
|
|
#include "source/spirv_validator_options.h"
|
|
|
|
#include "source/util/string_utils.h"
|
|
|
|
#include "source/val/function.h"
|
|
|
|
#include "source/val/validation_state.h"
|
2016-01-14 04:25:11 +00:00
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2018-07-10 03:18:44 +00:00
|
|
|
namespace val {
|
2016-01-23 19:14:32 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-08-29 18:49:00 +00:00
|
|
|
std::string ToString(const CapabilitySet& capabilities,
|
2016-01-31 04:32:09 +00:00
|
|
|
const AssemblyGrammar& grammar) {
|
|
|
|
std::stringstream ss;
|
2016-08-29 18:49:00 +00:00
|
|
|
capabilities.ForEach([&grammar, &ss](SpvCapability cap) {
|
2016-01-31 04:32:09 +00:00
|
|
|
spv_operand_desc desc;
|
|
|
|
if (SPV_SUCCESS ==
|
|
|
|
grammar.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY, cap, &desc))
|
|
|
|
ss << desc->name << " ";
|
|
|
|
else
|
|
|
|
ss << cap << " ";
|
|
|
|
});
|
|
|
|
return ss.str();
|
2016-01-23 19:14:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 04:32:09 +00:00
|
|
|
// Reports a missing-capability error to _'s diagnostic stream and returns
|
|
|
|
// SPV_ERROR_INVALID_CAPABILITY.
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t CapabilityError(const ValidationState_t& _, size_t which_operand,
|
2016-01-31 04:32:09 +00:00
|
|
|
SpvOp opcode,
|
|
|
|
const std::string& required_capabilities) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_CAPABILITY)
|
|
|
|
<< "Operand " << which_operand << " of " << spvOpcodeString(opcode)
|
|
|
|
<< " requires one of these capabilities: " << required_capabilities;
|
2016-01-23 19:14:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 19:18:17 +00:00
|
|
|
// Returns capabilities that enable an opcode. An empty result is interpreted
|
|
|
|
// as no prohibition of use of the opcode. If the result is non-empty, then
|
|
|
|
// the opcode may only be used if at least one of the capabilities is specified
|
|
|
|
// by the module.
|
|
|
|
CapabilitySet EnablingCapabilitiesForOp(const ValidationState_t& state,
|
|
|
|
SpvOp opcode) {
|
|
|
|
// Exceptions for SPV_AMD_shader_ballot
|
|
|
|
switch (opcode) {
|
|
|
|
// Normally these would require Group capability
|
|
|
|
case SpvOpGroupIAddNonUniformAMD:
|
|
|
|
case SpvOpGroupFAddNonUniformAMD:
|
|
|
|
case SpvOpGroupFMinNonUniformAMD:
|
|
|
|
case SpvOpGroupUMinNonUniformAMD:
|
|
|
|
case SpvOpGroupSMinNonUniformAMD:
|
|
|
|
case SpvOpGroupFMaxNonUniformAMD:
|
|
|
|
case SpvOpGroupUMaxNonUniformAMD:
|
|
|
|
case SpvOpGroupSMaxNonUniformAMD:
|
2018-06-20 17:29:38 +00:00
|
|
|
if (state.HasExtension(kSPV_AMD_shader_ballot)) return CapabilitySet();
|
2017-11-24 19:18:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Look it up in the grammar
|
|
|
|
spv_opcode_desc opcode_desc = {};
|
|
|
|
if (SPV_SUCCESS == state.grammar().lookupOpcode(opcode, &opcode_desc)) {
|
2018-03-14 17:06:18 +00:00
|
|
|
return state.grammar().filterCapsAgainstTargetEnv(
|
|
|
|
opcode_desc->capabilities, opcode_desc->numCapabilities);
|
2017-11-24 19:18:17 +00:00
|
|
|
}
|
|
|
|
return CapabilitySet();
|
|
|
|
}
|
|
|
|
|
2018-06-06 06:07:51 +00:00
|
|
|
// Returns SPV_SUCCESS if the given operand is enabled by capabilities declared
|
|
|
|
// in the module. Otherwise issues an error message and returns
|
|
|
|
// SPV_ERROR_INVALID_CAPABILITY.
|
|
|
|
spv_result_t CheckRequiredCapabilities(const ValidationState_t& state,
|
2018-07-10 14:57:52 +00:00
|
|
|
SpvOp opcode, size_t which_operand,
|
2018-06-06 06:07:51 +00:00
|
|
|
spv_operand_type_t type,
|
|
|
|
uint32_t operand) {
|
2016-08-29 18:49:00 +00:00
|
|
|
// Mere mention of PointSize, ClipDistance, or CullDistance in a Builtin
|
|
|
|
// decoration does not require the associated capability. The use of such
|
|
|
|
// a variable value should trigger the capability requirement, but that's
|
|
|
|
// not implemented yet. This rule is independent of target environment.
|
|
|
|
// See https://github.com/KhronosGroup/SPIRV-Tools/issues/365
|
|
|
|
if (type == SPV_OPERAND_TYPE_BUILT_IN) {
|
|
|
|
switch (operand) {
|
|
|
|
case SpvBuiltInPointSize:
|
|
|
|
case SpvBuiltInClipDistance:
|
|
|
|
case SpvBuiltInCullDistance:
|
2018-06-06 06:07:51 +00:00
|
|
|
return SPV_SUCCESS;
|
2016-08-29 18:49:00 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-03-01 20:51:44 +00:00
|
|
|
} else if (type == SPV_OPERAND_TYPE_FP_ROUNDING_MODE) {
|
|
|
|
// Allow all FP rounding modes if requested
|
|
|
|
if (state.features().free_fp_rounding_mode) {
|
2018-06-06 06:07:51 +00:00
|
|
|
return SPV_SUCCESS;
|
2017-03-01 20:51:44 +00:00
|
|
|
}
|
2018-06-06 06:07:51 +00:00
|
|
|
} else if (type == SPV_OPERAND_TYPE_GROUP_OPERATION &&
|
|
|
|
state.features().group_ops_reduce_and_scans &&
|
|
|
|
(operand <= uint32_t(SpvGroupOperationExclusiveScan))) {
|
|
|
|
// Allow certain group operations if requested.
|
|
|
|
return SPV_SUCCESS;
|
2016-08-29 18:49:00 +00:00
|
|
|
}
|
2016-07-07 21:03:22 +00:00
|
|
|
|
2018-06-06 06:07:51 +00:00
|
|
|
CapabilitySet enabling_capabilities;
|
|
|
|
spv_operand_desc operand_desc = nullptr;
|
|
|
|
const auto lookup_result =
|
|
|
|
state.grammar().lookupOperand(type, operand, &operand_desc);
|
|
|
|
if (lookup_result == SPV_SUCCESS) {
|
2017-11-24 19:18:17 +00:00
|
|
|
// Allow FPRoundingMode decoration if requested.
|
2018-03-14 17:06:18 +00:00
|
|
|
if (type == SPV_OPERAND_TYPE_DECORATION &&
|
2017-03-01 20:51:44 +00:00
|
|
|
operand_desc->value == SpvDecorationFPRoundingMode) {
|
2018-06-06 06:07:51 +00:00
|
|
|
if (state.features().free_fp_rounding_mode) return SPV_SUCCESS;
|
2018-03-14 17:06:18 +00:00
|
|
|
|
|
|
|
// Vulkan API requires more capabilities on rounding mode.
|
|
|
|
if (spvIsVulkanEnv(state.context()->target_env)) {
|
2018-06-06 06:07:51 +00:00
|
|
|
enabling_capabilities.Add(SpvCapabilityStorageUniformBufferBlock16);
|
|
|
|
enabling_capabilities.Add(SpvCapabilityStorageUniform16);
|
|
|
|
enabling_capabilities.Add(SpvCapabilityStoragePushConstant16);
|
|
|
|
enabling_capabilities.Add(SpvCapabilityStorageInputOutput16);
|
2018-03-14 17:06:18 +00:00
|
|
|
}
|
2018-06-06 06:07:51 +00:00
|
|
|
} else {
|
|
|
|
enabling_capabilities = state.grammar().filterCapsAgainstTargetEnv(
|
|
|
|
operand_desc->capabilities, operand_desc->numCapabilities);
|
2017-11-24 19:18:17 +00:00
|
|
|
}
|
2018-03-14 17:06:18 +00:00
|
|
|
|
2018-06-06 06:07:51 +00:00
|
|
|
if (!state.HasAnyOfCapabilities(enabling_capabilities)) {
|
|
|
|
return CapabilityError(state, which_operand, opcode,
|
|
|
|
ToString(enabling_capabilities, state.grammar()));
|
|
|
|
}
|
2016-07-07 21:03:22 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 06:07:51 +00:00
|
|
|
return SPV_SUCCESS;
|
2016-01-23 19:14:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 20:58:15 +00:00
|
|
|
// Returns operand's required extensions.
|
|
|
|
ExtensionSet RequiredExtensions(const ValidationState_t& state,
|
|
|
|
spv_operand_type_t type, uint32_t operand) {
|
|
|
|
spv_operand_desc operand_desc;
|
|
|
|
if (state.grammar().lookupOperand(type, operand, &operand_desc) ==
|
|
|
|
SPV_SUCCESS) {
|
|
|
|
assert(operand_desc);
|
2018-03-27 18:07:11 +00:00
|
|
|
// If this operand is incorporated into core SPIR-V before or in the current
|
|
|
|
// target environment, we don't require extensions anymore.
|
|
|
|
if (spvVersionForTargetEnv(state.grammar().target_env()) >=
|
|
|
|
operand_desc->minVersion)
|
|
|
|
return {};
|
2017-10-25 16:15:51 +00:00
|
|
|
return {operand_desc->numExtensions, operand_desc->extensions};
|
2017-03-10 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 18:07:11 +00:00
|
|
|
return {};
|
2017-03-10 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:29:38 +00:00
|
|
|
// Returns SPV_ERROR_INVALID_BINARY and emits a diagnostic if the instruction
|
2018-06-19 13:54:33 +00:00
|
|
|
// is explicitly reserved in the SPIR-V core spec. Otherwise return
|
|
|
|
// SPV_SUCCESS.
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t ReservedCheck(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const SpvOp opcode = inst->opcode();
|
2018-06-19 13:54:33 +00:00
|
|
|
switch (opcode) {
|
|
|
|
// These instructions are enabled by a capability, but should never
|
|
|
|
// be used anyway.
|
|
|
|
case SpvOpImageSparseSampleProjImplicitLod:
|
|
|
|
case SpvOpImageSparseSampleProjExplicitLod:
|
|
|
|
case SpvOpImageSparseSampleProjDrefImplicitLod:
|
|
|
|
case SpvOpImageSparseSampleProjDrefExplicitLod: {
|
|
|
|
spv_opcode_desc inst_desc;
|
|
|
|
_.grammar().lookupOpcode(opcode, &inst_desc);
|
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY)
|
|
|
|
<< "Invalid Opcode name 'Op" << inst_desc->name << "'";
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:29:38 +00:00
|
|
|
// Returns SPV_ERROR_INVALID_BINARY and emits a diagnostic if the instruction
|
|
|
|
// is invalid because of an execution environment constraint.
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t EnvironmentCheck(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const SpvOp opcode = inst->opcode();
|
2018-06-20 17:29:38 +00:00
|
|
|
switch (opcode) {
|
|
|
|
case SpvOpUndef:
|
|
|
|
if (_.features().bans_op_undef) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY) << "OpUndef is disallowed";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns SPV_ERROR_INVALID_CAPABILITY and emits a diagnostic if the
|
|
|
|
// instruction is invalid because the required capability isn't declared
|
|
|
|
// in the module.
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t CapabilityCheck(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const SpvOp opcode = inst->opcode();
|
2017-11-24 19:18:17 +00:00
|
|
|
CapabilitySet opcode_caps = EnablingCapabilitiesForOp(_, opcode);
|
|
|
|
if (!_.HasAnyOfCapabilities(opcode_caps)) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_CAPABILITY)
|
|
|
|
<< "Opcode " << spvOpcodeString(opcode)
|
|
|
|
<< " requires one of these capabilities: "
|
|
|
|
<< ToString(opcode_caps, _.grammar());
|
2017-10-25 16:15:51 +00:00
|
|
|
}
|
2018-07-10 14:57:52 +00:00
|
|
|
for (size_t i = 0; i < inst->operands().size(); ++i) {
|
|
|
|
const auto& operand = inst->operand(i);
|
|
|
|
const auto word = inst->word(operand.offset);
|
2016-02-02 17:12:48 +00:00
|
|
|
if (spvOperandIsConcreteMask(operand.type)) {
|
|
|
|
// Check for required capabilities for each bit position of the mask.
|
|
|
|
for (uint32_t mask_bit = 0x80000000; mask_bit; mask_bit >>= 1) {
|
|
|
|
if (word & mask_bit) {
|
2018-06-06 06:07:51 +00:00
|
|
|
spv_result_t status = CheckRequiredCapabilities(
|
|
|
|
_, opcode, i + 1, operand.type, mask_bit);
|
|
|
|
if (status != SPV_SUCCESS) return status;
|
2016-01-31 04:32:09 +00:00
|
|
|
}
|
2016-02-02 17:12:48 +00:00
|
|
|
}
|
2016-06-28 21:25:28 +00:00
|
|
|
} else if (spvIsIdType(operand.type)) {
|
|
|
|
// TODO(dneto): Check the value referenced by this Id, if we can compute
|
|
|
|
// it. For now, just punt, to fix issue 248:
|
|
|
|
// https://github.com/KhronosGroup/SPIRV-Tools/issues/248
|
2016-02-02 17:12:48 +00:00
|
|
|
} else {
|
|
|
|
// Check the operand word as a whole.
|
2018-06-06 06:07:51 +00:00
|
|
|
spv_result_t status =
|
|
|
|
CheckRequiredCapabilities(_, opcode, i + 1, operand.type, word);
|
|
|
|
if (status != SPV_SUCCESS) return status;
|
2016-02-02 17:12:48 +00:00
|
|
|
}
|
2016-01-23 19:14:32 +00:00
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
2016-01-14 04:25:11 +00:00
|
|
|
|
2018-03-14 17:06:18 +00:00
|
|
|
// Checks that all extensions required by the given instruction's operands were
|
|
|
|
// declared in the module.
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t ExtensionCheck(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const SpvOp opcode = inst->opcode();
|
|
|
|
for (size_t operand_index = 0; operand_index < inst->operands().size();
|
2017-03-10 20:58:15 +00:00
|
|
|
++operand_index) {
|
2018-07-10 14:57:52 +00:00
|
|
|
const auto& operand = inst->operand(operand_index);
|
|
|
|
const uint32_t word = inst->word(operand.offset);
|
2017-03-10 20:58:15 +00:00
|
|
|
const ExtensionSet required_extensions =
|
|
|
|
RequiredExtensions(_, operand.type, word);
|
|
|
|
if (!_.HasAnyOfExtensions(required_extensions)) {
|
|
|
|
return _.diag(SPV_ERROR_MISSING_EXTENSION)
|
2018-07-06 17:25:17 +00:00
|
|
|
<< spvtools::utils::CardinalToOrdinal(operand_index + 1)
|
|
|
|
<< " operand of " << spvOpcodeString(opcode) << ": operand "
|
|
|
|
<< word << " requires one of these extensions: "
|
2017-10-25 16:15:51 +00:00
|
|
|
<< ExtensionSetToString(required_extensions);
|
2017-03-10 20:58:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:29:38 +00:00
|
|
|
// Checks that the instruction can be used in this target environment's base
|
|
|
|
// version. Assumes that CapabilityCheck has checked direct capability
|
|
|
|
// dependencies for the opcode.
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t VersionCheck(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const auto opcode = inst->opcode();
|
2018-03-14 17:06:18 +00:00
|
|
|
spv_opcode_desc inst_desc;
|
2018-07-09 16:42:02 +00:00
|
|
|
const spv_result_t r = _.grammar().lookupOpcode(opcode, &inst_desc);
|
2018-03-14 17:06:18 +00:00
|
|
|
assert(r == SPV_SUCCESS);
|
|
|
|
(void)r;
|
|
|
|
|
|
|
|
const auto min_version = inst_desc->minVersion;
|
|
|
|
|
2018-06-19 13:54:33 +00:00
|
|
|
if (inst_desc->numCapabilities > 0u) {
|
|
|
|
// We already checked that the direct capability dependency has been
|
|
|
|
// satisfied. We don't need to check any further.
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-03-14 17:06:18 +00:00
|
|
|
ExtensionSet exts(inst_desc->numExtensions, inst_desc->extensions);
|
|
|
|
if (exts.IsEmpty()) {
|
|
|
|
// If no extensions can enable this instruction, then emit error messages
|
|
|
|
// only concerning core SPIR-V versions if errors happen.
|
|
|
|
if (min_version == ~0u) {
|
|
|
|
return _.diag(SPV_ERROR_WRONG_VERSION)
|
2017-10-25 16:15:51 +00:00
|
|
|
<< spvOpcodeString(opcode) << " is reserved for future use.";
|
2018-03-14 17:06:18 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 20:42:44 +00:00
|
|
|
if (spvVersionForTargetEnv(_.grammar().target_env()) < min_version) {
|
2018-03-14 17:06:18 +00:00
|
|
|
return _.diag(SPV_ERROR_WRONG_VERSION)
|
|
|
|
<< spvOpcodeString(opcode) << " requires "
|
|
|
|
<< spvTargetEnvDescription(
|
|
|
|
static_cast<spv_target_env>(min_version))
|
|
|
|
<< " at minimum.";
|
|
|
|
}
|
|
|
|
// Otherwise, we only error out when no enabling extensions are registered.
|
2018-07-10 17:09:46 +00:00
|
|
|
} else if (!_.HasAnyOfExtensions(exts)) {
|
2018-03-14 17:06:18 +00:00
|
|
|
if (min_version == ~0u) {
|
|
|
|
return _.diag(SPV_ERROR_MISSING_EXTENSION)
|
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< " requires one of the following extensions: "
|
|
|
|
<< ExtensionSetToString(exts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (static_cast<uint32_t>(_.grammar().target_env()) < min_version) {
|
|
|
|
return _.diag(SPV_ERROR_WRONG_VERSION)
|
|
|
|
<< spvOpcodeString(opcode) << " requires "
|
|
|
|
<< spvTargetEnvDescription(
|
|
|
|
static_cast<spv_target_env>(min_version))
|
|
|
|
<< " at minimum or one of the following extensions: "
|
|
|
|
<< ExtensionSetToString(exts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
2017-02-15 18:31:07 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 20:37:22 +00:00
|
|
|
// Checks that the Resuld <id> is within the valid bound.
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t LimitCheckIdBound(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
if (inst->id() >= _.getIdBound()) {
|
2016-11-24 20:37:22 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY)
|
2018-07-10 14:57:52 +00:00
|
|
|
<< "Result <id> '" << inst->id()
|
2016-11-24 20:37:22 +00:00
|
|
|
<< "' must be less than the ID bound '" << _.getIdBound() << "'.";
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-11-24 20:37:22 +00:00
|
|
|
// Checks that the number of OpTypeStruct members is within the limit.
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t LimitCheckStruct(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
if (SpvOpTypeStruct != inst->opcode()) {
|
2016-12-04 15:48:26 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-11-24 20:37:22 +00:00
|
|
|
// Number of members is the number of operands of the instruction minus 1.
|
|
|
|
// One operand is the result ID.
|
2017-02-15 18:29:33 +00:00
|
|
|
const uint16_t limit =
|
|
|
|
static_cast<uint16_t>(_.options()->universal_limits_.max_struct_members);
|
2018-07-10 14:57:52 +00:00
|
|
|
if (inst->operands().size() - 1 > limit) {
|
2016-11-24 20:37:22 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY)
|
2018-07-10 14:57:52 +00:00
|
|
|
<< "Number of OpTypeStruct members (" << inst->operands().size() - 1
|
2016-11-24 20:37:22 +00:00
|
|
|
<< ") has exceeded the limit (" << limit << ").";
|
|
|
|
}
|
2016-12-04 15:48:26 +00:00
|
|
|
|
|
|
|
// Section 2.17 of SPIRV Spec specifies that the "Structure Nesting Depth"
|
|
|
|
// must be less than or equal to 255.
|
|
|
|
// This is interpreted as structures including other structures as members.
|
|
|
|
// The code does not follow pointers or look into arrays to see if we reach a
|
|
|
|
// structure downstream.
|
|
|
|
// The nesting depth of a struct is 1+(largest depth of any member).
|
|
|
|
// Scalars are at depth 0.
|
|
|
|
uint32_t max_member_depth = 0;
|
|
|
|
// Struct members start at word 2 of OpTypeStruct instruction.
|
2018-07-10 14:57:52 +00:00
|
|
|
for (size_t word_i = 2; word_i < inst->words().size(); ++word_i) {
|
|
|
|
auto member = inst->word(word_i);
|
2016-12-04 15:48:26 +00:00
|
|
|
auto memberTypeInstr = _.FindDef(member);
|
|
|
|
if (memberTypeInstr && SpvOpTypeStruct == memberTypeInstr->opcode()) {
|
|
|
|
max_member_depth = std::max(
|
|
|
|
max_member_depth, _.struct_nesting_depth(memberTypeInstr->id()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 18:29:33 +00:00
|
|
|
const uint32_t depth_limit = _.options()->universal_limits_.max_struct_depth;
|
2016-12-04 15:48:26 +00:00
|
|
|
const uint32_t cur_depth = 1 + max_member_depth;
|
2018-07-10 14:57:52 +00:00
|
|
|
_.set_struct_nesting_depth(inst->id(), cur_depth);
|
2016-12-04 15:48:26 +00:00
|
|
|
if (cur_depth > depth_limit) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY)
|
|
|
|
<< "Structure Nesting Depth may not be larger than " << depth_limit
|
|
|
|
<< ". Found " << cur_depth << ".";
|
|
|
|
}
|
2016-11-24 20:37:22 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-11-25 14:26:26 +00:00
|
|
|
// Checks that the number of (literal, label) pairs in OpSwitch is within the
|
|
|
|
// limit.
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t LimitCheckSwitch(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
if (SpvOpSwitch == inst->opcode()) {
|
2016-11-25 14:26:26 +00:00
|
|
|
// The instruction syntax is as follows:
|
|
|
|
// OpSwitch <selector ID> <Default ID> literal label literal label ...
|
|
|
|
// literal,label pairs come after the first 2 operands.
|
|
|
|
// It is guaranteed at this point that num_operands is an even numner.
|
2018-07-10 14:57:52 +00:00
|
|
|
size_t num_pairs = (inst->operands().size() - 2) / 2;
|
2017-02-15 18:29:33 +00:00
|
|
|
const unsigned int num_pairs_limit =
|
|
|
|
_.options()->universal_limits_.max_switch_branches;
|
2016-11-25 14:26:26 +00:00
|
|
|
if (num_pairs > num_pairs_limit) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY)
|
|
|
|
<< "Number of (literal, label) pairs in OpSwitch (" << num_pairs
|
|
|
|
<< ") exceeds the limit (" << num_pairs_limit << ").";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:50:34 +00:00
|
|
|
// Ensure the number of variables of the given class does not exceed the limit.
|
2017-01-13 16:46:21 +00:00
|
|
|
spv_result_t LimitCheckNumVars(ValidationState_t& _, const uint32_t var_id,
|
2016-11-29 20:50:34 +00:00
|
|
|
const SpvStorageClass storage_class) {
|
|
|
|
if (SpvStorageClassFunction == storage_class) {
|
2017-01-13 16:46:21 +00:00
|
|
|
_.registerLocalVariable(var_id);
|
2017-02-15 18:29:33 +00:00
|
|
|
const uint32_t num_local_vars_limit =
|
|
|
|
_.options()->universal_limits_.max_local_variables;
|
2016-11-29 20:50:34 +00:00
|
|
|
if (_.num_local_vars() > num_local_vars_limit) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY)
|
|
|
|
<< "Number of local variables ('Function' Storage Class) "
|
|
|
|
"exceeded the valid limit ("
|
|
|
|
<< num_local_vars_limit << ").";
|
|
|
|
}
|
|
|
|
} else {
|
2017-01-13 16:46:21 +00:00
|
|
|
_.registerGlobalVariable(var_id);
|
2017-02-15 18:29:33 +00:00
|
|
|
const uint32_t num_global_vars_limit =
|
|
|
|
_.options()->universal_limits_.max_global_variables;
|
2016-11-29 20:50:34 +00:00
|
|
|
if (_.num_global_vars() > num_global_vars_limit) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY)
|
|
|
|
<< "Number of Global Variables (Storage Class other than "
|
|
|
|
"'Function') exceeded the valid limit ("
|
|
|
|
<< num_global_vars_limit << ").";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:51:23 +00:00
|
|
|
// Registers necessary decoration(s) for the appropriate IDs based on the
|
|
|
|
// instruction.
|
|
|
|
spv_result_t RegisterDecorations(ValidationState_t& _,
|
2018-07-10 14:57:52 +00:00
|
|
|
const Instruction* inst) {
|
|
|
|
switch (inst->opcode()) {
|
2017-01-11 15:51:23 +00:00
|
|
|
case SpvOpDecorate: {
|
2018-07-10 14:57:52 +00:00
|
|
|
const uint32_t target_id = inst->word(1);
|
|
|
|
const SpvDecoration dec_type = static_cast<SpvDecoration>(inst->word(2));
|
2017-01-11 15:51:23 +00:00
|
|
|
std::vector<uint32_t> dec_params;
|
2018-07-10 14:57:52 +00:00
|
|
|
if (inst->words().size() > 3) {
|
|
|
|
dec_params.insert(dec_params.end(), inst->words().begin() + 3,
|
|
|
|
inst->words().end());
|
2017-01-11 15:51:23 +00:00
|
|
|
}
|
|
|
|
_.RegisterDecorationForId(target_id, Decoration(dec_type, dec_params));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SpvOpMemberDecorate: {
|
2018-07-10 14:57:52 +00:00
|
|
|
const uint32_t struct_id = inst->word(1);
|
|
|
|
const uint32_t index = inst->word(2);
|
|
|
|
const SpvDecoration dec_type = static_cast<SpvDecoration>(inst->word(3));
|
2017-01-11 15:51:23 +00:00
|
|
|
std::vector<uint32_t> dec_params;
|
2018-07-10 14:57:52 +00:00
|
|
|
if (inst->words().size() > 4) {
|
|
|
|
dec_params.insert(dec_params.end(), inst->words().begin() + 4,
|
|
|
|
inst->words().end());
|
2017-01-11 15:51:23 +00:00
|
|
|
}
|
|
|
|
_.RegisterDecorationForId(struct_id,
|
|
|
|
Decoration(dec_type, dec_params, index));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SpvOpDecorationGroup: {
|
|
|
|
// We don't need to do anything right now. Assigning decorations to groups
|
|
|
|
// will be taken care of via OpGroupDecorate.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SpvOpGroupDecorate: {
|
|
|
|
// Word 1 is the group <id>. All subsequent words are target <id>s that
|
|
|
|
// are going to be decorated with the decorations.
|
2018-07-10 14:57:52 +00:00
|
|
|
const uint32_t decoration_group_id = inst->word(1);
|
2017-01-11 15:51:23 +00:00
|
|
|
std::vector<Decoration>& group_decorations =
|
|
|
|
_.id_decorations(decoration_group_id);
|
2018-07-10 14:57:52 +00:00
|
|
|
for (size_t i = 2; i < inst->words().size(); ++i) {
|
|
|
|
const uint32_t target_id = inst->word(i);
|
2017-01-11 15:51:23 +00:00
|
|
|
_.RegisterDecorationsForId(target_id, group_decorations.begin(),
|
|
|
|
group_decorations.end());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SpvOpGroupMemberDecorate: {
|
|
|
|
// Word 1 is the Decoration Group <id> followed by (struct<id>,literal)
|
|
|
|
// pairs. All decorations of the group should be applied to all the struct
|
|
|
|
// members that are specified in the instructions.
|
2018-07-10 14:57:52 +00:00
|
|
|
const uint32_t decoration_group_id = inst->word(1);
|
2017-01-11 15:51:23 +00:00
|
|
|
std::vector<Decoration>& group_decorations =
|
|
|
|
_.id_decorations(decoration_group_id);
|
|
|
|
// Grammar checks ensures that the number of arguments to this instruction
|
|
|
|
// is an odd number: 1 decoration group + (id,literal) pairs.
|
2018-07-10 14:57:52 +00:00
|
|
|
for (size_t i = 2; i + 1 < inst->words().size(); i = i + 2) {
|
|
|
|
const uint32_t struct_id = inst->word(i);
|
|
|
|
const uint32_t index = inst->word(i + 1);
|
2017-01-11 15:51:23 +00:00
|
|
|
// ID validation phase ensures this is in fact a struct instruction and
|
|
|
|
// that the index is not out of bound.
|
|
|
|
_.RegisterDecorationsForStructMember(struct_id, index,
|
|
|
|
group_decorations.begin(),
|
|
|
|
group_decorations.end());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-03-10 20:58:15 +00:00
|
|
|
// Parses OpExtension instruction and logs warnings if unsuccessful.
|
2018-07-10 14:57:52 +00:00
|
|
|
void CheckIfKnownExtension(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const std::string extension_str = GetExtensionString(&(inst->c_inst()));
|
2017-03-08 18:59:01 +00:00
|
|
|
Extension extension;
|
2018-02-15 15:19:15 +00:00
|
|
|
if (!GetExtensionFromString(extension_str.c_str(), &extension)) {
|
2017-03-10 20:58:15 +00:00
|
|
|
_.diag(SPV_SUCCESS) << "Found unrecognized extension " << extension_str;
|
2017-03-08 18:59:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:29:38 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t InstructionPass(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const SpvOp opcode = inst->opcode();
|
2018-03-27 15:13:39 +00:00
|
|
|
if (opcode == SpvOpExtension) {
|
|
|
|
CheckIfKnownExtension(_, inst);
|
|
|
|
} else if (opcode == SpvOpCapability) {
|
2018-07-10 14:57:52 +00:00
|
|
|
_.RegisterCapability(inst->GetOperandAs<SpvCapability>(0));
|
2018-03-27 15:13:39 +00:00
|
|
|
} else if (opcode == SpvOpMemoryModel) {
|
2018-05-22 17:13:14 +00:00
|
|
|
if (_.has_memory_model_specified()) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT)
|
|
|
|
<< "OpMemoryModel should only be provided once.";
|
|
|
|
}
|
2018-07-10 14:57:52 +00:00
|
|
|
_.set_addressing_model(inst->GetOperandAs<SpvAddressingModel>(0));
|
|
|
|
_.set_memory_model(inst->GetOperandAs<SpvMemoryModel>(1));
|
2018-03-27 15:13:39 +00:00
|
|
|
} else if (opcode == SpvOpExecutionMode) {
|
2018-07-10 14:57:52 +00:00
|
|
|
const uint32_t entry_point = inst->word(1);
|
2018-03-27 15:13:39 +00:00
|
|
|
_.RegisterExecutionModeForEntryPoint(entry_point,
|
2018-07-10 14:57:52 +00:00
|
|
|
SpvExecutionMode(inst->word(2)));
|
2018-03-27 15:13:39 +00:00
|
|
|
} else if (opcode == SpvOpVariable) {
|
2018-07-10 14:57:52 +00:00
|
|
|
const auto storage_class = inst->GetOperandAs<SpvStorageClass>(2);
|
|
|
|
if (auto error = LimitCheckNumVars(_, inst->id(), storage_class)) {
|
2016-11-29 20:50:34 +00:00
|
|
|
return error;
|
|
|
|
}
|
2016-02-22 20:25:50 +00:00
|
|
|
if (storage_class == SpvStorageClassGeneric)
|
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY)
|
|
|
|
<< "OpVariable storage class cannot be Generic";
|
2016-06-25 03:45:25 +00:00
|
|
|
if (_.current_layout_section() == kLayoutFunctionDefinitions) {
|
2016-02-22 20:25:50 +00:00
|
|
|
if (storage_class != SpvStorageClassFunction) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT)
|
|
|
|
<< "Variables must have a function[7] storage class inside"
|
|
|
|
" of a function";
|
|
|
|
}
|
2016-06-25 03:45:25 +00:00
|
|
|
if (_.current_function().IsFirstBlock(
|
|
|
|
_.current_function().current_block()->id()) == false) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_CFG) << "Variables can only be defined "
|
|
|
|
"in the first block of a "
|
|
|
|
"function";
|
2016-03-16 21:20:02 +00:00
|
|
|
}
|
2016-02-22 20:25:50 +00:00
|
|
|
} else {
|
|
|
|
if (storage_class == SpvStorageClassFunction) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT)
|
|
|
|
<< "Variables can not have a function[7] storage class "
|
|
|
|
"outside of a function";
|
|
|
|
}
|
2016-01-14 04:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-24 20:37:22 +00:00
|
|
|
|
2017-02-09 21:23:40 +00:00
|
|
|
// SPIR-V Spec 2.16.3: Validation Rules for Kernel Capabilities: The
|
|
|
|
// Signedness in OpTypeInt must always be 0.
|
2018-07-10 14:57:52 +00:00
|
|
|
if (SpvOpTypeInt == inst->opcode() && _.HasCapability(SpvCapabilityKernel) &&
|
|
|
|
inst->GetOperandAs<uint32_t>(2) != 0u) {
|
2017-02-09 21:23:40 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY) << "The Signedness in OpTypeInt "
|
|
|
|
"must always be 0 when Kernel "
|
|
|
|
"capability is used.";
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:51:23 +00:00
|
|
|
// In order to validate decoration rules, we need to know all the decorations
|
|
|
|
// that are applied to any given <id>.
|
|
|
|
RegisterDecorations(_, inst);
|
|
|
|
|
2017-03-10 20:58:15 +00:00
|
|
|
if (auto error = ExtensionCheck(_, inst)) return error;
|
2018-06-19 13:54:33 +00:00
|
|
|
if (auto error = ReservedCheck(_, inst)) return error;
|
2018-06-20 17:29:38 +00:00
|
|
|
if (auto error = EnvironmentCheck(_, inst)) return error;
|
2017-03-10 20:58:15 +00:00
|
|
|
if (auto error = CapabilityCheck(_, inst)) return error;
|
2016-11-24 20:37:22 +00:00
|
|
|
if (auto error = LimitCheckIdBound(_, inst)) return error;
|
2016-11-24 20:37:22 +00:00
|
|
|
if (auto error = LimitCheckStruct(_, inst)) return error;
|
2016-11-25 14:26:26 +00:00
|
|
|
if (auto error = LimitCheckSwitch(_, inst)) return error;
|
2018-03-14 17:06:18 +00:00
|
|
|
if (auto error = VersionCheck(_, inst)) return error;
|
2016-11-24 20:37:22 +00:00
|
|
|
|
|
|
|
// All instruction checks have passed.
|
|
|
|
return SPV_SUCCESS;
|
2016-01-14 04:25:11 +00:00
|
|
|
}
|
2018-07-10 03:18:44 +00:00
|
|
|
|
|
|
|
} // namespace val
|
2018-07-07 13:38:00 +00:00
|
|
|
} // namespace spvtools
|