|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal.h>
|
| #include <eh.h>
|
| #include <process.h>
|
|
|
| static long c_termination_complete = FALSE;
|
|
|
| extern "C" extern _onexit_table_t __acrt_atexit_table;
|
| extern "C" extern _onexit_table_t __acrt_at_quick_exit_table;
|
|
|
|
|
|
|
|
|
|
|
| static _tls_callback_type thread_local_exit_callback_func;
|
|
|
|
|
|
|
|
|
| #if !defined CRTDLL && defined _DEBUG
|
| extern "C" bool __cdecl __scrt_uninitialize_crt(bool is_terminating, bool from_exit);
|
| #endif
|
|
|
|
|
|
|
| #ifdef _UCRT_ENCLAVE_BUILD
|
|
|
| static bool __cdecl is_managed_app() throw() { return false; }
|
|
|
| static void __cdecl try_cor_exit_process(UINT const) throw() { }
|
|
|
|
|
| static void __cdecl exit_or_terminate_process(UINT const return_code, bool) throw()
|
| {
|
| TerminateProcess(GetCurrentProcess(), return_code);
|
| }
|
|
|
| static bool __cdecl should_call_terminate_process() throw() { return true; }
|
|
|
| #else
|
|
|
| typedef void (WINAPI* exit_process_pft)(UINT);
|
|
|
|
|
| static bool __cdecl is_managed_app() throw()
|
| {
|
| PIMAGE_DOS_HEADER const dos_header = reinterpret_cast<PIMAGE_DOS_HEADER>(GetModuleHandleW(nullptr));
|
| if (dos_header == nullptr)
|
| return false;
|
|
|
| if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
|
| return false;
|
|
|
| PIMAGE_NT_HEADERS const pe_header = reinterpret_cast<PIMAGE_NT_HEADERS>(
|
| reinterpret_cast<BYTE*>(dos_header) + dos_header->e_lfanew);
|
|
|
| if (pe_header->Signature != IMAGE_NT_SIGNATURE)
|
| return false;
|
|
|
| if (pe_header->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC)
|
| return false;
|
|
|
|
|
| #pragma warning(push)
|
| #pragma warning(disable: 26000)
|
|
|
| if (pe_header->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR)
|
| return false;
|
|
|
| #pragma warning(pop)
|
|
|
| if (pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress == 0)
|
| return false;
|
|
|
| return true;
|
| }
|
|
|
| static void __cdecl try_cor_exit_process(UINT const return_code) throw()
|
| {
|
| __crt_unique_hmodule mscoree;
|
| if (!GetModuleHandleExW(0, L"mscoree.dll", mscoree.get_address_of()))
|
| return;
|
|
|
| auto const cor_exit_process = __crt_get_proc_address<exit_process_pft>(mscoree.get(), "CorExitProcess");
|
| if (!cor_exit_process)
|
| return;
|
|
|
| cor_exit_process(return_code);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| static bool __cdecl should_call_terminate_process() throw()
|
| {
|
| if (__acrt_get_process_end_policy() == process_end_policy_exit_process)
|
| {
|
| return false;
|
| }
|
|
|
|
|
|
|
|
|
| if (__acrt_app_verifier_enabled())
|
| {
|
| return false;
|
| }
|
|
|
| return true;
|
| }
|
|
|
|
|
|
|
|
|
| static void __cdecl exit_or_terminate_process(
|
| UINT const return_code,
|
| bool should_call_terminate_proc) throw()
|
| {
|
| if (should_call_terminate_proc)
|
| {
|
| TerminateProcess(GetCurrentProcess(), return_code);
|
| }
|
|
|
| try_cor_exit_process(return_code);
|
|
|
|
|
|
|
| ExitProcess(return_code);
|
| }
|
|
|
| #endif
|
|
|
|
|
| static int __cdecl atexit_exception_filter(unsigned long const _exception_code) throw()
|
| {
|
| if (_exception_code == ('msc' | 0xE0000000))
|
| {
|
| return EXCEPTION_EXECUTE_HANDLER;
|
| }
|
|
|
| return EXCEPTION_CONTINUE_SEARCH;
|
| }
|
|
|
|
|
|
|
| extern "C" void __cdecl __acrt_initialize_thread_local_exit_callback(void * encoded_null)
|
| {
|
| thread_local_exit_callback_func = reinterpret_cast<_tls_callback_type>(encoded_null);
|
| }
|
|
|
|
|
|
|
| extern "C" void __cdecl _register_thread_local_exe_atexit_callback(_In_ _tls_callback_type const _Callback)
|
| {
|
|
|
| if (thread_local_exit_callback_func != __crt_fast_encode_pointer(nullptr))
|
| {
|
| terminate();
|
| }
|
|
|
| thread_local_exit_callback_func = __crt_fast_encode_pointer(_Callback);
|
| }
|
|
|
|
|
|
|
| static void __cdecl common_exit(
|
| int const return_code,
|
| _crt_exit_cleanup_mode const cleanup_mode,
|
| _crt_exit_return_mode const return_mode
|
| ) throw()
|
| {
|
|
|
|
|
|
|
|
|
|
|
|
|
| if (return_mode == _crt_exit_terminate_process && is_managed_app())
|
| {
|
| try_cor_exit_process(return_code);
|
| }
|
|
|
|
|
| bool crt_uninitialization_required = false;
|
|
|
| __acrt_lock_and_call(__acrt_select_exit_lock(), [&]
|
| {
|
| static bool c_exit_complete = false;
|
| if (c_exit_complete)
|
| {
|
| return;
|
| }
|
|
|
| _InterlockedExchange(&c_termination_complete, TRUE);
|
|
|
| __try
|
| {
|
| if (cleanup_mode == _crt_exit_full_cleanup)
|
| {
|
|
|
|
|
|
|
|
|
|
|
|
|
| if (thread_local_exit_callback_func != __crt_fast_encode_pointer(nullptr))
|
| {
|
| (__crt_fast_decode_pointer(thread_local_exit_callback_func))(nullptr, DLL_PROCESS_DETACH, nullptr);
|
| }
|
|
|
| _execute_onexit_table(&__acrt_atexit_table);
|
| }
|
| else if (cleanup_mode == _crt_exit_quick_cleanup)
|
| {
|
| _execute_onexit_table(&__acrt_at_quick_exit_table);
|
| }
|
| }
|
| __except (atexit_exception_filter(GetExceptionCode()))
|
| {
|
| terminate();
|
| }
|
|
|
| #ifndef CRTDLL
|
|
|
|
|
|
|
|
|
| if (cleanup_mode == _crt_exit_full_cleanup)
|
| {
|
| _initterm(__xp_a, __xp_z);
|
| }
|
|
|
| _initterm(__xt_a, __xt_z);
|
| #endif
|
|
|
| if (return_mode == _crt_exit_terminate_process)
|
| {
|
| c_exit_complete = true;
|
| crt_uninitialization_required = true;
|
| }
|
| });
|
|
|
|
|
|
|
|
|
| bool should_call_terminate_proc = false;
|
| if (return_mode == _crt_exit_terminate_process)
|
| {
|
| should_call_terminate_proc = should_call_terminate_process();
|
| }
|
|
|
|
|
|
|
| if (crt_uninitialization_required)
|
| {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #if !CRTDLL && _DEBUG
|
| __scrt_uninitialize_crt(true, true);
|
| #endif
|
| }
|
|
|
| if (return_mode == _crt_exit_terminate_process)
|
| {
|
| exit_or_terminate_process(return_code, should_call_terminate_proc);
|
| }
|
| }
|
|
|
| extern "C" int __cdecl _is_c_termination_complete()
|
| {
|
| return static_cast<int>(__crt_interlocked_read(&c_termination_complete));
|
| }
|
|
|
|
|
|
|
| extern "C" void __cdecl exit(int const return_code)
|
| {
|
| common_exit(return_code, _crt_exit_full_cleanup, _crt_exit_terminate_process);
|
| }
|
|
|
| extern "C" void __cdecl _exit(int const return_code)
|
| {
|
| common_exit(return_code, _crt_exit_no_cleanup, _crt_exit_terminate_process);
|
| }
|
|
|
| extern "C" void __cdecl _Exit(int const return_code)
|
| {
|
| common_exit(return_code, _crt_exit_no_cleanup, _crt_exit_terminate_process);
|
| }
|
|
|
| extern "C" void __cdecl quick_exit(int const return_code)
|
| {
|
| common_exit(return_code, _crt_exit_quick_cleanup, _crt_exit_terminate_process);
|
| }
|
|
|
| extern "C" void __cdecl _cexit()
|
| {
|
| common_exit(0, _crt_exit_full_cleanup, _crt_exit_return_to_caller);
|
| }
|
|
|
| extern "C" void __cdecl _c_exit()
|
| {
|
| common_exit(0, _crt_exit_no_cleanup, _crt_exit_return_to_caller);
|
| }
|
|
|