transformation_swap_conditional_branch_operands.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2020 Vasyl Teliman
  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_swap_conditional_branch_operands.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationSwapConditionalBranchOperands::
  20. TransformationSwapConditionalBranchOperands(
  21. protobufs::TransformationSwapConditionalBranchOperands message)
  22. : message_(std::move(message)) {}
  23. TransformationSwapConditionalBranchOperands::
  24. TransformationSwapConditionalBranchOperands(
  25. const protobufs::InstructionDescriptor& instruction_descriptor,
  26. uint32_t fresh_id) {
  27. *message_.mutable_instruction_descriptor() = instruction_descriptor;
  28. message_.set_fresh_id(fresh_id);
  29. }
  30. bool TransformationSwapConditionalBranchOperands::IsApplicable(
  31. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  32. const auto* inst =
  33. FindInstruction(message_.instruction_descriptor(), ir_context);
  34. return fuzzerutil::IsFreshId(ir_context, message_.fresh_id()) && inst &&
  35. inst->opcode() == spv::Op::OpBranchConditional;
  36. }
  37. void TransformationSwapConditionalBranchOperands::Apply(
  38. opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
  39. auto* branch_inst =
  40. FindInstruction(message_.instruction_descriptor(), ir_context);
  41. assert(branch_inst);
  42. auto* block = ir_context->get_instr_block(branch_inst);
  43. assert(block);
  44. // Compute the last instruction in the |block| that allows us to insert
  45. // OpLogicalNot above it.
  46. auto iter = fuzzerutil::GetIteratorForInstruction(block, branch_inst);
  47. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpLogicalNot,
  48. iter)) {
  49. // There might be a merge instruction before OpBranchConditional.
  50. --iter;
  51. }
  52. assert(fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpLogicalNot,
  53. iter) &&
  54. "We should now be able to insert spv::Op::OpLogicalNot before |iter|");
  55. // Get the instruction whose result is used as a condition for
  56. // OpBranchConditional.
  57. const auto* condition_inst = ir_context->get_def_use_mgr()->GetDef(
  58. branch_inst->GetSingleWordInOperand(0));
  59. assert(condition_inst);
  60. // We are swapping the labels in OpBranchConditional. This means that we must
  61. // invert the guard as well. We are using OpLogicalNot for that purpose here.
  62. auto new_instruction = MakeUnique<opt::Instruction>(
  63. ir_context, spv::Op::OpLogicalNot, condition_inst->type_id(),
  64. message_.fresh_id(),
  65. opt::Instruction::OperandList{
  66. {SPV_OPERAND_TYPE_ID, {condition_inst->result_id()}}});
  67. auto new_instruction_ptr = new_instruction.get();
  68. iter.InsertBefore(std::move(new_instruction));
  69. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  70. // Update OpBranchConditional condition operand.
  71. branch_inst->GetInOperand(0).words[0] = message_.fresh_id();
  72. // Swap label operands.
  73. std::swap(branch_inst->GetInOperand(1), branch_inst->GetInOperand(2));
  74. // Additionally, swap branch weights if present.
  75. if (branch_inst->NumInOperands() > 3) {
  76. std::swap(branch_inst->GetInOperand(3), branch_inst->GetInOperand(4));
  77. }
  78. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction_ptr);
  79. ir_context->set_instr_block(new_instruction_ptr, block);
  80. ir_context->get_def_use_mgr()->EraseUseRecordsOfOperandIds(branch_inst);
  81. ir_context->get_def_use_mgr()->AnalyzeInstUse(branch_inst);
  82. // No analyses need to be invalidated since the transformation is local to a
  83. // block and the def-use and instruction-to-block mappings have been updated.
  84. }
  85. protobufs::Transformation
  86. TransformationSwapConditionalBranchOperands::ToMessage() const {
  87. protobufs::Transformation result;
  88. *result.mutable_swap_conditional_branch_operands() = message_;
  89. return result;
  90. }
  91. std::unordered_set<uint32_t>
  92. TransformationSwapConditionalBranchOperands::GetFreshIds() const {
  93. return {message_.fresh_id()};
  94. }
  95. } // namespace fuzz
  96. } // namespace spvtools