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