4
0

genfuzz.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // genfuzz: crash and stuck-note fuzzer for LYNX's built-in generators (round 170i2; the effects' twin is fxfuzz).
  2. //
  3. // Builds every instrument the way the rack does (RackState::build) and plays it hard: note storms over the whole key
  4. // range with extreme and random velocities, chords, pitch bend, the next note's pan / cents / porta, Mod X / Y,
  5. // release velocities, channel pitch moves, cuts, all-notes-off, odd block sizes (1…4096) and rates 22.05…192 kHz.
  6. // Sources of channels:
  7. // - every FL engine a native project can add (natives::instrumentEngines(), from the FL installation's Default
  8. // preset when --fl is given, LYNX's own defaults otherwise)
  9. // - every channel of every .flp under a directory (importFlp, --dir)
  10. // Reports non-finite output (NaN / inf), output that stays loud after every note was released and the tail had 20 s
  11. // (a stuck note; engines with a sustain loop in their release still decay because all notes are cut at the end) and,
  12. // built with -fsanitize=address,undefined (scripts/genfuzz-asan.sh), any memory error.
  13. //
  14. // Usage: genfuzz [--seed n] [--rounds n] [--fl <FL Studio's Data folder>] [--dir <folder with .flp>]... [--only <name>]
  15. // [--ops <list>] only these event kinds (letters a–l = the cases of the storm, to narrow a finding down)
  16. // [--verbose]
  17. // Exit code: 0 = clean, 1 = non-finite output or a stuck note found (crashes abort on their own).
  18. #include <algorithm>
  19. #include <cmath>
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #include <filesystem>
  24. #include <fstream>
  25. #include <iterator>
  26. #include <random>
  27. #include <string>
  28. #include <vector>
  29. #include "core/FlpImport.h"
  30. #include "core/NativeEngines.h"
  31. #include "core/RackRenderer.h"
  32. #include "flp/Flp.h"
  33. #include "WavReader.h"
  34. namespace
  35. {
  36. namespace fs = std::filesystem;
  37. int problems = 0, channelsTested = 0;
  38. std::mt19937 rng { 170 };
  39. int rounds = 3;
  40. bool verbose = false;
  41. std::string ops; // (empty: every event kind)
  42. float uni() { return std::uniform_real_distribution<float> (0.0f, 1.0f) (rng); }
  43. int pick (int lo, int hi) { return std::uniform_int_distribution<int> (lo, hi) (rng); }
  44. std::vector<std::uint8_t> readFile (const fs::path& p)
  45. {
  46. std::ifstream f (p, std::ios::binary);
  47. return { std::istreambuf_iterator<char> (f), {} };
  48. }
  49. // an FL installation path (backslashes, any case) on this file system
  50. std::string local (std::string p)
  51. {
  52. std::replace (p.begin(), p.end(), '\\', '/');
  53. return lynx::existingPathIgnoringCase (p);
  54. }
  55. lynx::natives::FlAccess flAccess (const std::string& data)
  56. {
  57. lynx::natives::FlAccess fl;
  58. if (data.empty())
  59. return fl;
  60. fl.roots.data = fs::path (data).string();
  61. fl.roots.factoryData = fs::path (data).parent_path().string();
  62. fl.fileExists = [] (const std::string& p) { return fs::is_regular_file (local (p)); };
  63. fl.readFile = [] (const std::string& p) { return readFile (local (p)); };
  64. fl.listPresets = [] (const std::string& folder) {
  65. std::vector<std::string> out;
  66. std::error_code ec;
  67. for (const auto& e : fs::recursive_directory_iterator (local (folder), ec))
  68. if (e.is_regular_file())
  69. out.push_back (e.path().string());
  70. std::sort (out.begin(), out.end());
  71. return out;
  72. };
  73. fl.saveEmbedded = [] (const std::vector<std::uint8_t>& wav, const std::string& name) {
  74. const auto path = (fs::temp_directory_path() / ("lynx-genfuzz-" + std::to_string (wav.size()) + "-" + std::to_string (name.size()) + ".wav")).string();
  75. std::ofstream (path, std::ios::binary).write ((const char*) wav.data(), (std::streamsize) wav.size());
  76. return path;
  77. };
  78. return fl;
  79. }
  80. bool finite (const std::vector<float>& l, const std::vector<float>& r, int n)
  81. {
  82. for (int i = 0; i < n; ++i)
  83. if (! std::isfinite (l[(std::size_t) i]) || ! std::isfinite (r[(std::size_t) i]))
  84. return false;
  85. return true;
  86. }
  87. double peak (const std::vector<float>& l, const std::vector<float>& r, int n)
  88. {
  89. double p = 0.0;
  90. for (int i = 0; i < n; ++i)
  91. p = std::max ({ p, (double) std::abs (l[(std::size_t) i]), (double) std::abs (r[(std::size_t) i]) });
  92. return p;
  93. }
  94. // one channel alone in a rack: every round at another rate, a storm of events, then the release check
  95. void fuzzChannel (const lynx::ChannelDef& channel, const std::string& label)
  96. {
  97. lynx::Song s;
  98. s.mixer = lynx::Song::defaultMixer (2);
  99. auto c = channel;
  100. c.mixerTrack = 1;
  101. s.channels.push_back (c);
  102. s.patterns.emplace_back();
  103. auto samples = [] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> { return lynx::tools::readWav (local (path)); };
  104. const double rates[] { 22050.0, 44100.0, 48000.0, 96000.0, 192000.0 };
  105. ++channelsTested;
  106. for (int round = 0; round < rounds; ++round)
  107. {
  108. const double rate = rates[(std::size_t) pick (0, 4)];
  109. auto state = lynx::RackState::build (s, nullptr, rate, samples);
  110. if (state == nullptr || state->instruments.empty() || state->instruments[0] == nullptr)
  111. return;
  112. auto& in = *state->instruments[0];
  113. in.prepare (rate);
  114. std::vector<float> l (4096), r (4096);
  115. bool bad = false;
  116. std::vector<int> held;
  117. const int steps = 400;
  118. for (int step = 0; step < steps && ! bad; ++step)
  119. {
  120. int kind = pick (0, 11);
  121. while (! ops.empty() && ops.find ((char) ('a' + kind)) == std::string::npos)
  122. kind = pick (0, 11);
  123. switch (kind)
  124. {
  125. case 0: case 1: case 2: // a note: any key, extreme or random velocity, with an expression
  126. {
  127. const int key = pick (0, 2) == 0 ? (pick (0, 1) == 0 ? 0 : 127) : pick (0, 127);
  128. const float vel = pick (0, 3) == 0 ? (float) pick (0, 1) : uni();
  129. in.setNextNoteExpression (uni() * 2.0f - 1.0f, (uni() - 0.5f) * 2400.0f, pick (0, 5) == 0);
  130. in.setNextNoteModulation (uni(), uni());
  131. in.noteOn (key, vel);
  132. held.push_back (key);
  133. break;
  134. }
  135. case 3: // a chord
  136. for (int k = 0, root = pick (0, 115); k < 4; ++k)
  137. {
  138. in.noteOn (root + 3 * k, uni());
  139. held.push_back (root + 3 * k);
  140. }
  141. break;
  142. case 4: case 5:
  143. if (! held.empty())
  144. {
  145. const auto i = (std::size_t) pick (0, (int) held.size() - 1);
  146. in.setNextNoteRelease (uni());
  147. in.noteOff (held[i]);
  148. held.erase (held.begin() + (long) i);
  149. }
  150. break;
  151. case 6: in.pitchBend ((uni() - 0.5f) * 96.0f); break;
  152. case 7: in.setChannelPitchNow ((uni() - 0.5f) * 4800.0f, (uni() - 0.5f) * 200.0f); break;
  153. case 8: in.controlChange (pick (0, 127), uni()); break;
  154. case 9: if (pick (0, 7) == 0) in.cut(); break;
  155. case 10: if (pick (0, 15) == 0) { in.allNotesOff(); held.clear(); } break;
  156. default: in.slideTo (pick (0, 127), (uni() - 0.5f) * 200.0f, (float) pick (1, 20000)); break;
  157. }
  158. const int n = pick (0, 9) == 0 ? pick (1, 8) : pick (1, 4096);
  159. std::fill (l.begin(), l.begin() + n, 0.0f), std::fill (r.begin(), r.begin() + n, 0.0f);
  160. in.render (l.data(), r.data(), n, 1.0f, 1.0f);
  161. if (! finite (l, r, n))
  162. {
  163. std::printf ("NON-FINITE %s rate %.0f round %d step %d (block %d)\n", label.c_str(), rate, round, step, n);
  164. ++problems, bad = true;
  165. }
  166. }
  167. if (bad)
  168. continue;
  169. // every note released, then cut: 20 s later the instrument must be silent (a stuck note otherwise)
  170. for (auto k : held)
  171. in.noteOff (k);
  172. in.allNotesOff();
  173. double last = 0.0, mid = 0.0; // (the peak of the last block, the loudest block from 10 s to 11 s)
  174. for (int done = 0; done < (int) (20.0 * rate); done += 4096)
  175. {
  176. std::fill (l.begin(), l.end(), 0.0f), std::fill (r.begin(), r.end(), 0.0f);
  177. in.render (l.data(), r.data(), 4096, 1.0f, 1.0f);
  178. if (! finite (l, r, 4096))
  179. {
  180. std::printf ("NON-FINITE %s rate %.0f round %d in the release\n", label.c_str(), rate, round);
  181. ++problems, bad = true;
  182. break;
  183. }
  184. last = peak (l, r, 4096);
  185. if (done >= (int) (10.0 * rate) && done < (int) (11.0 * rate))
  186. mid = std::max (mid, last);
  187. }
  188. // −60 dBFS after 20 s of release, and not still dying away (FL Slayer's default guitar rings for > 20 s in the
  189. // DLL too: its tail falls ~3 dB a second, a stuck note does not fall)
  190. if (! bad && last > 1e-3 && last > 0.5 * mid)
  191. {
  192. std::printf ("STUCK %s rate %.0f round %d: peak %.4f after 20 s of release\n", label.c_str(), rate, round, last);
  193. ++problems;
  194. }
  195. else if (verbose)
  196. std::printf (" ok %s rate %.0f round %d (release peak %.2g, %.2g at 10 s)\n", label.c_str(), rate, round, last, mid);
  197. }
  198. }
  199. } // namespace
  200. int main (int argc, char** argv)
  201. {
  202. std::string flData, only;
  203. std::vector<std::string> dirs;
  204. for (int i = 1; i < argc; ++i)
  205. {
  206. const std::string a = argv[i];
  207. auto next = [&] { return i + 1 < argc ? std::string (argv[++i]) : std::string(); };
  208. if (a == "--seed")
  209. rng.seed ((unsigned) std::atoi (next().c_str()));
  210. else if (a == "--rounds")
  211. rounds = std::max (1, std::atoi (next().c_str()));
  212. else if (a == "--fl")
  213. flData = next();
  214. else if (a == "--dir")
  215. dirs.push_back (next());
  216. else if (a == "--ops")
  217. ops = next();
  218. else if (a == "--only")
  219. only = next();
  220. else if (a == "--verbose")
  221. verbose = true;
  222. else
  223. {
  224. std::printf ("usage: genfuzz [--seed n] [--rounds n] [--fl <FL Data folder>] [--dir <folder>]... [--only <name>] [--ops <a-l>] [--verbose]\n");
  225. return 2;
  226. }
  227. }
  228. const auto fl = flAccess (flData);
  229. for (const auto& e : lynx::natives::instrumentEngines())
  230. {
  231. if (! only.empty() && only != e.name)
  232. continue;
  233. if (const auto c = lynx::natives::makeChannel (e.kind, fl))
  234. fuzzChannel (*c, e.name);
  235. else if (verbose)
  236. std::printf (" (%s: not available)\n", e.name);
  237. }
  238. for (const auto& dir : dirs)
  239. {
  240. std::error_code ec;
  241. for (const auto& f : fs::recursive_directory_iterator (dir, ec))
  242. {
  243. if (! f.is_regular_file() || f.path().extension() != ".flp")
  244. continue;
  245. try
  246. {
  247. lynx::FlpImportOptions options;
  248. if (! flData.empty())
  249. {
  250. options.roots = { fs::path (flData).parent_path().string(), flData, "" };
  251. options.fileExists = [] (const std::string& p) { return fs::is_regular_file (local (p)); };
  252. options.readFile = [] (const std::string& p) { return readFile (local (p)); };
  253. }
  254. const auto song = lynx::importFlp (flp::parseProject (readFile (f.path())), options);
  255. for (const auto& c : song.channels)
  256. if (only.empty() || only == c.name)
  257. fuzzChannel (c, f.path().filename().string() + ": " + c.name);
  258. }
  259. catch (const std::exception& ex)
  260. {
  261. if (verbose)
  262. std::printf (" (%s: %s)\n", f.path().string().c_str(), ex.what());
  263. }
  264. }
  265. }
  266. std::printf ("genfuzz: %d channels, %d problems\n", channelsTested, problems);
  267. return problems > 0 ? 1 : 0;
  268. }