transformation_add_constant_composite.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (c) 2019 Google LLC
  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/fuzz/transformation_add_constant_composite.h"
  15. #include <vector>
  16. #include "source/fuzz/fuzzer_util.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationAddConstantComposite::TransformationAddConstantComposite(
  20. spvtools::fuzz::protobufs::TransformationAddConstantComposite message)
  21. : message_(std::move(message)) {}
  22. TransformationAddConstantComposite::TransformationAddConstantComposite(
  23. uint32_t fresh_id, uint32_t type_id,
  24. const std::vector<uint32_t>& constituent_ids, bool is_irrelevant) {
  25. message_.set_fresh_id(fresh_id);
  26. message_.set_type_id(type_id);
  27. message_.set_is_irrelevant(is_irrelevant);
  28. for (auto constituent_id : constituent_ids) {
  29. message_.add_constituent_id(constituent_id);
  30. }
  31. }
  32. bool TransformationAddConstantComposite::IsApplicable(
  33. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  34. // Check that the given id is fresh.
  35. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  36. return false;
  37. }
  38. // Check that the composite type id is an instruction id.
  39. auto composite_type_instruction =
  40. ir_context->get_def_use_mgr()->GetDef(message_.type_id());
  41. if (!composite_type_instruction) {
  42. return false;
  43. }
  44. // Gather up the operands for the composite constant, in the process checking
  45. // whether the given type really defines a composite and - in the case of a
  46. // struct - whether its decorations are OK.
  47. std::vector<uint32_t> constituent_type_ids;
  48. switch (composite_type_instruction->opcode()) {
  49. case spv::Op::OpTypeArray:
  50. for (uint32_t index = 0;
  51. index <
  52. fuzzerutil::GetArraySize(*composite_type_instruction, ir_context);
  53. index++) {
  54. constituent_type_ids.push_back(
  55. composite_type_instruction->GetSingleWordInOperand(0));
  56. }
  57. break;
  58. case spv::Op::OpTypeMatrix:
  59. case spv::Op::OpTypeVector:
  60. for (uint32_t index = 0;
  61. index < composite_type_instruction->GetSingleWordInOperand(1);
  62. index++) {
  63. constituent_type_ids.push_back(
  64. composite_type_instruction->GetSingleWordInOperand(0));
  65. }
  66. break;
  67. case spv::Op::OpTypeStruct:
  68. // We do not create constants of structs decorated with Block nor
  69. // BufferBlock. The SPIR-V spec does not explicitly disallow this, but it
  70. // seems like a strange thing to do, so we disallow it to avoid triggering
  71. // low priority edge case issues related to it.
  72. if (fuzzerutil::HasBlockOrBufferBlockDecoration(
  73. ir_context, composite_type_instruction->result_id())) {
  74. return false;
  75. }
  76. composite_type_instruction->ForEachInOperand(
  77. [&constituent_type_ids](const uint32_t* member_type_id) {
  78. constituent_type_ids.push_back(*member_type_id);
  79. });
  80. break;
  81. default:
  82. // Not a composite type.
  83. return false;
  84. }
  85. // Check that the number of provided operands matches the number of
  86. // constituents required by the type.
  87. if (constituent_type_ids.size() !=
  88. static_cast<uint32_t>(message_.constituent_id().size())) {
  89. return false;
  90. }
  91. // Check that every provided operand refers to an instruction of the
  92. // corresponding constituent type.
  93. for (uint32_t index = 0; index < constituent_type_ids.size(); index++) {
  94. auto constituent_instruction =
  95. ir_context->get_def_use_mgr()->GetDef(message_.constituent_id(index));
  96. if (!constituent_instruction) {
  97. return false;
  98. }
  99. if (constituent_instruction->type_id() != constituent_type_ids.at(index)) {
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105. void TransformationAddConstantComposite::Apply(
  106. opt::IRContext* ir_context,
  107. TransformationContext* transformation_context) const {
  108. opt::Instruction::OperandList in_operands;
  109. for (auto constituent_id : message_.constituent_id()) {
  110. in_operands.push_back({SPV_OPERAND_TYPE_ID, {constituent_id}});
  111. }
  112. auto new_instruction = MakeUnique<opt::Instruction>(
  113. ir_context, spv::Op::OpConstantComposite, message_.type_id(),
  114. message_.fresh_id(), in_operands);
  115. auto new_instruction_ptr = new_instruction.get();
  116. ir_context->module()->AddGlobalValue(std::move(new_instruction));
  117. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  118. // Inform the def-use manager of the new instruction. Invalidate the constant
  119. // manager as we have added a new constant.
  120. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction_ptr);
  121. ir_context->InvalidateAnalyses(opt::IRContext::kAnalysisConstants);
  122. if (message_.is_irrelevant()) {
  123. transformation_context->GetFactManager()->AddFactIdIsIrrelevant(
  124. message_.fresh_id());
  125. }
  126. }
  127. protobufs::Transformation TransformationAddConstantComposite::ToMessage()
  128. const {
  129. protobufs::Transformation result;
  130. *result.mutable_add_constant_composite() = message_;
  131. return result;
  132. }
  133. std::unordered_set<uint32_t> TransformationAddConstantComposite::GetFreshIds()
  134. const {
  135. return {message_.fresh_id()};
  136. }
  137. } // namespace fuzz
  138. } // namespace spvtools