-
Notifications
You must be signed in to change notification settings - Fork 386
/
CecilSymbolHelper.cs
1486 lines (1302 loc) · 60.8 KB
/
CecilSymbolHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Toni Solarin-Sodara
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Coverlet.Core.Abstractions;
using Coverlet.Core.Extensions;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
namespace Coverlet.Core.Symbols
{
internal class CecilSymbolHelper : ICecilSymbolHelper
{
private const int StepOverLineCode = 0xFEEFEE;
// Create single instance, we cannot collide because we use full method name as key
private readonly ConcurrentDictionary<string, int[]> _compilerGeneratedBranchesToExclude = new();
private readonly ConcurrentDictionary<string, List<int>> _sequencePointOffsetToSkip = new();
// In case of nested compiler generated classes, only the root one presents the CompilerGenerated attribute.
// So let's search up to the outermost declaring type to find the attribute
private static bool IsCompilerGenerated(MethodDefinition methodDefinition)
{
TypeDefinition declaringType = methodDefinition.DeclaringType;
while (declaringType != null)
{
if (declaringType.CustomAttributes.Any(ca => ca.AttributeType.FullName == typeof(CompilerGeneratedAttribute).FullName))
{
return true;
}
declaringType = declaringType.DeclaringType;
}
return false;
}
private static bool IsCompilerGenerated(FieldDefinition fieldDefinition)
{
return fieldDefinition.DeclaringType.CustomAttributes.Any(ca => ca.AttributeType.FullName == typeof(CompilerGeneratedAttribute).FullName);
}
private static bool IsMoveNextInsideAsyncStateMachine(MethodDefinition methodDefinition)
{
if (methodDefinition.FullName.EndsWith("::MoveNext()") && IsCompilerGenerated(methodDefinition))
{
foreach (InterfaceImplementation implementedInterface in methodDefinition.DeclaringType.Interfaces)
{
if (implementedInterface.InterfaceType.FullName == "System.Runtime.CompilerServices.IAsyncStateMachine")
{
return true;
}
}
}
return false;
}
private static bool IsMoveNextInsideAsyncIterator(MethodDefinition methodDefinition)
{
if (methodDefinition.FullName.EndsWith("::MoveNext()") && IsCompilerGenerated(methodDefinition))
{
foreach (InterfaceImplementation implementedInterface in methodDefinition.DeclaringType.Interfaces)
{
if (implementedInterface.InterfaceType.FullName.StartsWith("System.Collections.Generic.IAsyncEnumerator`1<"))
{
return true;
}
}
}
return false;
}
private static bool IsMoveNextInsideEnumerator(MethodDefinition methodDefinition)
{
if (!methodDefinition.FullName.EndsWith("::MoveNext()"))
{
return false;
}
if (methodDefinition.DeclaringType.CustomAttributes.Any(ca => ca.AttributeType.FullName == typeof(CompilerGeneratedAttribute).FullName))
{
foreach (InterfaceImplementation implementedInterface in methodDefinition.DeclaringType.Interfaces)
{
if (implementedInterface.InterfaceType.FullName == "System.Collections.IEnumerator")
{
return true;
}
}
}
return false;
}
private static bool IsMoveNextInsideAsyncStateMachineProlog(MethodDefinition methodDefinition)
{
/*
int num = <>1__state;
IL_0000: ldarg.0
IL_0001: ldfld ...::'<>1__state'
IL_0006: stloc.0
*/
return (methodDefinition.Body.Instructions[0].OpCode == OpCodes.Ldarg_0 ||
methodDefinition.Body.Instructions[0].OpCode == OpCodes.Ldarg) &&
methodDefinition.Body.Instructions[1].OpCode == OpCodes.Ldfld &&
((methodDefinition.Body.Instructions[1].Operand is FieldDefinition fd && fd.Name == "<>1__state") ||
(methodDefinition.Body.Instructions[1].Operand is FieldReference fr && fr.Name == "<>1__state")) &&
(methodDefinition.Body.Instructions[2].OpCode == OpCodes.Stloc &&
methodDefinition.Body.Instructions[2].Operand is VariableDefinition vd && vd.Index == 0) ||
methodDefinition.Body.Instructions[2].OpCode == OpCodes.Stloc_0;
}
private static bool SkipMoveNextPrologueBranches(Instruction instruction)
{
/*
If method is a generated MoveNext we'll skip first branches (could be a switch or a series of branches)
that check state machine value to jump to correct state (for instance after a true async call)
Check if it's a Cond_Branch on state machine current value int num = <>1__state;
We are on branch OpCode so we need to go back by max 3 operation to reach ldloc.0 the load of "num"
Max 3 because we handle following patterns
Swich
// switch (num)
IL_0007: ldloc.0 2
// (no C# code)
IL_0008: switch (IL_0037, IL_003c, ... 1
...
Single branch
// if (num != 0)
IL_0007: ldloc.0 2
// (no C# code)
IL_0008: brfalse.s IL_000c 1
IL_000a: br.s IL_000e
IL_000c: br.s IL_0049
IL_000e: nop
...
More tha one branch
// if (num != 0)
IL_0007: ldloc.0
// (no C# code)
IL_0008: brfalse.s IL_0012
IL_000a: br.s IL_000c
// if (num == 1)
IL_000c: ldloc.0 3
IL_000d: ldc.i4.1 2
IL_000e: beq.s IL_0014 1
// (no C# code)
IL_0010: br.s IL_0019
IL_0012: br.s IL_0060
IL_0014: br IL_00e5
IL_0019: nop
...
so we know that current branch are checking that field and we're not interested in.
*/
Instruction current = instruction.Previous;
for (int instructionBefore = 3; instructionBefore > 0 && current.Previous != null; current = current.Previous, instructionBefore--)
{
if (
(current.OpCode == OpCodes.Ldloc && current.Operand is VariableDefinition vo && vo.Index == 0) ||
current.OpCode == OpCodes.Ldloc_0
)
{
return true;
}
}
return false;
}
private static bool SkipIsCompleteAwaiters(Instruction instruction)
{
// Skip get_IsCompleted to avoid unuseful branch due to async/await state machine
if (
instruction.Previous.Operand is MethodReference operand &&
operand.Name == "get_IsCompleted" &&
(
operand.DeclaringType.FullName.StartsWith("System.Runtime.CompilerServices.TaskAwaiter") ||
operand.DeclaringType.FullName.StartsWith("System.Runtime.CompilerServices.ValueTaskAwaiter") ||
operand.DeclaringType.FullName.StartsWith("System.Runtime.CompilerServices.ConfiguredTaskAwaitable") ||
operand.DeclaringType.FullName.StartsWith("System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable")
)
&&
(
operand.DeclaringType.Scope.Name == "System.Runtime" ||
operand.DeclaringType.Scope.Name == "netstandard" ||
operand.DeclaringType.Scope.Name == "mscorlib" ||
operand.DeclaringType.Scope.Name == "System.Threading.Tasks" ||
operand.DeclaringType.Scope.Name == "System.Threading.Tasks.Extensions"
)
)
{
return true;
}
return false;
}
private static bool SkipLambdaCachedField(Instruction instruction)
{
/*
Lambda cached field pattern
IL_0074: ldloca.s 1
IL_0076: call instance void [System.Runtime]System.Runtime.CompilerServices.TaskAwaiter::GetResult()
IL_007b: nop
IL_007c: ldarg.0
IL_007d: ldarg.0
IL_007e: ldfld class [System.Runtime]System.Collections.Generic.IEnumerable`1<object> Coverlet.Core.Samples.Tests.Issue_730/'<DoSomethingAsyncWithLinq>d__1'::objects
IL_0083: ldsfld class [System.Runtime]System.Func`2<object, object> Coverlet.Core.Samples.Tests.Issue_730/'<>c'::'<>9__1_0'
IL_0088: dup
IL_0089: brtrue.s IL_00a2 -> CHECK IF CACHED FIELD IS NULL OR JUMP TO DELEGATE USAGE
(INIT STATIC FIELD)
IL_008b: pop
IL_008c: ldsfld class Coverlet.Core.Samples.Tests.Issue_730/'<>c' Coverlet.Core.Samples.Tests.Issue_730/'<>c'::'<>9'
IL_0091: ldftn instance object Coverlet.Core.Samples.Tests.Issue_730/'<>c'::'<DoSomethingAsyncWithLinq>b__1_0'(object)
IL_0097: newobj instance void class [System.Runtime]System.Func`2<object, object>::.ctor(object, native int)
IL_009c: dup
IL_009d: stsfld class [System.Runtime]System.Func`2<object, object> Coverlet.Core.Samples.Tests.Issue_730/'<>c'::'<>9__1_0'
(USE DELEGATE FIELD)
IL_00a2: call class [System.Runtime]System.Collections.Generic.IEnumerable`1<!!1> [System.Linq]System.Linq.Enumerable::Select<object, object>(class [System.Runtime]System.Collections.Generic.IEnumerable`1<!!0>, class [System.Runtime]System.Func`2<!!0, !!1>)
*/
Instruction current = instruction.Previous;
for (int instructionBefore = 2; instructionBefore > 0 && current.Previous != null; current = current.Previous, instructionBefore--)
{
if (current.OpCode == OpCodes.Ldsfld && current.Operand is FieldDefinition fd &&
// LambdaCacheField https://github.com/dotnet/roslyn/blob/e704ca635bd6de70a0250e34c4567c7a28fa9f6d/src/Compilers/CSharp/Portable/Symbols/Synthesized/GeneratedNameKind.cs#L31
// https://github.com/dotnet/roslyn/blob/master/src/Compilers/CSharp/Portable/Symbols/Synthesized/GeneratedNames.cs#L145
fd.Name.StartsWith("<>9_") &&
IsCompilerGenerated(fd))
{
return true;
}
}
return false;
}
private static bool SkipGeneratedBranchForExceptionRethrown(List<Instruction> instructions, Instruction instruction)
{
/*
In case of exception re-thrown inside the catch block,
the compiler generates a branch to check if the exception reference is null.
// Debug version:
IL_00b4: isinst [System.Runtime]System.Exception
IL_00b9: stloc.s 6
IL_00bb: ldloc.s 6
IL_00bd: brtrue.s IL_00c6
// Release version:
IL_008A: isinst[System.Runtime]System.Exception
IL_008F: dup
IL_0090: brtrue.s IL_0099
So we can go back to previous instructions and skip this branch if recognize that type of code block.
There are differences between debug and release configuration so we just look for the highlights.
*/
int branchIndex = instructions.BinarySearch(instruction, new InstructionByOffsetComparer());
return new[] { 2, 3 }.Any(x => branchIndex >= x &&
instructions[branchIndex - x].OpCode == OpCodes.Isinst &&
instructions[branchIndex - x].Operand is TypeReference tr &&
tr.FullName == "System.Exception")
&&
// check for throw opcode after branch
instructions.Count - branchIndex >= 3 &&
instructions[branchIndex + 1].OpCode == OpCodes.Ldarg &&
instructions[branchIndex + 2].OpCode == OpCodes.Ldfld &&
instructions[branchIndex + 3].OpCode == OpCodes.Throw;
}
private bool SkipGeneratedBranchesForExceptionHandlers(MethodDefinition methodDefinition, Instruction instruction, List<Instruction> bodyInstructions)
{
/*
This method is used to parse compiler generated code inside async state machine and find branches generated for exception catch blocks.
There are differences between debug and release configuration. Also variations of the shown examples exist e.g. multiple catch blocks,
nested catch blocks... Therefore we just look for the highlights.
Typical generated code for catch block is:
// Debug version:
catch ...
{
// (no C# code)
IL_0028: stloc.2
// object obj2 = <>s__1 = obj;
IL_0029: ldarg.0
// (no C# code)
IL_002a: ldloc.2
IL_002b: stfld object ...::'<>s__1'
// <>s__2 = 1;
IL_0030: ldarg.0
IL_0031: ldc.i4.1
IL_0032: stfld int32 ...::'<>s__2' <- store 1 into <>s__2
// (no C# code)
IL_0037: leave.s IL_0039
} // end handle
// int num2 = <>s__2;
IL_0039: ldarg.0
IL_003a: ldfld int32 ...::'<>s__2' <- load <>s__2 value and check if 1
IL_003f: stloc.3
// if (num2 == 1)
IL_0040: ldloc.3
IL_0041: ldc.i4.1
IL_0042: beq.s IL_0049 <- BRANCH : if <>s__2 value is 1 go to exception handler code
IL_0044: br IL_00d6
IL_0049: nop <- start exception handler code
// Release version:
catch ...
{
IL_001D: stloc.3
IL_001E: ldarg.0
IL_001F: ldloc.3
IL_0020: stfld object Coverlet.Core.Samples.Tests.CatchBlock / '<ParseAsync>d__0'::'<>7__wrap1'
IL_0025: ldc.i4.1
IL_0026: stloc.2
IL_0027: leave.s IL_0029
} // end handler
IL_0029: ldloc.2
IL_002A: ldc.i4.1
IL_002B: bne.un.s IL_00AC
In case of multiple catch blocks as
try
{
}
catch (ExceptionType1)
{
}
catch (ExceptionType2)
{
}
generated IL contains multiple branches:
// Debug version:
catch ...(type1)
{
...
}
catch ...(type2)
{
...
}
// int num2 = <>s__2;
IL_0039: ldarg.0
IL_003a: ldfld int32 ...::'<>s__2' <- load <>s__2 value and check if 1
IL_003f: stloc.3
// if (num2 == 1)
IL_0040: ldloc.3
IL_0041: ldc.i4.1
IL_0042: beq.s IL_0049 <- BRANCH 1 (type 1)
IL_0044: br IL_00d6
// if (num2 == 2)
IL_0067: ldloc.s 4
IL_0069: ldc.i4.2
IL_006a: beq IL_0104 <- BRANCH 2 (type 2)
// (no C# code)
IL_006f: br IL_0191
// Release version:
catch ...(type1)
{
...
}
catch ...(type2)
{
...
}
IL_003E: ldloc.2
IL_003F: ldc.i4.1
IL_0040: beq.s IL_004E
IL_0042: ldloc.2/
IL_0043: ldc.i4.2
IL_0044: beq IL_00CD
IL_0049: br IL_0147
*/
if (!_compilerGeneratedBranchesToExclude.ContainsKey(methodDefinition.FullName))
{
var detectedBranches = new List<int>();
Collection<ExceptionHandler> handlers = methodDefinition.Body.ExceptionHandlers;
int numberOfCatchBlocks = 1;
foreach (ExceptionHandler handler in handlers)
{
if (handlers.Any(h => h.HandlerStart == handler.HandlerEnd))
{
// In case of multiple consecutive catch block
numberOfCatchBlocks++;
continue;
}
int currentIndex = bodyInstructions.BinarySearch(handler.HandlerEnd, new InstructionByOffsetComparer());
for (int i = 0; i < numberOfCatchBlocks; i++)
{
// We expect the first branch after the end of the handler to be no more than five instructions away.
int firstBranchIndex = currentIndex + Enumerable.Range(1, 5).FirstOrDefault(x =>
currentIndex + x < bodyInstructions.Count &&
bodyInstructions[currentIndex + x].OpCode == OpCodes.Beq ||
bodyInstructions[currentIndex + x].OpCode == OpCodes.Bne_Un);
if (bodyInstructions.Count - firstBranchIndex > 2 &&
bodyInstructions[firstBranchIndex - 1].OpCode == OpCodes.Ldc_I4 &&
bodyInstructions[firstBranchIndex - 2].OpCode == OpCodes.Ldloc)
{
detectedBranches.Add(bodyInstructions[firstBranchIndex].Offset);
}
currentIndex = firstBranchIndex;
}
}
_compilerGeneratedBranchesToExclude.TryAdd(methodDefinition.FullName, detectedBranches.ToArray());
}
return _compilerGeneratedBranchesToExclude[methodDefinition.FullName].Contains(instruction.Offset);
}
private static bool SkipGeneratedBranchesForAwaitForeach(List<Instruction> instructions, Instruction instruction)
{
// An "await foreach" causes four additional branches to be generated
// by the compiler. We want to skip the last three, but we want to
// keep the first one.
//
// (1) At each iteration of the loop, a check that there is another
// item in the sequence. This is a branch that we want to keep,
// because it's basically "should we stay in the loop or not?",
// which is germane to code coverage testing.
// (2) A check near the end for whether the IAsyncEnumerator was ever
// obtained, so it can be disposed.
// (3) A check for whether an exception was thrown in the most recent
// loop iteration.
// (4) A check for whether the exception thrown in the most recent
// loop iteration has (at least) the type System.Exception.
//
// If we're looking at any of the last three of those four branches,
// we should be skipping it.
int currentIndex = instructions.BinarySearch(instruction, new InstructionByOffsetComparer());
return CheckForAsyncEnumerator(instructions, instruction, currentIndex) ||
CheckIfExceptionThrown(instructions, instruction, currentIndex) ||
CheckThrownExceptionType(instructions, instruction, currentIndex);
// The pattern for the "should we stay in the loop or not?", which we don't
// want to skip (so we have no method to try to find it), looks like this:
//
// IL_0111: ldloca.s 4
// IL_0113: call instance !0 valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.ValueTaskAwaiter`1<bool>::GetResult()
// IL_0118: brtrue IL_0047
//
// In Debug mode, there are additional things that happen in between
// the "call" and branch, but it's the same idea either way: branch
// if GetResult() returned true.
static bool CheckForAsyncEnumerator(List<Instruction> instructions, Instruction instruction, int currentIndex)
{
// We're looking for the following pattern, which checks whether a
// compiler-generated field of type IAsyncEnumerator<> is null.
//
// IL_012b: ldarg.0
// IL_012c: ldfld class [System.Private.CoreLib]System.Collections.Generic.IAsyncEnumerator`1<int32> AwaitForeachStateMachine/'<AsyncAwait>d__0'::'<>7__wrap1'
// IL_0131: brfalse.s IL_0196
if (instruction.OpCode != OpCodes.Brfalse &&
instruction.OpCode != OpCodes.Brfalse_S)
{
return false;
}
if (currentIndex >= 2 &&
(instructions[currentIndex - 2].OpCode == OpCodes.Ldarg ||
instructions[currentIndex - 2].OpCode == OpCodes.Ldarg_0) &&
instructions[currentIndex - 1].OpCode == OpCodes.Ldfld &&
(
(instructions[currentIndex - 1].Operand is FieldDefinition field && IsCompilerGenerated(field) && field.FieldType.FullName.StartsWith("System.Collections.Generic.IAsyncEnumerator")) ||
(instructions[currentIndex - 1].Operand is FieldReference fieldRef && IsCompilerGenerated(fieldRef.Resolve()) && fieldRef.FieldType.FullName.StartsWith("System.Collections.Generic.IAsyncEnumerator"))
)
)
{
return true;
}
return false;
}
static bool CheckIfExceptionThrown(List<Instruction> instructions, Instruction instruction, int currentIndex)
{
// Here, we want to find a pattern where we're checking whether a
// compiler-generated field of type Object is null. To narrow our
// search down and reduce the odds of false positives, we'll also
// expect a call to GetResult() to precede the loading of the field's
// value. The basic pattern looks like this:
//
// IL_018f: ldloca.s 2
// IL_0191: call instance void [System.Private.CoreLib]System.Runtime.CompilerServices.ValueTaskAwaiter::GetResult()
// IL_0196: ldarg.0
// IL_0197: ldfld object AwaitForeachStateMachine/'<AsyncAwait>d__0'::'<>7__wrap2'
// IL_019c: stloc.s 6
// IL_019e: ldloc.s 6
// IL_01a0: brfalse.s IL_01b9
//
// Variants are possible (e.g., a "dup" instruction instead of a
// "stloc.s" and "ldloc.s" pair), so we'll just look for the
// highlights.
if (instruction.OpCode != OpCodes.Brfalse &&
instruction.OpCode != OpCodes.Brfalse_S)
{
return false;
}
// We expect the field to be loaded no more than thre instructions before
// the branch, so that's how far we're willing to search for it.
int minFieldIndex = Math.Max(0, currentIndex - 3);
for (int i = currentIndex - 1; i >= minFieldIndex; --i)
{
if (instructions[i].OpCode == OpCodes.Ldfld &&
(
(instructions[i].Operand is FieldDefinition field && IsCompilerGenerated(field) && field.FieldType.FullName == "System.Object") ||
(instructions[i].Operand is FieldReference fieldRef && IsCompilerGenerated(fieldRef.Resolve()) && fieldRef.FieldType.FullName == "System.Object")
))
{
// We expect the call to GetResult() to be no more than four
// instructions before the loading of the field's value.
int minCallIndex = Math.Max(0, i - 4);
for (int j = i - 1; j >= minCallIndex; --j)
{
if (instructions[j].OpCode == OpCodes.Call &&
instructions[j].Operand is MethodReference callRef &&
callRef.DeclaringType.FullName.StartsWith("System.Runtime.CompilerServices") &&
callRef.DeclaringType.FullName.Contains("TaskAwait") &&
callRef.Name == "GetResult")
{
return true;
}
}
}
}
return false;
}
static bool CheckThrownExceptionType(List<Instruction> instructions, Instruction instruction, int currentIndex)
{
// In this case, we're looking for a branch generated by the compiler to
// check whether a previously-thrown exception has (at least) the type
// System.Exception, the pattern for which looks like this:
//
// IL_01db: ldloc.s 7
// IL_01dd: isinst [System.Private.CoreLib]System.Exception
// IL_01e2: stloc.s 9
// IL_01e4: ldloc.s 9
// IL_01e6: brtrue.s IL_01eb
//
// Once again, variants are possible here, such as a "dup" instruction in
// place of the "stloc.s" and "ldloc.s" pair, and we'll reduce the odds of
// a false positive by requiring a "ldloc.s" instruction to precede the
// "isinst" instruction.
if (instruction.OpCode != OpCodes.Brtrue &&
instruction.OpCode != OpCodes.Brtrue_S)
{
return false;
}
int minTypeCheckIndex = Math.Max(1, currentIndex - 3);
for (int i = currentIndex - 1; i >= minTypeCheckIndex; --i)
{
if (instructions[i].OpCode == OpCodes.Isinst &&
instructions[i].Operand is TypeReference typeRef &&
typeRef.FullName == "System.Exception" &&
(instructions[i - 1].OpCode == OpCodes.Ldloc ||
instructions[i - 1].OpCode == OpCodes.Ldloc_S ||
instructions[i - 1].OpCode == OpCodes.Ldloc_0 ||
instructions[i - 1].OpCode == OpCodes.Ldloc_1 ||
instructions[i - 1].OpCode == OpCodes.Ldloc_2 ||
instructions[i - 1].OpCode == OpCodes.Ldloc_3))
{
return true;
}
}
return false;
}
}
private static bool SkipGeneratedBranchesForAwaitUsing(List<Instruction> instructions, Instruction instruction)
{
int currentIndex = instructions.BinarySearch(instruction, new InstructionByOffsetComparer());
return CheckForSkipDisposal(instructions, instruction, currentIndex) ||
CheckForCleanup(instructions, instruction, currentIndex);
static bool CheckForSkipDisposal(List<Instruction> instructions, Instruction instruction, int currentIndex)
{
// The async state machine generated for an "await using" contains a branch
// that checks whether the call to DisposeAsync() needs to be skipped.
// As it turns out, the pattern is a little different in Debug and Release
// configurations, but the idea is the same in either case:
//
// * Check whether the IAsyncDisposable that's being managed by the "await
// using" is null. If so, skip the call to DisposeAsync(). If not,
// make the call.
//
// To avoid other places where this pattern is used, we'll require it to be
// preceded by the tail end of a try/catch generated by the compiler.
//
// The primary difference between the Debug and Release versions of this
// pattern are where that IAsyncDisposable is stored. In Debug mode, it's
// in a field; in Release mode, it's in a local variable. So what we'll
// look for, generally, is a brfalse instruction that checks a value,
// followed shortly by reloading that same value and calling DisposeAsync()
// on it, preceded shortly by a store into a compiler-generated field and
// a "leave" instruction.
//
// Debug version:
// IL_0041: stfld object Coverlet.Core.Samples.Tests.AwaitUsing/'<HasAwaitUsing>d__0'::'<>s__2'
// IL_0046: leave.s IL_0048
// IL_0048: ldarg.0
// IL_0049: ldfld class [System.Private.CoreLib]System.IO.MemoryStream Coverlet.Core.Samples.Tests.AwaitUsing/'<HasAwaitUsing>d__0'::'<ms>5__1'
// IL_004e: brfalse.s IL_00b9
// IL_0050: ldarg.0
// IL_0051: ldfld class [System.Private.CoreLib]System.IO.MemoryStream Coverlet.Core.Samples.Tests.AwaitUsing/'<HasAwaitUsing>d__0'::'<ms>5__1'
// IL_0056: callvirt instance valuetype [System.Private.CoreLib]System.Threading.Tasks.ValueTask [System.Private.CoreLib]System.IAsyncDisposable::DisposeAsync()
//
// Release version:
// IL_0032: stfld object Coverlet.Core.Samples.Tests.AwaitUsing/'<HasAwaitUsing>d__0'::'<>7__wrap1'
// IL_0037: leave.s IL_0039
// IL_0039: ldloc.1
// IL_003a: brfalse.s IL_0098
// IL_003c: ldloc.1
// IL_003d: callvirt instance valuetype [System.Private.CoreLib]System.Threading.Tasks.ValueTask [System.Private.CoreLib]System.IAsyncDisposable::DisposeAsync()
if (instruction.OpCode != OpCodes.Brfalse &&
instruction.OpCode != OpCodes.Brfalse_S)
{
return false;
}
bool isFollowedByDisposeAsync = false;
if (instructions[currentIndex - 1].OpCode == OpCodes.Ldfld &&
instructions[currentIndex - 1].Operand is FieldDefinition field &&
IsCompilerGenerated(field))
{
int maxReloadFieldIndex = Math.Min(currentIndex + 2, instructions.Count - 2);
for (int i = currentIndex + 1; i <= maxReloadFieldIndex; ++i)
{
if (instructions[i].OpCode == OpCodes.Ldfld &&
instructions[i].Operand is FieldDefinition reloadedField &&
field.Equals(reloadedField) &&
instructions[i + 1].OpCode == OpCodes.Callvirt &&
instructions[i + 1].Operand is MethodReference method &&
method.DeclaringType.FullName == "System.IAsyncDisposable" &&
method.Name == "DisposeAsync")
{
isFollowedByDisposeAsync = true;
break;
}
}
}
else if ((instructions[currentIndex - 1].OpCode == OpCodes.Ldloc ||
instructions[currentIndex - 1].OpCode == OpCodes.Ldloc_S ||
instructions[currentIndex - 1].OpCode == OpCodes.Ldloc_0 ||
instructions[currentIndex - 1].OpCode == OpCodes.Ldloc_1 ||
instructions[currentIndex - 1].OpCode == OpCodes.Ldloc_2 ||
instructions[currentIndex - 1].OpCode == OpCodes.Ldloc_3) &&
(instructions[currentIndex + 1].OpCode == OpCodes.Ldloc ||
instructions[currentIndex + 1].OpCode == OpCodes.Ldloc_S ||
instructions[currentIndex - 1].OpCode == OpCodes.Ldloc_0 ||
instructions[currentIndex - 1].OpCode == OpCodes.Ldloc_1 ||
instructions[currentIndex - 1].OpCode == OpCodes.Ldloc_2 ||
instructions[currentIndex - 1].OpCode == OpCodes.Ldloc_3) &&
instructions[currentIndex + 2].OpCode == OpCodes.Callvirt &&
instructions[currentIndex + 2].Operand is MethodReference method &&
method.DeclaringType.FullName == "System.IAsyncDisposable" &&
method.Name == "DisposeAsync")
{
isFollowedByDisposeAsync = true;
}
if (isFollowedByDisposeAsync)
{
int minLeaveIndex = Math.Max(1, currentIndex - 4);
for (int i = currentIndex - 1; i >= minLeaveIndex; --i)
{
if ((instructions[i].OpCode == OpCodes.Leave ||
instructions[i].OpCode == OpCodes.Leave_S) &&
instructions[i - 1].OpCode == OpCodes.Stfld &&
instructions[i - 1].Operand is FieldDefinition storeField &&
IsCompilerGenerated(storeField))
{
return true;
}
}
}
return false;
}
static bool CheckForCleanup(List<Instruction> instructions, Instruction instruction, int currentIndex)
{
// The pattern we're looking for here is this:
//
// IL_00c6: ldloc.s 5
// IL_00c8: call class [System.Private.CoreLib]System.Runtime.ExceptionServices.ExceptionDispatchInfo [System.Private.CoreLib]System.Runtime.ExceptionServices.ExceptionDispatchInfo::Capture(class [System.Private.CoreLib]System.Exception)
// IL_00cd: callvirt instance void [System.Private.CoreLib]System.Runtime.ExceptionServices.ExceptionDispatchInfo::Throw()
// IL_00d2: nop
// IL_00d3: ldarg.0
// IL_00d4: ldfld int32 Coverlet.Core.Samples.Tests.AwaitUsing/'<Issue914_Repro_Example1>d__2'::'<>s__3'
// IL_00d9: stloc.s 6
// IL_00db: ldloc.s 6
// IL_00dd: ldc.i4.1
// IL_00de: beq.s IL_00e2
// IL_00e0: br.s IL_00e4
// IL_00e2: leave.s IL_0115
//
// It appears that this pattern is not generated in every "await using",
// but only in an "await using" without curly braces (i.e., that is
// scoped to the end of the method). It's also a slightly different
// pattern in Release vs. Debug (bne.un.s instead of beq.s followed by
// br.s). To be as safe as we can, we'll expect an ldc.i4 to precede
// the branch, then we want a load from a compiler-generated field within
// a few instructions before that, then we want an exception to be
// rethrown before that.
if (instruction.OpCode != OpCodes.Beq &&
instruction.OpCode != OpCodes.Beq_S &&
instruction.OpCode != OpCodes.Bne_Un &&
instruction.OpCode != OpCodes.Bne_Un_S)
{
return false;
}
if (currentIndex >= 1 &&
(instructions[currentIndex - 1].OpCode == OpCodes.Ldc_I4 ||
instructions[currentIndex - 1].OpCode == OpCodes.Ldc_I4_1))
{
int minLoadFieldIndex = Math.Max(1, currentIndex - 5);
for (int i = currentIndex - 2; i >= minLoadFieldIndex; --i)
{
if (instructions[i].OpCode == OpCodes.Ldfld &&
instructions[i].Operand is FieldDefinition loadedField &&
IsCompilerGenerated(loadedField))
{
int minRethrowIndex = Math.Max(0, i - 4);
for (int j = i - 1; j >= minRethrowIndex; --j)
{
if (instructions[j].OpCode == OpCodes.Callvirt &&
instructions[j].Operand is MethodReference method &&
method.DeclaringType.FullName == "System.Runtime.ExceptionServices.ExceptionDispatchInfo" &&
method.Name == "Throw")
{
return true;
}
}
}
}
}
return false;
}
}
// https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8
private static bool SkipGeneratedBranchesForAsyncIterator(List<Instruction> instructions, Instruction instruction)
{
// There are two branch patterns that we want to eliminate in the
// MoveNext() method in compiler-generated async iterators.
//
// (1) A "switch" instruction near the beginning of MoveNext() that checks
// the state machine's current state and jumps to the right place.
// (2) A check that the compiler-generated field "<>w__disposeMode" is false,
// which is used to know whether the enumerator has been disposed (so it's
// necessary not to iterate any further). This is done in more than once
// place, but we always want to skip it.
int currentIndex = instructions.BinarySearch(instruction, new InstructionByOffsetComparer());
return CheckForStateSwitch(instructions, instruction, currentIndex) ||
DisposeCheck(instructions, instruction, currentIndex);
static bool CheckForStateSwitch(List<Instruction> instructions, Instruction instruction, int currentIndex)
{
// The pattern we're looking for here is this one:
//
// IL_0000: ldarg.0
// IL_0001: ldfld int32 Test.AsyncEnumerableStateMachine/'<CreateSequenceAsync>d__0'::'<>1__state'
// IL_0006: stloc.0
// .try
// {
// IL_0007: ldloc.0
// IL_0008: ldc.i4.s -4
// IL_000a: sub
// IL_000b: switch (IL_0026, IL_002b, IL_002f, IL_002f, IL_002d)
//
// The "switch" instruction is the branch we want to skip. To eliminate
// false positives, we'll search back for the "ldfld" of the compiler-
// generated "<>1__state" field, making sure it precedes it within five
// instructions. To be safe, we'll also require a "ldarg.0" instruction
// before the "ldfld".
if (instruction.OpCode != OpCodes.Switch)
{
return false;
}
int minLoadStateFieldIndex = Math.Max(1, currentIndex - 5);
for (int i = currentIndex - 1; i >= minLoadStateFieldIndex; --i)
{
if (instructions[i].OpCode == OpCodes.Ldfld &&
instructions[i].Operand is FieldDefinition field &&
IsCompilerGenerated(field) && field.FullName.EndsWith("__state") &&
(instructions[i - 1].OpCode == OpCodes.Ldarg ||
instructions[i - 1].OpCode == OpCodes.Ldarg_0))
{
return true;
}
}
return false;
}
static bool DisposeCheck(List<Instruction> instructions, Instruction instruction, int currentIndex)
{
// Within the compiler-generated async iterator, there are at least a
// couple of places where we find this pattern, in which the async
// iterator is checking whether it's been disposed, so it'll know to
// stop iterating.
//
// IL_0024: ldarg.0
// IL_0025: ldfld bool Test.AsyncEnumerableStateMachine/'<CreateSequenceAsync>d__0'::'<>w__disposeMode'
// IL_002a: brfalse.s IL_0031
//
// We'll eliminate these wherever they appear. It's reasonable to just
// look for a "brfalse" or "brfalse.s" instruction, preceded immediately
// by "ldfld" of the compiler-generated "<>w__disposeMode" field.
if (instruction.OpCode != OpCodes.Brfalse &&
instruction.OpCode != OpCodes.Brfalse_S)
{
return false;
}
if (currentIndex >= 2 &&
instructions[currentIndex - 1].OpCode == OpCodes.Ldfld &&
(
(instructions[currentIndex - 1].Operand is FieldDefinition field && IsCompilerGenerated(field) && field.FullName.EndsWith("__disposeMode")) ||
(instructions[currentIndex - 1].Operand is FieldReference fieldRef && IsCompilerGenerated(fieldRef.Resolve()) && fieldRef.FullName.EndsWith("__disposeMode"))
) &&
(instructions[currentIndex - 2].OpCode == OpCodes.Ldarg ||
instructions[currentIndex - 2].OpCode == OpCodes.Ldarg_0))
{
return true;
}
return false;
}
}
private static bool SkipGeneratedBranchesForEnumeratorCancellationAttribute(List<Instruction> instructions, Instruction instruction)
{
// For async-enumerable methods an additional cancellation token despite the default one can be passed.
// The EnumeratorCancellation attribute marks the parameter whose value is received by GetAsyncEnumerator(CancellationToken).
// Therefore the compiler generates the field x__combinedTokens and generates some additional branch points.
//
// IL_0118: ldarg.0
// IL_0119: ldfld class [System.Runtime]System.Threading.CancellationTokenSource Issue1275.AwaitForeachReproduction/'<AsyncEnumerable>d__1'::'<>x__combinedTokens'
// IL_011E: brfalse.s IL_0133
//
// We'll eliminate these wherever they appear. It's reasonable to just look for a "brfalse" or "brfalse.s" instruction, preceded
// immediately by "ldfld" of the compiler-generated "<>x__combinedTokens" field.
int branchIndex = instructions.BinarySearch(instruction, new InstructionByOffsetComparer());
if (instruction.OpCode != OpCodes.Brfalse &&
instruction.OpCode != OpCodes.Brfalse_S)
{
return false;
}
if (branchIndex >= 2 &&
instructions[branchIndex - 1].OpCode == OpCodes.Ldfld &&
instructions[branchIndex - 1].Operand is FieldDefinition field &&
field.FieldType.FullName.Equals("System.Threading.CancellationTokenSource") &&
field.FullName.EndsWith("x__combinedTokens") &&
(instructions[branchIndex - 2].OpCode == OpCodes.Ldarg ||
instructions[branchIndex - 2].OpCode == OpCodes.Ldarg_0))
{
return true;
}
return false;
}
// https://github.com/dotnet/roslyn/blob/master/docs/compilers/CSharp/Expression%20Breakpoints.md
private static bool SkipExpressionBreakpointsBranches(Instruction instruction) => instruction.Previous is not null && instruction.Previous.OpCode == OpCodes.Ldc_I4 &&
instruction.Previous.Operand is int operandValue && operandValue == 1 &&
instruction.Next is not null && instruction.Next.OpCode == OpCodes.Nop &&
instruction.Operand == instruction.Next?.Next;
public IReadOnlyList<BranchPoint> GetBranchPoints(MethodDefinition methodDefinition)
{
var list = new List<BranchPoint>();
if (methodDefinition is null)
{
return list;
}
uint ordinal = 0;
var instructions = methodDefinition.Body.Instructions.ToList();
bool isAsyncStateMachineMoveNext = IsMoveNextInsideAsyncStateMachine(methodDefinition);
bool isMoveNextInsideAsyncStateMachineProlog = isAsyncStateMachineMoveNext && IsMoveNextInsideAsyncStateMachineProlog(methodDefinition);
bool isMoveNextInsideAsyncIterator = isAsyncStateMachineMoveNext && IsMoveNextInsideAsyncIterator(methodDefinition);
// State machine for enumerator uses `brfalse.s`/`beq` or `switch` opcode depending on how many `yield` we have in the method body.
// For more than one `yield` a `switch` is emitted so we should only skip the first branch. In case of a single `yield` we need to
// skip the first two branches to avoid reporting a phantom branch. The first branch (`brfalse.s`) jumps to the `yield`ed value,
// the second one (`beq`) exits the enumeration.
bool skipFirstBranch = IsMoveNextInsideEnumerator(methodDefinition);
bool skipSecondBranch = false;
foreach (Instruction instruction in instructions.Where(instruction => instruction.OpCode.FlowControl == FlowControl.Cond_Branch))
{
try
{
if (skipFirstBranch)
{
skipFirstBranch = false;
skipSecondBranch = instruction.OpCode.Code != Code.Switch;
continue;
}
if (skipSecondBranch)
{
skipSecondBranch = false;
continue;
}
if (isMoveNextInsideAsyncStateMachineProlog)
{
if (SkipMoveNextPrologueBranches(instruction) || SkipIsCompleteAwaiters(instruction))
{
continue;
}
}
if (SkipExpressionBreakpointsBranches(instruction))
{
continue;
}
if (SkipLambdaCachedField(instruction))
{
continue;
}
if (isAsyncStateMachineMoveNext)
{
if (SkipGeneratedBranchesForExceptionHandlers(methodDefinition, instruction, instructions) ||
SkipGeneratedBranchForExceptionRethrown(instructions, instruction) ||
SkipGeneratedBranchesForAwaitForeach(instructions, instruction) ||
SkipGeneratedBranchesForAwaitUsing(instructions, instruction))
{
continue;
}