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/fuzzer_pass_copy_objects.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
19 #include "source/fuzz/transformation_add_synonym.h"
20
21 namespace spvtools {
22 namespace fuzz {
23
FuzzerPassCopyObjects(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)24 FuzzerPassCopyObjects::FuzzerPassCopyObjects(
25 opt::IRContext* ir_context, TransformationContext* transformation_context,
26 FuzzerContext* fuzzer_context,
27 protobufs::TransformationSequence* transformations)
28 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
29 transformations) {}
30
31 FuzzerPassCopyObjects::~FuzzerPassCopyObjects() = default;
32
Apply()33 void FuzzerPassCopyObjects::Apply() {
34 ForEachInstructionWithInstructionDescriptor(
35 [this](opt::Function* function, opt::BasicBlock* block,
36 opt::BasicBlock::iterator inst_it,
37 const protobufs::InstructionDescriptor& instruction_descriptor)
38 -> void {
39 assert(inst_it->opcode() ==
40 instruction_descriptor.target_instruction_opcode() &&
41 "The opcode of the instruction we might insert before must be "
42 "the same as the opcode in the descriptor for the instruction");
43
44 if (GetTransformationContext()->GetFactManager()->BlockIsDead(
45 block->id())) {
46 // Don't create synonyms in dead blocks.
47 return;
48 }
49
50 // Check whether it is legitimate to insert a copy before this
51 // instruction.
52 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpCopyObject,
53 inst_it)) {
54 return;
55 }
56
57 // Randomly decide whether to try inserting an object copy here.
58 if (!GetFuzzerContext()->ChoosePercentage(
59 GetFuzzerContext()->GetChanceOfCopyingObject())) {
60 return;
61 }
62
63 const auto relevant_instructions = FindAvailableInstructions(
64 function, block, inst_it,
65 [this](opt::IRContext* ir_context, opt::Instruction* inst) {
66 return TransformationAddSynonym::IsInstructionValid(
67 ir_context, *GetTransformationContext(), inst,
68 protobufs::TransformationAddSynonym::COPY_OBJECT);
69 });
70
71 // At this point, |relevant_instructions| contains all the instructions
72 // we might think of copying.
73 if (relevant_instructions.empty()) {
74 return;
75 }
76
77 // Choose a copyable instruction at random, and create and apply an
78 // object copying transformation based on it.
79 ApplyTransformation(TransformationAddSynonym(
80 relevant_instructions[GetFuzzerContext()->RandomIndex(
81 relevant_instructions)]
82 ->result_id(),
83 protobufs::TransformationAddSynonym::COPY_OBJECT,
84 GetFuzzerContext()->GetFreshId(), instruction_descriptor));
85 });
86 }
87
88 } // namespace fuzz
89 } // namespace spvtools
90