【问题标题】:JIT-ed Exception handler implementationJIT-ed 异常处理程序实现
【发布时间】:2013-10-07 07:30:59
【问题描述】:

考虑以下 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
    }

CLR 如何在 JIT 代码中定义 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 块的开始和结束在哪里?

【问题讨论】:

    标签: .net assembly exception-handling clr il


    【解决方案1】:

    抖动产生的不仅仅是您可以通过调试器轻松查看的机器代码。你会想阅读this answer,它谈到了抖动生成的表以帮助垃圾收集器。

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

    【讨论】:

      猜你喜欢
      • 2020-12-21
      • 2012-04-12
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 2013-09-11
      • 1970-01-01
      相关资源
      最近更新 更多