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