1 // Copyright 2021 The Tint Authors. 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 FUZZERS_TINT_COMMON_FUZZER_H_ 16 #define FUZZERS_TINT_COMMON_FUZZER_H_ 17 18 #include <cassert> 19 #include <cstring> 20 #include <memory> 21 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include "include/tint/tint.h" 26 27 #include "fuzzers/data_builder.h" 28 29 namespace tint { 30 namespace fuzzers { 31 32 void GenerateSpirvOptions(DataBuilder* b, writer::spirv::Options* options); 33 void GenerateWgslOptions(DataBuilder* b, writer::wgsl::Options* options); 34 void GenerateHlslOptions(DataBuilder* b, writer::hlsl::Options* options); 35 void GenerateMslOptions(DataBuilder* b, writer::msl::Options* options); 36 37 enum class InputFormat { kWGSL, kSpv }; 38 39 enum class OutputFormat { kWGSL, kSpv, kHLSL, kMSL }; 40 41 class CommonFuzzer { 42 public: 43 explicit CommonFuzzer(InputFormat input, OutputFormat output); 44 ~CommonFuzzer(); 45 SetTransformManager(transform::Manager * tm,transform::DataMap * inputs)46 void SetTransformManager(transform::Manager* tm, transform::DataMap* inputs) { 47 assert((!tm || inputs) && "DataMap must be !nullptr if Manager !nullptr"); 48 transform_manager_ = tm; 49 transform_inputs_ = inputs; 50 } 51 SetDumpInput(bool enabled)52 void SetDumpInput(bool enabled) { dump_input_ = enabled; } 53 54 int Run(const uint8_t* data, size_t size); 55 Diagnostics()56 const tint::diag::List& Diagnostics() const { return diagnostics_; } 57 HasErrors()58 bool HasErrors() const { return diagnostics_.contains_errors(); } 59 GetGeneratedSpirv()60 const std::vector<uint32_t>& GetGeneratedSpirv() const { 61 return generated_spirv_; 62 } 63 GetGeneratedWgsl()64 const std::string& GetGeneratedWgsl() const { return generated_wgsl_; } 65 GetGeneratedHlsl()66 const std::string& GetGeneratedHlsl() const { return generated_hlsl_; } 67 GetGeneratedMsl()68 const std::string& GetGeneratedMsl() const { return generated_msl_; } 69 SetOptionsSpirv(const writer::spirv::Options & options)70 void SetOptionsSpirv(const writer::spirv::Options& options) { 71 options_spirv_ = options; 72 } 73 SetOptionsWgsl(const writer::wgsl::Options & options)74 void SetOptionsWgsl(const writer::wgsl::Options& options) { 75 options_wgsl_ = options; 76 } 77 SetOptionsHlsl(const writer::hlsl::Options & options)78 void SetOptionsHlsl(const writer::hlsl::Options& options) { 79 options_hlsl_ = options; 80 } 81 SetOptionsMsl(const writer::msl::Options & options)82 void SetOptionsMsl(const writer::msl::Options& options) { 83 options_msl_ = options; 84 } 85 86 private: 87 InputFormat input_; 88 OutputFormat output_; 89 transform::Manager* transform_manager_ = nullptr; 90 transform::DataMap* transform_inputs_ = nullptr; 91 bool dump_input_ = false; 92 tint::diag::List diagnostics_; 93 94 std::vector<uint32_t> generated_spirv_; 95 std::string generated_wgsl_; 96 std::string generated_hlsl_; 97 std::string generated_msl_; 98 99 writer::spirv::Options options_spirv_; 100 writer::wgsl::Options options_wgsl_; 101 writer::hlsl::Options options_hlsl_; 102 writer::msl::Options options_msl_; 103 104 #if TINT_BUILD_WGSL_READER 105 /// The source file needs to live at least as long as #diagnostics_ 106 std::unique_ptr<Source::File> file_; 107 #endif // TINT_BUILD_WGSL_READER 108 109 // Run series of reflection operations to exercise the Inspector API. 110 void RunInspector(Program* program); 111 }; 112 113 } // namespace fuzzers 114 } // namespace tint 115 116 #endif // FUZZERS_TINT_COMMON_FUZZER_H_ 117