Checks that the number of OpTypeStruct members is within the limit.
| 356 | |
| 357 | // Checks that the number of OpTypeStruct members is within the limit. |
| 358 | spv_result_t LimitCheckStruct(ValidationState_t& _, const Instruction* inst) { |
| 359 | if (spv::Op::OpTypeStruct != inst->opcode()) { |
| 360 | return SPV_SUCCESS; |
| 361 | } |
| 362 | |
| 363 | // Number of members is the number of operands of the instruction minus 1. |
| 364 | // One operand is the result ID. |
| 365 | const uint16_t limit = |
| 366 | static_cast<uint16_t>(_.options()->universal_limits_.max_struct_members); |
| 367 | if (inst->operands().size() - 1 > limit) { |
| 368 | return _.diag(SPV_ERROR_INVALID_BINARY, inst) |
| 369 | << "Number of OpTypeStruct members (" << inst->operands().size() - 1 |
| 370 | << ") has exceeded the limit (" << limit << ")."; |
| 371 | } |
| 372 | |
| 373 | // Section 2.17 of SPIRV Spec specifies that the "Structure Nesting Depth" |
| 374 | // must be less than or equal to 255. |
| 375 | // This is interpreted as structures including other structures as |
| 376 | // members. The code does not follow pointers or look into arrays to see |
| 377 | // if we reach a structure downstream. The nesting depth of a struct is |
| 378 | // 1+(largest depth of any member). Scalars are at depth 0. |
| 379 | uint32_t max_member_depth = 0; |
| 380 | // Struct members start at word 2 of OpTypeStruct instruction. |
| 381 | for (size_t word_i = 2; word_i < inst->words().size(); ++word_i) { |
| 382 | auto member = inst->word(word_i); |
| 383 | auto memberTypeInstr = _.FindDef(member); |
| 384 | if (memberTypeInstr && spv::Op::OpTypeStruct == memberTypeInstr->opcode()) { |
| 385 | max_member_depth = std::max( |
| 386 | max_member_depth, _.struct_nesting_depth(memberTypeInstr->id())); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | const uint32_t depth_limit = _.options()->universal_limits_.max_struct_depth; |
| 391 | const uint32_t cur_depth = 1 + max_member_depth; |
| 392 | _.set_struct_nesting_depth(inst->id(), cur_depth); |
| 393 | if (cur_depth > depth_limit) { |
| 394 | return _.diag(SPV_ERROR_INVALID_BINARY, inst) |
| 395 | << "Structure Nesting Depth may not be larger than " << depth_limit |
| 396 | << ". Found " << cur_depth << "."; |
| 397 | } |
| 398 | return SPV_SUCCESS; |
| 399 | } |
| 400 | |
| 401 | // Checks that the number of (literal, label) pairs in OpSwitch is within |
| 402 | // the limit. |
no test coverage detected