fxfuzz.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // fxfuzz: crash fuzzer for LYNX's built-in effects (round 111).
  2. //
  3. // Builds every effect the way the engine and the editors do and drives it hard: every parameter swept across its
  4. // range (both extremes, random values, every parameter random at once), several sample rates and block sizes, the
  5. // editors' read-outs (formatEffectParam, the parametric EQ's response graph, the state-backed controls of
  6. // core/StateParams.h) and loud / silent / denormal / DC input. Sources of slots:
  7. // - the default slot of every EffectKind (makeEffectSlot)
  8. // - every FL effect preset (.fst) under a directory (effectFromFlPreset, the Browser's drop on a mixer insert)
  9. // - every effect slot of every .flp under a directory (importFlp)
  10. // Reports non-finite output (NaN / inf) and, built with -fsanitize=address,undefined, any memory error.
  11. //
  12. // Usage: fxfuzz [--seed n] [--rounds n] [--no-kinds] [--dir <folder with .flp / .fst>]... [--only <kind index>] [--verbose]
  13. // [--low-rates] [--replay <kind> <rate> "[i]=v ..."]
  14. // Exit code: 0 = clean, 1 = non-finite output found (crashes abort on their own).
  15. #include <cmath>
  16. #include <cstdio>
  17. #include <cstdlib>
  18. #include <cstring>
  19. #include <filesystem>
  20. #include <fstream>
  21. #include <iterator>
  22. #include <random>
  23. #include <string>
  24. #include <vector>
  25. #include "core/Effects.h"
  26. #include "core/FlEffects.h"
  27. #include "core/FlpImport.h"
  28. #include "core/PluginLayout.h"
  29. #include "core/StateParams.h"
  30. #include "flp/Flp.h"
  31. namespace
  32. {
  33. int nonFinite = 0, slotsTested = 0;
  34. std::mt19937 rng { 111 };
  35. int rounds = 6;
  36. bool verbose = false;
  37. bool lowRates = false;
  38. float uni() { return std::uniform_real_distribution<float> (0.0f, 1.0f) (rng); }
  39. std::vector<std::uint8_t> readFile (const std::filesystem::path& p)
  40. {
  41. std::ifstream f (p, std::ios::binary);
  42. return { std::istreambuf_iterator<char> (f), {} };
  43. }
  44. void fillInput (float* l, float* r, int n, int mode, double& phase)
  45. {
  46. for (int i = 0; i < n; ++i)
  47. {
  48. float v = 0.0f;
  49. switch (mode)
  50. {
  51. case 0: v = uni() * 2.0f - 1.0f; break; // noise
  52. case 1: v = (float) std::sin (phase); phase += 0.0571; break; // sine
  53. case 2: v = 0.0f; break; // silence
  54. case 3: v = 1e-39f; break; // denormal
  55. case 4: v = (uni() * 2.0f - 1.0f) * 30.0f; break; // very loud noise (+30 dB)
  56. case 5: v = 1.0f; break; // DC
  57. default: v = (i == 0) ? 1.0f : 0.0f; break; // impulse
  58. }
  59. l[i] = v;
  60. r[i] = mode == 1 ? -v : v * 0.7f;
  61. }
  62. }
  63. bool run (lynx::Effect& fx, const lynx::EffectParams& params, int blocks, int maxBlock, const char* what, const std::string& name)
  64. {
  65. static std::vector<float> l (8192), r (8192);
  66. static const float sc[8192] {};
  67. double phase = 0.0;
  68. const int mode = (int) (rng() % 7);
  69. bool bad = false;
  70. for (int b = 0; b < blocks; ++b)
  71. {
  72. static const int sizes[] = { 1, 2, 7, 32, 64, 100, 128, 255, 256, 441, 480, 511, 512, 513, 1024, 2048 };
  73. int n = sizes[rng() % (sizeof (sizes) / sizeof (sizes[0]))];
  74. n = std::min (n, maxBlock);
  75. fillInput (l.data(), r.data(), n, mode, phase);
  76. fx.setSongPosition (b * 96.0);
  77. fx.setPlaying (b % 5 != 4);
  78. fx.setSidechain (b % 3 == 0 ? nullptr : sc, b % 3 == 0 ? nullptr : sc);
  79. fx.process (l.data(), r.data(), n, params, 60.0 + (b % 4) * 60.0);
  80. for (int i = 0; i < n; ++i)
  81. if (! std::isfinite (l[i]) || ! std::isfinite (r[i]))
  82. {
  83. bad = true;
  84. break;
  85. }
  86. if (bad)
  87. break;
  88. }
  89. if (bad)
  90. {
  91. ++nonFinite;
  92. std::printf ("NONFINITE %s [%s] (input mode %d)\n", name.c_str(), what, mode);
  93. if (verbose)
  94. {
  95. std::printf (" params:");
  96. for (std::size_t i = 0; i < params.size(); ++i)
  97. if (params[i] != 0.0f)
  98. std::printf (" [%d]=%.9g", (int) i, params[i]);
  99. std::printf ("\n");
  100. }
  101. }
  102. return ! bad;
  103. }
  104. void fuzzSlot (lynx::EffectSlot slot, const std::string& name)
  105. {
  106. using namespace lynx;
  107. ++slotsTested;
  108. const auto kind = slot.kind;
  109. const auto& info = effectInfo (kind);
  110. const int np = std::min<int> ((int) info.params.size(), maxEffectParams);
  111. // editor read-outs and the EQ graph
  112. for (int p = 0; p < plugui::paramCount (kind) + 2; ++p)
  113. for (float v : { 0.0f, 1e-7f, 0.25f, 0.5f, 0.9999999f, 1.0f, uni() })
  114. {
  115. (void) formatEffectParam (kind, p, v, 120.0);
  116. (void) formatEffectParam (kind, p, v, 120.0, slot.params);
  117. (void) plugui::paramName (kind, p);
  118. if (plugui::stateIndex (kind, p) >= 0)
  119. {
  120. (void) plugui::stateValue01 (kind, p, slot.state);
  121. (void) plugui::stateRaw (kind, p, v);
  122. }
  123. }
  124. if (kind == EffectKind::parametricEq)
  125. for (int b = 0; b < peq::bands; ++b)
  126. {
  127. (void) peq::orderText (slot.params, b);
  128. (void) peq::widthText (slot.params, b);
  129. }
  130. const auto& layout = plugui::layoutFor (kind);
  131. for (const auto& item : layout.items)
  132. {
  133. (void) plugui::optionLabels (item);
  134. for (int page = -1; page < 4; ++page)
  135. (void) plugui::isShown (kind, item, page, slot.params, &slot.state);
  136. }
  137. for (const double sr : { 44100.0, 48000.0, 96000.0, 192000.0, 22050.0 })
  138. {
  139. if (sr < 44100.0 && ! lowRates) // (FL's exact engines are defined from 44.1 kHz; --low-rates adds 22.05 kHz)
  140. continue;
  141. auto fx = makeEffect (slot);
  142. if (fx == nullptr)
  143. continue;
  144. fx->prepare (sr);
  145. run (*fx, slot.params, 6, 2048, "as loaded", name);
  146. if (kind == EffectKind::parametricEq)
  147. for (double hz : { 1.0, 20.0, 1000.0, sr * 0.49, sr * 0.5, sr })
  148. (void) peq::responseDb (slot.params, sr, hz);
  149. // every parameter alone at its extremes and at random values (what dragging a knob does)
  150. for (int p = 0; p < np; ++p)
  151. for (float v : { 0.0f, 1.0f, uni(), 1e-6f, 0.999999f })
  152. {
  153. auto params = slot.params;
  154. params[(std::size_t) p] = v;
  155. char what[64];
  156. std::snprintf (what, sizeof (what), "sr %.0f, param %d (%s) = %g", sr, p, info.params[(std::size_t) p].name, v);
  157. if (! run (*fx, params, 3, 2048, what, name))
  158. break;
  159. if (kind == EffectKind::parametricEq)
  160. (void) peq::responseDb (params, sr, 1000.0);
  161. (void) formatEffectParam (kind, p, v, 120.0, params);
  162. }
  163. // everything random at once, and all at 0 / all at 1, also with a fresh effect built from those params
  164. for (int k = 0; k < rounds; ++k)
  165. {
  166. auto params = slot.params;
  167. const float fixed = k == 0 ? 0.0f : k == 1 ? 1.0f : -1.0f;
  168. for (int p = 0; p < np; ++p)
  169. params[(std::size_t) p] = fixed >= 0.0f ? fixed : (rng() % 4 == 0 ? (float) (rng() % 2) : uni());
  170. char what[64];
  171. std::snprintf (what, sizeof (what), "sr %.0f, all params %s", sr, k == 0 ? "0" : k == 1 ? "1" : "random");
  172. run (*fx, params, 4, 2048, what, name);
  173. auto copy = slot;
  174. copy.params = params;
  175. if (auto fresh = makeEffect (copy))
  176. {
  177. fresh->prepare (sr);
  178. run (*fresh, params, 3, 2048, what, name);
  179. }
  180. if (kind == EffectKind::parametricEq)
  181. for (int b = 0; b < peq::bands; ++b)
  182. {
  183. (void) peq::orderText (params, b);
  184. (void) peq::widthText (params, b);
  185. (void) peq::responseDb (params, sr, 20.0 + 1000.0 * b);
  186. }
  187. }
  188. // automated plugin parameters
  189. for (int p = 0; p < np + 2; ++p)
  190. fx->setParameter (p, uni());
  191. char label[64];
  192. std::snprintf (label, sizeof (label), "sr %.0f, setParameter", sr);
  193. run (*fx, slot.params, 2, 2048, label, name);
  194. fx->reset();
  195. std::snprintf (label, sizeof (label), "sr %.0f, after reset", sr);
  196. run (*fx, slot.params, 2, 2048, label, name);
  197. }
  198. // controls kept in the slot's state (EQUO bands, Delay Bank banks, Convolver knobs, Maximus bands...)
  199. if (const auto* table = stateparams::tableFor (kind))
  200. {
  201. auto fx = makeEffect (slot);
  202. fx->prepare (48000.0);
  203. auto state = slot.state;
  204. for (int k = 0; k < rounds * 4; ++k)
  205. {
  206. for (int i = 0; i < (int) table->params.size(); ++i)
  207. {
  208. const auto& p = table->params[(std::size_t) i];
  209. const int choice = (int) (rng() % 3);
  210. const int raw = choice == 0 ? p.minimum : choice == 1 ? p.maximum : p.minimum + (int) (rng() % (std::uint32_t) std::max (1, p.maximum - p.minimum + 1));
  211. if (rng() % 3 == 0 || k == 0)
  212. {
  213. state = table->with (state, i, raw);
  214. (void) table->get (state, i);
  215. (void) table->text (state, i, raw);
  216. }
  217. }
  218. auto copy = slot;
  219. copy.state = state;
  220. if (! fx->acceptState (state))
  221. {
  222. fx = makeEffect (copy);
  223. fx->prepare (48000.0);
  224. }
  225. run (*fx, slot.params, 3, 2048, "state controls (48 kHz)", name);
  226. }
  227. }
  228. }
  229. void fuzzDir (const std::filesystem::path& dir)
  230. {
  231. int presets = 0, projects = 0;
  232. std::error_code ec;
  233. for (auto it = std::filesystem::recursive_directory_iterator (dir, std::filesystem::directory_options::skip_permission_denied, ec);
  234. it != std::filesystem::recursive_directory_iterator(); it.increment (ec))
  235. {
  236. if (ec || ! it->is_regular_file())
  237. continue;
  238. auto ext = it->path().extension().string();
  239. for (auto& c : ext)
  240. c = (char) std::tolower ((unsigned char) c);
  241. const auto bytes = readFile (it->path());
  242. if (ext == ".fst")
  243. {
  244. if (auto slot = lynx::effectFromFlPreset (bytes, 120.0))
  245. {
  246. ++presets;
  247. fuzzSlot (*slot, it->path().string());
  248. }
  249. }
  250. else if (ext == ".flp")
  251. {
  252. lynx::Song song;
  253. try
  254. {
  255. song = lynx::importFlp (flp::parseProject (bytes));
  256. }
  257. catch (const std::exception&)
  258. {
  259. continue;
  260. }
  261. ++projects;
  262. for (std::size_t t = 0; t < song.mixer.size(); ++t)
  263. for (std::size_t s = 0; s < song.mixer[t].effects.size(); ++s)
  264. if (song.mixer[t].effects[s].kind != lynx::EffectKind::plugin)
  265. fuzzSlot (song.mixer[t].effects[s], it->path().string() + " insert " + std::to_string (t) + " slot " + std::to_string (s));
  266. }
  267. }
  268. std::printf (" %s: %d effect presets, %d projects\n", dir.string().c_str(), presets, projects);
  269. }
  270. } // namespace
  271. int main (int argc, char** argv)
  272. {
  273. std::vector<std::string> dirs;
  274. bool kinds = true;
  275. int only = -1;
  276. for (int i = 1; i < argc; ++i)
  277. {
  278. const std::string a = argv[i];
  279. if (a == "--seed" && i + 1 < argc) rng.seed ((unsigned) std::atoi (argv[++i]));
  280. else if (a == "--rounds" && i + 1 < argc) rounds = std::atoi (argv[++i]);
  281. else if (a == "--dir" && i + 1 < argc) dirs.push_back (argv[++i]);
  282. else if (a == "--no-kinds") kinds = false;
  283. else if (a == "--verbose") verbose = true;
  284. else if (a == "--low-rates") lowRates = true;
  285. else if (a == "--only" && i + 1 < argc) only = std::atoi (argv[++i]);
  286. }
  287. // --replay <kind> <sr> "<[i]=v ...>": a fresh instance of that kind with those parameters, every input mode
  288. for (int i = 1; i + 3 < argc; ++i)
  289. if (std::string (argv[i]) == "--replay")
  290. {
  291. auto slot = lynx::makeEffectSlot ((lynx::EffectKind) std::atoi (argv[i + 1]));
  292. const double sr = std::atof (argv[i + 2]);
  293. const char* s = argv[i + 3];
  294. int index = 0, used = 0;
  295. float value = 0.0f;
  296. while (std::sscanf (s, " [%d]=%f%n", &index, &value, &used) == 2)
  297. {
  298. slot.params[(std::size_t) index] = value;
  299. s += used;
  300. }
  301. for (int k = 0; k < 20; ++k)
  302. {
  303. auto fx = lynx::makeEffect (slot);
  304. fx->prepare (sr);
  305. run (*fx, slot.params, 6, 2048, "replay", "replay");
  306. }
  307. std::printf ("replay: %d of 20 non-finite\n", nonFinite);
  308. return 0;
  309. }
  310. if (kinds)
  311. for (int k = 0; k <= (int) lynx::lastEffectKind; ++k)
  312. {
  313. const auto kind = (lynx::EffectKind) k;
  314. if (kind == lynx::EffectKind::plugin || (only >= 0 && k != only))
  315. continue;
  316. std::printf ("kind %d %s\n", k, lynx::effectInfo (kind).name);
  317. std::fflush (stdout);
  318. fuzzSlot (lynx::makeEffectSlot (kind), lynx::effectInfo (kind).name);
  319. }
  320. for (const auto& d : dirs)
  321. fuzzDir (d);
  322. std::printf ("fxfuzz: %d slots, %d with non-finite output\n", slotsTested, nonFinite);
  323. return nonFinite == 0 ? 0 : 1;
  324. }