使用.NET的ICorProfilerInfo::SetILFunctionBody,是否可以向没有任何异常处理的方法添加try-finally EH子句?

Dan*_*ler 5 .net c# profiler clr-profiling-api

我正在开发一个 IL 重写分析器,我的目标是能够向方法添加 try-finally 块。本质上:

// IL to set some state 
try {
   // original IL 
} finally { 
   // IL to revert state
}
Run Code Online (Sandbox Code Playgroud)

根据分析 API ( https://msdn.microsoft.com/en-us/library/ms232096.aspx ) 的有限文档和信息,似乎应该能够使用 SetILFunctionBody 添加新的异常处理子句。

我一直在关注来自http://clrprofiler.codeplex.com/SourceControl/list/changesets?branch=master的 Microsoft 示例 ILRewrite 探查器。我添加了代码以将“EHCLause”添加到“ILRewriter”类维护的 EHCLause 列表中,并添加了适当的 left.s 和 endfinally IL 指令。从探查器的角度来看,一切似乎都正常(SetILFunctionBody 成功),但是当调用修改后的方法时,我们会收到可怕的“公共语言运行时检测到无效程序”。例外,没有进一步的信息。

我尝试过的事情:

  • 检查了仪器,代码没有在保护区内做非法的事情(例如返回或分支到外部)。
  • 如果我删除 EHCLause 和 left.s/endfinally 指令,则仪表化方法运行良好。
  • 我在 ILRewriting 代码中添加了大量日志记录,以在末尾转储修改后的 IL、EH 信息和字节。我使用所需的 try-finally 和状态跟踪代码制作了类似的方法,并且两种方法(检测与编译)的 IL 是相同的。然而,实际的“导出”字节有很大不同。

这让我相信分析 API 可能不支持向没有任何异常处理子句的方法添加新的异常处理子句。我很想听听您的其他想法以及如何解决此问题的任何想法。


以下是来自日志记录的一些信息 - * 是原始 IL。

EXPORTING IL:
Offset IL   notes
0   0x28    call EnterScope
5   0x10e   stloc (store isInScope bool)
9   0x00    nop BeginTry
10  0x00    *
11  0x14    *
12  0x0a    *
13  0x02    *
14  0x28    *
19  0x0a    *
20  0x06    *
21  0x0b    *
22  0x2b    *
24  0x07    *
25  0xde    leave.s
27  0x10c   ldloc scope bool
31  0x39    brfalse (if not in scope then go to nop-at-endfinally)
36  0x28    call LeaveScope
41  0x00    nop-at-endfinally
42  0xdc    endfinally
43  0x2a    *

EXPORT EHClause count:1
EXPORT EHClause 0: ClassToken[0x0], ExceptionFilter[0x0] (null?:1), ExceptionFlags:[0x2]; TryBegin:[0x0]@9, TryEnd:[0x10C]@27, HandlerBegin:[0x10C]@27, HandlerEnd:[0xDC]@42
EXPORT EHClause -- using classToken because (clause->ExceptionFlags &       COR_ILEXCEPTION_CLAUSE_FILTER) == 0
Export EHClause -- has classToken [0x0] 0
ILWriter::Export. MaxStack:9, EHCount:1, InstructionCount:20, CodeSize:44, TotalSize:84, 
Method Bytes (84): 0x1b 0x30 0x09 0x00 0x2c 0x00 0x00 0x00 0x10 0x00 0x00 0x11 0x28 0x46 0x00 0x00 0x0a 0xfe 0x0e 0x02 0x00 0x00 0x00 0x14 0x0a 0x02 0x28 0x11 0x00 0x00 0x0a 0x0a 0x06 0x0b 0x2b 0x00 0x07 0xde 0x10 0xfe 0x0c 0x02 0x00 0x39 0x05 0x00 0x00 0x00 0x28 0x46 0x00 0x00 0x0a 0x00 0xdc 0x2a 0x41 0x1c 0x00 0x00 0x02 0x00 0x00 0x00 0x09 0x00 0x00 0x00 0x12 0x00 0x00 0x00 0x1b 0x00 0x00 0x00 0x10 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
Run Code Online (Sandbox Code Playgroud)

下面是在当前分析器中添加 nop 的样子:

ILWriter::Import finished. MaxStack:6, EHCount:0, InstructionCount:11, CodeSize:16, MethodSize:28
EXPORTING IL:
0   0x00    nop
1   0x00    
2   0x14    
3   0x0a    
4   0x02    
5   0x28    
10  0x0a    
11  0x06    
12  0x0b    
13  0x2b    
15  0x07    
16  0x2a    
ILWriter::Export. MaxStack:6, EHCount:0, InstructionCount:12, CodeSize:17, TotalSize:32, 
Method Bytes (32): 0x13 0x30 0x06 0x00 0x11 0x00 0x00 0x00 0x01 0x00 0x00 0x11 0x00 0x00 0x14 0x0a 0x02 0x28 0x11 0x00 0x00 0x0a 0x0a 0x06 0x0b 0x2b 0x00 0x07 0x2a 0x00 0x00 0x00 
Run Code Online (Sandbox Code Playgroud)

Dan*_*ler 3

简短的回答是,是的,这是可能的。

长的答案是,添加新的 EH 条款时需要牢记许多要求。例如,添加 CorILMethod_MoreSects 标志,将微小方法转换为胖方法,并且还可能需要对该方法的 IL 进行一些小的更改。例如,添加leave和endfinally指令并确保leave的目标有效,并记住leave会清除堆栈,因此您可能需要一种方法来从try块内部获取返回值到return:)