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_propagate_instructions_down.h"
16
17 #include "source/fuzz/fuzzer_context.h"
18 #include "source/fuzz/transformation_propagate_instruction_down.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
FuzzerPassPropagateInstructionsDown(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations,bool ignore_inapplicable_transformations)23 FuzzerPassPropagateInstructionsDown::FuzzerPassPropagateInstructionsDown(
24 opt::IRContext* ir_context, TransformationContext* transformation_context,
25 FuzzerContext* fuzzer_context,
26 protobufs::TransformationSequence* transformations,
27 bool ignore_inapplicable_transformations)
28 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
29 transformations, ignore_inapplicable_transformations) {}
30
Apply()31 void FuzzerPassPropagateInstructionsDown::Apply() {
32 for (const auto& function : *GetIRContext()->module()) {
33 std::vector<const opt::BasicBlock*> reachable_blocks;
34 for (const auto& block : function) {
35 if (GetIRContext()->IsReachable(block)) {
36 reachable_blocks.push_back(&block);
37 }
38 }
39
40 for (const auto* block : reachable_blocks) {
41 if (!GetFuzzerContext()->ChoosePercentage(
42 GetFuzzerContext()->GetChanceOfPropagatingInstructionsDown())) {
43 continue;
44 }
45
46 if (TransformationPropagateInstructionDown::IsApplicableToBlock(
47 GetIRContext(), block->id())) {
48 // Record fresh ids for every successor of the |block| that we can
49 // propagate an instruction into.
50 std::map<uint32_t, uint32_t> fresh_ids;
51 for (auto id :
52 TransformationPropagateInstructionDown::GetAcceptableSuccessors(
53 GetIRContext(), block->id())) {
54 fresh_ids[id] = GetFuzzerContext()->GetFreshId();
55 }
56
57 ApplyTransformation(TransformationPropagateInstructionDown(
58 block->id(), GetFuzzerContext()->GetFreshId(), fresh_ids));
59 }
60 }
61 }
62 }
63
64 } // namespace fuzz
65 } // namespace spvtools
66