Skip to content

Commit 92d479a

Browse files
committed
[VCRUNTIME] Add initialization / cleanup of wine code
1 parent 32c349d commit 92d479a

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

sdk/lib/vcruntime/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ list(APPEND VCRT_COMMON_SOURCES
2020
__report_gsfailure.c
2121
__report_rangecheckfailure.c
2222
__security_init_cookie.c
23-
__vcrt_init.c
2423
_fltused.c
2524
initializers.cpp
2625
isa_available.cpp
@@ -29,11 +28,13 @@ list(APPEND VCRT_COMMON_SOURCES
2928

3029
list(APPEND VCRT_RUNTIME_SOURCES
3130
__std_terminate.c
31+
__vcrt_init.c
3232
)
3333

3434
list(APPEND VCRT_STARTUP_SOURCES
3535
__acrt_initialize_stub.cpp
3636
__scrt_uninitialize_crt.cpp
37+
__vcrt_init_stubs.c
3738
mainCRTStartup.cpp
3839
wmainCRTStartup.cpp
3940
)

sdk/lib/vcruntime/__vcrt_init.c

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,64 @@
88
// SPDX-License-Identifier: MIT
99
//
1010

11-
#include <vcruntime_startup.h>
11+
#include <internal_shared.h>
12+
13+
void msvcrt_init_exception(HINSTANCE hinstDLL);
14+
BOOL msvcrt_init_tls(void);
15+
void msvcrt_free_tls_mem(void);
16+
BOOL msvcrt_free_tls(void);
1217

1318
__vcrt_bool __cdecl __vcrt_initialize(void)
1419
{
15-
return 1;
20+
msvcrt_init_exception(GetModuleHandle(NULL));
21+
22+
if (!msvcrt_init_tls())
23+
return FALSE;
24+
25+
return TRUE;
1626
}
1727

1828
__vcrt_bool __cdecl __vcrt_uninitialize(_In_ __vcrt_bool _Terminating)
1929
{
20-
return 1;
30+
if (!msvcrt_free_tls())
31+
return FALSE;
32+
33+
return TRUE;
2134
}
2235

2336
__vcrt_bool __cdecl __vcrt_uninitialize_critical(void)
2437
{
25-
return 1;
38+
return TRUE;
2639
}
2740

2841
__vcrt_bool __cdecl __vcrt_thread_attach(void)
2942
{
30-
return 1;
43+
// Not called by UCRT
44+
return TRUE;
3145
}
3246

3347
__vcrt_bool __cdecl __vcrt_thread_detach(void)
3448
{
35-
return 1;
49+
// Not called by UCRT
50+
return TRUE;
3651
}
52+
53+
// UCRT doesn't have a thread detach callback for the vcruntime TLS, because
54+
// the native vcruntime uses FlsAlloc and provides a cleanup callback there.
55+
// Since we don't have that, we use TLS callbacks.
56+
const IMAGE_TLS_DIRECTORY* __p_tls_used = &_tls_used;
57+
58+
static
59+
VOID
60+
WINAPI
61+
wine_tls_cleanup_callback(PVOID hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
62+
{
63+
// For the last thread, only DLL_PROCESS_DETACH is called
64+
if ((fdwReason == DLL_THREAD_DETACH) ||
65+
(fdwReason == DLL_PROCESS_DETACH))
66+
{
67+
msvcrt_free_tls_mem();
68+
}
69+
}
70+
71+
_CRTALLOC(".CRT$XLD") PIMAGE_TLS_CALLBACK wine_tls_cleanup_ptr = wine_tls_cleanup_callback;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// __vcrt_init_stubs.c
3+
//
4+
// Copyright (c) 2024 Timo Kreuzer
5+
//
6+
// Stubs for vcruntime initialization and termination in vcstartup.
7+
//
8+
// SPDX-License-Identifier: MIT
9+
//
10+
11+
#include <vcruntime_startup.h>
12+
13+
__vcrt_bool __cdecl __vcrt_initialize(void)
14+
{
15+
return 1;
16+
}
17+
18+
__vcrt_bool __cdecl __vcrt_uninitialize(_In_ __vcrt_bool _Terminating)
19+
{
20+
return 1;
21+
}
22+
23+
__vcrt_bool __cdecl __vcrt_uninitialize_critical(void)
24+
{
25+
return 1;
26+
}
27+
28+
__vcrt_bool __cdecl __vcrt_thread_attach(void)
29+
{
30+
return 1;
31+
}
32+
33+
__vcrt_bool __cdecl __vcrt_thread_detach(void)
34+
{
35+
return 1;
36+
}

0 commit comments

Comments
 (0)