1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
/*
* Fuzzer for CMake's list file parser (CMakeLists.txt full parsing)
*
* This fuzzer tests the complete parsing of CMake files including:
* - Command parsing
* - Argument handling
* - Bracket arguments
* - Comments
* - Line continuations
*/
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <string>
#include <unistd.h>
#include "cmListFileCache.h"
#include "cmMessenger.h"
#include "cmSystemTools.h"
static constexpr size_t kMaxInputSize = 256 * 1024;
static std::string g_testDir;
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
{
(void)argc;
(void)argv;
char tmpl[] = "/tmp/cmake_fuzz_listfile_XXXXXX";
char* dir = mkdtemp(tmpl);
if (dir) {
g_testDir = dir;
} else {
g_testDir = "/tmp/cmake_fuzz_listfile";
cmSystemTools::MakeDirectory(g_testDir);
}
return 0;
}
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
{
if (size == 0 || size > kMaxInputSize) {
return 0;
}
// Write input to temp file
std::string testFile = g_testDir + "/CMakeLists.txt";
{
FILE* fp = fopen(testFile.c_str(), "wb");
if (!fp)
return 0;
fwrite(data, 1, size, fp);
fclose(fp);
}
// Create a messenger for error handling
cmMessenger messenger;
// Parse the file
cmListFile listFile;
cmListFileBacktrace backtrace;
if (listFile.ParseFile(testFile.c_str(), &messenger, backtrace)) {
// Successfully parsed - examine results
for (auto const& func : listFile.Functions) {
(void)func.LowerCaseName();
(void)func.OriginalName();
for (auto const& arg : func.Arguments()) {
(void)arg.Value;
(void)arg.Delim;
}
}
}
// Clean up
unlink(testFile.c_str());
return 0;
}
|