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