Skip to content

Commit

Permalink
Fix finally generation for AppContext.OnProcessExit (#1291)
Browse files Browse the repository at this point in the history
Fix finally generation for AppContext.OnProcessExit
  • Loading branch information
MarcoRossignoli committed Feb 5, 2022
1 parent c8eafe5 commit 83b5b35
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/coverlet.core/Instrumentation/Instrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,22 @@ private void InstrumentModule()
onProcessExitIl.InsertAfter(lastInst, firstNullParam);
onProcessExitIl.InsertAfter(firstNullParam, secondNullParam);
onProcessExitIl.InsertAfter(secondNullParam, callUnload);
var endFinally = Instruction.Create(OpCodes.Endfinally);
onProcessExitIl.InsertAfter(callUnload, endFinally);
var ret = onProcessExitIl.Create(OpCodes.Ret);
var leave = onProcessExitIl.Create(OpCodes.Leave, ret);
onProcessExitIl.InsertAfter(callUnload, leave);
onProcessExitIl.InsertAfter(leave, ret);
var leaveAfterFinally = onProcessExitIl.Create(OpCodes.Leave, ret);
onProcessExitIl.InsertAfter(endFinally, ret);
foreach (var inst in onProcessExitMethod.Body.Instructions.ToArray())
{
// Patch ret to leave after the finally
if (inst.OpCode == OpCodes.Ret && inst != ret)
{
var leaveBodyInstAfterFinally = onProcessExitIl.Create(OpCodes.Leave, ret);
var prevInst = inst.Previous;
onProcessExitMethod.Body.Instructions.Remove(inst);
onProcessExitIl.InsertAfter(prevInst, leaveBodyInstAfterFinally);
}
}
var handler = new ExceptionHandler(ExceptionHandlerType.Finally)
{
TryStart = onProcessExitIl.Body.Instructions.First(),
Expand Down

0 comments on commit 83b5b35

Please sign in to comment.