mdl_fuzz.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Mutates every .mdl of the given .pkg files (truncation, random bytes, extreme 32 bit fields, copied chunks) and
  2. // pushes each mutant through the puppet readers (readPuppetMeshFile, readMdlvMeshes, PuppetClipping, PuppetRig::load
  3. // and a few pose updates). It is only worth running against an engine library built with AddressSanitizer:
  4. //
  5. // cmake -S . -B build-asan -DCMAKE_CXX_FLAGS="-fsanitize=address -fno-sanitize=vptr" && cmake --build build-asan
  6. // g++ -O1 -g -fsanitize=address -fno-sanitize=vptr -std=gnu++20 <the defines and -I flags of any engine source in
  7. // build-asan/compile_commands.json> tools/mdl_fuzz.cpp -o mdl_fuzz -Lbuild-asan/output
  8. // -llinux-wallpaperengine-lib -Wl,-rpath,$PWD/build-asan/output
  9. // ASAN_OPTIONS=allocator_may_return_null=1:max_allocation_size_mb=1024:detect_leaks=0:hard_rss_limit_mb=3500 \
  10. // ./mdl_fuzz <seed> <mutants per file> ~/.local/share/Steam/steamapps/workshop/content/431960/*/*.pkg
  11. //
  12. // usage: mdl_fuzz <seed> <mutants per file> <pkg>... (mutant 0 of every file is the original)
  13. // A crash is attributed by the last "MUT <pkg> <file> seed= index= kind=" line on stderr, a mutant that runs for more
  14. // than 20 s prints HANG and that line and exits 3. Replay one: MDLFUZZ_ONLY=<name substring> MDLFUZZ_INDEX=<n> with the
  15. // same seed and mutant count. 417 installed models, 4 seeds, about 230k mutants: no reports since the 2026-10-17 fixes.
  16. #include "WallpaperEngine/Data/Parsers/PackageParser.h"
  17. #include "WallpaperEngine/FileSystem/Adapters/Package.h"
  18. #include "WallpaperEngine/Render/Objects/PuppetClipping.h"
  19. #include "WallpaperEngine/Render/Objects/PuppetMeshFile.h"
  20. #include "WallpaperEngine/Render/Objects/PuppetRig.h"
  21. #include <csignal>
  22. #include <cstdio>
  23. #include <cstdlib>
  24. #include <cstring>
  25. #include <fstream>
  26. #include <iostream>
  27. #include <iterator>
  28. #include <random>
  29. #include <unistd.h>
  30. using namespace WallpaperEngine;
  31. using namespace WallpaperEngine::Render::Objects;
  32. static char g_current[512];
  33. static void onAlarm (int) {
  34. const char msg[] = "\nHANG: ";
  35. write (2, msg, sizeof (msg) - 1);
  36. write (2, g_current, strlen (g_current));
  37. write (2, "\n", 1);
  38. _exit (3);
  39. }
  40. static std::vector<char> mutate (const std::vector<char>& original, std::mt19937_64& rng, int kind) {
  41. std::vector<char> data = original;
  42. if (data.empty ()) {
  43. return data;
  44. }
  45. static const uint32_t interesting[]
  46. = { 0u, 1u, 2u, 0xFFu, 0xFFFFu, 0x10000u, 0x7FFFFFFFu,
  47. 0x80000000u, 0xFFFFFFFFu, 0x00FFFFFFu, 0x01000000u, 0x3FFFFFFFu, 0xFFFFFFFEu, 0x00020000u };
  48. switch (kind) {
  49. case 0: // truncate
  50. data.resize (rng () % data.size ());
  51. break;
  52. case 1:
  53. { // a few random bytes
  54. const int count = 1 + rng () % 8;
  55. for (int i = 0; i < count; i++) {
  56. data[rng () % data.size ()] = static_cast<char> (rng ());
  57. }
  58. break;
  59. }
  60. case 2:
  61. { // an aligned 32 bit field gets an extreme value (counts, sizes, offsets)
  62. const int count = 1 + rng () % 3;
  63. for (int i = 0; i < count && data.size () >= 4; i++) {
  64. const size_t at = (rng () % (data.size () / 4)) * 4;
  65. const uint32_t value = interesting[rng () % (sizeof (interesting) / sizeof (interesting[0]))];
  66. std::memcpy (data.data () + at, &value, 4);
  67. }
  68. break;
  69. }
  70. case 3:
  71. { // an unaligned one
  72. const size_t at = rng () % (data.size () > 4 ? data.size () - 4 : 1);
  73. const uint32_t value = interesting[rng () % (sizeof (interesting) / sizeof (interesting[0]))];
  74. if (data.size () >= at + 4) {
  75. std::memcpy (data.data () + at, &value, 4);
  76. }
  77. break;
  78. }
  79. default:
  80. { // a chunk of the file copied over another place
  81. const size_t length = 1 + rng () % std::min<size_t> (256, data.size ());
  82. const size_t from = rng () % (data.size () - length + 1);
  83. const size_t to = rng () % (data.size () - length + 1);
  84. std::memmove (data.data () + to, data.data () + from, length);
  85. break;
  86. }
  87. }
  88. return data;
  89. }
  90. static void parseOne (const std::vector<char>& data, const std::string& name) {
  91. const auto mesh = readPuppetMeshFile (data);
  92. try {
  93. std::ignore = readMdlvMeshes (data);
  94. } catch (const std::exception&) { }
  95. if (!mesh.has_value ()) {
  96. return;
  97. }
  98. const int flags = static_cast<int> (mesh->flags);
  99. try {
  100. if (flags & 8) {
  101. std::ignore = PuppetClipping::readParts (data, mesh->indexEnd, mesh->version, mesh->indices.size ());
  102. }
  103. } catch (const std::exception&) { }
  104. try {
  105. auto clipping = PuppetClipping::read (data, mesh->indexEnd, mesh->version, mesh->indices.size ());
  106. if (clipping.has_value ()) {
  107. clipping->build (mesh->indices);
  108. }
  109. } catch (const std::exception&) { }
  110. if (mesh->skeleton < data.size () && !mesh->blendIndices.empty ()) {
  111. try {
  112. PuppetRig rig;
  113. rig.load (data, mesh->sections, mesh->meshCount, name);
  114. rig.addSceneLayers ({});
  115. for (int i = 0; i < 4; i++) {
  116. rig.updatePose (glm::mat4 (1.0f));
  117. }
  118. } catch (const std::exception&) { }
  119. }
  120. }
  121. int main (int argc, char** argv) {
  122. if (argc < 4) {
  123. std::cerr << "usage: mdlfuzz <seed> <mutants per file> <pkg>..." << std::endl;
  124. return 2;
  125. }
  126. const uint64_t seed = std::strtoull (argv[1], nullptr, 10);
  127. const int mutants = std::atoi (argv[2]);
  128. const char* only = std::getenv ("MDLFUZZ_ONLY");
  129. const char* onlyIndex = std::getenv ("MDLFUZZ_INDEX");
  130. std::signal (SIGALRM, onAlarm);
  131. size_t files = 0, runs = 0;
  132. for (int i = 3; i < argc; i++) {
  133. auto stream = std::make_shared<std::ifstream> (argv[i], std::ios::binary);
  134. try {
  135. FileSystem::Adapters::PackageAdapter adapter (Data::Parsers::PackageParser::parse (stream));
  136. for (const auto& entry : adapter.package->files) {
  137. if (!entry->filename.ends_with (".mdl")
  138. || (only != nullptr && entry->filename.find (only) == std::string::npos)) {
  139. continue;
  140. }
  141. const auto handle = adapter.open (entry->filename);
  142. const std::vector<char> original (
  143. (std::istreambuf_iterator<char> (*handle)), std::istreambuf_iterator<char> ()
  144. );
  145. files++;
  146. for (int m = 0; m < mutants; m++) {
  147. if (onlyIndex != nullptr && m != std::atoi (onlyIndex)) {
  148. continue;
  149. }
  150. std::mt19937_64 rng (seed * 1000003 + files * 7919 + static_cast<uint64_t> (m));
  151. const auto data = m == 0 && onlyIndex == nullptr ? original : mutate (original, rng, m % 5);
  152. std::snprintf (
  153. g_current, sizeof (g_current), "%s %s seed=%llu index=%d kind=%d bytes=%zu", argv[i],
  154. entry->filename.c_str (), static_cast<unsigned long long> (seed), m, m % 5, data.size ()
  155. );
  156. std::fprintf (stderr, "MUT %s\n", g_current);
  157. alarm (20);
  158. parseOne (data, entry->filename);
  159. alarm (0);
  160. runs++;
  161. }
  162. }
  163. } catch (const std::exception& e) {
  164. std::cerr << argv[i] << " : package : " << e.what () << std::endl;
  165. }
  166. }
  167. std::cout << "mdlfuzz: " << files << " mdl files, " << runs << " runs, no crash" << std::endl;
  168. return 0;
  169. }