1 // Copyright (c) 2020 Google LLC 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 #ifndef SOURCE_FUZZ_REPEATED_PASS_MANAGER_H_ 16 #define SOURCE_FUZZ_REPEATED_PASS_MANAGER_H_ 17 18 #include "source/fuzz/fuzzer_context.h" 19 #include "source/fuzz/fuzzer_pass.h" 20 #include "source/fuzz/pass_management/repeated_pass_instances.h" 21 #include "source/fuzz/pass_management/repeated_pass_recommender.h" 22 #include "source/fuzz/protobufs/spirvfuzz_protobufs.h" 23 24 namespace spvtools { 25 namespace fuzz { 26 27 // Each field of this enum corresponds to an available repeated pass 28 // strategy, and is used to decide which kind of RepeatedPassManager object 29 // to create. 30 enum class RepeatedPassStrategy { 31 kSimple, 32 kRandomWithRecommendations, 33 kLoopedWithRecommendations 34 }; 35 36 // An interface to encapsulate the manner in which the sequence of repeated 37 // passes that are applied during fuzzing is chosen. An implementation of this 38 // interface could, for example, keep track of the history of passes that have 39 // been run and bias the selection of future passes according to this history. 40 class RepeatedPassManager { 41 public: 42 RepeatedPassManager(FuzzerContext* fuzzer_context, 43 RepeatedPassInstances* pass_instances); 44 45 virtual ~RepeatedPassManager(); 46 47 // Returns the fuzzer pass instance that should be run next. The 48 // transformations that have been applied so far are provided via 49 // |applied_transformations| and can be used to influence the decision. 50 virtual FuzzerPass* ChoosePass( 51 const protobufs::TransformationSequence& applied_transformations) = 0; 52 53 // Creates a corresponding RepeatedPassManager based on the |strategy|. 54 static std::unique_ptr<RepeatedPassManager> Create( 55 RepeatedPassStrategy strategy, FuzzerContext* fuzzer_context, 56 RepeatedPassInstances* pass_instances, 57 RepeatedPassRecommender* pass_recommender); 58 59 protected: GetFuzzerContext()60 FuzzerContext* GetFuzzerContext() { return fuzzer_context_; } 61 GetPassInstances()62 RepeatedPassInstances* GetPassInstances() { return pass_instances_; } 63 64 private: 65 // Provided in order to allow the pass manager to make random decisions. 66 FuzzerContext* fuzzer_context_; 67 68 // The repeated fuzzer passes that are enabled. 69 RepeatedPassInstances* pass_instances_; 70 }; 71 72 } // namespace fuzz 73 } // namespace spvtools 74 75 #endif // SOURCE_FUZZ_REPEATED_PASS_MANAGER_H_ 76