2
0

transformation_composite_extract.cpp 4.7 KB

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