CrashFuzzTests111.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Round 111: crash fuzzing of the built-in effects (docs/analise/REA_rodada111.md; the full run is tools/fxfuzz.cpp and
  2. // LYNX.exe --fxfuzz). Every effect, every parameter at both ends and random, several rates and block sizes: no crash
  3. // and no NaN / inf output.
  4. #include <limits>
  5. #include "core/Biquad.h"
  6. #include "core/RackRenderer.h"
  7. namespace fuzz111
  8. {
  9. bool finite (const std::vector<float>& x)
  10. {
  11. for (const auto v : x)
  12. if (! std::isfinite (v))
  13. return false;
  14. return true;
  15. }
  16. // runs `blocks` blocks of noise through the effect; false = non-finite output
  17. bool drive (lynx::Effect& fx, const lynx::EffectParams& params, std::mt19937& rng, int blocks)
  18. {
  19. static const int sizes[] = { 1, 7, 64, 255, 512, 513, 2048 };
  20. std::vector<float> l (2048), r (2048);
  21. for (int b = 0; b < blocks; ++b)
  22. {
  23. const auto n = sizes[rng() % 7];
  24. for (int i = 0; i < n; ++i)
  25. {
  26. l[(std::size_t) i] = std::uniform_real_distribution<float> (-1.0f, 1.0f) (rng);
  27. r[(std::size_t) i] = -0.5f * l[(std::size_t) i];
  28. }
  29. fx.process (l.data(), r.data(), n, params, 128.0);
  30. for (int i = 0; i < n; ++i)
  31. if (! std::isfinite (l[(std::size_t) i]) || ! std::isfinite (r[(std::size_t) i]))
  32. return false;
  33. }
  34. return true;
  35. }
  36. } // namespace fuzz111
  37. TEST (fuzz_every_effect_survives_extreme_parameters)
  38. {
  39. using namespace lynx;
  40. std::mt19937 rng (111);
  41. int checked = 0;
  42. for (int k = 0; k <= (int) lastEffectKind; ++k)
  43. {
  44. const auto kind = (EffectKind) k;
  45. if (kind == EffectKind::plugin)
  46. continue;
  47. const auto slot = makeEffectSlot (kind);
  48. const auto count = (int) effectInfo (kind).params.size();
  49. for (const double rate : { 44100.0, 48000.0, 96000.0 })
  50. {
  51. auto fx = makeEffect (slot);
  52. fx->prepare (rate);
  53. bool ok = fuzz111::drive (*fx, slot.params, rng, 2);
  54. for (int p = 0; p < count && ok; ++p)
  55. for (const float v : { 0.0f, 1.0f })
  56. {
  57. auto params = slot.params;
  58. params[(std::size_t) p] = v;
  59. ok = ok && fuzz111::drive (*fx, params, rng, 1);
  60. (void) formatEffectParam (kind, p, v, 120.0, params);
  61. }
  62. for (int round = 0; round < 3 && ok; ++round)
  63. {
  64. auto params = slot.params;
  65. for (int p = 0; p < count; ++p)
  66. params[(std::size_t) p] = round == 0 ? 0.0f : round == 1 ? 1.0f : std::uniform_real_distribution<float> (0.0f, 1.0f) (rng);
  67. ok = fuzz111::drive (*fx, params, rng, 2);
  68. }
  69. if (! ok)
  70. std::printf (" non-finite: %s at %.0f Hz\n", effectInfo (kind).name, rate);
  71. CHECK (ok);
  72. ++checked;
  73. }
  74. }
  75. CHECK (checked > 200);
  76. }
  77. TEST (fuzz_parametric_eq_steep_shelf_stays_finite)
  78. {
  79. using namespace lynx;
  80. // round 111: a shelf band with a high gain and the Q knob near its top (slope S above what the gain allows) made
  81. // the cookbook's square root negative: NaN coefficients, a silent / broken insert and a NaN curve in the editor
  82. auto slot = makeEffectSlot (EffectKind::parametricEq);
  83. for (const auto type : { peq::BandType::lowShelf, peq::BandType::highShelf })
  84. for (const double db : { -18.0, 18.0 })
  85. for (const float qKnob : { 0.6f, 0.8f, 1.0f })
  86. {
  87. auto p = slot.params;
  88. p[(std::size_t) peq::param (0, 0)] = peq::typeValue (type);
  89. p[(std::size_t) peq::param (0, 2)] = peq::gainValue (db);
  90. p[(std::size_t) peq::param (0, 3)] = qKnob;
  91. for (const double hz : { 20.0, 1000.0, 20000.0 })
  92. CHECK (std::isfinite (peq::responseDb (p, 48000.0, hz)));
  93. auto fx = makeEffect (EffectKind::parametricEq);
  94. fx->prepare (48000.0);
  95. std::mt19937 rng (7);
  96. CHECK (fuzz111::drive (*fx, p, rng, 4));
  97. }
  98. Biquad b;
  99. b.shelf (48000.0, 1000.0, 18.0, false, 4.0);
  100. CHECK (std::isfinite (b.b0) && std::isfinite (b.a1) && std::isfinite (b.a2) && std::abs (b.a2) < 1.0);
  101. b.shelf (48000.0, 1000.0, 6.0, true, 1.0); // the usual range is unchanged: S = 1 at 6 dB is the cookbook value
  102. const auto a = std::pow (10.0, 6.0 / 40.0), w = 2.0 * 3.141592653589793 * 1000.0 / 48000.0;
  103. const auto alpha = std::sin (w) / 2.0 * std::sqrt (2.0);
  104. const auto a0 = (a + 1) - (a - 1) * std::cos (w) + 2.0 * std::sqrt (a) * alpha;
  105. CHECK (std::abs (b.a2 - ((a + 1) - (a - 1) * std::cos (w) - 2.0 * std::sqrt (a) * alpha) / a0) < 1e-12);
  106. }
  107. TEST (fuzz_effects_at_unusual_rates_do_not_crash)
  108. {
  109. using namespace lynx;
  110. // round 111: Multiband Delay wrote past its crossover mask below 44.1 kHz; Fruity Vocoder's band top overflowed an
  111. // int above 107 kHz
  112. std::mt19937 rng (3);
  113. for (const auto kind : { EffectKind::multibandDelay, EffectKind::vocoder })
  114. for (const double rate : { 22050.0, 32000.0, 176400.0, 192000.0 })
  115. {
  116. auto fx = makeEffect (makeEffectSlot (kind));
  117. fx->prepare (rate);
  118. CHECK (fuzz111::drive (*fx, makeEffectSlot (kind).params, rng, 3));
  119. }
  120. }
  121. TEST (fuzz_renderer_mutes_an_effect_that_outputs_nan)
  122. {
  123. using namespace lynx;
  124. // a NaN reaching an insert effect (here from the source itself) is muted after that effect and its state reset,
  125. // so it never spreads to the rest of the mix
  126. Song s;
  127. s.mixer = Song::defaultMixer (2);
  128. ChannelDef c;
  129. c.instrument = InstrumentKind::sampler;
  130. c.samplePath = "nan";
  131. c.volume = 1.0f;
  132. c.mixerTrack = 1;
  133. s.channels.push_back (c);
  134. s.patterns.emplace_back();
  135. s.mixer[1].effects.push_back (makeEffectSlot (EffectKind::parametricEq));
  136. auto provider = [] (const std::string&) {
  137. auto d = std::make_shared<SampleData>();
  138. d->sampleRate = 48000.0;
  139. d->left.assign (4800, 0.25f);
  140. d->left[10] = std::numeric_limits<float>::quiet_NaN();
  141. return std::shared_ptr<const SampleData> (d);
  142. };
  143. auto st = RackState::build (s, nullptr, 48000.0, provider);
  144. RackRenderer r;
  145. r.prepare (48000.0, 256);
  146. LiveEventQueue live;
  147. NoteEvent on;
  148. on.channel = 0;
  149. on.key = 60;
  150. on.velocity = 1.0f;
  151. live.push (on);
  152. const auto before = RackRenderer::nonFiniteEffectBlocks();
  153. std::vector<float> l (1024), rr (1024);
  154. r.process (st.get(), false, 0.0, 1024, l.data(), rr.data(), &live);
  155. CHECK (fuzz111::finite (l) && fuzz111::finite (rr));
  156. CHECK (RackRenderer::nonFiniteEffectBlocks() > before);
  157. }