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