4
0

lynx_test_crash_vst3.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // A VST3 effect that crashes on purpose (round 190dt: LYNX's plugin crash guard on Linux / macOS, and Windows' SEH
  2. // guard through JUCE's VST3 host). Main stereo in -> out at 0.5 gain, a 4-byte state ("LXV1"); no parameters, no
  3. // editor. Built only against the VST3 SDK's pluginterfaces (no SDK helper classes), like lynx_test_sidechain_vst3.cpp.
  4. // Environment variable LYNX_TEST_CRASH:
  5. // "process" - a null-pointer write (SIGSEGV / access violation) in the Nth process() call with audio,
  6. // N = LYNX_TEST_CRASH_AFTER (default 20); counted per loaded module, so an instance made again later
  7. // (LYNX's Reload) does not crash again
  8. // "memcpy" - the same as a memcpy from a null pointer: the fault is inside the C library (what nih-plug did, #155)
  9. // "state" - a null-pointer write when a saved state is loaded into it (IComponent::setState)
  10. // "thread" - (Linux / macOS; round 190gg) in the Nth process() call it starts a thread of its own that writes to a
  11. // null pointer: a fault outside every call the host makes (LYNX --crashguardtest ... thread)
  12. // "runloop" - (Linux; round 190rf) it has an editor (an empty X11 view) that asks its frame for the host's run loop
  13. // (Linux::IRunLoop), keeps it, and releases it when the plugin terminates, as u-he's plugins do. JUCE
  14. // handed out the editor window itself, deleted with the editor: the release went through freed memory
  15. // (LYNX_TEST_CRASH=runloop LYNX --plugwintest "LYNX Test Crash.vst3" report.txt 2: the window opens, the
  16. // plugin is removed, it must not crash)
  17. // "editorclose" - (Linux; round 190rp) the same editor, but its IPlugView::removed() writes to a null pointer: a
  18. // plugin that faults in its own window code while its editor closes (the Cakewalk SI-* VST2 crash reports,
  19. // Vst2Editor::~Vst2Editor). LYNX_TEST_CRASH=editorclose LYNX --plugwintest "LYNX Test Crash.vst3" r.txt 2:
  20. // LYNX must survive the editor's close (the teardown runs inside the plugin guard)
  21. //
  22. // V=<JUCE>/modules/juce_audio_processors/format_types/VST3_SDK
  23. // Linux: B="LYNX Test Crash.vst3/Contents/x86_64-linux"; mkdir -p "$B"
  24. // c++ -shared -fPIC -O2 -std=c++17 -I$V tools/testplugin/lynx_test_crash_vst3.cpp
  25. // $V/pluginterfaces/base/funknown.cpp -o "$B/LYNX Test Crash.so" (one line)
  26. // Windows: zig c++ -shared -O2 -std=c++17 -target x86_64-windows-gnu -I$V tools/testplugin/lynx_test_crash_vst3.cpp
  27. // $V/pluginterfaces/base/funknown.cpp -lole32 -o "LYNX Test Crash.vst3" (one line)
  28. // then: LYNX_TEST_CRASH=process LYNX --crashguardtest "LYNX Test Crash.vst3" report.txt
  29. #define INIT_CLASS_IID
  30. #include "pluginterfaces/base/funknown.h"
  31. #include "pluginterfaces/base/ipluginbase.h"
  32. #include "pluginterfaces/base/ibstream.h"
  33. #include "pluginterfaces/vst/ivstaudioprocessor.h"
  34. #include "pluginterfaces/vst/ivstcomponent.h"
  35. #include "pluginterfaces/vst/ivsteditcontroller.h"
  36. #include "pluginterfaces/gui/iplugview.h"
  37. #include <atomic>
  38. #include <cstdint>
  39. #include <cstdlib>
  40. #include <cstring>
  41. #if ! defined(_WIN32)
  42. #include <pthread.h>
  43. #endif
  44. #if defined(_WIN32)
  45. #include <windows.h>
  46. #define LYNX_TEST_EXPORT __declspec (dllexport)
  47. #else
  48. #define LYNX_TEST_EXPORT __attribute__ ((visibility ("default")))
  49. #endif
  50. using namespace Steinberg;
  51. using namespace Steinberg::Vst;
  52. namespace
  53. {
  54. const FUID effectUid (0x4C594E58, 0x43524153, 0x48313930, 0x44545633); // "LYNXCRASH190DTV3"
  55. // (read at every use: the host may change them between calls; on Windows from the process, not this DLL's C runtime)
  56. const char* environment (const char* name, char (&buffer)[32])
  57. {
  58. #if defined(_WIN32)
  59. return GetEnvironmentVariableA (name, buffer, sizeof (buffer)) > 0 ? buffer : nullptr;
  60. #else
  61. (void) buffer;
  62. return std::getenv (name);
  63. #endif
  64. }
  65. bool crashMode (const char* what)
  66. {
  67. char buffer[32] {};
  68. const char* value = environment ("LYNX_TEST_CRASH", buffer);
  69. return value != nullptr && std::strcmp (value, what) == 0;
  70. }
  71. int crashAfter()
  72. {
  73. char buffer[32] {};
  74. const char* value = environment ("LYNX_TEST_CRASH_AFTER", buffer);
  75. const auto n = value != nullptr ? std::atoi (value) : 0;
  76. return n > 0 ? n : 20;
  77. }
  78. volatile int sink = 0;
  79. std::atomic<int> processCalls { 0 }; // (per module)
  80. #if defined(__GNUC__) || defined(__clang__)
  81. __attribute__ ((noinline))
  82. #endif
  83. void nullWrite()
  84. {
  85. volatile std::uintptr_t address = 0; // (opaque: a write to a constant null is undefined, and g++ drops the whole path)
  86. *reinterpret_cast<volatile int*> (address) = 1;
  87. }
  88. #if defined(__GNUC__) || defined(__clang__)
  89. __attribute__ ((noinline))
  90. #endif
  91. void nullCopy()
  92. {
  93. char to[64];
  94. const char* volatile from = nullptr;
  95. volatile std::size_t size = sizeof (to); // (a real call into the C library)
  96. std::memcpy (to, (const char*) from, size);
  97. sink = to[0];
  98. }
  99. #if ! defined(_WIN32)
  100. void* faultingThread (void*)
  101. {
  102. nullWrite();
  103. return nullptr;
  104. }
  105. void startFaultingThread()
  106. {
  107. pthread_t thread;
  108. if (pthread_create (&thread, nullptr, faultingThread, nullptr) == 0)
  109. pthread_detach (thread);
  110. }
  111. #endif
  112. void copyString (String128 to, const char* from)
  113. {
  114. int i = 0;
  115. for (; from[i] != 0 && i < 127; ++i)
  116. to[i] = (char16) from[i];
  117. to[i] = 0;
  118. }
  119. class TestCrash final : public IComponent, public IAudioProcessor, public IEditController
  120. {
  121. public:
  122. // FUnknown
  123. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  124. {
  125. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPluginBase::iid)
  126. || FUnknownPrivate::iidEqual (iid, IComponent::iid))
  127. {
  128. addRef();
  129. *obj = static_cast<IComponent*> (this);
  130. return kResultOk;
  131. }
  132. if (FUnknownPrivate::iidEqual (iid, IAudioProcessor::iid))
  133. {
  134. addRef();
  135. *obj = static_cast<IAudioProcessor*> (this);
  136. return kResultOk;
  137. }
  138. if (FUnknownPrivate::iidEqual (iid, IEditController::iid))
  139. {
  140. addRef();
  141. *obj = static_cast<IEditController*> (this);
  142. return kResultOk;
  143. }
  144. *obj = nullptr;
  145. return kNoInterface;
  146. }
  147. uint32 PLUGIN_API addRef() override { return ++refs; }
  148. uint32 PLUGIN_API release() override
  149. {
  150. const auto r = --refs;
  151. if (r == 0)
  152. delete this;
  153. return r;
  154. }
  155. // IPluginBase
  156. tresult PLUGIN_API initialize (FUnknown*) override { return kResultOk; }
  157. tresult PLUGIN_API terminate() override
  158. {
  159. #if defined(__linux__)
  160. keepRunLoop (nullptr); // ("runloop": the run loop the view got from its frame, released only now)
  161. #endif
  162. return kResultOk;
  163. }
  164. // IComponent: one stereo main input and output
  165. tresult PLUGIN_API getControllerClassId (TUID) override { return kNotImplemented; } // single component
  166. tresult PLUGIN_API setIoMode (IoMode) override { return kResultOk; }
  167. int32 PLUGIN_API getBusCount (MediaType type, BusDirection) override { return type == kAudio ? 1 : 0; }
  168. tresult PLUGIN_API getBusInfo (MediaType type, BusDirection dir, int32 index, BusInfo& bus) override
  169. {
  170. if (type != kAudio || index != 0)
  171. return kInvalidArgument;
  172. std::memset (&bus, 0, sizeof (bus));
  173. bus.mediaType = type;
  174. bus.direction = dir;
  175. bus.channelCount = 2;
  176. bus.busType = kMain;
  177. bus.flags = BusInfo::kDefaultActive;
  178. copyString (bus.name, dir == kOutput ? "Output" : "Input");
  179. return kResultOk;
  180. }
  181. tresult PLUGIN_API getRoutingInfo (RoutingInfo&, RoutingInfo&) override { return kNotImplemented; }
  182. tresult PLUGIN_API activateBus (MediaType, BusDirection, int32, TBool) override { return kResultOk; }
  183. tresult PLUGIN_API setActive (TBool) override { return kResultOk; }
  184. tresult PLUGIN_API setState (IBStream* stream) override
  185. {
  186. if (crashMode ("state"))
  187. nullWrite();
  188. char data[4] {};
  189. int32 read = 0;
  190. if (stream != nullptr)
  191. stream->read (data, 4, &read);
  192. return kResultOk;
  193. }
  194. tresult PLUGIN_API getState (IBStream* stream) override
  195. {
  196. int32 written = 0;
  197. return stream != nullptr ? stream->write (const_cast<char*> ("LXV1"), 4, &written) : kInvalidArgument;
  198. }
  199. // IAudioProcessor
  200. tresult PLUGIN_API setBusArrangements (SpeakerArrangement* ins, int32 numIns, SpeakerArrangement* outs, int32 numOuts) override
  201. {
  202. return numIns == 1 && numOuts == 1 && ins[0] == SpeakerArr::kStereo && outs[0] == SpeakerArr::kStereo ? kResultTrue : kResultFalse;
  203. }
  204. tresult PLUGIN_API getBusArrangement (BusDirection, int32 index, SpeakerArrangement& arr) override
  205. {
  206. if (index != 0)
  207. return kInvalidArgument;
  208. arr = SpeakerArr::kStereo;
  209. return kResultOk;
  210. }
  211. tresult PLUGIN_API canProcessSampleSize (int32 size) override { return size == kSample32 ? kResultTrue : kResultFalse; }
  212. uint32 PLUGIN_API getLatencySamples() override { return 0; }
  213. tresult PLUGIN_API setupProcessing (ProcessSetup&) override { return kResultOk; }
  214. tresult PLUGIN_API setProcessing (TBool) override { return kResultOk; }
  215. uint32 PLUGIN_API getTailSamples() override { return 0; }
  216. tresult PLUGIN_API process (ProcessData& data) override
  217. {
  218. const auto n = data.numSamples;
  219. if (n <= 0 || data.numOutputs < 1 || data.outputs == nullptr || data.outputs[0].numChannels < 2)
  220. return kResultOk; // (a parameter flush)
  221. if (++processCalls == crashAfter())
  222. {
  223. if (crashMode ("process"))
  224. nullWrite();
  225. if (crashMode ("memcpy"))
  226. nullCopy();
  227. #if ! defined(_WIN32)
  228. if (crashMode ("thread"))
  229. startFaultingThread();
  230. #endif
  231. }
  232. auto& out = data.outputs[0];
  233. for (int c = 0; c < 2; ++c)
  234. {
  235. auto* to = out.channelBuffers32[c];
  236. const auto* from = data.numInputs > 0 && data.inputs != nullptr && data.inputs[0].numChannels > c
  237. && data.inputs[0].channelBuffers32 != nullptr
  238. ? data.inputs[0].channelBuffers32[c]
  239. : nullptr;
  240. if (to == nullptr)
  241. continue;
  242. for (int32 s = 0; s < n; ++s)
  243. to[s] = from != nullptr ? 0.5f * from[s] : 0.0f;
  244. }
  245. return kResultOk;
  246. }
  247. // IEditController (no parameters, no editor)
  248. tresult PLUGIN_API setComponentState (IBStream*) override { return kResultOk; }
  249. int32 PLUGIN_API getParameterCount() override { return 0; }
  250. tresult PLUGIN_API getParameterInfo (int32, ParameterInfo&) override { return kInvalidArgument; }
  251. tresult PLUGIN_API getParamStringByValue (ParamID, ParamValue, String128) override { return kInvalidArgument; }
  252. tresult PLUGIN_API getParamValueByString (ParamID, TChar*, ParamValue&) override { return kInvalidArgument; }
  253. ParamValue PLUGIN_API normalizedParamToPlain (ParamID, ParamValue v) override { return v; }
  254. ParamValue PLUGIN_API plainParamToNormalized (ParamID, ParamValue v) override { return v; }
  255. ParamValue PLUGIN_API getParamNormalized (ParamID) override { return 0.0; }
  256. tresult PLUGIN_API setParamNormalized (ParamID, ParamValue) override { return kResultOk; }
  257. tresult PLUGIN_API setComponentHandler (IComponentHandler*) override { return kResultOk; }
  258. IPlugView* PLUGIN_API createView (FIDString) override;
  259. #if defined(__linux__)
  260. void keepRunLoop (Linux::IRunLoop* loop)
  261. {
  262. if (heldLoop != nullptr)
  263. heldLoop->release();
  264. heldLoop = loop;
  265. }
  266. #endif
  267. private:
  268. std::atomic<uint32> refs { 1 };
  269. #if defined(__linux__)
  270. Linux::IRunLoop* heldLoop = nullptr;
  271. #endif
  272. };
  273. #if defined(__linux__)
  274. // "runloop": an empty X11 view that keeps its frame's run loop (held by the plugin until it terminates)
  275. class RunLoopView final : public IPlugView
  276. {
  277. public:
  278. explicit RunLoopView (TestCrash& o) : owner (o) { owner.addRef(); }
  279. ~RunLoopView() { owner.release(); }
  280. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  281. {
  282. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPlugView::iid))
  283. {
  284. addRef();
  285. *obj = static_cast<IPlugView*> (this);
  286. return kResultOk;
  287. }
  288. *obj = nullptr;
  289. return kNoInterface;
  290. }
  291. uint32 PLUGIN_API addRef() override { return ++refs; }
  292. uint32 PLUGIN_API release() override
  293. {
  294. const auto r = --refs;
  295. if (r == 0)
  296. delete this;
  297. return r;
  298. }
  299. tresult PLUGIN_API isPlatformTypeSupported (FIDString type) override
  300. {
  301. return type != nullptr && std::strcmp (type, kPlatformTypeX11EmbedWindowID) == 0 ? kResultTrue : kResultFalse;
  302. }
  303. tresult PLUGIN_API attached (void*, FIDString) override { return kResultOk; }
  304. tresult PLUGIN_API removed() override
  305. {
  306. if (crashOnRemove)
  307. *(volatile int*) nullptr = 1; // ("editorclose")
  308. return kResultOk;
  309. }
  310. bool crashOnRemove = false;
  311. tresult PLUGIN_API onWheel (float) override { return kResultFalse; }
  312. tresult PLUGIN_API onKeyDown (char16, int16, int16) override { return kResultFalse; }
  313. tresult PLUGIN_API onKeyUp (char16, int16, int16) override { return kResultFalse; }
  314. tresult PLUGIN_API getSize (ViewRect* size) override
  315. {
  316. if (size == nullptr)
  317. return kInvalidArgument;
  318. *size = ViewRect (0, 0, 240, 120);
  319. return kResultOk;
  320. }
  321. tresult PLUGIN_API onSize (ViewRect*) override { return kResultOk; }
  322. tresult PLUGIN_API onFocus (TBool) override { return kResultOk; }
  323. tresult PLUGIN_API setFrame (IPlugFrame* frame) override
  324. {
  325. Linux::IRunLoop* loop = nullptr;
  326. if (frame != nullptr && frame->queryInterface (Linux::IRunLoop::iid, (void**) &loop) == kResultOk && loop != nullptr)
  327. owner.keepRunLoop (loop);
  328. return kResultOk;
  329. }
  330. tresult PLUGIN_API canResize() override { return kResultFalse; }
  331. tresult PLUGIN_API checkSizeConstraint (ViewRect*) override { return kResultOk; }
  332. private:
  333. TestCrash& owner;
  334. std::atomic<uint32> refs { 1 };
  335. };
  336. #endif
  337. IPlugView* PLUGIN_API TestCrash::createView (FIDString name)
  338. {
  339. #if defined(__linux__)
  340. char buffer[32];
  341. const auto* mode = environment ("LYNX_TEST_CRASH", buffer);
  342. if (mode != nullptr && (std::strcmp (mode, "runloop") == 0 || std::strcmp (mode, "editorclose") == 0) && name != nullptr
  343. && std::strcmp (name, ViewType::kEditor) == 0)
  344. {
  345. auto* view = new RunLoopView (*this);
  346. view->crashOnRemove = std::strcmp (mode, "editorclose") == 0;
  347. return view;
  348. }
  349. #else
  350. (void) name;
  351. #endif
  352. return nullptr;
  353. }
  354. class Factory final : public IPluginFactory2
  355. {
  356. public:
  357. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  358. {
  359. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPluginFactory::iid)
  360. || FUnknownPrivate::iidEqual (iid, IPluginFactory2::iid))
  361. {
  362. *obj = static_cast<IPluginFactory2*> (this);
  363. return kResultOk;
  364. }
  365. *obj = nullptr;
  366. return kNoInterface;
  367. }
  368. uint32 PLUGIN_API addRef() override { return 1; }
  369. uint32 PLUGIN_API release() override { return 1; }
  370. tresult PLUGIN_API getFactoryInfo (PFactoryInfo* info) override
  371. {
  372. std::memset (static_cast<void*> (info), 0, sizeof (*info));
  373. std::strcpy (info->vendor, "LYNX");
  374. std::strcpy (info->url, "https://github.com/Ka1-S/LYNX-SHARE");
  375. info->flags = PFactoryInfo::kUnicode;
  376. return kResultOk;
  377. }
  378. int32 PLUGIN_API countClasses() override { return 1; }
  379. tresult PLUGIN_API getClassInfo (int32 index, PClassInfo* info) override
  380. {
  381. if (index != 0)
  382. return kInvalidArgument;
  383. std::memset (static_cast<void*> (info), 0, sizeof (*info));
  384. effectUid.toTUID (info->cid);
  385. info->cardinality = PClassInfo::kManyInstances;
  386. std::strcpy (info->category, kVstAudioEffectClass);
  387. std::strcpy (info->name, "LYNX Test Crash VST3");
  388. return kResultOk;
  389. }
  390. tresult PLUGIN_API getClassInfo2 (int32 index, PClassInfo2* info) override
  391. {
  392. if (index != 0)
  393. return kInvalidArgument;
  394. std::memset (static_cast<void*> (info), 0, sizeof (*info));
  395. effectUid.toTUID (info->cid);
  396. info->cardinality = PClassInfo::kManyInstances;
  397. std::strcpy (info->category, kVstAudioEffectClass);
  398. std::strcpy (info->name, "LYNX Test Crash VST3");
  399. std::strcpy (info->subCategories, "Fx");
  400. std::strcpy (info->vendor, "LYNX");
  401. std::strcpy (info->version, "1.0.0");
  402. std::strcpy (info->sdkVersion, "VST 3.7");
  403. return kResultOk;
  404. }
  405. tresult PLUGIN_API createInstance (FIDString cid, FIDString iid, void** obj) override
  406. {
  407. if (! FUnknownPrivate::iidEqual (cid, effectUid.toTUID()))
  408. {
  409. *obj = nullptr;
  410. return kNoInterface;
  411. }
  412. auto* effect = new TestCrash();
  413. const auto result = effect->queryInterface (iid, obj);
  414. effect->release();
  415. return result;
  416. }
  417. };
  418. Factory factory;
  419. } // namespace
  420. extern "C"
  421. {
  422. LYNX_TEST_EXPORT IPluginFactory* PLUGIN_API GetPluginFactory() { return static_cast<IPluginFactory2*> (&factory); }
  423. #if defined(_WIN32)
  424. LYNX_TEST_EXPORT bool InitDll() { return true; }
  425. LYNX_TEST_EXPORT bool ExitDll() { return true; }
  426. #else
  427. LYNX_TEST_EXPORT bool ModuleEntry (void*) { return true; }
  428. LYNX_TEST_EXPORT bool ModuleExit() { return true; }
  429. #endif
  430. }