1 // Copyright (c) 2020 Vasyl Teliman
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_add_copy_memory.h"
16
17 #include "source/fuzz/fuzzer_context.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "source/fuzz/instruction_descriptor.h"
20 #include "source/fuzz/transformation_add_copy_memory.h"
21
22 namespace spvtools {
23 namespace fuzz {
24
FuzzerPassAddCopyMemory(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)25 FuzzerPassAddCopyMemory::FuzzerPassAddCopyMemory(
26 opt::IRContext* ir_context, TransformationContext* transformation_context,
27 FuzzerContext* fuzzer_context,
28 protobufs::TransformationSequence* transformations)
29 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
30 transformations) {}
31
Apply()32 void FuzzerPassAddCopyMemory::Apply() {
33 ForEachInstructionWithInstructionDescriptor(
34 [this](opt::Function* function, opt::BasicBlock* block,
35 opt::BasicBlock::iterator inst_it,
36 const protobufs::InstructionDescriptor& instruction_descriptor) {
37 // Check that we can insert an OpCopyMemory before this instruction.
38 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpCopyMemory,
39 inst_it)) {
40 return;
41 }
42
43 if (!GetFuzzerContext()->ChoosePercentage(
44 GetFuzzerContext()->GetChanceOfAddingCopyMemory())) {
45 return;
46 }
47
48 // Get all instructions available before |inst_it| according to the
49 // domination rules.
50 auto instructions = FindAvailableInstructions(
51 function, block, inst_it,
52 TransformationAddCopyMemory::IsInstructionSupported);
53
54 if (instructions.empty()) {
55 return;
56 }
57
58 const auto* inst =
59 instructions[GetFuzzerContext()->RandomIndex(instructions)];
60
61 // Decide whether to create global or local variable.
62 auto storage_class = GetFuzzerContext()->ChooseEven()
63 ? SpvStorageClassPrivate
64 : SpvStorageClassFunction;
65
66 auto pointee_type_id = fuzzerutil::GetPointeeTypeIdFromPointerType(
67 GetIRContext(), inst->type_id());
68
69 // Create a pointer type with |storage_class| if needed.
70 FindOrCreatePointerType(pointee_type_id, storage_class);
71
72 ApplyTransformation(TransformationAddCopyMemory(
73 instruction_descriptor, GetFuzzerContext()->GetFreshId(),
74 inst->result_id(), storage_class,
75 FindOrCreateZeroConstant(pointee_type_id, false)));
76 });
77 }
78
79 } // namespace fuzz
80 } // namespace spvtools
81