validate_composites.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // Copyright (c) 2017 Google 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. // Validates correctness of composite SPIR-V instructions.
  15. #include "validate.h"
  16. #include "diagnostic.h"
  17. #include "opcode.h"
  18. #include "val/instruction.h"
  19. #include "val/validation_state.h"
  20. namespace libspirv {
  21. namespace {
  22. // Returns the type of the value accessed by OpCompositeExtract or
  23. // OpCompositeInsert instruction. The function traverses the hierarchy of
  24. // nested data structures (structs, arrays, vectors, matrices) as directed by
  25. // the sequence of indices in the instruction. May return error if traversal
  26. // fails (encountered non-composite, out of bounds, nesting too deep).
  27. // Returns the type of Composite operand if the instruction has no indices.
  28. spv_result_t GetExtractInsertValueType(ValidationState_t& _,
  29. const spv_parsed_instruction_t& inst,
  30. uint32_t* member_type) {
  31. const SpvOp opcode = static_cast<SpvOp>(inst.opcode);
  32. assert(opcode == SpvOpCompositeExtract || opcode == SpvOpCompositeInsert);
  33. uint32_t word_index = opcode == SpvOpCompositeExtract ? 4 : 5;
  34. const uint32_t num_words = static_cast<uint32_t>(inst.num_words);
  35. const uint32_t composite_id_index = word_index - 1;
  36. const uint32_t num_indices = num_words - word_index;
  37. const uint32_t kCompositeExtractInsertMaxNumIndices = 255;
  38. if (num_indices > kCompositeExtractInsertMaxNumIndices) {
  39. return _.diag(SPV_ERROR_INVALID_DATA)
  40. << "The number of indexes in Op" << spvOpcodeString(opcode)
  41. << " may not exceed " << kCompositeExtractInsertMaxNumIndices
  42. << ". Found " << num_indices << " indexes.";
  43. }
  44. *member_type = _.GetTypeId(inst.words[composite_id_index]);
  45. if (*member_type == 0) {
  46. return _.diag(SPV_ERROR_INVALID_DATA)
  47. << spvOpcodeString(opcode)
  48. << ": expected Composite to be an object of composite type";
  49. }
  50. for (; word_index < num_words; ++word_index) {
  51. const uint32_t component_index = inst.words[word_index];
  52. const Instruction* const type_inst = _.FindDef(*member_type);
  53. assert(type_inst);
  54. switch (type_inst->opcode()) {
  55. case SpvOpTypeVector: {
  56. *member_type = type_inst->word(2);
  57. const uint32_t vector_size = type_inst->word(3);
  58. if (component_index >= vector_size) {
  59. return _.diag(SPV_ERROR_INVALID_DATA)
  60. << spvOpcodeString(opcode)
  61. << ": vector access is out of bounds, vector size is "
  62. << vector_size << ", but access index is " << component_index;
  63. }
  64. break;
  65. }
  66. case SpvOpTypeMatrix: {
  67. *member_type = type_inst->word(2);
  68. const uint32_t num_cols = type_inst->word(3);
  69. if (component_index >= num_cols) {
  70. return _.diag(SPV_ERROR_INVALID_DATA)
  71. << spvOpcodeString(opcode)
  72. << ": matrix access is out of bounds, matrix has " << num_cols
  73. << " columns, but access index is " << component_index;
  74. }
  75. break;
  76. }
  77. case SpvOpTypeArray: {
  78. uint64_t array_size = 0;
  79. auto size = _.FindDef(type_inst->word(3));
  80. *member_type = type_inst->word(2);
  81. if (spvOpcodeIsSpecConstant(size->opcode())) {
  82. // Cannot verify against the size of this array.
  83. break;
  84. }
  85. if (!_.GetConstantValUint64(type_inst->word(3), &array_size)) {
  86. assert(0 && "Array type definition is corrupt");
  87. }
  88. if (component_index >= array_size) {
  89. return _.diag(SPV_ERROR_INVALID_DATA)
  90. << spvOpcodeString(opcode)
  91. << ": array access is out of bounds, array size is "
  92. << array_size << ", but access index is " << component_index;
  93. }
  94. break;
  95. }
  96. case SpvOpTypeRuntimeArray: {
  97. *member_type = type_inst->word(2);
  98. // Array size is unknown.
  99. break;
  100. }
  101. case SpvOpTypeStruct: {
  102. const size_t num_struct_members = type_inst->words().size() - 2;
  103. if (component_index >= num_struct_members) {
  104. return _.diag(SPV_ERROR_INVALID_DATA)
  105. << "Index is out of bounds: Op" << spvOpcodeString(opcode)
  106. << " can not find index " << component_index
  107. << " into the structure <id> '" << type_inst->id()
  108. << "'. This structure has " << num_struct_members
  109. << " members. Largest valid index is "
  110. << num_struct_members - 1 << ".";
  111. }
  112. *member_type = type_inst->word(component_index + 2);
  113. break;
  114. }
  115. default:
  116. return _.diag(SPV_ERROR_INVALID_DATA)
  117. << "Op" << spvOpcodeString(opcode)
  118. << " reached non-composite type while indexes still remain to "
  119. "be traversed.";
  120. }
  121. }
  122. return SPV_SUCCESS;
  123. }
  124. } // anonymous namespace
  125. // Validates correctness of composite instructions.
  126. spv_result_t CompositesPass(ValidationState_t& _,
  127. const spv_parsed_instruction_t* inst) {
  128. const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
  129. const uint32_t result_type = inst->type_id;
  130. const uint32_t num_operands = static_cast<uint32_t>(inst->num_operands);
  131. switch (opcode) {
  132. case SpvOpVectorExtractDynamic: {
  133. const SpvOp result_opcode = _.GetIdOpcode(result_type);
  134. if (!spvOpcodeIsScalarType(result_opcode)) {
  135. return _.diag(SPV_ERROR_INVALID_DATA)
  136. << spvOpcodeString(opcode)
  137. << ": expected Result Type to be a scalar type";
  138. }
  139. const uint32_t vector_type = _.GetOperandTypeId(inst, 2);
  140. const SpvOp vector_opcode = _.GetIdOpcode(vector_type);
  141. if (vector_opcode != SpvOpTypeVector) {
  142. return _.diag(SPV_ERROR_INVALID_DATA)
  143. << spvOpcodeString(opcode)
  144. << ": expected Vector type to be OpTypeVector";
  145. }
  146. if (_.GetComponentType(vector_type) != result_type) {
  147. return _.diag(SPV_ERROR_INVALID_DATA)
  148. << spvOpcodeString(opcode)
  149. << ": expected Vector component type to be equal to Result Type";
  150. }
  151. const uint32_t index_type = _.GetOperandTypeId(inst, 3);
  152. if (!_.IsIntScalarType(index_type)) {
  153. return _.diag(SPV_ERROR_INVALID_DATA)
  154. << spvOpcodeString(opcode)
  155. << ": expected Index to be int scalar";
  156. }
  157. break;
  158. }
  159. case SpvOpVectorInsertDynamic: {
  160. const SpvOp result_opcode = _.GetIdOpcode(result_type);
  161. if (result_opcode != SpvOpTypeVector) {
  162. return _.diag(SPV_ERROR_INVALID_DATA)
  163. << spvOpcodeString(opcode)
  164. << ": expected Result Type to be OpTypeVector";
  165. }
  166. const uint32_t vector_type = _.GetOperandTypeId(inst, 2);
  167. if (vector_type != result_type) {
  168. return _.diag(SPV_ERROR_INVALID_DATA)
  169. << spvOpcodeString(opcode)
  170. << ": expected Vector type to be equal to Result Type";
  171. }
  172. const uint32_t component_type = _.GetOperandTypeId(inst, 3);
  173. if (_.GetComponentType(result_type) != component_type) {
  174. return _.diag(SPV_ERROR_INVALID_DATA)
  175. << spvOpcodeString(opcode)
  176. << ": expected Component type to be equal to Result Type "
  177. << "component type";
  178. }
  179. const uint32_t index_type = _.GetOperandTypeId(inst, 4);
  180. if (!_.IsIntScalarType(index_type)) {
  181. return _.diag(SPV_ERROR_INVALID_DATA)
  182. << spvOpcodeString(opcode)
  183. << ": expected Index to be int scalar";
  184. }
  185. break;
  186. }
  187. case SpvOpVectorShuffle: {
  188. // Handled in validate_id.cpp.
  189. // TODO([email protected]) Consider moving it here.
  190. break;
  191. }
  192. case SpvOpCompositeConstruct: {
  193. const SpvOp result_opcode = _.GetIdOpcode(result_type);
  194. switch (result_opcode) {
  195. case SpvOpTypeVector: {
  196. const uint32_t num_result_components = _.GetDimension(result_type);
  197. const uint32_t result_component_type =
  198. _.GetComponentType(result_type);
  199. uint32_t given_component_count = 0;
  200. if (num_operands <= 3) {
  201. return _.diag(SPV_ERROR_INVALID_DATA)
  202. << spvOpcodeString(opcode)
  203. << ": expected number of constituents to be at least 2";
  204. }
  205. for (uint32_t operand_index = 2; operand_index < num_operands;
  206. ++operand_index) {
  207. const uint32_t operand_type =
  208. _.GetOperandTypeId(inst, operand_index);
  209. if (operand_type == result_component_type) {
  210. ++given_component_count;
  211. } else {
  212. if (_.GetIdOpcode(operand_type) != SpvOpTypeVector ||
  213. _.GetComponentType(operand_type) != result_component_type) {
  214. return _.diag(SPV_ERROR_INVALID_DATA)
  215. << spvOpcodeString(opcode)
  216. << ": expected Constituents to be scalars or vectors of "
  217. << "the same type as Result Type components";
  218. }
  219. given_component_count += _.GetDimension(operand_type);
  220. }
  221. }
  222. if (num_result_components != given_component_count) {
  223. return _.diag(SPV_ERROR_INVALID_DATA)
  224. << spvOpcodeString(opcode)
  225. << ": expected total number of given components to be equal "
  226. << "to the size of Result Type vector";
  227. }
  228. break;
  229. }
  230. case SpvOpTypeMatrix: {
  231. uint32_t result_num_rows = 0;
  232. uint32_t result_num_cols = 0;
  233. uint32_t result_col_type = 0;
  234. uint32_t result_component_type = 0;
  235. if (!_.GetMatrixTypeInfo(result_type, &result_num_rows,
  236. &result_num_cols, &result_col_type,
  237. &result_component_type)) {
  238. assert(0);
  239. }
  240. if (result_num_cols + 2 != num_operands) {
  241. return _.diag(SPV_ERROR_INVALID_DATA)
  242. << spvOpcodeString(opcode)
  243. << ": expected total number of Constituents to be equal "
  244. << "to the number of columns of Result Type matrix";
  245. }
  246. for (uint32_t operand_index = 2; operand_index < num_operands;
  247. ++operand_index) {
  248. const uint32_t operand_type =
  249. _.GetOperandTypeId(inst, operand_index);
  250. if (operand_type != result_col_type) {
  251. return _.diag(SPV_ERROR_INVALID_DATA)
  252. << spvOpcodeString(opcode)
  253. << ": expected Constituent type to be equal to the column "
  254. << "type Result Type matrix";
  255. }
  256. }
  257. break;
  258. }
  259. case SpvOpTypeArray: {
  260. const Instruction* const array_inst = _.FindDef(result_type);
  261. assert(array_inst);
  262. assert(array_inst->opcode() == SpvOpTypeArray);
  263. auto size = _.FindDef(array_inst->word(3));
  264. if (spvOpcodeIsSpecConstant(size->opcode())) {
  265. // Cannot verify against the size of this array.
  266. break;
  267. }
  268. uint64_t array_size = 0;
  269. if (!_.GetConstantValUint64(array_inst->word(3), &array_size)) {
  270. assert(0 && "Array type definition is corrupt");
  271. }
  272. if (array_size + 2 != num_operands) {
  273. return _.diag(SPV_ERROR_INVALID_DATA)
  274. << spvOpcodeString(opcode)
  275. << ": expected total number of Constituents to be equal "
  276. << "to the number of elements of Result Type array";
  277. }
  278. const uint32_t result_component_type = array_inst->word(2);
  279. for (uint32_t operand_index = 2; operand_index < num_operands;
  280. ++operand_index) {
  281. const uint32_t operand_type =
  282. _.GetOperandTypeId(inst, operand_index);
  283. if (operand_type != result_component_type) {
  284. return _.diag(SPV_ERROR_INVALID_DATA)
  285. << spvOpcodeString(opcode)
  286. << ": expected Constituent type to be equal to the column "
  287. << "type Result Type array";
  288. }
  289. }
  290. break;
  291. }
  292. case SpvOpTypeStruct: {
  293. const Instruction* const struct_inst = _.FindDef(result_type);
  294. assert(struct_inst);
  295. assert(struct_inst->opcode() == SpvOpTypeStruct);
  296. if (struct_inst->operands().size() + 1 != num_operands) {
  297. return _.diag(SPV_ERROR_INVALID_DATA)
  298. << spvOpcodeString(opcode)
  299. << ": expected total number of Constituents to be equal "
  300. << "to the number of members of Result Type struct";
  301. }
  302. for (uint32_t operand_index = 2; operand_index < num_operands;
  303. ++operand_index) {
  304. const uint32_t operand_type =
  305. _.GetOperandTypeId(inst, operand_index);
  306. const uint32_t member_type = struct_inst->word(operand_index);
  307. if (operand_type != member_type) {
  308. return _.diag(SPV_ERROR_INVALID_DATA)
  309. << spvOpcodeString(opcode)
  310. << ": expected Constituent type to be equal to the "
  311. << "corresponding member type of Result Type struct";
  312. }
  313. }
  314. break;
  315. }
  316. default: {
  317. return _.diag(SPV_ERROR_INVALID_DATA)
  318. << spvOpcodeString(opcode)
  319. << ": expected Result Type to be a composite type";
  320. }
  321. }
  322. break;
  323. }
  324. case SpvOpCompositeExtract: {
  325. uint32_t member_type = 0;
  326. if (spv_result_t error =
  327. GetExtractInsertValueType(_, *inst, &member_type)) {
  328. return error;
  329. }
  330. if (result_type != member_type) {
  331. return _.diag(SPV_ERROR_INVALID_DATA)
  332. << "Op" << spvOpcodeString(opcode) << " result type (Op"
  333. << spvOpcodeString(_.GetIdOpcode(result_type))
  334. << ") does not match the type that results from indexing into "
  335. "the "
  336. "composite (Op"
  337. << spvOpcodeString(_.GetIdOpcode(member_type)) << ").";
  338. }
  339. break;
  340. }
  341. case SpvOpCompositeInsert: {
  342. const uint32_t object_type = _.GetOperandTypeId(inst, 2);
  343. const uint32_t composite_type = _.GetOperandTypeId(inst, 3);
  344. if (result_type != composite_type) {
  345. return _.diag(SPV_ERROR_INVALID_DATA)
  346. << "The Result Type must be the same as Composite type in Op"
  347. << spvOpcodeString(opcode) << " yielding Result Id "
  348. << result_type << ".";
  349. }
  350. uint32_t member_type = 0;
  351. if (spv_result_t error =
  352. GetExtractInsertValueType(_, *inst, &member_type)) {
  353. return error;
  354. }
  355. if (object_type != member_type) {
  356. return _.diag(SPV_ERROR_INVALID_DATA)
  357. << "The Object type (Op"
  358. << spvOpcodeString(_.GetIdOpcode(object_type)) << ") in Op"
  359. << spvOpcodeString(opcode)
  360. << " does not match the type that results from indexing into "
  361. "the Composite (Op"
  362. << spvOpcodeString(_.GetIdOpcode(member_type)) << ").";
  363. }
  364. break;
  365. }
  366. case SpvOpCopyObject: {
  367. if (!spvOpcodeGeneratesType(_.GetIdOpcode(result_type))) {
  368. return _.diag(SPV_ERROR_INVALID_DATA)
  369. << spvOpcodeString(opcode)
  370. << ": expected Result Type to be a type";
  371. }
  372. const uint32_t operand_type = _.GetOperandTypeId(inst, 2);
  373. if (operand_type != result_type) {
  374. return _.diag(SPV_ERROR_INVALID_DATA)
  375. << spvOpcodeString(opcode)
  376. << ": expected Result Type and Operand type to be the same";
  377. }
  378. break;
  379. }
  380. case SpvOpTranspose: {
  381. uint32_t result_num_rows = 0;
  382. uint32_t result_num_cols = 0;
  383. uint32_t result_col_type = 0;
  384. uint32_t result_component_type = 0;
  385. if (!_.GetMatrixTypeInfo(result_type, &result_num_rows, &result_num_cols,
  386. &result_col_type, &result_component_type)) {
  387. return _.diag(SPV_ERROR_INVALID_DATA)
  388. << spvOpcodeString(opcode)
  389. << ": expected Result Type to be a matrix type";
  390. }
  391. const uint32_t matrix_type = _.GetOperandTypeId(inst, 2);
  392. uint32_t matrix_num_rows = 0;
  393. uint32_t matrix_num_cols = 0;
  394. uint32_t matrix_col_type = 0;
  395. uint32_t matrix_component_type = 0;
  396. if (!_.GetMatrixTypeInfo(matrix_type, &matrix_num_rows, &matrix_num_cols,
  397. &matrix_col_type, &matrix_component_type)) {
  398. return _.diag(SPV_ERROR_INVALID_DATA)
  399. << spvOpcodeString(opcode)
  400. << ": expected Matrix to be of type OpTypeMatrix";
  401. }
  402. if (result_component_type != matrix_component_type) {
  403. return _.diag(SPV_ERROR_INVALID_DATA)
  404. << spvOpcodeString(opcode)
  405. << ": expected component types of Matrix and Result Type to be "
  406. << "identical";
  407. }
  408. if (result_num_rows != matrix_num_cols ||
  409. result_num_cols != matrix_num_rows) {
  410. return _.diag(SPV_ERROR_INVALID_DATA)
  411. << spvOpcodeString(opcode)
  412. << ": expected number of columns and the column size of Matrix "
  413. << "to be the reverse of those of Result Type";
  414. }
  415. break;
  416. }
  417. default:
  418. break;
  419. }
  420. return SPV_SUCCESS;
  421. }
  422. } // namespace libspirv