transformation_composite_extract.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_composite_extract.h"
  15. #include <vector>
  16. #include "source/fuzz/data_descriptor.h"
  17. #include "source/fuzz/fuzzer_util.h"
  18. #include "source/fuzz/instruction_descriptor.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. TransformationCompositeExtract::TransformationCompositeExtract(
  22. const spvtools::fuzz::protobufs::TransformationCompositeExtract& message)
  23. : message_(message) {}
  24. TransformationCompositeExtract::TransformationCompositeExtract(
  25. const protobufs::InstructionDescriptor& instruction_to_insert_before,
  26. uint32_t fresh_id, uint32_t composite_id,
  27. const std::vector<uint32_t>& index) {
  28. *message_.mutable_instruction_to_insert_before() =
  29. instruction_to_insert_before;
  30. message_.set_fresh_id(fresh_id);
  31. message_.set_composite_id(composite_id);
  32. for (auto an_index : index) {
  33. message_.add_index(an_index);
  34. }
  35. }
  36. bool TransformationCompositeExtract::IsApplicable(
  37. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  38. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  39. return false;
  40. }
  41. auto instruction_to_insert_before =
  42. FindInstruction(message_.instruction_to_insert_before(), ir_context);
  43. if (!instruction_to_insert_before) {
  44. return false;
  45. }
  46. auto composite_instruction =
  47. ir_context->get_def_use_mgr()->GetDef(message_.composite_id());
  48. if (!composite_instruction) {
  49. return false;
  50. }
  51. if (!fuzzerutil::IdIsAvailableBeforeInstruction(
  52. ir_context, instruction_to_insert_before, message_.composite_id())) {
  53. return false;
  54. }
  55. auto composite_type =
  56. ir_context->get_type_mgr()->GetType(composite_instruction->type_id());
  57. if (!fuzzerutil::IsCompositeType(composite_type)) {
  58. return false;
  59. }
  60. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
  61. SpvOpCompositeExtract, instruction_to_insert_before)) {
  62. return false;
  63. }
  64. return fuzzerutil::WalkCompositeTypeIndices(ir_context,
  65. composite_instruction->type_id(),
  66. message_.index()) != 0;
  67. }
  68. void TransformationCompositeExtract::Apply(
  69. opt::IRContext* ir_context,
  70. TransformationContext* transformation_context) const {
  71. opt::Instruction::OperandList extract_operands;
  72. extract_operands.push_back({SPV_OPERAND_TYPE_ID, {message_.composite_id()}});
  73. for (auto an_index : message_.index()) {
  74. extract_operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {an_index}});
  75. }
  76. auto composite_instruction =
  77. ir_context->get_def_use_mgr()->GetDef(message_.composite_id());
  78. auto extracted_type = fuzzerutil::WalkCompositeTypeIndices(
  79. ir_context, composite_instruction->type_id(), message_.index());
  80. FindInstruction(message_.instruction_to_insert_before(), ir_context)
  81. ->InsertBefore(MakeUnique<opt::Instruction>(
  82. ir_context, SpvOpCompositeExtract, extracted_type,
  83. message_.fresh_id(), extract_operands));
  84. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  85. ir_context->InvalidateAnalysesExceptFor(
  86. opt::IRContext::Analysis::kAnalysisNone);
  87. AddDataSynonymFacts(ir_context, transformation_context);
  88. }
  89. protobufs::Transformation TransformationCompositeExtract::ToMessage() const {
  90. protobufs::Transformation result;
  91. *result.mutable_composite_extract() = message_;
  92. return result;
  93. }
  94. std::unordered_set<uint32_t> TransformationCompositeExtract::GetFreshIds()
  95. const {
  96. return {message_.fresh_id()};
  97. }
  98. void TransformationCompositeExtract::AddDataSynonymFacts(
  99. opt::IRContext* ir_context,
  100. TransformationContext* transformation_context) const {
  101. // Don't add synonyms if the composite being extracted from is not suitable,
  102. // or if the result id into which we are extracting is irrelevant.
  103. if (!fuzzerutil::CanMakeSynonymOf(
  104. ir_context, *transformation_context,
  105. ir_context->get_def_use_mgr()->GetDef(message_.composite_id())) ||
  106. transformation_context->GetFactManager()->IdIsIrrelevant(
  107. message_.fresh_id())) {
  108. return;
  109. }
  110. // Add the fact that the id storing the extracted element is synonymous with
  111. // the index into the structure.
  112. std::vector<uint32_t> indices(message_.index().begin(),
  113. message_.index().end());
  114. auto data_descriptor_for_extracted_element =
  115. MakeDataDescriptor(message_.composite_id(), indices);
  116. auto data_descriptor_for_result_id =
  117. MakeDataDescriptor(message_.fresh_id(), {});
  118. transformation_context->GetFactManager()->AddFactDataSynonym(
  119. data_descriptor_for_extracted_element, data_descriptor_for_result_id);
  120. }
  121. } // namespace fuzz
  122. } // namespace spvtools