https://git.reactos.org/?p=reactos.git;a=commitdiff;h=92d479a54b1960acf420a981fc6f4c7c87d7a09b
commit 92d479a54b1960acf420a981fc6f4c7c87d7a09b Author: Timo Kreuzer <timo.kreu...@reactos.org> AuthorDate: Wed Jan 29 13:15:16 2025 +0200 Commit: Timo Kreuzer <timo.kreu...@reactos.org> CommitDate: Thu Feb 6 09:17:37 2025 +0200 [VCRUNTIME] Add initialization / cleanup of wine code --- sdk/lib/vcruntime/CMakeLists.txt | 3 +- sdk/lib/vcruntime/__vcrt_init.c | 47 +++++++++++++++++++--- .../{__vcrt_init.c => __vcrt_init_stubs.c} | 4 +- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/sdk/lib/vcruntime/CMakeLists.txt b/sdk/lib/vcruntime/CMakeLists.txt index 633a1b7a466..405dc705bd9 100644 --- a/sdk/lib/vcruntime/CMakeLists.txt +++ b/sdk/lib/vcruntime/CMakeLists.txt @@ -20,7 +20,6 @@ list(APPEND VCRT_COMMON_SOURCES __report_gsfailure.c __report_rangecheckfailure.c __security_init_cookie.c - __vcrt_init.c _fltused.c initializers.cpp isa_available.cpp @@ -29,11 +28,13 @@ list(APPEND VCRT_COMMON_SOURCES list(APPEND VCRT_RUNTIME_SOURCES __std_terminate.c + __vcrt_init.c ) list(APPEND VCRT_STARTUP_SOURCES __acrt_initialize_stub.cpp __scrt_uninitialize_crt.cpp + __vcrt_init_stubs.c mainCRTStartup.cpp wmainCRTStartup.cpp ) diff --git a/sdk/lib/vcruntime/__vcrt_init.c b/sdk/lib/vcruntime/__vcrt_init.c index 45cce866e92..839c24de7e8 100644 --- a/sdk/lib/vcruntime/__vcrt_init.c +++ b/sdk/lib/vcruntime/__vcrt_init.c @@ -8,29 +8,64 @@ // SPDX-License-Identifier: MIT // -#include <vcruntime_startup.h> +#include <internal_shared.h> + +void msvcrt_init_exception(HINSTANCE hinstDLL); +BOOL msvcrt_init_tls(void); +void msvcrt_free_tls_mem(void); +BOOL msvcrt_free_tls(void); __vcrt_bool __cdecl __vcrt_initialize(void) { - return 1; + msvcrt_init_exception(GetModuleHandle(NULL)); + + if (!msvcrt_init_tls()) + return FALSE; + + return TRUE; } __vcrt_bool __cdecl __vcrt_uninitialize(_In_ __vcrt_bool _Terminating) { - return 1; + if (!msvcrt_free_tls()) + return FALSE; + + return TRUE; } __vcrt_bool __cdecl __vcrt_uninitialize_critical(void) { - return 1; + return TRUE; } __vcrt_bool __cdecl __vcrt_thread_attach(void) { - return 1; + // Not called by UCRT + return TRUE; } __vcrt_bool __cdecl __vcrt_thread_detach(void) { - return 1; + // Not called by UCRT + return TRUE; } + +// UCRT doesn't have a thread detach callback for the vcruntime TLS, because +// the native vcruntime uses FlsAlloc and provides a cleanup callback there. +// Since we don't have that, we use TLS callbacks. +const IMAGE_TLS_DIRECTORY* __p_tls_used = &_tls_used; + +static +VOID +WINAPI +wine_tls_cleanup_callback(PVOID hinstDLL, DWORD fdwReason, LPVOID lpvReserved) +{ + // For the last thread, only DLL_PROCESS_DETACH is called + if ((fdwReason == DLL_THREAD_DETACH) || + (fdwReason == DLL_PROCESS_DETACH)) + { + msvcrt_free_tls_mem(); + } +} + +_CRTALLOC(".CRT$XLD") PIMAGE_TLS_CALLBACK wine_tls_cleanup_ptr = wine_tls_cleanup_callback; diff --git a/sdk/lib/vcruntime/__vcrt_init.c b/sdk/lib/vcruntime/__vcrt_init_stubs.c similarity index 84% copy from sdk/lib/vcruntime/__vcrt_init.c copy to sdk/lib/vcruntime/__vcrt_init_stubs.c index 45cce866e92..3b01a3f119b 100644 --- a/sdk/lib/vcruntime/__vcrt_init.c +++ b/sdk/lib/vcruntime/__vcrt_init_stubs.c @@ -1,9 +1,9 @@ // -// __vcrt_init.c +// __vcrt_init_stubs.c // // Copyright (c) 2024 Timo Kreuzer // -// Implementation of vcruntime initialization and termination functions. +// Stubs for vcruntime initialization and termination in vcstartup. // // SPDX-License-Identifier: MIT //