lynx_test_crash_vst3.cpp 13 KB

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