lynx_test_crash_vst3.cpp 17 KB

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