2013-10-01 46 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文章中有一些粗略的信息。我不知道用調試器檢查這些表的簡單方法。