专利协会网站建设方案,揭阳专业网站设计公司,网页设计模板html免费,网站排版工具楔子前面一篇研究了下C异常的#xff0c;这篇来看下#xff0c;CLR的异常内存模型#xff0c;实际上都是一个模型#xff0c;承继自windows异常处理机制。不同的是#xff0c;有VC编译器(vcruntime.dll#xff09;接管的部分#xff0c;被CLR里面的函数ProcessCLRExcept…楔子前面一篇研究了下C异常的这篇来看下CLR的异常内存模型实际上都是一个模型承继自windows异常处理机制。不同的是有VC编译器(vcruntime.dll接管的部分被CLR里面的函数ProcessCLRException接管了。注意这里面省略了一部分不必要赘述的细节问题版本号分别为(CLR PreView 7和vcruntime 14.0)C异常栈vcruntime140_1d.dll!__FrameHandler4::CxxCallCatchBlock
(jmp rdx)ntdll.dll!RcConsolidateFrames
ntdll.dll!RtlRestoreContext
ntdll.dll!RtlGuardRestoreContext
ntdll.dll!RtlUnwindEx
vcruntime140_1d.dll!__FrameHandler4::UnwindNestedFrames
vcruntime140_1d.dll!CatchIt
vcruntime140_1d.dll!FindHandler
vcruntime140_1d.dll!__InternalCxxFrameHandler
vcruntime140_1d.dll!__CxxFrameHandler4
ntdll.dll!RtlpExecuteHandlerForException()
ntdll.dll!RtlDispatchException
ntdll.dll!KiUserExceptionDispatch()
KernelBase.dll!RaiseException()
vcruntime140d.dll!_CxxThrowException
ConsoleApplication2.exe!mainCLR异常栈 coreclr.dll!ProcessCLRException Cntdll.dll!RtlpExecuteHandlerForExceptionntdll.dll!RtlDispatchExceptionntdll.dll!KiUserExceptionDispatchKernelBase.dll!RaiseExceptioncoreclr.dll!RaiseTheExceptionInternalOnly::53::__Body::Runcoreclr.dll!RaiseTheExceptionInternalOnlycoreclr.dll!IL_Throw00007ffa6faf040c()对比CLR异常栈的地址00007ffa6faf040c()实际上就是C#的main函数入口。对比的是C异常栈的函数入口main。其它的一一对应C# ----C1.IL_Throw-》_CxxThrowException2.RaiseTheExceptionInternalOnly和RaiseTheExceptionInternalOnly以及RaiseException-》RaiseException()3.KiUserExceptionDispatch-》KiUserExceptionDispatch()4.RtlDispatchException-》RtlDispatchException5.RtlpExecuteHandlerForException》RtlpExecuteHandlerForException6.ProcessCLRException -》__CxxFrameHandler4注意粗体部分对应的这个地方开始VC和CLR分道扬镳了。各自实现了后面函数异常处理的实现。下面是windows 异常楔子以win11 vs2022运行VC 编译观察的结果。如果安装了Visual Studio 2022,比如安装在D盘则路径D:\Visual Studio\IDE\VC\Tools\MSVC\14.33.31629下面包含了vcruntime.dll的源码主要VC编译器和ntdll.dll 以及KernelBase.dll交互。注本篇不叙述正常的windows用户态和内核态异常处理仅看用户态下偏角的运作方式。代码void main()
{char* pStr NULL;try{throw pStr;}catch (char* s){printf(Hello S);}getchar();
}try里面抛出一个异常异常调用堆栈如下分析红色箭头throw抛出异常之后调用了_CxxThrowException函数这个函数刚好在vcruntime.dll里面。_CxxThrowException函数源码在VS路径D:\Visual Studio\IDE\VC\Tools\MSVC\14.33.31629\crt\src\vcruntime\throw.cppextern C __declspec(noreturn) void __stdcall _CxxThrowException(void *pExceptionObject, // The object thrown_ThrowInfo *pThrowInfo // Everything we need to know about it
) {//为了方便观看此处省略一万字RaiseException(EH_EXCEPTION_NUMBER, EXCEPTION_NONCONTINUABLE, _countof(parameters), parameters);
}_CxxThrowException又调用了RaiseException函数。RaiseException函数会进入到内核里面分别调用如下ntdll.dll!KiUserExceptionDispatch-》
ntdll.dll!RtlDispatchException-》
ntdll.dll!RtlpExecuteHandlerForException-》windows异常分为内核态和用户态处理过程RtlpExecuteHandlerForException则刚好是用户态处理过程。这些过程过于复杂此处为了避免无端枝节不赘述。RtlpExecuteHandlerForException是调用异常处理的函数通俗点就是跳转到catch地址然后执行catch后面的代码。在VS2022里面异常处理函数是__CxxFrameHandler4此函数在vcruntime.dll里面源码在路径D:\Visual Studio\IDE\VC\Tools\MSVC\14.33.31629\crt\src\vcruntime\risctrnsctrl.cpp__CxxFrameHandler4后面的调用函数是__CxxFrameHandler4-》
vcruntime140_1d.dll!__InternalCxxFrameHandler-》
vcruntime140_1d.dll!FindHandler-》
vcruntime140_1d.dll!CatchIt-》
vcruntime140_1d.dll!__FrameHandler4::UnwindNestedFrames-》
ntdll.dll!RtlUnwindEx-》
ntdll.dll!RtlGuardRestoreContext-》
ntdll.dll!RtlRestoreContext-》
ntdll.dll!RtlpExecuteHandlerForUnwind-》
vcruntime140_1d.dll!__CxxFrameHandler4-》到了这里看似已经接近完成了但是实际上还远不止如此。如果再继续调用会直接跳到函数ntdll.dll!RcConsolidateFrames -》
vcruntime140_1d.dll!__FrameHandler4::CxxCallCatchBlock从__CxxFrameHandler4到RcConsolidateFrames经历什么会发现跟上面的对不上。堆栈也没有显示。为此还需要继续跟踪汇编为了能看到从__CxxFrameHandler4到RcConsolidateFrames经历什么我们跟踪下汇编__CxxFrameHandler4调用了RtlGuardRestoreContext继续单步F11,RtlGuardRestoreContext里面调用了函数RtlRestoreContextRtlRestoreContext里面有个跳转指令jmp rdx。看下图jmp指令调到了如下而call rax的rax就是CxxCallCatchBlock函数的指针。因为RcConsolidateFrames函数是在ntdll.dll里面没有被开源所以两次跳转jmp 和 call 应该是这个函数里面所做的动作如此一来就对上上面的那个函数调用顺序从上到下但是还有一个问题这个try里面抛出了异常那么catch是何时被执行的呢Catch理顺了RcConsolidateFrames函数调用顺序RcConsolidateFrames自己则调用了函数CxxCallCatchBlock。这个函数里面调用了catch处理异常。CxxCallCatchBlock函数源码地址D:\Visual Studio\IDE\VC\Tools\MSVC\14.33.31629\crt\src\vcruntime\frame.cpp1344行源码void * RENAME_EH_EXTERN(__FrameHandler4)::CxxCallCatchBlock(EXCEPTION_RECORD *pExcept)
{//为了方便观看此处省略一万行continuationAddress RENAME_EH_EXTERN(_CallSettingFrame_LookupContinuationIndex)
}RENAME_EH_EXTERN(_CallSettingFrame_LookupContinuationIndex)
这段的原型是注意的点CxxCallCatchBlock函数不会返回直接跳转到catch大括号下面的代码里面继续执行后面的代码段。void * RENAME_EH_EXTERN(__FrameHandler4)::CxxCallCatchBlock(EXCEPTION_RECORD *pExcept)
{//为了便于观察 此处省略一万字return continuationAddress;
}总结下堆栈的调用如下vcruntime140_1d.dll!__FrameHandler4::CxxCallCatchBlock(jmp rdx)ntdll.dll!RcConsolidateFramesntdll.dll!RtlRestoreContextntdll.dll!RtlGuardRestoreContext ntdll.dll!RtlUnwindEx vcruntime140_1d.dll!__FrameHandler4::UnwindNestedFramesvcruntime140_1d.dll!CatchItvcruntime140_1d.dll!FindHandlervcruntime140_1d.dll!__InternalCxxFrameHandlervcruntime140_1d.dll!__CxxFrameHandler4 ntdll.dll!RtlpExecuteHandlerForException() ntdll.dll!RtlDispatchExceptionntdll.dll!KiUserExceptionDispatch()KernelBase.dll!RaiseException()vcruntime140d.dll!_CxxThrowExceptionConsoleApplication2.exe!main作者江湖评谈版权本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。