Skip to content

Commit

Permalink
fix crash on windows in idealvin#348
Browse files Browse the repository at this point in the history
  • Loading branch information
idealvin committed Oct 30, 2023
1 parent d9cece6 commit b0c2dfc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/co/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ void hook_sleep(bool x);
#define __sys_api(x) _sys_##x
#define _CO_DEC_SYS_API(x) extern x##_fp_t __sys_api(x)

struct HookInitializer {
HookInitializer();
~HookInitializer();
};

static HookInitializer g_hook_initializer;

#ifdef _WIN32
#include <WinSock2.h>
#include <ws2tcpip.h> // for inet_ntop...
Expand Down Expand Up @@ -116,13 +123,6 @@ _CO_DEC_SYS_API(GetQueuedCompletionStatusEx);

#define _CO_DEF_SYS_API(x) x##_fp_t __sys_api(x) = 0

struct HookInitializer {
HookInitializer();
~HookInitializer();
};

static HookInitializer g_hook_initializer;

namespace co {

// deduce type of the second parameter of ioctl
Expand Down
10 changes: 9 additions & 1 deletion src/co/hook_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,6 @@ inline void detour_detach(PVOID* ppbReal, PVOID pbMine, PCHAR psz) {

// it will be called at initialization of coroutine schedulers
void init_hook() {
g_hook = co::_make_static<co::Hook>();
__sys_api(WSARecvMsg) = get_WSARecvMsg_fp();
__sys_api(WSASendMsg) = get_WSASendMsg_fp();

Expand Down Expand Up @@ -1458,6 +1457,15 @@ void hook_sleep(bool x) {

} // co

static int g_nifty_counter;
HookInitializer::HookInitializer() {
if (g_nifty_counter++ == 0) {
g_hook = co::_make_static<co::Hook>();
}
}

HookInitializer::~HookInitializer() {}

#undef attach_hook
#undef detach_hook
#undef do_hard_hook
Expand Down

0 comments on commit b0c2dfc

Please sign in to comment.