validate.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/val/validate.h"
  15. #include <functional>
  16. #include <iterator>
  17. #include <memory>
  18. #include <string>
  19. #include <vector>
  20. #include "source/binary.h"
  21. #include "source/diagnostic.h"
  22. #include "source/enum_string_mapping.h"
  23. #include "source/extensions.h"
  24. #include "source/opcode.h"
  25. #include "source/spirv_constant.h"
  26. #include "source/spirv_endian.h"
  27. #include "source/spirv_target_env.h"
  28. #include "source/val/construct.h"
  29. #include "source/val/instruction.h"
  30. #include "source/val/validation_state.h"
  31. #include "spirv-tools/libspirv.h"
  32. namespace {
  33. // TODO(issue 1950): The validator only returns a single message anyway, so no
  34. // point in generating more than 1 warning.
  35. static uint32_t kDefaultMaxNumOfWarnings = 1;
  36. } // namespace
  37. namespace spvtools {
  38. namespace val {
  39. namespace {
  40. // Parses OpExtension instruction and registers extension.
  41. void RegisterExtension(ValidationState_t& _,
  42. const spv_parsed_instruction_t* inst) {
  43. const std::string extension_str = spvtools::GetExtensionString(inst);
  44. Extension extension;
  45. if (!GetExtensionFromString(extension_str.c_str(), &extension)) {
  46. // The error will be logged in the ProcessInstruction pass.
  47. return;
  48. }
  49. _.RegisterExtension(extension);
  50. }
  51. // Parses the beginning of the module searching for OpExtension instructions.
  52. // Registers extensions if recognized. Returns SPV_REQUESTED_TERMINATION
  53. // once an instruction which is not spv::Op::OpCapability and
  54. // spv::Op::OpExtension is encountered. According to the SPIR-V spec extensions
  55. // are declared after capabilities and before everything else.
  56. spv_result_t ProcessExtensions(void* user_data,
  57. const spv_parsed_instruction_t* inst) {
  58. const spv::Op opcode = static_cast<spv::Op>(inst->opcode);
  59. if (opcode == spv::Op::OpCapability) return SPV_SUCCESS;
  60. if (opcode == spv::Op::OpExtension) {
  61. ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data));
  62. RegisterExtension(_, inst);
  63. return SPV_SUCCESS;
  64. }
  65. // OpExtension block is finished, requesting termination.
  66. return SPV_REQUESTED_TERMINATION;
  67. }
  68. spv_result_t ProcessInstruction(void* user_data,
  69. const spv_parsed_instruction_t* inst) {
  70. ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data));
  71. auto* instruction = _.AddOrderedInstruction(inst);
  72. _.RegisterDebugInstruction(instruction);
  73. return SPV_SUCCESS;
  74. }
  75. spv_result_t ValidateForwardDecls(ValidationState_t& _) {
  76. if (_.unresolved_forward_id_count() == 0) return SPV_SUCCESS;
  77. std::stringstream ss;
  78. std::vector<uint32_t> ids = _.UnresolvedForwardIds();
  79. std::transform(
  80. std::begin(ids), std::end(ids),
  81. std::ostream_iterator<std::string>(ss, " "),
  82. bind(&ValidationState_t::getIdName, std::ref(_), std::placeholders::_1));
  83. auto id_str = ss.str();
  84. return _.diag(SPV_ERROR_INVALID_ID, nullptr)
  85. << "The following forward referenced IDs have not been defined:\n"
  86. << id_str.substr(0, id_str.size() - 1);
  87. }
  88. // Entry point validation. Based on 2.16.1 (Universal Validation Rules) of the
  89. // SPIRV spec:
  90. // * There is at least one OpEntryPoint instruction, unless the Linkage
  91. // capability is being used.
  92. // * No function can be targeted by both an OpEntryPoint instruction and an
  93. // OpFunctionCall instruction.
  94. //
  95. // Additionally enforces that entry points for Vulkan should not have recursion.
  96. spv_result_t ValidateEntryPoints(ValidationState_t& _) {
  97. _.ComputeFunctionToEntryPointMapping();
  98. _.ComputeRecursiveEntryPoints();
  99. if (_.entry_points().empty() && !_.HasCapability(spv::Capability::Linkage)) {
  100. return _.diag(SPV_ERROR_INVALID_BINARY, nullptr)
  101. << "No OpEntryPoint instruction was found. This is only allowed if "
  102. "the Linkage capability is being used.";
  103. }
  104. for (const auto& entry_point : _.entry_points()) {
  105. if (_.IsFunctionCallTarget(entry_point)) {
  106. return _.diag(SPV_ERROR_INVALID_BINARY, _.FindDef(entry_point))
  107. << "A function (" << entry_point
  108. << ") may not be targeted by both an OpEntryPoint instruction and "
  109. "an OpFunctionCall instruction.";
  110. }
  111. // For Vulkan, the static function-call graph for an entry point
  112. // must not contain cycles.
  113. if (spvIsVulkanEnv(_.context()->target_env)) {
  114. if (_.recursive_entry_points().find(entry_point) !=
  115. _.recursive_entry_points().end()) {
  116. return _.diag(SPV_ERROR_INVALID_BINARY, _.FindDef(entry_point))
  117. << _.VkErrorID(4634)
  118. << "Entry points may not have a call graph with cycles.";
  119. }
  120. }
  121. }
  122. if (auto error = ValidateFloatControls2(_)) {
  123. return error;
  124. }
  125. if (auto error = ValidateDuplicateExecutionModes(_)) {
  126. return error;
  127. }
  128. return SPV_SUCCESS;
  129. }
  130. spv_result_t ValidateBinaryUsingContextAndValidationState(
  131. const spv_context_t& context, const uint32_t* words, const size_t num_words,
  132. spv_diagnostic* pDiagnostic, ValidationState_t* vstate) {
  133. auto binary = std::unique_ptr<spv_const_binary_t>(
  134. new spv_const_binary_t{words, num_words});
  135. spv_endianness_t endian;
  136. spv_position_t position = {};
  137. if (spvBinaryEndianness(binary.get(), &endian)) {
  138. return DiagnosticStream(position, context.consumer, "",
  139. SPV_ERROR_INVALID_BINARY)
  140. << "Invalid SPIR-V magic number.";
  141. }
  142. spv_header_t header;
  143. if (spvBinaryHeaderGet(binary.get(), endian, &header)) {
  144. return DiagnosticStream(position, context.consumer, "",
  145. SPV_ERROR_INVALID_BINARY)
  146. << "Invalid SPIR-V header.";
  147. }
  148. if (header.version > spvVersionForTargetEnv(context.target_env)) {
  149. return DiagnosticStream(position, context.consumer, "",
  150. SPV_ERROR_WRONG_VERSION)
  151. << "Invalid SPIR-V binary version "
  152. << SPV_SPIRV_VERSION_MAJOR_PART(header.version) << "."
  153. << SPV_SPIRV_VERSION_MINOR_PART(header.version)
  154. << " for target environment "
  155. << spvTargetEnvDescription(context.target_env) << ".";
  156. }
  157. if (header.bound > vstate->options()->universal_limits_.max_id_bound) {
  158. return DiagnosticStream(position, context.consumer, "",
  159. SPV_ERROR_INVALID_BINARY)
  160. << "Invalid SPIR-V. The id bound is larger than the max id bound "
  161. << vstate->options()->universal_limits_.max_id_bound << ".";
  162. }
  163. // Look for OpExtension instructions and register extensions.
  164. // This parse should not produce any error messages. Hijack the context and
  165. // replace the message consumer so that we do not pollute any state in input
  166. // consumer.
  167. spv_context_t hijacked_context = context;
  168. hijacked_context.consumer = [](spv_message_level_t, const char*,
  169. const spv_position_t&, const char*) {};
  170. spvBinaryParse(&hijacked_context, vstate, words, num_words,
  171. /* parsed_header = */ nullptr, ProcessExtensions,
  172. /* diagnostic = */ nullptr);
  173. // Parse the module and perform inline validation checks. These checks do
  174. // not require the knowledge of the whole module.
  175. if (auto error = spvBinaryParse(&context, vstate, words, num_words,
  176. /*parsed_header =*/nullptr,
  177. ProcessInstruction, pDiagnostic)) {
  178. return error;
  179. }
  180. bool has_mask_task_nv = false;
  181. bool has_mask_task_ext = false;
  182. std::vector<Instruction*> visited_entry_points;
  183. for (auto& instruction : vstate->ordered_instructions()) {
  184. {
  185. // In order to do this work outside of Process Instruction we need to be
  186. // able to, briefly, de-const the instruction.
  187. Instruction* inst = const_cast<Instruction*>(&instruction);
  188. if (inst->opcode() == spv::Op::OpEntryPoint) {
  189. const auto entry_point = inst->GetOperandAs<uint32_t>(1);
  190. const auto execution_model = inst->GetOperandAs<spv::ExecutionModel>(0);
  191. const std::string desc_name = inst->GetOperandAs<std::string>(2);
  192. ValidationState_t::EntryPointDescription desc;
  193. desc.name = desc_name;
  194. std::vector<uint32_t> interfaces;
  195. for (size_t j = 3; j < inst->operands().size(); ++j)
  196. desc.interfaces.push_back(inst->word(inst->operand(j).offset));
  197. vstate->RegisterEntryPoint(entry_point, execution_model,
  198. std::move(desc));
  199. if (visited_entry_points.size() > 0) {
  200. for (const Instruction* check_inst : visited_entry_points) {
  201. const auto check_execution_model =
  202. check_inst->GetOperandAs<spv::ExecutionModel>(0);
  203. const std::string check_name =
  204. check_inst->GetOperandAs<std::string>(2);
  205. if (desc_name == check_name &&
  206. execution_model == check_execution_model) {
  207. return vstate->diag(SPV_ERROR_INVALID_DATA, inst)
  208. << "2 Entry points cannot share the same name and "
  209. "ExecutionMode.";
  210. }
  211. }
  212. }
  213. visited_entry_points.push_back(inst);
  214. has_mask_task_nv |= (execution_model == spv::ExecutionModel::TaskNV ||
  215. execution_model == spv::ExecutionModel::MeshNV);
  216. has_mask_task_ext |= (execution_model == spv::ExecutionModel::TaskEXT ||
  217. execution_model == spv::ExecutionModel::MeshEXT);
  218. }
  219. if (inst->opcode() == spv::Op::OpFunctionCall) {
  220. if (!vstate->in_function_body()) {
  221. return vstate->diag(SPV_ERROR_INVALID_LAYOUT, &instruction)
  222. << "A FunctionCall must happen within a function body.";
  223. }
  224. const auto called_id = inst->GetOperandAs<uint32_t>(2);
  225. vstate->AddFunctionCallTarget(called_id);
  226. }
  227. if (vstate->in_function_body()) {
  228. inst->set_function(&(vstate->current_function()));
  229. inst->set_block(vstate->current_function().current_block());
  230. if (vstate->in_block() && spvOpcodeIsBlockTerminator(inst->opcode())) {
  231. vstate->current_function().current_block()->set_terminator(inst);
  232. }
  233. }
  234. if (auto error = IdPass(*vstate, inst)) return error;
  235. }
  236. if (auto error = CapabilityPass(*vstate, &instruction)) return error;
  237. if (auto error = ModuleLayoutPass(*vstate, &instruction)) return error;
  238. if (auto error = CfgPass(*vstate, &instruction)) return error;
  239. if (auto error = InstructionPass(*vstate, &instruction)) return error;
  240. // Now that all of the checks are done, update the state.
  241. {
  242. Instruction* inst = const_cast<Instruction*>(&instruction);
  243. vstate->RegisterInstruction(inst);
  244. if (inst->opcode() == spv::Op::OpTypeForwardPointer) {
  245. vstate->RegisterForwardPointer(inst->GetOperandAs<uint32_t>(0));
  246. }
  247. }
  248. }
  249. if (!vstate->has_memory_model_specified())
  250. return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
  251. << "Missing required OpMemoryModel instruction.";
  252. if (vstate->in_function_body())
  253. return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
  254. << "Missing OpFunctionEnd at end of module.";
  255. if (vstate->HasCapability(spv::Capability::BindlessTextureNV) &&
  256. !vstate->has_samplerimage_variable_address_mode_specified())
  257. return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
  258. << "Missing required OpSamplerImageAddressingModeNV instruction.";
  259. if (has_mask_task_ext && has_mask_task_nv)
  260. return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr)
  261. << vstate->VkErrorID(7102)
  262. << "Module can't mix MeshEXT/TaskEXT with MeshNV/TaskNV Execution "
  263. "Model.";
  264. // Catch undefined forward references before performing further checks.
  265. if (auto error = ValidateForwardDecls(*vstate)) return error;
  266. // Calculate reachability after all the blocks are parsed, but early that it
  267. // can be relied on in subsequent pases.
  268. ReachabilityPass(*vstate);
  269. // ID usage needs be handled in its own iteration of the instructions,
  270. // between the two others. It depends on the first loop to have been
  271. // finished, so that all instructions have been registered. And the following
  272. // loop depends on all of the usage data being populated. Thus it cannot live
  273. // in either of those iterations.
  274. // It should also live after the forward declaration check, since it will
  275. // have problems with missing forward declarations, but give less useful error
  276. // messages.
  277. for (size_t i = 0; i < vstate->ordered_instructions().size(); ++i) {
  278. auto& instruction = vstate->ordered_instructions()[i];
  279. if (auto error = UpdateIdUse(*vstate, &instruction)) return error;
  280. }
  281. // Validate individual opcodes.
  282. for (size_t i = 0; i < vstate->ordered_instructions().size(); ++i) {
  283. auto& instruction = vstate->ordered_instructions()[i];
  284. // Keep these passes in the order they appear in the SPIR-V specification
  285. // sections to maintain test consistency.
  286. if (auto error = MiscPass(*vstate, &instruction)) return error;
  287. if (auto error = DebugPass(*vstate, &instruction)) return error;
  288. if (auto error = AnnotationPass(*vstate, &instruction)) return error;
  289. if (auto error = ExtensionPass(*vstate, &instruction)) return error;
  290. if (auto error = ModeSettingPass(*vstate, &instruction)) return error;
  291. if (auto error = TypePass(*vstate, &instruction)) return error;
  292. if (auto error = ConstantPass(*vstate, &instruction)) return error;
  293. if (auto error = MemoryPass(*vstate, &instruction)) return error;
  294. if (auto error = FunctionPass(*vstate, &instruction)) return error;
  295. if (auto error = ImagePass(*vstate, &instruction)) return error;
  296. if (auto error = ConversionPass(*vstate, &instruction)) return error;
  297. if (auto error = CompositesPass(*vstate, &instruction)) return error;
  298. if (auto error = ArithmeticsPass(*vstate, &instruction)) return error;
  299. if (auto error = BitwisePass(*vstate, &instruction)) return error;
  300. if (auto error = LogicalsPass(*vstate, &instruction)) return error;
  301. if (auto error = ControlFlowPass(*vstate, &instruction)) return error;
  302. if (auto error = DerivativesPass(*vstate, &instruction)) return error;
  303. if (auto error = AtomicsPass(*vstate, &instruction)) return error;
  304. if (auto error = PrimitivesPass(*vstate, &instruction)) return error;
  305. if (auto error = BarriersPass(*vstate, &instruction)) return error;
  306. // Group
  307. // Device-Side Enqueue
  308. // Pipe
  309. if (auto error = NonUniformPass(*vstate, &instruction)) return error;
  310. if (auto error = LiteralsPass(*vstate, &instruction)) return error;
  311. if (auto error = RayQueryPass(*vstate, &instruction)) return error;
  312. if (auto error = RayTracingPass(*vstate, &instruction)) return error;
  313. if (auto error = RayReorderNVPass(*vstate, &instruction)) return error;
  314. if (auto error = MeshShadingPass(*vstate, &instruction)) return error;
  315. if (auto error = TensorLayoutPass(*vstate, &instruction)) return error;
  316. }
  317. // Validate the preconditions involving adjacent instructions. e.g.
  318. // spv::Op::OpPhi must only be preceded by spv::Op::OpLabel, spv::Op::OpPhi,
  319. // or spv::Op::OpLine.
  320. if (auto error = ValidateAdjacency(*vstate)) return error;
  321. if (auto error = ValidateEntryPoints(*vstate)) return error;
  322. // CFG checks are performed after the binary has been parsed
  323. // and the CFGPass has collected information about the control flow
  324. if (auto error = PerformCfgChecks(*vstate)) return error;
  325. if (auto error = CheckIdDefinitionDominateUse(*vstate)) return error;
  326. if (auto error = ValidateDecorations(*vstate)) return error;
  327. if (auto error = ValidateInterfaces(*vstate)) return error;
  328. // TODO(dsinclair): Restructure ValidateBuiltins so we can move into the
  329. // for() above as it loops over all ordered_instructions internally.
  330. if (auto error = ValidateBuiltIns(*vstate)) return error;
  331. // These checks must be performed after individual opcode checks because
  332. // those checks register the limitation checked here.
  333. for (const auto& inst : vstate->ordered_instructions()) {
  334. if (auto error = ValidateExecutionLimitations(*vstate, &inst)) return error;
  335. if (auto error = ValidateSmallTypeUses(*vstate, &inst)) return error;
  336. if (auto error = ValidateQCOMImageProcessingTextureUsages(*vstate, &inst))
  337. return error;
  338. }
  339. return SPV_SUCCESS;
  340. }
  341. } // namespace
  342. spv_result_t ValidateBinaryAndKeepValidationState(
  343. const spv_const_context context, spv_const_validator_options options,
  344. const uint32_t* words, const size_t num_words, spv_diagnostic* pDiagnostic,
  345. std::unique_ptr<ValidationState_t>* vstate) {
  346. spv_context_t hijack_context = *context;
  347. if (pDiagnostic) {
  348. *pDiagnostic = nullptr;
  349. UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
  350. }
  351. vstate->reset(new ValidationState_t(&hijack_context, options, words,
  352. num_words, kDefaultMaxNumOfWarnings));
  353. return ValidateBinaryUsingContextAndValidationState(
  354. hijack_context, words, num_words, pDiagnostic, vstate->get());
  355. }
  356. } // namespace val
  357. } // namespace spvtools
  358. spv_result_t spvValidate(const spv_const_context context,
  359. const spv_const_binary binary,
  360. spv_diagnostic* pDiagnostic) {
  361. return spvValidateBinary(context, binary->code, binary->wordCount,
  362. pDiagnostic);
  363. }
  364. spv_result_t spvValidateBinary(const spv_const_context context,
  365. const uint32_t* words, const size_t num_words,
  366. spv_diagnostic* pDiagnostic) {
  367. spv_context_t hijack_context = *context;
  368. if (pDiagnostic) {
  369. *pDiagnostic = nullptr;
  370. spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
  371. }
  372. // This interface is used for default command line options.
  373. spv_validator_options default_options = spvValidatorOptionsCreate();
  374. // Create the ValidationState using the context and default options.
  375. spvtools::val::ValidationState_t vstate(&hijack_context, default_options,
  376. words, num_words,
  377. kDefaultMaxNumOfWarnings);
  378. spv_result_t result =
  379. spvtools::val::ValidateBinaryUsingContextAndValidationState(
  380. hijack_context, words, num_words, pDiagnostic, &vstate);
  381. spvValidatorOptionsDestroy(default_options);
  382. return result;
  383. }
  384. spv_result_t spvValidateWithOptions(const spv_const_context context,
  385. spv_const_validator_options options,
  386. const spv_const_binary binary,
  387. spv_diagnostic* pDiagnostic) {
  388. spv_context_t hijack_context = *context;
  389. if (pDiagnostic) {
  390. *pDiagnostic = nullptr;
  391. spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
  392. }
  393. // Create the ValidationState using the context.
  394. spvtools::val::ValidationState_t vstate(&hijack_context, options,
  395. binary->code, binary->wordCount,
  396. kDefaultMaxNumOfWarnings);
  397. return spvtools::val::ValidateBinaryUsingContextAndValidationState(
  398. hijack_context, binary->code, binary->wordCount, pDiagnostic, &vstate);
  399. }