2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
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
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-05-22 17:26:19 +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.
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/val/validate.h"
|
2016-06-02 22:51:05 +00:00
|
|
|
|
2016-03-16 21:20:02 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
2017-06-03 23:08:22 +00:00
|
|
|
#include <memory>
|
2016-03-16 21:20:02 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/binary.h"
|
|
|
|
#include "source/diagnostic.h"
|
|
|
|
#include "source/enum_string_mapping.h"
|
|
|
|
#include "source/extensions.h"
|
|
|
|
#include "source/instruction.h"
|
|
|
|
#include "source/opcode.h"
|
|
|
|
#include "source/operand.h"
|
|
|
|
#include "source/spirv_constant.h"
|
|
|
|
#include "source/spirv_endian.h"
|
|
|
|
#include "source/spirv_target_env.h"
|
|
|
|
#include "source/spirv_validator_options.h"
|
|
|
|
#include "source/val/construct.h"
|
|
|
|
#include "source/val/function.h"
|
|
|
|
#include "source/val/instruction.h"
|
|
|
|
#include "source/val/validation_state.h"
|
2016-02-22 20:25:50 +00:00
|
|
|
#include "spirv-tools/libspirv.h"
|
2015-11-11 17:45:23 +00:00
|
|
|
|
2018-10-03 19:59:40 +00:00
|
|
|
namespace {
|
|
|
|
// TODO(issue 1950): The validator only returns a single message anyway, so no
|
|
|
|
// point in generating more than 1 warning.
|
|
|
|
static uint32_t kDefaultMaxNumOfWarnings = 1;
|
|
|
|
} // namespace
|
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
namespace spvtools {
|
|
|
|
namespace val {
|
|
|
|
namespace {
|
2015-09-11 18:31:27 +00:00
|
|
|
|
2015-11-18 20:43:43 +00:00
|
|
|
// TODO(umar): Validate header
|
|
|
|
// TODO(umar): The binary parser validates the magic word, and the length of the
|
|
|
|
// header, but nothing else.
|
2018-08-02 16:01:26 +00:00
|
|
|
spv_result_t setHeader(void* user_data, spv_endianness_t, uint32_t,
|
2015-11-18 20:43:43 +00:00
|
|
|
uint32_t version, uint32_t generator, uint32_t id_bound,
|
2018-08-02 16:01:26 +00:00
|
|
|
uint32_t) {
|
2016-11-24 20:37:22 +00:00
|
|
|
// Record the ID bound so that the validator can ensure no ID is out of bound.
|
|
|
|
ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data));
|
|
|
|
_.setIdBound(id_bound);
|
2018-08-02 16:01:26 +00:00
|
|
|
_.setGenerator(generator);
|
|
|
|
_.setVersion(version);
|
2016-11-24 20:37:22 +00:00
|
|
|
|
2015-11-18 20:43:43 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-03-10 20:58:15 +00:00
|
|
|
// Parses OpExtension instruction and registers extension.
|
|
|
|
void RegisterExtension(ValidationState_t& _,
|
|
|
|
const spv_parsed_instruction_t* inst) {
|
2018-07-07 13:38:00 +00:00
|
|
|
const std::string extension_str = spvtools::GetExtensionString(inst);
|
2017-03-10 20:58:15 +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
|
|
|
// The error will be logged in the ProcessInstruction pass.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_.RegisterExtension(extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses the beginning of the module searching for OpExtension instructions.
|
|
|
|
// Registers extensions if recognized. Returns SPV_REQUESTED_TERMINATION
|
|
|
|
// once an instruction which is not SpvOpCapability and SpvOpExtension is
|
|
|
|
// encountered. According to the SPIR-V spec extensions are declared after
|
|
|
|
// capabilities and before everything else.
|
2017-11-08 17:40:02 +00:00
|
|
|
spv_result_t ProcessExtensions(void* user_data,
|
|
|
|
const spv_parsed_instruction_t* inst) {
|
2017-03-10 20:58:15 +00:00
|
|
|
const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
|
2017-11-08 17:40:02 +00:00
|
|
|
if (opcode == SpvOpCapability) return SPV_SUCCESS;
|
2017-03-10 20:58:15 +00:00
|
|
|
|
|
|
|
if (opcode == SpvOpExtension) {
|
|
|
|
ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data));
|
|
|
|
RegisterExtension(_, inst);
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpExtension block is finished, requesting termination.
|
|
|
|
return SPV_REQUESTED_TERMINATION;
|
|
|
|
}
|
|
|
|
|
2016-01-14 04:25:11 +00:00
|
|
|
spv_result_t ProcessInstruction(void* user_data,
|
2016-01-15 16:25:11 +00:00
|
|
|
const spv_parsed_instruction_t* inst) {
|
2015-11-18 20:43:43 +00:00
|
|
|
ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data));
|
|
|
|
|
2018-07-31 13:54:25 +00:00
|
|
|
auto* instruction = _.AddOrderedInstruction(inst);
|
|
|
|
_.RegisterDebugInstruction(instruction);
|
2018-07-03 19:06:54 +00:00
|
|
|
|
2016-01-05 19:56:02 +00:00
|
|
|
return SPV_SUCCESS;
|
2015-11-18 20:43:43 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
void printDot(const ValidationState_t& _, const BasicBlock& other) {
|
2018-08-01 18:58:12 +00:00
|
|
|
std::string block_string;
|
2016-07-08 13:44:10 +00:00
|
|
|
if (other.successors()->empty()) {
|
|
|
|
block_string += "end ";
|
|
|
|
} else {
|
|
|
|
for (auto block : *other.successors()) {
|
|
|
|
block_string += _.getIdOrName(block->id()) + " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("%10s -> {%s\b}\n", _.getIdOrName(other.id()).c_str(),
|
|
|
|
block_string.c_str());
|
|
|
|
}
|
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
void PrintBlocks(ValidationState_t& _, Function func) {
|
2016-07-08 13:44:10 +00:00
|
|
|
assert(func.first_block());
|
|
|
|
|
|
|
|
printf("%10s -> %s\n", _.getIdOrName(func.id()).c_str(),
|
|
|
|
_.getIdOrName(func.first_block()->id()).c_str());
|
|
|
|
for (const auto& block : func.ordered_blocks()) {
|
|
|
|
printDot(_, *block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __clang__
|
|
|
|
#define UNUSED(func) [[gnu::unused]] func
|
|
|
|
#elif defined(__GNUC__)
|
2016-09-02 13:51:44 +00:00
|
|
|
#define UNUSED(func) \
|
|
|
|
func __attribute__((unused)); \
|
|
|
|
func
|
2016-07-08 13:44:10 +00:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#define UNUSED(func) func
|
|
|
|
#endif
|
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
UNUSED(void PrintDotGraph(ValidationState_t& _, Function func)) {
|
2016-07-08 13:44:10 +00:00
|
|
|
if (func.first_block()) {
|
2018-08-01 18:58:12 +00:00
|
|
|
std::string func_name(_.getIdOrName(func.id()));
|
2016-07-08 13:44:10 +00:00
|
|
|
printf("digraph %s {\n", func_name.c_str());
|
|
|
|
PrintBlocks(_, func);
|
|
|
|
printf("}\n");
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 21:32:44 +00:00
|
|
|
|
2018-08-02 19:12:06 +00:00
|
|
|
spv_result_t ValidateForwardDecls(ValidationState_t& _) {
|
|
|
|
if (_.unresolved_forward_id_count() == 0) return SPV_SUCCESS;
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
std::vector<uint32_t> ids = _.UnresolvedForwardIds();
|
|
|
|
|
|
|
|
std::transform(
|
|
|
|
std::begin(ids), std::end(ids),
|
|
|
|
std::ostream_iterator<std::string>(ss, " "),
|
|
|
|
bind(&ValidationState_t::getIdName, std::ref(_), std::placeholders::_1));
|
|
|
|
|
|
|
|
auto id_str = ss.str();
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, nullptr)
|
|
|
|
<< "The following forward referenced IDs have not been defined:\n"
|
|
|
|
<< id_str.substr(0, id_str.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry point validation. Based on 2.16.1 (Universal Validation Rules) of the
|
|
|
|
// SPIRV spec:
|
|
|
|
// * There is at least one OpEntryPoint instruction, unless the Linkage
|
|
|
|
// capability is being used.
|
|
|
|
// * No function can be targeted by both an OpEntryPoint instruction and an
|
|
|
|
// OpFunctionCall instruction.
|
|
|
|
spv_result_t ValidateEntryPoints(ValidationState_t& _) {
|
|
|
|
_.ComputeFunctionToEntryPointMapping();
|
|
|
|
|
|
|
|
if (_.entry_points().empty() && !_.HasCapability(SpvCapabilityLinkage)) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY, nullptr)
|
|
|
|
<< "No OpEntryPoint instruction was found. This is only allowed if "
|
|
|
|
"the Linkage capability is being used.";
|
|
|
|
}
|
|
|
|
for (const auto& entry_point : _.entry_points()) {
|
|
|
|
if (_.IsFunctionCallTarget(entry_point)) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_BINARY, _.FindDef(entry_point))
|
|
|
|
<< "A function (" << entry_point
|
|
|
|
<< ") may not be targeted by both an OpEntryPoint instruction and "
|
|
|
|
"an OpFunctionCall instruction.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:03:53 +00:00
|
|
|
spv_result_t ValidateBinaryUsingContextAndValidationState(
|
|
|
|
const spv_context_t& context, const uint32_t* words, const size_t num_words,
|
|
|
|
spv_diagnostic* pDiagnostic, ValidationState_t* vstate) {
|
2017-06-03 23:08:22 +00:00
|
|
|
auto binary = std::unique_ptr<spv_const_binary_t>(
|
2017-11-08 17:40:02 +00:00
|
|
|
new spv_const_binary_t{words, num_words});
|
2015-05-22 17:26:19 +00:00
|
|
|
|
|
|
|
spv_endianness_t endian;
|
|
|
|
spv_position_t position = {};
|
2017-06-03 23:08:22 +00:00
|
|
|
if (spvBinaryEndianness(binary.get(), &endian)) {
|
2018-07-10 03:18:44 +00:00
|
|
|
return DiagnosticStream(position, context.consumer, "",
|
|
|
|
SPV_ERROR_INVALID_BINARY)
|
2016-09-02 13:49:56 +00:00
|
|
|
<< "Invalid SPIR-V magic number.";
|
2015-09-11 18:31:27 +00:00
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
|
|
|
|
spv_header_t header;
|
2017-06-03 23:08:22 +00:00
|
|
|
if (spvBinaryHeaderGet(binary.get(), endian, &header)) {
|
2018-07-10 03:18:44 +00:00
|
|
|
return DiagnosticStream(position, context.consumer, "",
|
|
|
|
SPV_ERROR_INVALID_BINARY)
|
2016-09-02 13:49:56 +00:00
|
|
|
<< "Invalid SPIR-V header.";
|
2015-09-11 18:31:27 +00:00
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2018-04-06 18:15:27 +00:00
|
|
|
if (header.version > spvVersionForTargetEnv(context.target_env)) {
|
2018-07-10 03:18:44 +00:00
|
|
|
return DiagnosticStream(position, context.consumer, "",
|
|
|
|
SPV_ERROR_WRONG_VERSION)
|
2018-04-06 18:15:27 +00:00
|
|
|
<< "Invalid SPIR-V binary version "
|
|
|
|
<< SPV_SPIRV_VERSION_MAJOR_PART(header.version) << "."
|
|
|
|
<< SPV_SPIRV_VERSION_MINOR_PART(header.version)
|
|
|
|
<< " for target environment "
|
|
|
|
<< spvTargetEnvDescription(context.target_env) << ".";
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:30:19 +00:00
|
|
|
if (header.bound > vstate->options()->universal_limits_.max_id_bound) {
|
|
|
|
return DiagnosticStream(position, context.consumer, "",
|
|
|
|
SPV_ERROR_INVALID_BINARY)
|
|
|
|
<< "Invalid SPIR-V. The id bound is larger than the max id bound "
|
|
|
|
<< vstate->options()->universal_limits_.max_id_bound << ".";
|
|
|
|
}
|
2017-03-10 20:58:15 +00:00
|
|
|
// Look for OpExtension instructions and register extensions.
|
|
|
|
spvBinaryParse(&context, vstate, words, num_words,
|
|
|
|
/* parsed_header = */ nullptr, ProcessExtensions,
|
|
|
|
/* diagnostic = */ nullptr);
|
|
|
|
|
2018-08-01 14:37:36 +00:00
|
|
|
// Parse the module and perform inline validation checks. These checks do
|
|
|
|
// not require the the knowledge of the whole module.
|
2017-11-08 17:40:02 +00:00
|
|
|
if (auto error = spvBinaryParse(&context, vstate, words, num_words, setHeader,
|
2018-08-01 14:37:36 +00:00
|
|
|
ProcessInstruction, pDiagnostic)) {
|
2016-09-02 13:51:44 +00:00
|
|
|
return error;
|
2018-08-01 14:37:36 +00:00
|
|
|
}
|
2015-11-18 20:43:43 +00:00
|
|
|
|
2018-08-03 16:57:11 +00:00
|
|
|
for (auto& instruction : vstate->ordered_instructions()) {
|
2018-08-02 19:12:06 +00:00
|
|
|
{
|
|
|
|
// In order to do this work outside of Process Instruction we need to be
|
|
|
|
// able to, briefly, de-const the instruction.
|
|
|
|
Instruction* inst = const_cast<Instruction*>(&instruction);
|
|
|
|
|
|
|
|
if (inst->opcode() == SpvOpEntryPoint) {
|
|
|
|
const auto entry_point = inst->GetOperandAs<uint32_t>(1);
|
|
|
|
const auto execution_model = inst->GetOperandAs<SpvExecutionModel>(0);
|
|
|
|
const char* str = reinterpret_cast<const char*>(
|
|
|
|
inst->words().data() + inst->operand(2).offset);
|
|
|
|
|
|
|
|
ValidationState_t::EntryPointDescription desc;
|
|
|
|
desc.name = str;
|
|
|
|
|
|
|
|
std::vector<uint32_t> interfaces;
|
|
|
|
for (size_t j = 3; j < inst->operands().size(); ++j)
|
|
|
|
desc.interfaces.push_back(inst->word(inst->operand(j).offset));
|
|
|
|
|
|
|
|
vstate->RegisterEntryPoint(entry_point, execution_model,
|
|
|
|
std::move(desc));
|
|
|
|
}
|
2018-08-02 20:58:45 +00:00
|
|
|
if (inst->opcode() == SpvOpFunctionCall) {
|
|
|
|
if (!vstate->in_function_body()) {
|
|
|
|
return vstate->diag(SPV_ERROR_INVALID_LAYOUT, &instruction)
|
|
|
|
<< "A FunctionCall must happen within a function body.";
|
|
|
|
}
|
|
|
|
|
2018-08-02 19:12:06 +00:00
|
|
|
vstate->AddFunctionCallTarget(inst->GetOperandAs<uint32_t>(2));
|
2018-08-02 20:58:45 +00:00
|
|
|
}
|
2018-08-02 19:12:06 +00:00
|
|
|
|
|
|
|
if (vstate->in_function_body()) {
|
|
|
|
inst->set_function(&(vstate->current_function()));
|
|
|
|
inst->set_block(vstate->current_function().current_block());
|
|
|
|
|
|
|
|
if (vstate->in_block() && spvOpcodeIsBlockTerminator(inst->opcode())) {
|
|
|
|
vstate->current_function().current_block()->set_terminator(inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto error = IdPass(*vstate, inst)) return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto error = CapabilityPass(*vstate, &instruction)) return error;
|
|
|
|
if (auto error = DataRulesPass(*vstate, &instruction)) return error;
|
|
|
|
if (auto error = ModuleLayoutPass(*vstate, &instruction)) return error;
|
|
|
|
if (auto error = CfgPass(*vstate, &instruction)) return error;
|
|
|
|
if (auto error = InstructionPass(*vstate, &instruction)) return error;
|
2018-08-15 17:18:47 +00:00
|
|
|
|
|
|
|
// Now that all of the checks are done, update the state.
|
|
|
|
{
|
|
|
|
Instruction* inst = const_cast<Instruction*>(&instruction);
|
|
|
|
vstate->RegisterInstruction(inst);
|
|
|
|
}
|
2018-08-02 14:00:52 +00:00
|
|
|
if (auto error = UpdateIdUse(*vstate, &instruction)) return error;
|
|
|
|
}
|
|
|
|
|
2018-05-22 17:09:50 +00:00
|
|
|
if (!vstate->has_memory_model_specified())
|
2018-07-31 18:53:10 +00:00
|
|
|
return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
|
2018-05-22 17:09:50 +00:00
|
|
|
<< "Missing required OpMemoryModel instruction.";
|
|
|
|
|
2017-01-11 20:03:53 +00:00
|
|
|
if (vstate->in_function_body())
|
2018-07-31 18:53:10 +00:00
|
|
|
return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
|
2016-06-24 06:14:16 +00:00
|
|
|
<< "Missing OpFunctionEnd at end of module.";
|
|
|
|
|
2018-08-13 15:22:27 +00:00
|
|
|
// Catch undefined forward references before performing further checks.
|
2018-08-02 19:12:06 +00:00
|
|
|
if (auto error = ValidateForwardDecls(*vstate)) return error;
|
2016-03-16 21:20:02 +00:00
|
|
|
|
2018-08-03 16:57:11 +00:00
|
|
|
// Validate individual opcodes.
|
|
|
|
for (size_t i = 0; i < vstate->ordered_instructions().size(); ++i) {
|
|
|
|
auto& instruction = vstate->ordered_instructions()[i];
|
2018-08-03 15:38:51 +00:00
|
|
|
|
|
|
|
// Keep these passes in the order they appear in the SPIR-V specification
|
|
|
|
// sections to maintain test consistency.
|
|
|
|
// Miscellaneous
|
2018-08-08 17:47:09 +00:00
|
|
|
if (auto error = DebugPass(*vstate, &instruction)) return error;
|
2018-08-08 18:21:27 +00:00
|
|
|
if (auto error = AnnotationPass(*vstate, &instruction)) return error;
|
2018-08-03 16:57:11 +00:00
|
|
|
if (auto error = ExtInstPass(*vstate, &instruction)) return error;
|
2018-08-08 18:49:59 +00:00
|
|
|
if (auto error = ModeSettingPass(*vstate, &instruction)) return error;
|
2018-08-03 15:38:51 +00:00
|
|
|
if (auto error = TypePass(*vstate, &instruction)) return error;
|
2018-08-17 15:20:55 +00:00
|
|
|
if (auto error = ConstantPass(*vstate, &instruction)) return error;
|
2018-09-27 16:34:14 +00:00
|
|
|
if (auto error = MemoryPass(*vstate, &instruction)) return error;
|
2018-08-10 13:53:17 +00:00
|
|
|
if (auto error = FunctionPass(*vstate, &instruction)) return error;
|
2018-08-03 16:57:11 +00:00
|
|
|
if (auto error = ImagePass(*vstate, &instruction)) return error;
|
2018-08-03 15:38:51 +00:00
|
|
|
if (auto error = ConversionPass(*vstate, &instruction)) return error;
|
|
|
|
if (auto error = CompositesPass(*vstate, &instruction)) return error;
|
|
|
|
if (auto error = ArithmeticsPass(*vstate, &instruction)) return error;
|
|
|
|
if (auto error = BitwisePass(*vstate, &instruction)) return error;
|
|
|
|
if (auto error = LogicalsPass(*vstate, &instruction)) return error;
|
2018-08-10 16:49:26 +00:00
|
|
|
if (auto error = ControlFlowPass(*vstate, &instruction)) return error;
|
2018-08-03 15:38:51 +00:00
|
|
|
if (auto error = DerivativesPass(*vstate, &instruction)) return error;
|
2018-08-03 16:57:11 +00:00
|
|
|
if (auto error = AtomicsPass(*vstate, &instruction)) return error;
|
|
|
|
if (auto error = PrimitivesPass(*vstate, &instruction)) return error;
|
2018-08-03 15:38:51 +00:00
|
|
|
if (auto error = BarriersPass(*vstate, &instruction)) return error;
|
|
|
|
// Group
|
|
|
|
// Device-Side Enqueue
|
|
|
|
// Pipe
|
2018-08-03 16:57:11 +00:00
|
|
|
if (auto error = NonUniformPass(*vstate, &instruction)) return error;
|
|
|
|
|
2018-08-03 15:38:51 +00:00
|
|
|
if (auto error = LiteralsPass(*vstate, &instruction)) return error;
|
2018-08-03 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 15:09:47 +00:00
|
|
|
// Validate the preconditions involving adjacent instructions. e.g. SpvOpPhi
|
|
|
|
// must only be preceeded by SpvOpLabel, SpvOpPhi, or SpvOpLine.
|
|
|
|
if (auto error = ValidateAdjacency(*vstate)) return error;
|
|
|
|
|
2018-08-13 15:22:27 +00:00
|
|
|
if (auto error = ValidateEntryPoints(*vstate)) return error;
|
|
|
|
// CFG checks are performed after the binary has been parsed
|
|
|
|
// and the CFGPass has collected information about the control flow
|
|
|
|
if (auto error = PerformCfgChecks(*vstate)) return error;
|
|
|
|
if (auto error = CheckIdDefinitionDominateUse(*vstate)) return error;
|
|
|
|
if (auto error = ValidateDecorations(*vstate)) return error;
|
|
|
|
if (auto error = ValidateInterfaces(*vstate)) return error;
|
|
|
|
// TODO(dsinclair): Restructure ValidateBuiltins so we can move into the
|
|
|
|
// for() above as it loops over all ordered_instructions internally.
|
|
|
|
if (auto error = ValidateBuiltIns(*vstate)) return error;
|
2018-08-10 13:53:17 +00:00
|
|
|
// These checks must be performed after individual opcode checks because
|
|
|
|
// those checks register the limitation checked here.
|
|
|
|
for (const auto inst : vstate->ordered_instructions()) {
|
|
|
|
if (auto error = ValidateExecutionLimitations(*vstate, &inst)) return error;
|
|
|
|
}
|
2018-08-13 15:22:27 +00:00
|
|
|
|
2018-02-07 16:50:26 +00:00
|
|
|
return SPV_SUCCESS;
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
2018-07-10 03:18:44 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
spv_result_t ValidateBinaryAndKeepValidationState(
|
|
|
|
const spv_const_context context, spv_const_validator_options options,
|
|
|
|
const uint32_t* words, const size_t num_words, spv_diagnostic* pDiagnostic,
|
|
|
|
std::unique_ptr<ValidationState_t>* vstate) {
|
|
|
|
spv_context_t hijack_context = *context;
|
|
|
|
if (pDiagnostic) {
|
|
|
|
*pDiagnostic = nullptr;
|
|
|
|
UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
|
|
|
|
}
|
|
|
|
|
2018-10-03 19:59:40 +00:00
|
|
|
vstate->reset(new ValidationState_t(&hijack_context, options, words,
|
|
|
|
num_words, kDefaultMaxNumOfWarnings));
|
2018-07-10 03:18:44 +00:00
|
|
|
|
|
|
|
return ValidateBinaryUsingContextAndValidationState(
|
|
|
|
hijack_context, words, num_words, pDiagnostic, vstate->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace val
|
|
|
|
} // namespace spvtools
|
2017-11-22 22:03:55 +00:00
|
|
|
|
|
|
|
spv_result_t spvValidate(const spv_const_context context,
|
|
|
|
const spv_const_binary binary,
|
|
|
|
spv_diagnostic* pDiagnostic) {
|
|
|
|
return spvValidateBinary(context, binary->code, binary->wordCount,
|
|
|
|
pDiagnostic);
|
|
|
|
}
|
2017-01-11 20:03:53 +00:00
|
|
|
|
|
|
|
spv_result_t spvValidateBinary(const spv_const_context context,
|
|
|
|
const uint32_t* words, const size_t num_words,
|
|
|
|
spv_diagnostic* pDiagnostic) {
|
|
|
|
spv_context_t hijack_context = *context;
|
|
|
|
if (pDiagnostic) {
|
|
|
|
*pDiagnostic = nullptr;
|
2018-07-07 13:38:00 +00:00
|
|
|
spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
|
2017-01-11 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 18:29:33 +00:00
|
|
|
// This interface is used for default command line options.
|
|
|
|
spv_validator_options default_options = spvValidatorOptionsCreate();
|
|
|
|
|
|
|
|
// Create the ValidationState using the context and default options.
|
2018-07-10 03:18:44 +00:00
|
|
|
spvtools::val::ValidationState_t vstate(&hijack_context, default_options,
|
2018-10-03 19:59:40 +00:00
|
|
|
words, num_words,
|
|
|
|
kDefaultMaxNumOfWarnings);
|
2017-02-15 18:29:33 +00:00
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
spv_result_t result =
|
|
|
|
spvtools::val::ValidateBinaryUsingContextAndValidationState(
|
|
|
|
hijack_context, words, num_words, pDiagnostic, &vstate);
|
2017-02-15 18:29:33 +00:00
|
|
|
|
|
|
|
spvValidatorOptionsDestroy(default_options);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t spvValidateWithOptions(const spv_const_context context,
|
|
|
|
spv_const_validator_options options,
|
|
|
|
const spv_const_binary binary,
|
|
|
|
spv_diagnostic* pDiagnostic) {
|
|
|
|
spv_context_t hijack_context = *context;
|
|
|
|
if (pDiagnostic) {
|
|
|
|
*pDiagnostic = nullptr;
|
2018-07-07 13:38:00 +00:00
|
|
|
spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
|
2017-02-15 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:03:53 +00:00
|
|
|
// Create the ValidationState using the context.
|
2018-07-10 03:18:44 +00:00
|
|
|
spvtools::val::ValidationState_t vstate(&hijack_context, options,
|
2018-10-03 19:59:40 +00:00
|
|
|
binary->code, binary->wordCount,
|
|
|
|
kDefaultMaxNumOfWarnings);
|
2017-01-11 20:03:53 +00:00
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
return spvtools::val::ValidateBinaryUsingContextAndValidationState(
|
2017-02-15 18:29:33 +00:00
|
|
|
hijack_context, binary->code, binary->wordCount, pDiagnostic, &vstate);
|
2017-01-11 20:03:53 +00:00
|
|
|
}
|