From 4a5bff8368f60506e24fa9832abedd7c99d65932 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Fri, 29 Mar 2019 11:37:27 +0100 Subject: [PATCH] msvcrt: Only add the onexit functions to the import library of msvcrt itself. This partially reverts 0673ddf3a55eb524f8284be3b2fc9ab62a2acf52. Signed-off-by: Alexandre Julliard --- dlls/msvcr100/Makefile.in | 1 - dlls/msvcr110/Makefile.in | 1 - dlls/msvcr120/Makefile.in | 1 - dlls/msvcr70/Makefile.in | 1 - dlls/msvcr71/Makefile.in | 1 - dlls/msvcr80/Makefile.in | 1 - dlls/msvcr90/Makefile.in | 1 - dlls/msvcrt/exit.c | 138 +++++++++++++++++++++++++++++++++--- dlls/ucrtbase/Makefile.in | 1 - dlls/ucrtbase/ucrtbase.spec | 12 ++-- 10 files changed, 135 insertions(+), 23 deletions(-) diff --git a/dlls/msvcr100/Makefile.in b/dlls/msvcr100/Makefile.in index 50182263a47..07a3ece3cbb 100644 --- a/dlls/msvcr100/Makefile.in +++ b/dlls/msvcr100/Makefile.in @@ -26,7 +26,6 @@ C_SRCS = \ math.c \ mbcs.c \ misc.c \ - onexit.c \ process.c \ scanf.c \ scheduler.c \ diff --git a/dlls/msvcr110/Makefile.in b/dlls/msvcr110/Makefile.in index d00d24e36b0..056eb3989f1 100644 --- a/dlls/msvcr110/Makefile.in +++ b/dlls/msvcr110/Makefile.in @@ -26,7 +26,6 @@ C_SRCS = \ math.c \ mbcs.c \ misc.c \ - onexit.c \ process.c \ scanf.c \ scheduler.c \ diff --git a/dlls/msvcr120/Makefile.in b/dlls/msvcr120/Makefile.in index 7f32a175d01..1ee05d9a1be 100644 --- a/dlls/msvcr120/Makefile.in +++ b/dlls/msvcr120/Makefile.in @@ -26,7 +26,6 @@ C_SRCS = \ math.c \ mbcs.c \ misc.c \ - onexit.c \ process.c \ scanf.c \ scheduler.c \ diff --git a/dlls/msvcr70/Makefile.in b/dlls/msvcr70/Makefile.in index f44f43787cb..e068b13c235 100644 --- a/dlls/msvcr70/Makefile.in +++ b/dlls/msvcr70/Makefile.in @@ -26,7 +26,6 @@ C_SRCS = \ math.c \ mbcs.c \ misc.c \ - onexit.c \ process.c \ scanf.c \ string.c \ diff --git a/dlls/msvcr71/Makefile.in b/dlls/msvcr71/Makefile.in index e2a86798ea7..58563b7f5cb 100644 --- a/dlls/msvcr71/Makefile.in +++ b/dlls/msvcr71/Makefile.in @@ -26,7 +26,6 @@ C_SRCS = \ math.c \ mbcs.c \ misc.c \ - onexit.c \ process.c \ scanf.c \ string.c \ diff --git a/dlls/msvcr80/Makefile.in b/dlls/msvcr80/Makefile.in index e9355091052..05b7a5b5af2 100644 --- a/dlls/msvcr80/Makefile.in +++ b/dlls/msvcr80/Makefile.in @@ -26,7 +26,6 @@ C_SRCS = \ math.c \ mbcs.c \ misc.c \ - onexit.c \ process.c \ scanf.c \ string.c \ diff --git a/dlls/msvcr90/Makefile.in b/dlls/msvcr90/Makefile.in index 550878f72e6..590b4b87075 100644 --- a/dlls/msvcr90/Makefile.in +++ b/dlls/msvcr90/Makefile.in @@ -26,7 +26,6 @@ C_SRCS = \ math.c \ mbcs.c \ misc.c \ - onexit.c \ process.c \ scanf.c \ string.c \ diff --git a/dlls/msvcrt/exit.c b/dlls/msvcrt/exit.c index 61112993bb9..2f344b6dab4 100644 --- a/dlls/msvcrt/exit.c +++ b/dlls/msvcrt/exit.c @@ -32,11 +32,19 @@ WINE_DEFAULT_DEBUG_CHANNEL(msvcrt); static MSVCRT_purecall_handler purecall_handler = NULL; static MSVCRT__onexit_table_t MSVCRT_atexit_table; -static MSVCRT__onexit_table_t MSVCRT_quick_exit_table; typedef void (__stdcall *_tls_callback_type)(void*,ULONG,void*); static _tls_callback_type tls_atexit_callback; +static CRITICAL_SECTION MSVCRT_onexit_cs; +static CRITICAL_SECTION_DEBUG MSVCRT_onexit_cs_debug = +{ + 0, 0, &MSVCRT_onexit_cs, + { &MSVCRT_onexit_cs_debug.ProcessLocksList, &MSVCRT_onexit_cs_debug.ProcessLocksList }, + 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_onexit_cs") } +}; +static CRITICAL_SECTION MSVCRT_onexit_cs = { &MSVCRT_onexit_cs_debug, -1, 0, 0, 0, 0 }; + extern int MSVCRT_app_type; extern MSVCRT_wchar_t *MSVCRT__wpgmptr; @@ -45,15 +53,93 @@ static int MSVCRT_error_mode = MSVCRT__OUT_TO_DEFAULT; void (*CDECL _aexit_rtn)(int) = MSVCRT__exit; -extern int CDECL _initialize_onexit_table(MSVCRT__onexit_table_t *table); -extern int CDECL _register_onexit_function(MSVCRT__onexit_table_t *table, MSVCRT__onexit_t func); -extern int CDECL _execute_onexit_table(MSVCRT__onexit_table_t *table); +static int initialize_onexit_table(MSVCRT__onexit_table_t *table) +{ + if (!table) + return -1; + + if (table->_first == table->_end) + table->_last = table->_end = table->_first = NULL; + return 0; +} + +static int register_onexit_function(MSVCRT__onexit_table_t *table, MSVCRT__onexit_t func) +{ + if (!table) + return -1; + + EnterCriticalSection(&MSVCRT_onexit_cs); + if (!table->_first) + { + table->_first = MSVCRT_calloc(32, sizeof(void *)); + if (!table->_first) + { + WARN("failed to allocate initial table.\n"); + LeaveCriticalSection(&MSVCRT_onexit_cs); + return -1; + } + table->_last = table->_first; + table->_end = table->_first + 32; + } + + /* grow if full */ + if (table->_last == table->_end) + { + int len = table->_end - table->_first; + MSVCRT__onexit_t *tmp = MSVCRT_realloc(table->_first, 2 * len * sizeof(void *)); + if (!tmp) + { + WARN("failed to grow table.\n"); + LeaveCriticalSection(&MSVCRT_onexit_cs); + return -1; + } + table->_first = tmp; + table->_end = table->_first + 2 * len; + table->_last = table->_first + len; + } + + *table->_last = func; + table->_last++; + LeaveCriticalSection(&MSVCRT_onexit_cs); + return 0; +} + +static int execute_onexit_table(MSVCRT__onexit_table_t *table) +{ + MSVCRT__onexit_t *func; + MSVCRT__onexit_table_t copy; + + if (!table) + return -1; + + EnterCriticalSection(&MSVCRT_onexit_cs); + if (!table->_first || table->_first >= table->_last) + { + LeaveCriticalSection(&MSVCRT_onexit_cs); + return 0; + } + copy._first = table->_first; + copy._last = table->_last; + copy._end = table->_end; + memset(table, 0, sizeof(*table)); + initialize_onexit_table(table); + LeaveCriticalSection(&MSVCRT_onexit_cs); + + for (func = copy._last - 1; func >= copy._first; func--) + { + if (*func) + (*func)(); + } + + MSVCRT_free(copy._first); + return 0; +} static void call_atexit(void) { /* Note: should only be called with the exit lock held */ if (tls_atexit_callback) tls_atexit_callback(NULL, DLL_PROCESS_DETACH, NULL); - _execute_onexit_table(&MSVCRT_atexit_table); + execute_onexit_table(&MSVCRT_atexit_table); } /********************************************************************* @@ -269,7 +355,7 @@ MSVCRT__onexit_t CDECL MSVCRT__onexit(MSVCRT__onexit_t func) return NULL; LOCK_EXIT; - _register_onexit_function(&MSVCRT_atexit_table, func); + register_onexit_function(&MSVCRT_atexit_table, func); UNLOCK_EXIT; return func; @@ -309,23 +395,26 @@ int CDECL MSVCRT_atexit(void (__cdecl *func)(void)) return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1; } +#if _MSVCR_VER >= 140 +static MSVCRT__onexit_table_t MSVCRT_quick_exit_table; + /********************************************************************* * _crt_at_quick_exit (UCRTBASE.@) */ int CDECL MSVCRT__crt_at_quick_exit(void (__cdecl *func)(void)) { TRACE("(%p)\n", func); - return _register_onexit_function(&MSVCRT_quick_exit_table, (MSVCRT__onexit_t)func); + return register_onexit_function(&MSVCRT_quick_exit_table, (MSVCRT__onexit_t)func); } /********************************************************************* - * quick_exit (MSVCRT.@) + * quick_exit (UCRTBASE.@) */ void CDECL MSVCRT_quick_exit(int exitcode) { TRACE("(%d)\n", exitcode); - _execute_onexit_table(&MSVCRT_quick_exit_table); + execute_onexit_table(&MSVCRT_quick_exit_table); MSVCRT__exit(exitcode); } @@ -338,6 +427,37 @@ int CDECL MSVCRT__crt_atexit(void (__cdecl *func)(void)) return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1; } +/********************************************************************* + * _initialize_onexit_table (UCRTBASE.@) + */ +int CDECL MSVCRT__initialize_onexit_table(MSVCRT__onexit_table_t *table) +{ + TRACE("(%p)\n", table); + + return initialize_onexit_table(table); +} + +/********************************************************************* + * _register_onexit_function (UCRTBASE.@) + */ +int CDECL MSVCRT__register_onexit_function(MSVCRT__onexit_table_t *table, MSVCRT__onexit_t func) +{ + TRACE("(%p %p)\n", table, func); + + return register_onexit_function(table, func); +} + +/********************************************************************* + * _execute_onexit_table (UCRTBASE.@) + */ +int CDECL MSVCRT__execute_onexit_table(MSVCRT__onexit_table_t *table) +{ + TRACE("(%p)\n", table); + + return execute_onexit_table(table); +} +#endif + /********************************************************************* * _register_thread_local_exe_atexit_callback (UCRTBASE.@) */ diff --git a/dlls/ucrtbase/Makefile.in b/dlls/ucrtbase/Makefile.in index c7cc63df25e..23d8292a535 100644 --- a/dlls/ucrtbase/Makefile.in +++ b/dlls/ucrtbase/Makefile.in @@ -26,7 +26,6 @@ C_SRCS = \ math.c \ mbcs.c \ misc.c \ - onexit.c \ process.c \ scanf.c \ string.c \ diff --git a/dlls/ucrtbase/ucrtbase.spec b/dlls/ucrtbase/ucrtbase.spec index 6dab5e7d4c5..544ebf8c926 100644 --- a/dlls/ucrtbase/ucrtbase.spec +++ b/dlls/ucrtbase/ucrtbase.spec @@ -279,7 +279,7 @@ @ varargs _execle(str str) @ varargs _execlp(str str) @ varargs _execlpe(str str) -@ cdecl _execute_onexit_table(ptr) +@ cdecl _execute_onexit_table(ptr) MSVCRT__execute_onexit_table @ cdecl _execv(str ptr) @ cdecl _execve(str ptr ptr) MSVCRT__execve @ cdecl _execvp(str ptr) @@ -419,7 +419,7 @@ @ cdecl _i64tow(int64 ptr long) ntdll._i64tow @ cdecl _i64tow_s(int64 ptr long long) MSVCRT__i64tow_s @ cdecl _initialize_narrow_environment() -@ cdecl _initialize_onexit_table(ptr) +@ cdecl _initialize_onexit_table(ptr) MSVCRT__initialize_onexit_table @ cdecl _initialize_wide_environment() @ cdecl _initterm(ptr ptr) @ cdecl _initterm_e(ptr ptr) @@ -906,7 +906,7 @@ @ stub _o__eof @ cdecl _o__errno() MSVCRT__errno @ stub _o__except1 -@ cdecl _o__execute_onexit_table(ptr) _execute_onexit_table +@ cdecl _o__execute_onexit_table(ptr) MSVCRT__execute_onexit_table @ stub _o__execv @ stub _o__execve @ stub _o__execvp @@ -1029,7 +1029,7 @@ @ stub _o__i64tow @ stub _o__i64tow_s @ stub _o__initialize_narrow_environment -@ cdecl _o__initialize_onexit_table(ptr) _initialize_onexit_table +@ cdecl _o__initialize_onexit_table(ptr) MSVCRT__initialize_onexit_table @ cdecl _o__initialize_wide_environment() _initialize_wide_environment @ stub _o__invalid_parameter_noinfo @ stub _o__invalid_parameter_noinfo_noreturn @@ -1316,7 +1316,7 @@ @ stub _o__read @ stub _o__realloc_base @ stub _o__recalloc -@ cdecl _o__register_onexit_function(ptr ptr) _register_onexit_function +@ cdecl _o__register_onexit_function(ptr ptr) MSVCRT__register_onexit_function @ stub _o__resetstkoflw @ stub _o__rmdir @ stub _o__rmtmp @@ -1861,7 +1861,7 @@ @ cdecl _read(long ptr long) MSVCRT__read @ cdecl _realloc_base(ptr long) @ cdecl _recalloc(ptr long long) -@ cdecl _register_onexit_function(ptr ptr) +@ cdecl _register_onexit_function(ptr ptr) MSVCRT__register_onexit_function @ cdecl _register_thread_local_exe_atexit_callback(ptr) @ cdecl _resetstkoflw() MSVCRT__resetstkoflw @ cdecl _rmdir(str) MSVCRT__rmdir