| 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 |
|
| 15 |
#include "source/fuzz/instruction_descriptor.h" |
| 16 |
|
| 17 |
namespace spvtools { |
| 18 |
namespace fuzz { |
| 19 |
|
| 20 |
opt::Instruction* FindInstruction( |
| 21 |
const protobufs::InstructionDescriptor& instruction_descriptor, |
| 22 |
spvtools::opt::IRContext* context) { |
| 23 |
for (auto& function : *context->module()) { |
| 24 |
for (auto& block : function) { |
| 25 |
bool found_base = |
| 26 |
block.id() == instruction_descriptor.base_instruction_result_id(); |
| 27 |
uint32_t num_ignored = 0; |
| 28 |
for (auto& instruction : block) { |
| 29 |
if (instruction.HasResultId() && |
| 30 |
instruction.result_id() == |
| 31 |
instruction_descriptor.base_instruction_result_id()) { |
| 32 |
assert(!found_base && |
| 33 |
"It should not be possible to find the base instruction " |
| 34 |
"multiple times."); |
| 35 |
found_base = true; |
| 36 |
assert(num_ignored == 0 && |
| 37 |
"The skipped instruction count should only be incremented " |
| 38 |
"after the instruction base has been found."); |
| 39 |
} |
| 40 |
if (found_base && |
| 41 |
instruction.opcode() == |
| 42 |
instruction_descriptor.target_instruction_opcode()) { |
| 43 |
if (num_ignored == instruction_descriptor.num_opcodes_to_ignore()) { |
| 44 |
return &instruction; |
| 45 |
} |
| 46 |
num_ignored++; |
| 47 |
} |
| 48 |
} |
| 49 |
if (found_base) { |
| 50 |
// We found the base instruction, but did not find the target |
| 51 |
// instruction in the same block. |
| 52 |
return nullptr; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
return nullptr; |
| 57 |
} |
| 58 |
|
| 59 |
protobufs::InstructionDescriptor MakeInstructionDescriptor( |
| 60 |
uint32_t base_instruction_result_id, SpvOp target_instruction_opcode, |
| 61 |
uint32_t num_opcodes_to_ignore) { |
| 62 |
protobufs::InstructionDescriptor result; |
| 63 |
result.set_base_instruction_result_id(base_instruction_result_id); |
| 64 |
result.set_target_instruction_opcode(target_instruction_opcode); |
| 65 |
result.set_num_opcodes_to_ignore(num_opcodes_to_ignore); |
| 66 |
return result; |
| 67 |
} |
| 68 |
|
| 69 |
protobufs::InstructionDescriptor MakeInstructionDescriptor( |
| 70 |
const opt::BasicBlock& block, |
| 71 |
const opt::BasicBlock::const_iterator& inst_it) { |
| 72 |
const SpvOp opcode = |
| 73 |
inst_it->opcode(); // The opcode of the instruction being described. |
| 74 |
uint32_t skip_count = 0; // The number of these opcodes we have skipped when |
| 75 |
// searching backwards. |
| 76 |
|
| 77 |
// Consider instructions in the block in reverse order, starting from |
| 78 |
// |inst_it|. |
| 79 |
for (opt::BasicBlock::const_iterator backwards_iterator = inst_it;; |
| 80 |
--backwards_iterator) { |
| 81 |
if (backwards_iterator->HasResultId()) { |
| 82 |
// As soon as we find an instruction with a result id, we can return a |
| 83 |
// descriptor for |inst_it|. |
| 84 |
return MakeInstructionDescriptor(backwards_iterator->result_id(), opcode, |
| 85 |
skip_count); |
| 86 |
} |
| 87 |
if (backwards_iterator != inst_it && |
| 88 |
backwards_iterator->opcode() == opcode) { |
| 89 |
// We are skipping over an instruction with the same opcode as |inst_it|; |
| 90 |
// we increase our skip count to reflect this. |
| 91 |
skip_count++; |
| 92 |
} |
| 93 |
if (backwards_iterator == block.begin()) { |
| 94 |
// We exit the loop when we reach the start of the block, but only after |
| 95 |
// we have processed the first instruction in the block. |
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
// We did not find an instruction inside the block with a result id, so we use |
| 100 |
// the block's label's id. |
| 101 |
return MakeInstructionDescriptor(block.id(), opcode, skip_count); |
| 102 |
} |
| 103 |
|
| 104 |
protobufs::InstructionDescriptor MakeInstructionDescriptor( |
| 105 |
opt::IRContext* context, opt::Instruction* inst) { |
| 106 |
auto block = context->get_instr_block(inst); |
| 107 |
uint32_t base_instruction_result_id = block->id(); |
| 108 |
uint32_t num_opcodes_to_ignore = 0; |
| 109 |
for (auto& inst_in_block : *block) { |
| 110 |
if (inst_in_block.HasResultId()) { |
| 111 |
base_instruction_result_id = inst_in_block.result_id(); |
| 112 |
num_opcodes_to_ignore = 0; |
| 113 |
} |
| 114 |
if (&inst_in_block == inst) { |
| 115 |
return MakeInstructionDescriptor(base_instruction_result_id, |
| 116 |
inst->opcode(), num_opcodes_to_ignore); |
| 117 |
} |
| 118 |
if (inst_in_block.opcode() == inst->opcode()) { |
| 119 |
num_opcodes_to_ignore++; |
| 120 |
} |
| 121 |
} |
| 122 |
assert(false && "No matching instruction was found."); |
| 123 |
return protobufs::InstructionDescriptor(); |
| 124 |
} |
| 125 |
|
| 126 |
} // namespace fuzz |
| 127 |
} // namespace spvtools |