JIT-ed异常处理程序实现

use*_*923 5 .net clr assembly il exception-handling

考虑以下IL代码:

    .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
    }
Run Code Online (Sandbox Code Playgroud)

CLR如何try在JIT编码中定义块?本机代码看起来如下:

...
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
Run Code Online (Sandbox Code Playgroud)

我们可以看到块clr!JIT_EndCatch的开始和结束在try哪里?

Han*_*ant 4

抖动生成的不仅仅是您可以使用调试器轻松查看的机器代码。您需要阅读这个答案,它讨论了抖动生成的用于协助垃圾收集器的表。

这与异常处理的工作方式非常相似,抖动生成 SafeSEH 实现使用的函数表,让操作系统发现异常过滤器。这样的表具有用于 try 块的起始和结束地址的条目以及用于过滤器的函数指针。它的确切工作方式严重缺乏记录,过去异常处理已被恶意软件大量利用,而谷歌对“safeseh”的点击并不是我想在这里重复的内容。MSDN 文章中关于汇编器选项的内容有一些粗略的信息。我不知道使用调试器检查这些表的简单方法。