lynx_test_crash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* A CLAP effect that crashes on purpose, for testing LYNX's crash protection (round 111; Linux / macOS: round 190dt):
  2. * Windows: zig cc -shared -O2 -target x86_64-windows-gnu -Ithird_party/clap/include tools/testplugin/lynx_test_crash.c -luser32 -lgdi32 \
  3. * -o "LYNX Test Crash.clap"
  4. * Linux / macOS: cc -shared -fPIC -O2 -Ithird_party/clap/include tools/testplugin/lynx_test_crash.c -o "LYNX Test Crash.clap"
  5. * Otherwise a 0.5 gain (in -> out), with a 4-byte state ("LXC1").
  6. * Environment variable LYNX_TEST_CRASH:
  7. * "process" - a null-pointer write (access violation / SIGSEGV) in the Nth process() call, N = LYNX_TEST_CRASH_AFTER
  8. * (default 20); counted per loaded module, so an instance made again later does not crash again
  9. * (LYNX --plugintest / --crashguardtest: the slot must be switched off, the render must finish)
  10. * "memcpy" - the same, but as a memcpy from a null pointer (the fault is inside the C library, issue #155)
  11. * "state" - a null-pointer write when a saved state is loaded into it (state extension load)
  12. * "thread" - (Linux / macOS, round 190gg; Windows, round 190oh) in the Nth process() call it starts a thread of its own that writes to a
  13. * null pointer: a fault outside every call the host makes (LYNX --crashguardtest ... thread)
  14. * "editor-create" - (Linux / macOS; round 190gg) it offers an editor (CLAP gui, X11 / Cocoa) and writes to a null
  15. * pointer when the host creates it (LYNX --crashguardtest ... ui: the editor is made under the guard)
  16. * "exit" - (Windows) a fail-fast (0xC0000409) when the DLL is unloaded at process exit, like the Waves shell whose
  17. * teardown crashed LYNX's scan child (LYNX.exe --scantest: the plugin must be found, not blacklisted)
  18. * "editor" - (Windows; issue #32, round 174a) an access violation in its own editor window's procedure, on the
  19. * window's first WM_TIMER (~0.3 s after it opens), like the synth editor that took LYNX down
  20. * (LYNX.exe --editorcrashtest: the editor must be closed, LYNX must keep running)
  21. * Its editor (CLAP gui, Win32 child window; Windows only) is a plain grey 240x120 window.
  22. */
  23. #if defined(_WIN32)
  24. #include <windows.h>
  25. #else
  26. #include <pthread.h>
  27. #endif
  28. #include <clap/clap.h>
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. static const char* const features[] = { CLAP_PLUGIN_FEATURE_AUDIO_EFFECT, CLAP_PLUGIN_FEATURE_UTILITY, NULL };
  33. static const clap_plugin_descriptor_t descriptor = {
  34. CLAP_VERSION_INIT, "org.lynx.test-crash", "LYNX Test Crash", "LYNX", "https://github.com/Ka1-S/LYNX-SHARE", "", "", "1.0.0",
  35. "Crashes on purpose (crash protection tests)", features
  36. };
  37. static uint32_t ports_count (const clap_plugin_t* p, bool in) { (void) p; (void) in; return 1; }
  38. static bool ports_get (const clap_plugin_t* p, uint32_t index, bool in, clap_audio_port_info_t* info)
  39. {
  40. (void) p;
  41. if (index != 0) return false;
  42. memset (info, 0, sizeof (*info));
  43. info->id = in ? 0 : 1;
  44. strcpy (info->name, in ? "In" : "Out");
  45. info->flags = CLAP_AUDIO_PORT_IS_MAIN;
  46. info->channel_count = 2;
  47. info->port_type = CLAP_PORT_STEREO;
  48. info->in_place_pair = CLAP_INVALID_ID;
  49. return true;
  50. }
  51. static const clap_plugin_audio_ports_t audio_ports = { ports_count, ports_get };
  52. static bool p_init (const clap_plugin_t* p) { (void) p; return true; }
  53. static void p_destroy (const clap_plugin_t* p) { free ((void*) p); }
  54. static bool p_activate (const clap_plugin_t* p, double r, uint32_t a, uint32_t b) { (void) p; (void) r; (void) a; (void) b; return true; }
  55. static void p_deactivate (const clap_plugin_t* p) { (void) p; }
  56. static bool p_start (const clap_plugin_t* p) { (void) p; return true; }
  57. static void p_stop (const clap_plugin_t* p) { (void) p; }
  58. static void p_reset (const clap_plugin_t* p) { (void) p; }
  59. static int crash_mode (const char* what)
  60. {
  61. #if defined(_WIN32)
  62. char value[32] = { 0 };
  63. return GetEnvironmentVariableA ("LYNX_TEST_CRASH", value, sizeof (value)) > 0 && strcmp (value, what) == 0;
  64. #else
  65. const char* value = getenv ("LYNX_TEST_CRASH");
  66. return value != NULL && strcmp (value, what) == 0;
  67. #endif
  68. }
  69. static int crash_after (void)
  70. {
  71. #if defined(_WIN32)
  72. char value[32] = { 0 };
  73. const int n = GetEnvironmentVariableA ("LYNX_TEST_CRASH_AFTER", value, sizeof (value)) > 0 ? atoi (value) : 0;
  74. #else
  75. const char* value = getenv ("LYNX_TEST_CRASH_AFTER");
  76. const int n = value != NULL ? atoi (value) : 0;
  77. #endif
  78. return n > 0 ? n : 20;
  79. }
  80. static volatile int sink = 0;
  81. static void null_write (void)
  82. {
  83. volatile uintptr_t address = 0; /* (opaque: a compiler may drop a write to a constant null) */
  84. *(volatile int*) address = 1;
  85. }
  86. static void null_copy (void)
  87. {
  88. char to[64];
  89. const char* volatile from = NULL;
  90. volatile size_t size = sizeof (to); /* (a real call into the C library) */
  91. memcpy (to, (const char*) from, size);
  92. sink = to[0];
  93. }
  94. #if ! defined(_WIN32)
  95. static void* faulting_thread (void* arg)
  96. {
  97. (void) arg;
  98. null_write();
  99. return NULL;
  100. }
  101. #else
  102. static DWORD WINAPI faulting_thread (LPVOID arg) /* (round 190oh: Windows) */
  103. {
  104. (void) arg;
  105. null_write();
  106. return 0;
  107. }
  108. #endif
  109. static clap_process_status p_process (const clap_plugin_t* p, const clap_process_t* proc)
  110. {
  111. static int calls = 0;
  112. (void) p;
  113. if (++calls == crash_after())
  114. {
  115. if (crash_mode ("process"))
  116. null_write();
  117. if (crash_mode ("memcpy"))
  118. null_copy();
  119. if (crash_mode ("thread"))
  120. {
  121. #if ! defined(_WIN32)
  122. pthread_t thread;
  123. if (pthread_create (&thread, NULL, faulting_thread, NULL) == 0)
  124. pthread_detach (thread);
  125. #else
  126. HANDLE thread = CreateThread (NULL, 0, faulting_thread, NULL, 0, NULL);
  127. if (thread != NULL)
  128. CloseHandle (thread);
  129. #endif
  130. }
  131. }
  132. if (proc->audio_inputs_count < 1 || proc->audio_outputs_count < 1) return CLAP_PROCESS_CONTINUE;
  133. for (uint32_t c = 0; c < 2 && c < proc->audio_inputs[0].channel_count && c < proc->audio_outputs[0].channel_count; ++c)
  134. for (uint32_t i = 0; i < proc->frames_count; ++i)
  135. proc->audio_outputs[0].data32[c][i] = 0.5f * proc->audio_inputs[0].data32[c][i];
  136. return CLAP_PROCESS_CONTINUE;
  137. }
  138. #if defined(_WIN32)
  139. /* the editor: one child window; "editor" mode crashes in its window procedure */
  140. static HWND editor_window = NULL;
  141. static LRESULT CALLBACK editor_proc (HWND h, UINT m, WPARAM w, LPARAM l)
  142. {
  143. if (m == WM_TIMER && crash_mode ("editor"))
  144. *(volatile int*) (uintptr_t) 0 = 1;
  145. if (m == WM_PAINT)
  146. {
  147. PAINTSTRUCT ps;
  148. HDC dc = BeginPaint (h, &ps);
  149. FillRect (dc, &ps.rcPaint, (HBRUSH) GetStockObject (GRAY_BRUSH));
  150. EndPaint (h, &ps);
  151. return 0;
  152. }
  153. return DefWindowProcA (h, m, w, l);
  154. }
  155. static bool g_api (const clap_plugin_t* p, const char* api, bool floating) { (void) p; return ! floating && strcmp (api, CLAP_WINDOW_API_WIN32) == 0; }
  156. static bool g_preferred (const clap_plugin_t* p, const char** api, bool* floating) { (void) p; *api = CLAP_WINDOW_API_WIN32; *floating = false; return true; }
  157. static bool g_create (const clap_plugin_t* p, const char* api, bool floating) { (void) p; return g_api (p, api, floating); }
  158. static void g_destroy (const clap_plugin_t* p)
  159. {
  160. (void) p;
  161. if (editor_window != NULL) DestroyWindow (editor_window);
  162. editor_window = NULL;
  163. }
  164. static bool g_scale (const clap_plugin_t* p, double s) { (void) p; (void) s; return true; }
  165. static bool g_size (const clap_plugin_t* p, uint32_t* w, uint32_t* h) { (void) p; *w = 240; *h = 120; return true; }
  166. static bool g_can_resize (const clap_plugin_t* p) { (void) p; return false; }
  167. static bool g_hints (const clap_plugin_t* p, clap_gui_resize_hints_t* h) { (void) p; (void) h; return false; }
  168. static bool g_adjust (const clap_plugin_t* p, uint32_t* w, uint32_t* h) { (void) p; *w = 240; *h = 120; return true; }
  169. static bool g_set_size (const clap_plugin_t* p, uint32_t w, uint32_t h) { (void) p; (void) w; (void) h; return true; }
  170. static bool g_parent (const clap_plugin_t* p, const clap_window_t* window)
  171. {
  172. static int registered = 0;
  173. (void) p;
  174. if (! registered)
  175. {
  176. WNDCLASSA c;
  177. memset (&c, 0, sizeof (c));
  178. c.lpfnWndProc = editor_proc;
  179. c.hInstance = GetModuleHandleA (NULL);
  180. c.lpszClassName = "LynxTestCrashEditor";
  181. RegisterClassA (&c);
  182. registered = 1;
  183. }
  184. editor_window = CreateWindowExA (0, "LynxTestCrashEditor", "", WS_CHILD | WS_VISIBLE, 0, 0, 240, 120, (HWND) window->win32, NULL,
  185. GetModuleHandleA (NULL), NULL);
  186. if (editor_window != NULL)
  187. SetTimer (editor_window, 1, 300, NULL);
  188. return editor_window != NULL;
  189. }
  190. static bool g_transient (const clap_plugin_t* p, const clap_window_t* w) { (void) p; (void) w; return false; }
  191. static void g_title (const clap_plugin_t* p, const char* t) { (void) p; (void) t; }
  192. static bool g_show (const clap_plugin_t* p) { (void) p; if (editor_window != NULL) ShowWindow (editor_window, SW_SHOW); return true; }
  193. static bool g_hide (const clap_plugin_t* p) { (void) p; if (editor_window != NULL) ShowWindow (editor_window, SW_HIDE); return true; }
  194. static const clap_plugin_gui_t gui = { g_api, g_preferred, g_create, g_destroy, g_scale, g_size, g_can_resize, g_hints, g_adjust,
  195. g_set_size, g_parent, g_transient, g_title, g_show, g_hide };
  196. #else
  197. /* Linux / macOS: an editor only in "editor-create" mode, which crashes as the host creates it */
  198. static bool g_api (const clap_plugin_t* p, const char* api, bool floating)
  199. {
  200. (void) p;
  201. return ! floating && (strcmp (api, CLAP_WINDOW_API_X11) == 0 || strcmp (api, CLAP_WINDOW_API_COCOA) == 0);
  202. }
  203. static bool g_preferred (const clap_plugin_t* p, const char** api, bool* floating)
  204. {
  205. (void) p;
  206. #if defined(__APPLE__)
  207. *api = CLAP_WINDOW_API_COCOA;
  208. #else
  209. *api = CLAP_WINDOW_API_X11;
  210. #endif
  211. *floating = false;
  212. return true;
  213. }
  214. static bool g_create (const clap_plugin_t* p, const char* api, bool floating)
  215. {
  216. if (crash_mode ("editor-create"))
  217. null_write();
  218. return g_api (p, api, floating);
  219. }
  220. static void g_destroy (const clap_plugin_t* p) { (void) p; }
  221. static bool g_scale (const clap_plugin_t* p, double s) { (void) p; (void) s; return true; }
  222. static bool g_size (const clap_plugin_t* p, uint32_t* w, uint32_t* h) { (void) p; *w = 240; *h = 120; return true; }
  223. static bool g_can_resize (const clap_plugin_t* p) { (void) p; return false; }
  224. static bool g_hints (const clap_plugin_t* p, clap_gui_resize_hints_t* h) { (void) p; (void) h; return false; }
  225. static bool g_adjust (const clap_plugin_t* p, uint32_t* w, uint32_t* h) { (void) p; *w = 240; *h = 120; return true; }
  226. static bool g_set_size (const clap_plugin_t* p, uint32_t w, uint32_t h) { (void) p; (void) w; (void) h; return true; }
  227. static bool g_parent (const clap_plugin_t* p, const clap_window_t* window) { (void) p; (void) window; return true; }
  228. static bool g_transient (const clap_plugin_t* p, const clap_window_t* w) { (void) p; (void) w; return false; }
  229. static void g_title (const clap_plugin_t* p, const char* t) { (void) p; (void) t; }
  230. static bool g_show (const clap_plugin_t* p) { (void) p; return true; }
  231. static bool g_hide (const clap_plugin_t* p) { (void) p; return true; }
  232. static const clap_plugin_gui_t gui = { g_api, g_preferred, g_create, g_destroy, g_scale, g_size, g_can_resize, g_hints, g_adjust,
  233. g_set_size, g_parent, g_transient, g_title, g_show, g_hide };
  234. #endif
  235. /* state: 4 bytes; "state" mode crashes when one is loaded */
  236. static bool s_save (const clap_plugin_t* p, const clap_ostream_t* stream)
  237. {
  238. (void) p;
  239. return stream->write (stream, "LXC1", 4) == 4;
  240. }
  241. static bool s_load (const clap_plugin_t* p, const clap_istream_t* stream)
  242. {
  243. char data[4];
  244. (void) p;
  245. if (crash_mode ("state"))
  246. null_write();
  247. return stream->read (stream, data, 4) == 4;
  248. }
  249. static const clap_plugin_state_t state_ext = { s_save, s_load };
  250. static const void* p_extension (const clap_plugin_t* p, const char* id)
  251. {
  252. (void) p;
  253. #if defined(_WIN32)
  254. if (strcmp (id, CLAP_EXT_GUI) == 0)
  255. return &gui;
  256. #else
  257. if (strcmp (id, CLAP_EXT_GUI) == 0 && crash_mode ("editor-create"))
  258. return &gui;
  259. #endif
  260. if (strcmp (id, CLAP_EXT_STATE) == 0)
  261. return &state_ext;
  262. return strcmp (id, CLAP_EXT_AUDIO_PORTS) == 0 ? &audio_ports : NULL;
  263. }
  264. static void p_main_thread (const clap_plugin_t* p) { (void) p; }
  265. static uint32_t f_count (const clap_plugin_factory_t* f) { (void) f; return 1; }
  266. static const clap_plugin_descriptor_t* f_descriptor (const clap_plugin_factory_t* f, uint32_t i) { (void) f; return i == 0 ? &descriptor : NULL; }
  267. static const clap_plugin_t* f_create (const clap_plugin_factory_t* f, const clap_host_t* host, const char* id)
  268. {
  269. (void) f; (void) host;
  270. if (strcmp (id, descriptor.id) != 0) return NULL;
  271. clap_plugin_t* p = (clap_plugin_t*) calloc (1, sizeof (clap_plugin_t));
  272. p->desc = &descriptor;
  273. p->init = p_init; p->destroy = p_destroy; p->activate = p_activate; p->deactivate = p_deactivate;
  274. p->start_processing = p_start; p->stop_processing = p_stop; p->reset = p_reset; p->process = p_process;
  275. p->get_extension = p_extension; p->on_main_thread = p_main_thread;
  276. return p;
  277. }
  278. static const clap_plugin_factory_t factory = { f_count, f_descriptor, f_create };
  279. static bool e_init (const char* path) { (void) path; return true; }
  280. static void e_deinit (void) {}
  281. static const void* e_factory (const char* id) { return strcmp (id, CLAP_PLUGIN_FACTORY_ID) == 0 ? &factory : NULL; }
  282. CLAP_EXPORT const clap_plugin_entry_t clap_entry = { CLAP_VERSION_INIT, e_init, e_deinit, e_factory };
  283. #if defined(_WIN32)
  284. BOOL WINAPI DllMain (HINSTANCE instance, DWORD reason, LPVOID reserved)
  285. {
  286. (void) instance;
  287. if (reason == DLL_PROCESS_DETACH && reserved != NULL && crash_mode ("exit")) /* the process is exiting */
  288. __fastfail (7);
  289. return TRUE;
  290. }
  291. #endif