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_synonyms.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_synonym.h"
21
22 namespace spvtools {
23 namespace fuzz {
24
FuzzerPassAddSynonyms(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)25 FuzzerPassAddSynonyms::FuzzerPassAddSynonyms(
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
32 FuzzerPassAddSynonyms::~FuzzerPassAddSynonyms() = default;
33
Apply()34 void FuzzerPassAddSynonyms::Apply() {
35 ForEachInstructionWithInstructionDescriptor(
36 [this](opt::Function* function, opt::BasicBlock* block,
37 opt::BasicBlock::iterator inst_it,
38 const protobufs::InstructionDescriptor& instruction_descriptor) {
39 if (GetTransformationContext()->GetFactManager()->BlockIsDead(
40 block->id())) {
41 // Don't create synonyms in dead blocks.
42 return;
43 }
44
45 // Skip |inst_it| if we can't insert anything above it. OpIAdd is just
46 // a representative of some instruction that might be produced by the
47 // transformation.
48 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpIAdd, inst_it)) {
49 return;
50 }
51
52 if (!GetFuzzerContext()->ChoosePercentage(
53 GetFuzzerContext()->GetChanceOfAddingSynonyms())) {
54 return;
55 }
56
57 auto synonym_type = GetFuzzerContext()->GetRandomSynonymType();
58
59 // Select all instructions that can be used to create a synonym to.
60 auto available_instructions = FindAvailableInstructions(
61 function, block, inst_it,
62 [synonym_type, this](opt::IRContext* ir_context,
63 opt::Instruction* inst) {
64 // Check that we can create a synonym to |inst| as described by
65 // the |synonym_type| and insert it before |inst_it|.
66 return TransformationAddSynonym::IsInstructionValid(
67 ir_context, *GetTransformationContext(), inst, synonym_type);
68 });
69
70 if (available_instructions.empty()) {
71 return;
72 }
73
74 const auto* existing_synonym =
75 available_instructions[GetFuzzerContext()->RandomIndex(
76 available_instructions)];
77
78 // Make sure the module contains all instructions required to apply the
79 // transformation.
80 switch (synonym_type) {
81 case protobufs::TransformationAddSynonym::ADD_ZERO:
82 case protobufs::TransformationAddSynonym::SUB_ZERO:
83 case protobufs::TransformationAddSynonym::LOGICAL_OR:
84 // Create a zero constant to be used as an operand of the synonymous
85 // instruction.
86 FindOrCreateZeroConstant(existing_synonym->type_id(), false);
87 break;
88 case protobufs::TransformationAddSynonym::MUL_ONE:
89 case protobufs::TransformationAddSynonym::LOGICAL_AND: {
90 const auto* existing_synonym_type =
91 GetIRContext()->get_type_mgr()->GetType(
92 existing_synonym->type_id());
93 assert(existing_synonym_type && "Instruction has invalid type");
94
95 if (const auto* vector = existing_synonym_type->AsVector()) {
96 auto element_type_id =
97 GetIRContext()->get_type_mgr()->GetId(vector->element_type());
98 assert(element_type_id && "Vector's element type is invalid");
99
100 auto one_word = vector->element_type()->AsFloat()
101 ? fuzzerutil::FloatToWord(1)
102 : 1u;
103 FindOrCreateCompositeConstant(
104 std::vector<uint32_t>(
105 vector->element_count(),
106 FindOrCreateConstant({one_word}, element_type_id, false)),
107 existing_synonym->type_id(), false);
108 } else {
109 FindOrCreateConstant(
110 {existing_synonym_type->AsFloat() ? fuzzerutil::FloatToWord(1)
111 : 1u},
112 existing_synonym->type_id(), false);
113 }
114 } break;
115 default:
116 // This assertion will fail if some SynonymType is missing from the
117 // switch statement.
118 assert(
119 !TransformationAddSynonym::IsAdditionalConstantRequired(
120 synonym_type) &&
121 "|synonym_type| requires an additional constant to be present "
122 "in the module");
123 break;
124 }
125
126 ApplyTransformation(TransformationAddSynonym(
127 existing_synonym->result_id(), synonym_type,
128 GetFuzzerContext()->GetFreshId(), instruction_descriptor));
129 });
130 }
131
132 } // namespace fuzz
133 } // namespace spvtools
134