From 49595d2c3767ce2b5d98215ee8818859172de521 Mon Sep 17 00:00:00 2001 From: Tau Date: Sun, 23 May 2021 14:17:14 -0400 Subject: [PATCH] Fix hook startup failure path Returning from the EXE PE entry point merely terminates the main thread and leaves the process hanging. We need to explicitly exit from the process since the NTDLL RTL Start code won't do it for us. --- chunihook/dllmain.c | 12 +++++++----- divahook/dllmain.c | 12 +++++++----- idzhook/dllmain.c | 10 ++++++---- minihook/dllmain.c | 26 +++++++++++++++++++++++--- mu3hook/dllmain.c | 13 +++++++++---- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/chunihook/dllmain.c b/chunihook/dllmain.c index 1a7dc39..1cd3ebd 100644 --- a/chunihook/dllmain.c +++ b/chunihook/dllmain.c @@ -1,6 +1,5 @@ #include -#include #include #include "amex/amex.h" @@ -73,25 +72,25 @@ static DWORD CALLBACK chuni_pre_startup(void) chuni_hook_mod); if (FAILED(hr)) { - return EXIT_FAILURE; + goto fail; } hr = amex_hook_init(&chuni_hook_cfg.amex, chunithm_jvs_init); if (FAILED(hr)) { - return EXIT_FAILURE; + goto fail; } hr = slider_hook_init(&chuni_hook_cfg.slider); if (FAILED(hr)) { - return EXIT_FAILURE; + goto fail; } hr = sg_reader_hook_init(&chuni_hook_cfg.aime, 12); if (FAILED(hr)) { - return EXIT_FAILURE; + goto fail; } /* Initialize debug helpers */ @@ -103,6 +102,9 @@ static DWORD CALLBACK chuni_pre_startup(void) /* Jump to EXE start address */ return chuni_startup(); + +fail: + ExitProcess(EXIT_FAILURE); } BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx) diff --git a/divahook/dllmain.c b/divahook/dllmain.c index 1a6449a..a59a066 100644 --- a/divahook/dllmain.c +++ b/divahook/dllmain.c @@ -1,6 +1,5 @@ #include -#include #include #include "amex/amex.h" @@ -48,25 +47,25 @@ static DWORD CALLBACK diva_pre_startup(void) diva_hook_mod); if (FAILED(hr)) { - return EXIT_FAILURE; + goto fail; } hr = amex_hook_init(&diva_hook_cfg.amex, diva_jvs_init); if (FAILED(hr)) { - return EXIT_FAILURE; + goto fail; } hr = sg_reader_hook_init(&diva_hook_cfg.aime, 10); if (FAILED(hr)) { - return EXIT_FAILURE; + goto fail; } hr = slider_hook_init(&diva_hook_cfg.slider); if (FAILED(hr)) { - return EXIT_FAILURE; + goto fail; } /* Initialize debug helpers */ @@ -78,6 +77,9 @@ static DWORD CALLBACK diva_pre_startup(void) /* Jump to EXE start address */ return diva_startup(); + +fail: + ExitProcess(EXIT_FAILURE); } BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx) diff --git a/idzhook/dllmain.c b/idzhook/dllmain.c index a54e4bd..80ca7ef 100644 --- a/idzhook/dllmain.c +++ b/idzhook/dllmain.c @@ -1,6 +1,5 @@ #include -#include #include #include "amex/amex.h" @@ -49,19 +48,19 @@ static DWORD CALLBACK idz_pre_startup(void) idz_hook_mod); if (FAILED(hr)) { - return hr; + goto fail; } hr = amex_hook_init(&idz_hook_cfg.amex, idz_jvs_init); if (FAILED(hr)) { - return hr; + goto fail; } hr = sg_reader_hook_init(&idz_hook_cfg.aime, 10); if (FAILED(hr)) { - return hr; + goto fail; } /* Initialize debug helpers */ @@ -73,6 +72,9 @@ static DWORD CALLBACK idz_pre_startup(void) /* Jump to EXE start address */ return idz_startup(); + +fail: + ExitProcess(EXIT_FAILURE); } BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx) diff --git a/minihook/dllmain.c b/minihook/dllmain.c index 0c1512d..bdc45b8 100644 --- a/minihook/dllmain.c +++ b/minihook/dllmain.c @@ -1,5 +1,7 @@ #include +#include + #include "amex/config.h" #include "amex/ds.h" @@ -20,6 +22,7 @@ static DWORD CALLBACK app_pre_startup(void) struct clock_config clock_cfg; struct ds_config ds_cfg; struct nusec_config nusec_cfg; + HRESULT hr; dprintf("--- Begin %s ---\n", __func__); @@ -28,13 +31,30 @@ static DWORD CALLBACK app_pre_startup(void) nusec_config_load(&nusec_cfg, L".\\segatools.ini"); spike_hook_init(L".\\segatools.ini"); - clock_hook_init(&clock_cfg); - nusec_hook_init(&nusec_cfg, "SSSS", "AAV0"); - ds_hook_init(&ds_cfg); + hr = clock_hook_init(&clock_cfg); + + if (FAILED(hr)) { + goto fail; + } + + hr = nusec_hook_init(&nusec_cfg, "SSSS", "AAV0"); + + if (FAILED(hr)) { + goto fail; + } + + hr = ds_hook_init(&ds_cfg); + + if (FAILED(hr)) { + goto fail; + } dprintf("--- End %s ---\n", __func__); return app_startup(); + +fail: + ExitProcess(EXIT_FAILURE); } BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx) diff --git a/mu3hook/dllmain.c b/mu3hook/dllmain.c index 7161261..ebfff0b 100644 --- a/mu3hook/dllmain.c +++ b/mu3hook/dllmain.c @@ -1,5 +1,7 @@ #include +#include + #include "board/io4.h" #include "board/sg-reader.h" #include "board/vfd.h" @@ -45,25 +47,25 @@ static DWORD CALLBACK mu3_pre_startup(void) mu3_hook_mod); if (FAILED(hr)) { - return hr; + goto fail; } hr = sg_reader_hook_init(&mu3_hook_cfg.aime, 1); if (FAILED(hr)) { - return hr; + goto fail; } hr = vfd_hook_init(2); if (FAILED(hr)) { - return hr; + goto fail; } hr = mu3_io4_hook_init(); if (FAILED(hr)) { - return hr; + goto fail; } /* Initialize Unity native plugin DLL hooks @@ -82,6 +84,9 @@ static DWORD CALLBACK mu3_pre_startup(void) /* Jump to EXE start address */ return mu3_startup(); + +fail: + ExitProcess(EXIT_FAILURE); } BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx)