Skip to content

Commit 5637470

Browse files
authored
Make targets changes linear (#1388)
Make targets changes linear
1 parent 2b8a456 commit 5637470

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

Diff for: src/coverlet.core/Instrumentation/Instrumenter.cs

+24-25
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ private void InstrumentIL(MethodDefinition method)
572572
int index = 0;
573573
int count = processor.Body.Instructions.Count;
574574
IReadOnlyList<BranchPoint> branchPoints = _cecilSymbolHelper.GetBranchPoints(method);
575+
IDictionary<int, Instruction> targetsMap = new Dictionary<int, Instruction>();
575576
System.Collections.Immutable.ImmutableArray<ReachabilityHelper.UnreachableRange> unreachableRanges = _reachabilityHelper.FindUnreachableIL(processor.Body.Instructions, processor.Body.ExceptionHandlers);
576577
int currentUnreachableRangeIx = 0;
577578
for (int n = 0; n < count; n++)
@@ -611,11 +612,7 @@ private void InstrumentIL(MethodDefinition method)
611612
}
612613

613614
Instruction firstInjectedInstrumentedOpCode = AddInstrumentationCode(method, processor, currentInstruction, sequencePoint);
614-
foreach (Instruction bodyInstruction in processor.Body.Instructions)
615-
ReplaceInstructionTarget(bodyInstruction, currentInstruction, firstInjectedInstrumentedOpCode);
616-
617-
foreach (ExceptionHandler handler in processor.Body.ExceptionHandlers)
618-
ReplaceExceptionHandlerBoundary(handler, currentInstruction, firstInjectedInstrumentedOpCode);
615+
targetsMap.Add(currentInstruction.Offset, firstInjectedInstrumentedOpCode);
619616

620617
index += 2;
621618
}
@@ -632,18 +629,21 @@ private void InstrumentIL(MethodDefinition method)
632629
continue;
633630

634631
Instruction firstInjectedInstrumentedOpCode = AddInstrumentationCode(method, processor, currentInstruction, branchTarget);
635-
foreach (Instruction bodyInstruction in processor.Body.Instructions)
636-
ReplaceInstructionTarget(bodyInstruction, currentInstruction, firstInjectedInstrumentedOpCode);
637-
638-
foreach (ExceptionHandler handler in processor.Body.ExceptionHandlers)
639-
ReplaceExceptionHandlerBoundary(handler, currentInstruction, firstInjectedInstrumentedOpCode);
632+
if (!targetsMap.ContainsKey(currentInstruction.Offset))
633+
targetsMap.Add(currentInstruction.Offset, firstInjectedInstrumentedOpCode);
640634

641635
index += 2;
642636
}
643637

644638
index++;
645639
}
646640

641+
foreach (Instruction bodyInstruction in processor.Body.Instructions)
642+
ReplaceInstructionTarget(bodyInstruction, targetsMap);
643+
644+
foreach (ExceptionHandler handler in processor.Body.ExceptionHandlers)
645+
ReplaceExceptionHandlerBoundary(handler, targetsMap);
646+
647647
method.Body.OptimizeMacros();
648648
}
649649

@@ -744,42 +744,41 @@ private Instruction AddInstrumentationInstructions(MethodDefinition method, ILPr
744744
return indxInstr;
745745
}
746746

747-
private static void ReplaceInstructionTarget(Instruction instruction, Instruction oldTarget, Instruction newTarget)
747+
private static void ReplaceInstructionTarget(Instruction instruction, IDictionary<int, Instruction> targetsMap)
748748
{
749749
if (instruction.Operand is Instruction operandInstruction)
750750
{
751-
if (operandInstruction == oldTarget)
751+
if (targetsMap.TryGetValue(operandInstruction.Offset, out Instruction newTarget))
752752
{
753753
instruction.Operand = newTarget;
754-
return;
755754
}
756755
}
757756
else if (instruction.Operand is Instruction[] operandInstructions)
758757
{
759758
for (int i = 0; i < operandInstructions.Length; i++)
760759
{
761-
if (operandInstructions[i] == oldTarget)
760+
if (targetsMap.TryGetValue(operandInstructions[i].Offset, out Instruction newTarget))
762761
operandInstructions[i] = newTarget;
763762
}
764763
}
765764
}
766765

767-
private static void ReplaceExceptionHandlerBoundary(ExceptionHandler handler, Instruction oldTarget, Instruction newTarget)
766+
private static void ReplaceExceptionHandlerBoundary(ExceptionHandler handler, IDictionary<int, Instruction> targetsMap)
768767
{
769-
if (handler.FilterStart == oldTarget)
770-
handler.FilterStart = newTarget;
768+
if (handler.FilterStart is not null && targetsMap.TryGetValue(handler.FilterStart.Offset, out Instruction newFilterStart))
769+
handler.FilterStart = newFilterStart;
771770

772-
if (handler.HandlerEnd == oldTarget)
773-
handler.HandlerEnd = newTarget;
771+
if (handler.HandlerEnd is not null && targetsMap.TryGetValue(handler.HandlerEnd.Offset, out Instruction newHandlerEnd))
772+
handler.HandlerEnd = newHandlerEnd;
774773

775-
if (handler.HandlerStart == oldTarget)
776-
handler.HandlerStart = newTarget;
774+
if (handler.HandlerStart is not null && targetsMap.TryGetValue(handler.HandlerStart.Offset, out Instruction newHandlerStart))
775+
handler.HandlerStart = newHandlerStart;
777776

778-
if (handler.TryEnd == oldTarget)
779-
handler.TryEnd = newTarget;
777+
if (handler.TryEnd is not null && targetsMap.TryGetValue(handler.TryEnd.Offset, out Instruction newTryEnd))
778+
handler.TryEnd = newTryEnd;
780779

781-
if (handler.TryStart == oldTarget)
782-
handler.TryStart = newTarget;
780+
if (handler.TryStart is not null && targetsMap.TryGetValue(handler.TryStart.Offset, out Instruction newTryStart))
781+
handler.TryStart = newTryStart;
783782
}
784783

785784
private bool IsExcludeAttribute(CustomAttribute customAttribute)

0 commit comments

Comments
 (0)