WinGetYamlFuzzing.cpp (1635B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include <cstdint> 4 #include <optional> 5 #include <winget/ManifestYamlParser.h> 6 7 extern "C" int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size) 8 { 9 std::string input{ reinterpret_cast<const char*>(data), size }; 10 11 try 12 { 13 AppInstaller::Manifest::Manifest manifest = AppInstaller::Manifest::YamlParser::Create(input); 14 } 15 catch (...) {} 16 17 return 0; 18 } 19 20 #ifndef WINGET_DISABLE_FOR_FUZZING 21 22 #include <AppInstallerStrings.h> 23 24 // Emulate libFuzzer main by just sending all files in the corpus (last arg) to the fuzzer. 25 int main(int argc, char** argv) 26 { 27 if (argc <= 1) 28 { 29 return 1; 30 } 31 32 std::filesystem::path corpus = argv[argc - 1]; 33 34 if (std::filesystem::is_directory(corpus)) 35 { 36 for (auto& file : std::filesystem::directory_iterator{ corpus }) 37 { 38 if (!file.is_directory()) 39 { 40 std::ifstream stream{ file.path(), std::ios_base::in | std::ios_base::binary }; 41 std::string contents = AppInstaller::Utility::ReadEntireStream(stream); 42 43 LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t*>(contents.data()), contents.size()); 44 } 45 } 46 } 47 else 48 { 49 std::ifstream stream{ corpus, std::ios_base::in | std::ios_base::binary }; 50 std::string contents = AppInstaller::Utility::ReadEntireStream(stream); 51 52 LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t*>(contents.data()), contents.size()); 53 } 54 55 return 0; 56 } 57 58 #endif