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(spv::Op::OpIAdd,
48 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 case protobufs::TransformationAddSynonym::BITWISE_OR:
85 case protobufs::TransformationAddSynonym::BITWISE_XOR:
86 // Create a zero constant to be used as an operand of the synonymous
87 // instruction.
88 FindOrCreateZeroConstant(existing_synonym->type_id(), false);
89 break;
90 case protobufs::TransformationAddSynonym::MUL_ONE:
91 case protobufs::TransformationAddSynonym::LOGICAL_AND: {
92 const auto* existing_synonym_type =
93 GetIRContext()->get_type_mgr()->GetType(
94 existing_synonym->type_id());
95 assert(existing_synonym_type && "Instruction has invalid type");
96
97 if (const auto* vector = existing_synonym_type->AsVector()) {
98 auto element_type_id =
99 GetIRContext()->get_type_mgr()->GetId(vector->element_type());
100 assert(element_type_id && "Vector's element type is invalid");
101
102 auto one_word = vector->element_type()->AsFloat()
103 ? fuzzerutil::FloatToWord(1)
104 : 1u;
105 FindOrCreateCompositeConstant(
106 std::vector<uint32_t>(
107 vector->element_count(),
108 FindOrCreateConstant({one_word}, element_type_id, false)),
109 existing_synonym->type_id(), false);
110 } else {
111 FindOrCreateConstant(
112 {existing_synonym_type->AsFloat() ? fuzzerutil::FloatToWord(1)
113 : 1u},
114 existing_synonym->type_id(), false);
115 }
116 } break;
117 default:
118 // This assertion will fail if some SynonymType is missing from the
119 // switch statement.
120 assert(
121 !TransformationAddSynonym::IsAdditionalConstantRequired(
122 synonym_type) &&
123 "|synonym_type| requires an additional constant to be present "
124 "in the module");
125 break;
126 }
127
128 ApplyTransformation(TransformationAddSynonym(
129 existing_synonym->result_id(), synonym_type,
130 GetFuzzerContext()->GetFreshId(), instruction_descriptor));
131 });
132 }
133
134 } // namespace fuzz
135 } // namespace spvtools
136