2013-10-01 44 views
5

考虑下面的IL代码:JIT-ED异常处理程序实现

.method public static void Main() 
    { 
     ldstr "Starts Here" 
     call void [mscorlib] System.Console::WriteLine(string) 
     .try {  
      ldstr "Try Me!" 
      call void [mscorlib] System.Console::WriteLine(string) 
      leave.s Done 
     } 
     catch [mscorlib] System.Exception { 
      ldstr "Catch Me!" 
      call void [mscorlib] System.Console::WriteLine(string) 
      leave.s Done 
     } 
    Done: 
     ldstr "Ends Here" 
     call void [mscorlib] System.Console::WriteLine(string) 
     ret 
    } 

如何CLR定义JIT-ED代码try块?本机代码看起来方式如下:

... 
00900076 8b0538214703 mov  eax,dword ptr ds:[3472138h] ("Starts Here") 
... 

00900090 8b053c214703 mov  eax,dword ptr ds:[347213Ch] ("Try Me!") 
... 

009000a2 eb1b   jmp  009000bf ;// Done 

009000a4 8945d4   mov  dword ptr [ebp-2Ch],eax 
009000a7 8b0540214703 mov  eax,dword ptr ds:[3472140h] ("Catch Me!") 
... 

009000b8 e888293b73  call clr!JIT_EndCatch (73cb2a45) 
009000bd eb00   jmp  009000bf ;// Done 

;// Done: 
009000bf 8b0544214703 mov  eax,dword ptr ds:[3472144h] ("Ends Here") 
... 
009000d6 c3    ret 

我们可以看到clr!JIT_EndCatch但如果是开始和try块的结束?

回答

4

抖动产生的不仅仅是您可以通过调试器轻松看到的机器代码。您需要阅读this answer,它会介绍抖动产生的表格以协助垃圾收集器。

这种工作方式与异常处理方式非常相似,抖动会生成SafeSEH实现使用的函数表,让操作系统发现异常过滤器。这样的表具有用于尝试块的开始和结束地址以及过滤器的函数指针的条目。它的确切工作方式大量记录不足,过去的异常处理已被恶意软件严重利用,而google的“safeseh”命中不是我想在此重复的任何内容。关于assembler's option的MSDN文章中有一些粗略的信息。我不知道用调试器检查这些表的简单方法。