@@ -572,6 +572,7 @@ private void InstrumentIL(MethodDefinition method)
572
572
int index = 0 ;
573
573
int count = processor . Body . Instructions . Count ;
574
574
IReadOnlyList < BranchPoint > branchPoints = _cecilSymbolHelper . GetBranchPoints ( method ) ;
575
+ IDictionary < int , Instruction > targetsMap = new Dictionary < int , Instruction > ( ) ;
575
576
System . Collections . Immutable . ImmutableArray < ReachabilityHelper . UnreachableRange > unreachableRanges = _reachabilityHelper . FindUnreachableIL ( processor . Body . Instructions , processor . Body . ExceptionHandlers ) ;
576
577
int currentUnreachableRangeIx = 0 ;
577
578
for ( int n = 0 ; n < count ; n ++ )
@@ -611,11 +612,7 @@ private void InstrumentIL(MethodDefinition method)
611
612
}
612
613
613
614
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 ) ;
619
616
620
617
index += 2 ;
621
618
}
@@ -632,18 +629,21 @@ private void InstrumentIL(MethodDefinition method)
632
629
continue ;
633
630
634
631
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 ) ;
640
634
641
635
index += 2 ;
642
636
}
643
637
644
638
index ++ ;
645
639
}
646
640
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
+
647
647
method . Body . OptimizeMacros ( ) ;
648
648
}
649
649
@@ -744,42 +744,41 @@ private Instruction AddInstrumentationInstructions(MethodDefinition method, ILPr
744
744
return indxInstr ;
745
745
}
746
746
747
- private static void ReplaceInstructionTarget ( Instruction instruction , Instruction oldTarget , Instruction newTarget )
747
+ private static void ReplaceInstructionTarget ( Instruction instruction , IDictionary < int , Instruction > targetsMap )
748
748
{
749
749
if ( instruction . Operand is Instruction operandInstruction )
750
750
{
751
- if ( operandInstruction == oldTarget )
751
+ if ( targetsMap . TryGetValue ( operandInstruction . Offset , out Instruction newTarget ) )
752
752
{
753
753
instruction . Operand = newTarget ;
754
- return ;
755
754
}
756
755
}
757
756
else if ( instruction . Operand is Instruction [ ] operandInstructions )
758
757
{
759
758
for ( int i = 0 ; i < operandInstructions . Length ; i ++ )
760
759
{
761
- if ( operandInstructions [ i ] == oldTarget )
760
+ if ( targetsMap . TryGetValue ( operandInstructions [ i ] . Offset , out Instruction newTarget ) )
762
761
operandInstructions [ i ] = newTarget ;
763
762
}
764
763
}
765
764
}
766
765
767
- private static void ReplaceExceptionHandlerBoundary ( ExceptionHandler handler , Instruction oldTarget , Instruction newTarget )
766
+ private static void ReplaceExceptionHandlerBoundary ( ExceptionHandler handler , IDictionary < int , Instruction > targetsMap )
768
767
{
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 ;
771
770
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 ;
774
773
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 ;
777
776
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 ;
780
779
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 ;
783
782
}
784
783
785
784
private bool IsExcludeAttribute ( CustomAttribute customAttribute )
0 commit comments