5

I am working on an IL-rewriting profiler and my goal is to be able to add a try-finally block to methods. Essentially:

// IL to set some state 
try {
   // original IL 
} finally { 
   // IL to revert state
}

Based on the limited documentation and info for the profiling API (https://msdn.microsoft.com/en-us/library/ms232096.aspx), it appears one should be able use SetILFunctionBody to add new exception handling clauses.

I've been following Microsoft's example ILRewrite profiler from http://clrprofiler.codeplex.com/SourceControl/list/changesets?branch=master. I've added code to add an "EHClause" to the EHClause list maintained by the "ILRewriter" class and added the appropriate leave.s and endfinally IL instructions. Everything appears to work from the profiler's standpoint (SetILFunctionBody is successful) but when the modified method is invoked, we get the dreaded "Common Language Runtime detected an invalid program." exception with no further information.

Things I've tried:

  • Reviewed the instrumentation and the code is not doing things that are illegal in protected regions (e.g. return or branching outside).
  • The instrumented method runs fine if I remove the EHClause and leave.s/endfinally instructions.
  • I've added lots of logging to the ILRewriting code to dump the modified IL, EH info, and bytes at the end. I've made a similar method with the desired try-finally and state tracking code and the IL of the two methods (instrumented vs. compiled) is identical. However the actual "exported" bytes are a good bit different.

This leads me to believe that maybe adding a new exception-handling clause to a method without any to begin with simply isn't supported by the profiling API. I'd love to hear otherwise and any of your ideas for how to resolve this problem.


Here's some information from logging - * is the original 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 

And here's what just adding a nop looks like with the current profiler:

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 
valiano
  • 16,433
  • 7
  • 64
  • 79
Dan Fiedler
  • 161
  • 8

1 Answers1

3

The short answer is that yes, it is possible.

The long answer is that there are lots of requirements to keep in mind when adding a new EH clause. For example, adding the CorILMethod_MoreSects flag, converting tiny methods to fat methods, and there are also probably small changes needed to the method's IL. For example, adding leave and endfinally instructions and making sure the leave's target is valid and remember that leave clears the stack so you probably need a way to get the return value from inside the try block out to the return :)

Dan Fiedler
  • 161
  • 8