cmScriptFuzzer.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. /*
  4. * Fuzzer for CMake script execution
  5. *
  6. * This fuzzer executes CMake scripts in script mode (-P).
  7. * This exercises the majority of CMake's codebase including:
  8. * - All built-in commands
  9. * - Variable expansion
  10. * - Control flow (if, foreach, while, function, macro)
  11. * - String/list/file operations
  12. * - Generator expressions
  13. *
  14. * This is the highest-impact fuzzer for coverage.
  15. *
  16. * Performance notes:
  17. * - Uses memfd_create on Linux for memory-backed file I/O
  18. * - Falls back to temp files on other platforms
  19. */
  20. #include <cstddef>
  21. #include <cstdint>
  22. #include <cstdio>
  23. #include <cstdlib>
  24. #include <string>
  25. #include <vector>
  26. #include <unistd.h>
  27. #include "cmCMakePolicyCommand.h"
  28. #include "cmExecutionStatus.h"
  29. #include "cmGlobalGenerator.h"
  30. #include "cmMakefile.h"
  31. #include "cmMessenger.h"
  32. #include "cmState.h"
  33. #include "cmStateSnapshot.h"
  34. #include "cmSystemTools.h"
  35. #include "cmake.h"
  36. #ifdef __linux__
  37. # include <sys/mman.h>
  38. # ifndef MFD_CLOEXEC
  39. # define MFD_CLOEXEC 0x0001U
  40. # endif
  41. #endif
  42. static constexpr size_t kMaxInputSize = 256 * 1024;
  43. static std::string g_testDir;
  44. static std::string g_scriptFile;
  45. static bool g_useMemfd = false;
  46. extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
  47. {
  48. (void)argc;
  49. (void)argv;
  50. // Suppress output during fuzzing (set once at init)
  51. cmSystemTools::SetMessageCallback(
  52. [](std::string const&, cmMessageMetadata const&) {});
  53. cmSystemTools::SetStdoutCallback([](std::string const&) {});
  54. cmSystemTools::SetStderrCallback([](std::string const&) {});
  55. // Create unique test directory (even with memfd, scripts can create files)
  56. char tmpl[] = "/tmp/cmake_fuzz_script_XXXXXX";
  57. char* dir = mkdtemp(tmpl);
  58. if (dir) {
  59. g_testDir = dir;
  60. } else {
  61. g_testDir = "/tmp/cmake_fuzz_script";
  62. cmSystemTools::MakeDirectory(g_testDir);
  63. }
  64. #ifdef __linux__
  65. // Try to use memfd for better performance
  66. int fd = memfd_create("cmake_fuzz", MFD_CLOEXEC);
  67. if (fd >= 0) {
  68. g_useMemfd = true;
  69. // Create path via /proc/self/fd
  70. g_scriptFile = "/proc/self/fd/" + std::to_string(fd);
  71. // Keep fd open - will be reused
  72. } else
  73. #endif
  74. {
  75. g_scriptFile = g_testDir + "/fuzz_script.cmake";
  76. }
  77. return 0;
  78. }
  79. extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
  80. {
  81. if (size == 0 || size > kMaxInputSize) {
  82. return 0;
  83. }
  84. #ifdef __linux__
  85. if (g_useMemfd) {
  86. // Extract fd from path and write directly
  87. int fd = std::atoi(g_scriptFile.c_str() + 14); // "/proc/self/fd/"
  88. ftruncate(fd, 0);
  89. lseek(fd, 0, SEEK_SET);
  90. if (write(fd, data, size) != static_cast<ssize_t>(size)) {
  91. return 0;
  92. }
  93. } else
  94. #endif
  95. {
  96. // Write script to temp file
  97. FILE* fp = fopen(g_scriptFile.c_str(), "wb");
  98. if (!fp)
  99. return 0;
  100. fwrite(data, 1, size, fp);
  101. fclose(fp);
  102. }
  103. // Save CWD in case script uses file(CHDIR)
  104. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  105. // Create cmake instance for script mode
  106. cmake cm(cmState::Role::Script);
  107. cm.SetHomeDirectory(g_testDir);
  108. cm.SetHomeOutputDirectory(g_testDir);
  109. // Run the script
  110. std::vector<std::string> args;
  111. args.push_back("cmake");
  112. args.push_back("-P");
  113. args.push_back(g_scriptFile);
  114. (void)cm.Run(args, false);
  115. // Restore CWD before cleanup (script may have changed it via file(CHDIR))
  116. cmSystemTools::ChangeDirectory(cwd);
  117. // Cleanup temp file (memfd doesn't need cleanup)
  118. if (!g_useMemfd) {
  119. unlink(g_scriptFile.c_str());
  120. }
  121. // Clean up any files the script may have created in g_testDir
  122. // This prevents disk growth and non-determinism from previous iterations
  123. cmSystemTools::RemoveADirectory(g_testDir);
  124. cmSystemTools::MakeDirectory(g_testDir);
  125. return 0;
  126. }