transformation_composite_extract.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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, std::vector<uint32_t>&& index) {
  27. *message_.mutable_instruction_to_insert_before() =
  28. instruction_to_insert_before;
  29. message_.set_fresh_id(fresh_id);
  30. message_.set_composite_id(composite_id);
  31. for (auto an_index : index) {
  32. message_.add_index(an_index);
  33. }
  34. }
  35. bool TransformationCompositeExtract::IsApplicable(
  36. opt::IRContext* context,
  37. const spvtools::fuzz::FactManager& /*unused*/) const {
  38. if (!fuzzerutil::IsFreshId(context, message_.fresh_id())) {
  39. return false;
  40. }
  41. auto instruction_to_insert_before =
  42. FindInstruction(message_.instruction_to_insert_before(), context);
  43. if (!instruction_to_insert_before) {
  44. return false;
  45. }
  46. auto composite_instruction =
  47. context->get_def_use_mgr()->GetDef(message_.composite_id());
  48. if (!composite_instruction) {
  49. return false;
  50. }
  51. if (auto block = context->get_instr_block(composite_instruction)) {
  52. if (composite_instruction == instruction_to_insert_before ||
  53. !context->GetDominatorAnalysis(block->GetParent())
  54. ->Dominates(composite_instruction, instruction_to_insert_before)) {
  55. return false;
  56. }
  57. }
  58. assert(composite_instruction->type_id() &&
  59. "An instruction in a block cannot have a result id but no type id.");
  60. auto composite_type =
  61. context->get_type_mgr()->GetType(composite_instruction->type_id());
  62. if (!composite_type) {
  63. return false;
  64. }
  65. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
  66. SpvOpCompositeExtract, instruction_to_insert_before)) {
  67. return false;
  68. }
  69. return fuzzerutil::WalkCompositeTypeIndices(
  70. context, composite_instruction->type_id(), message_.index()) != 0;
  71. }
  72. void TransformationCompositeExtract::Apply(
  73. opt::IRContext* context, spvtools::fuzz::FactManager* fact_manager) const {
  74. opt::Instruction::OperandList extract_operands;
  75. extract_operands.push_back({SPV_OPERAND_TYPE_ID, {message_.composite_id()}});
  76. for (auto an_index : message_.index()) {
  77. extract_operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {an_index}});
  78. }
  79. auto composite_instruction =
  80. context->get_def_use_mgr()->GetDef(message_.composite_id());
  81. auto extracted_type = fuzzerutil::WalkCompositeTypeIndices(
  82. context, composite_instruction->type_id(), message_.index());
  83. FindInstruction(message_.instruction_to_insert_before(), context)
  84. ->InsertBefore(MakeUnique<opt::Instruction>(
  85. context, SpvOpCompositeExtract, extracted_type, message_.fresh_id(),
  86. extract_operands));
  87. fuzzerutil::UpdateModuleIdBound(context, message_.fresh_id());
  88. context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
  89. // Add the fact that the id storing the extracted element is synonymous with
  90. // the index into the structure.
  91. std::vector<uint32_t> indices;
  92. for (auto an_index : message_.index()) {
  93. indices.push_back(an_index);
  94. }
  95. protobufs::DataDescriptor data_descriptor_for_extracted_element =
  96. MakeDataDescriptor(message_.composite_id(), std::move(indices));
  97. protobufs::DataDescriptor data_descriptor_for_result_id =
  98. MakeDataDescriptor(message_.fresh_id(), {});
  99. fact_manager->AddFactDataSynonym(data_descriptor_for_extracted_element,
  100. data_descriptor_for_result_id, context);
  101. }
  102. protobufs::Transformation TransformationCompositeExtract::ToMessage() const {
  103. protobufs::Transformation result;
  104. *result.mutable_composite_extract() = message_;
  105. return result;
  106. }
  107. } // namespace fuzz
  108. } // namespace spvtools