default ctor.
one arg:int n ctor
func() called.
start of main.
Before the main function is called,IO initialization was completed.
a and b is right. b.n = 1
Heap initialization was completed too.
argc and argv's value is right,too.argc = 3,argv[0]=a,argv[1]=at,argv[2]=1121Input initialization was completed!n=121!str + to_string(n) = aa121end of main.func2() called.dtor.dtor.
你看,在 start of main 之前和 end of main 之后都有输出,而在 start of main 之前,其实 IO 初始化已经完成(可以用 printf,scanf,cin,cout),a,b 初始化完成(b.n=1),argc 和 argv 被正确传入,str 被正确构造。而在 main 之后,调用了 str,a 和 b 的析构函数,还调用了由 atexit 函数注册的 func2 函数。
所以,铁证如山,程序真的不是从 main 函数开始执行的!
如果你按 F11 无法跳到 exe_common.inl,而是程序直接停止了:
用这个:
C++
int main()
{
int* p = new int;
for (int* q = p - 1000; q <= p + 1000; q++)
{
*q = 123123123;
}
return 0;
}
原理大致是越界访问内存但又在程序自己的内存空间中,导致 CRT 的部分数据损坏。
然后会这样:
然后在第 289 行设置断点,main 函数结束后断点就会被触发。
所以,CRT 在 main 函数调用前/结束后,做了什么?
我们注意到关键的函数是 __scrt_common_main_seh,所以我们仔细看看它。
C++
static __declspec(noinline) int __cdecl __scrt_common_main_seh()
{
if (!__scrt_initialize_crt(__scrt_module_type::exe))
__scrt_fastfail(FAST_FAIL_FATAL_APP_EXIT);
bool has_cctor = false;
__try
{
bool const is_nested = __scrt_acquire_startup_lock();
if (__scrt_current_native_startup_state == __scrt_native_startup_state::initializing)
{
__scrt_fastfail(FAST_FAIL_FATAL_APP_EXIT);
}
else if (__scrt_current_native_startup_state == __scrt_native_startup_state::uninitialized)
{
__scrt_current_native_startup_state = __scrt_native_startup_state::initializing;
if (_initterm_e(__xi_a, __xi_z) != 0)
return 255;
_initterm(__xc_a, __xc_z);
__scrt_current_native_startup_state = __scrt_native_startup_state::initialized;
}
else
{
has_cctor = true;
}
__scrt_release_startup_lock(is_nested);
// If this module has any dynamically initialized __declspec(thread) // variables, then we invoke their initialization for the primary thread // used to start the process:
_tls_callback_type const* const tls_init_callback = __scrt_get_dyn_tls_init_callback();
if (*tls_init_callback != nullptr && __scrt_is_nonwritable_in_current_image(tls_init_callback))
{
(*tls_init_callback)(nullptr, DLL_THREAD_ATTACH, nullptr);
}
// If this module has any thread-local destructors, register the // callback function with the Unified CRT to run on exit.
_tls_callback_type const * const tls_dtor_callback = __scrt_get_dyn_tls_dtor_callback();
if (*tls_dtor_callback != nullptr && __scrt_is_nonwritable_in_current_image(tls_dtor_callback))
{
_register_thread_local_exe_atexit_callback(*tls_dtor_callback);
}
// // Initialization is complete; invoke main... //int const main_result = invoke_main();
// // main has returned; exit somehow... //if (!__scrt_is_managed_app())
exit(main_result);
if (!has_cctor)
_cexit();
// Finally, we terminate the CRT:__scrt_uninitialize_crt(true, false);
return main_result;
}
__except (_seh_filter_exe(GetExceptionCode(), GetExceptionInformation()))
{
// Note: We should never reach this except clause.int const main_result = GetExceptionCode();
if (!__scrt_is_managed_app())
_exit(main_result);
if (!has_cctor)
_c_exit();
return main_result;
}
}
初始化 CRT
C++
if (!__scrt_initialize_crt(__scrt_module_type::exe))
__scrt_fastfail(FAST_FAIL_FATAL_APP_EXIT);
可以看出,这里如果初始化未成功,程序就会直接退出。
而 __scrt_initialize_crt 函数,源代码我就不放了,说一下主要执行流程:
参数代表以什么方式初始化 CRT(这里是 exe,即可执行文件)
调用 __isa_available_init,用途不明,但是可以猜出用于初始化。
这里有个注释:// Notify the CRT components of the process attach, bottom-to-top,意思大概是以自下往上的方式继续初始化 CRT。
// If this module has any dynamically initialized __declspec(thread)// variables, then we invoke their initialization for the primary thread// used to start the process:
_tls_callback_type const* const tls_init_callback = __scrt_get_dyn_tls_init_callback();
if (*tls_init_callback != nullptr && __scrt_is_nonwritable_in_current_image(tls_init_callback))
{
(*tls_init_callback)(nullptr, DLL_THREAD_ATTACH, nullptr);
}
// If this module has any thread-local destructors, register the// callback function with the Unified CRT to run on exit.
_tls_callback_type const * const tls_dtor_callback = __scrt_get_dyn_tls_dtor_callback();
if (*tls_dtor_callback != nullptr && __scrt_is_nonwritable_in_current_image(tls_dtor_callback))
{
_register_thread_local_exe_atexit_callback(*tls_dtor_callback);
}
这里有一个小细节:就是调用 main 函数使用的是 invoke_main,invoke_main 实际上是一个简单的不能再简单的函数:
C++
static int __cdecl invoke_main()
{
return main(__argc, __argv, _get_initial_narrow_environment());
}
这里给 main 函数传了三个参数,分别是我们熟悉的命令行参数个数和参数列表,还有一个环境变量。
收尾
C++
// // main has returned; exit somehow... //if (!__scrt_is_managed_app())
exit(main_result);
if (!has_cctor)
_cexit();
// Finally, we terminate the CRT:__scrt_uninitialize_crt(true, false);
return main_result;
// Note: We should never reach this except clause.int const main_result = GetExceptionCode();
if (!__scrt_is_managed_app())
_exit(main_result);
if (!has_cctor)
_c_exit();
return main_result;
// First, check to see if we're loaded in a managed app. If we are, try to // call CorExitProcess to let the CLR handle the process termination. If // the call to CorExitProcess is successful, then it will call back through // this function with a return mode of _crt_exit_return_to_caller, at which // point we will run the C termination routines. It will then terminate the // process itself and not return.if (return_mode == _crt_exit_terminate_process && is_managed_app())
{
try_cor_exit_process(return_code);
}
但是 is_managed_app() 返回 false,因为 is_managed_app():
C++
if (pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress == 0)
return false;
然后是:
C++
// Run the C termination:bool crt_uninitialization_required = false;