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