.ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ IL_000a: ldarg.0
+ IL_000b: call "object..ctor()"
+ IL_0010: nop
+ IL_0011: ldarg.0
+ IL_0012: ldarg.1
+ IL_0013: ldfld "int R.k__BackingField"
+ IL_0018: stfld "int R.
k__BackingField"
+ // sequence point: R
+ IL_001d: nop
+ IL_001e: ret
+ }
+ """);
+
+ // We instrument the primary constructor for simplicity, even though it does not contain any user code.
+ verifier.VerifyMethodBody("R..ctor(int)", """
+ {
+ // Code size 25 (0x19)
+ .maxstack 2
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point:
+ IL_000a: ldarg.0
+ IL_000b: ldarg.1
+ IL_000c: stfld "int R.k__BackingField"
+ // sequence point: R(int P)
+ IL_0011: ldarg.0
+ IL_0012: call "object..ctor()"
+ IL_0017: nop
+ IL_0018: ret
+ }
+ """);
+ }
+
+ [Fact]
+ public void While()
+ {
+ var source = """
+ using System;
+
+ class C
+ {
+ void F(bool b)
+ {
+ while(b) { Console.WriteLine(); }
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ verifier.VerifyMethodBody("C.F", """
+ {
+ // Code size 37 (0x25)
+ .maxstack 1
+ .locals init (bool V_0)
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point:
+ IL_000b: br.s IL_0015
+ // sequence point: {
+ IL_000d: nop
+ // sequence point: Console.WriteLine();
+ IL_000e: call "void System.Console.WriteLine()"
+ IL_0013: nop
+ // sequence point: }
+ IL_0014: nop
+ // sequence point: while(b)
+ IL_0015: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_001a: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ IL_001f: ldarg.1
+ IL_0020: stloc.0
+ // sequence point:
+ IL_0021: ldloc.0
+ IL_0022: brtrue.s IL_000d
+ // sequence point: }
+ IL_0024: ret
+ }
+ """);
+ }
+
+ [Fact]
+ public void DoWhile()
+ {
+ var source = """
+ using System;
+
+ class C
+ {
+ void F(bool b)
+ {
+ do { Console.WriteLine(); } while (b);
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ verifier.VerifyMethodBody("C.F", """
+ {
+ // Code size 35 (0x23)
+ .maxstack 1
+ .locals init (bool V_0)
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point: {
+ IL_000b: nop
+ // sequence point: Console.WriteLine();
+ IL_000c: call "void System.Console.WriteLine()"
+ IL_0011: nop
+ // sequence point: }
+ IL_0012: nop
+ // sequence point: while (b);
+ IL_0013: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0018: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ IL_001d: ldarg.1
+ IL_001e: stloc.0
+ // sequence point:
+ IL_001f: ldloc.0
+ IL_0020: brtrue.s IL_000b
+ // sequence point: }
+ IL_0022: ret
+ }
+ """);
+ }
+
+ [Fact]
+ public void For()
+ {
+ var source = """
+ using System;
+
+ class C
+ {
+ void F()
+ {
+ for (;;) { Console.WriteLine(); }
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ verifier.VerifyMethodBody("C.F", """
+ {
+ // Code size 33 (0x21)
+ .maxstack 1
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point:
+ IL_000b: br.s IL_0015
+ // sequence point: {
+ IL_000d: nop
+ // sequence point: Console.WriteLine();
+ IL_000e: call "void System.Console.WriteLine()"
+ IL_0013: nop
+ // sequence point: }
+ IL_0014: nop
+ // sequence point:
+ IL_0015: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_001a: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ IL_001f: br.s IL_000d
+ }
+ """);
+ }
+
+ [Fact]
+ public void ForEach_Array()
+ {
+ var source = """
+ using System;
+
+ class C
+ {
+ void F(int[] items)
+ {
+ foreach (var item in items) { Console.WriteLine(item); }
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ verifier.VerifyMethodBody("C.F", """
+ {
+ // Code size 52 (0x34)
+ .maxstack 2
+ .locals init (int[] V_0,
+ int V_1,
+ int V_2) //item
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point: foreach
+ IL_000b: nop
+ // sequence point: items
+ IL_000c: ldarg.1
+ IL_000d: stloc.0
+ IL_000e: ldc.i4.0
+ IL_000f: stloc.1
+ // sequence point:
+ IL_0010: br.s IL_0023
+ // sequence point: var item
+ IL_0012: ldloc.0
+ IL_0013: ldloc.1
+ IL_0014: ldelem.i4
+ IL_0015: stloc.2
+ // sequence point: {
+ IL_0016: nop
+ // sequence point: Console.WriteLine(item);
+ IL_0017: ldloc.2
+ IL_0018: call "void System.Console.WriteLine(int)"
+ IL_001d: nop
+ // sequence point: }
+ IL_001e: nop
+ // sequence point:
+ IL_001f: ldloc.1
+ IL_0020: ldc.i4.1
+ IL_0021: add
+ IL_0022: stloc.1
+ // sequence point: in
+ IL_0023: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0028: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ IL_002d: ldloc.1
+ IL_002e: ldloc.0
+ IL_002f: ldlen
+ IL_0030: conv.i4
+ IL_0031: blt.s IL_0012
+ // sequence point: }
+ IL_0033: ret
+ }
+ """);
+ }
+
+ [Fact]
+ public void ForEach_Enumerable()
+ {
+ var source = """
+ using System;
+ using System.Collections.Generic;
+
+ class C
+ {
+ void F(IEnumerable items)
+ {
+ foreach (var item in items) { Console.WriteLine(item); }
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ verifier.VerifyMethodBody("C.F", """
+ {
+ // Code size 69 (0x45)
+ .maxstack 1
+ .locals init (System.Collections.Generic.IEnumerator V_0,
+ int V_1) //item
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point: foreach
+ IL_000b: nop
+ // sequence point: items
+ IL_000c: ldarg.1
+ IL_000d: callvirt "System.Collections.Generic.IEnumerator System.Collections.Generic.IEnumerable.GetEnumerator()"
+ IL_0012: stloc.0
+ .try
+ {
+ // sequence point:
+ IL_0013: br.s IL_0025
+ // sequence point: var item
+ IL_0015: ldloc.0
+ IL_0016: callvirt "int System.Collections.Generic.IEnumerator.Current.get"
+ IL_001b: stloc.1
+ // sequence point: {
+ IL_001c: nop
+ // sequence point: Console.WriteLine(item);
+ IL_001d: ldloc.1
+ IL_001e: call "void System.Console.WriteLine(int)"
+ IL_0023: nop
+ // sequence point: }
+ IL_0024: nop
+ // sequence point: in
+ IL_0025: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_002a: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ IL_002f: ldloc.0
+ IL_0030: callvirt "bool System.Collections.IEnumerator.MoveNext()"
+ IL_0035: brtrue.s IL_0015
+ IL_0037: leave.s IL_0044
+ }
+ finally
+ {
+ // sequence point:
+ IL_0039: ldloc.0
+ IL_003a: brfalse.s IL_0043
+ IL_003c: ldloc.0
+ IL_003d: callvirt "void System.IDisposable.Dispose()"
+ IL_0042: nop
+ // sequence point:
+ IL_0043: endfinally
+ }
+ // sequence point: }
+ IL_0044: ret
+ }
+ """);
+ }
+
+ [Fact]
+ public void GoTo()
+ {
+ var source = """
+ using System;
+
+ class C
+ {
+ void F()
+ {
+ label:
+ Console.WriteLine();
+ goto label;
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ // TODO: odd sequence point // sequence point: { ... }
+ verifier.VerifyMethodBody("C.F", """
+ {
+ // Code size 30 (0x1e)
+ .maxstack 1
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point: label:
+ IL_000b: nop
+ // sequence point: Console.WriteLine();
+ IL_000c: call "void System.Console.WriteLine()"
+ IL_0011: nop
+ // sequence point: { ... }
+ IL_0012: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0017: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ IL_001c: br.s IL_000b
+ }
+ """);
+ }
+
+ [Fact]
+ public void GoToCase()
+ {
+ var source = """
+ using System;
+
+ class C
+ {
+ void F(int x)
+ {
+ switch (x)
+ {
+ case 1: Console.WriteLine(1); goto case 2;
+ case 2: Console.WriteLine(2); goto case 1;
+ }
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ // TODO: odd sequence points: // sequence point: case 1: Console.WriteLine(1); goto case 2;
+ verifier.VerifyMethodBody("C.F", """
+ {
+ // Code size 66 (0x42)
+ .maxstack 2
+ .locals init (int V_0,
+ int V_1)
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point: switch (x)
+ IL_000b: ldarg.1
+ IL_000c: stloc.1
+ // sequence point:
+ IL_000d: ldloc.1
+ IL_000e: stloc.0
+ // sequence point:
+ IL_000f: ldloc.0
+ IL_0010: ldc.i4.1
+ IL_0011: beq.s IL_001b
+ IL_0013: br.s IL_0015
+ IL_0015: ldloc.0
+ IL_0016: ldc.i4.2
+ IL_0017: beq.s IL_002e
+ IL_0019: br.s IL_0041
+ // sequence point: Console.WriteLine(1);
+ IL_001b: ldc.i4.1
+ IL_001c: call "void System.Console.WriteLine(int)"
+ IL_0021: nop
+ // sequence point: case 1: Console.WriteLine(1); goto case 2;
+ IL_0022: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0027: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ IL_002c: br.s IL_002e
+ // sequence point: Console.WriteLine(2);
+ IL_002e: ldc.i4.2
+ IL_002f: call "void System.Console.WriteLine(int)"
+ IL_0034: nop
+ // sequence point: case 2: Console.WriteLine(2); goto case 1;
+ IL_0035: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_003a: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ IL_003f: br.s IL_001b
+ // sequence point: }
+ IL_0041: ret
+ }
+ """);
+ }
+
+ [Fact]
+ public void ArgumentReplacement_SourceVsLibrary()
+ {
+ var source = """
+ using System.Threading;
+ using System.Threading.Tasks;
+
+ class C
+ {
+ void F(CancellationToken token)
+ {
+ G(token);
+ var _ = Task.FromCanceled(token);
+ }
+
+ void G(CancellationToken token)
+ {
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ // Replace token regardless of whether the target method is from source or defined externally.
+ // Allows us to be consistent when the target method is invoked indirectly via a delegate.
+ // In that case we wouldn't know where it's defined.
+ verifier.VerifyMethodBody("C.F", """
+ {
+ // Code size 35 (0x23)
+ .maxstack 2
+ .locals init (System.Threading.Tasks.Task V_0) //_
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point: G(token);
+ IL_000b: ldarg.0
+ IL_000c: ldsfld "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0011: call "void C.G(System.Threading.CancellationToken)"
+ IL_0016: nop
+ // sequence point: var _ = Task.FromCanceled(token);
+ IL_0017: ldsfld "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_001c: call "System.Threading.Tasks.Task System.Threading.Tasks.Task.FromCanceled(System.Threading.CancellationToken)"
+ IL_0021: stloc.0
+ // sequence point: }
+ IL_0022: ret
+ }
+ """);
+ }
+
+ [Theory]
+ [InlineData("ref")]
+ [InlineData("out")]
+ [InlineData("in")]
+ public void ArgumentReplacement_RefKinds(string refKind)
+ {
+ var source = $$"""
+ using System.Threading;
+
+ class C
+ {
+ void F(CancellationToken token)
+ {
+ G({{refKind}} token);
+ }
+
+ void G({{refKind}} CancellationToken token)
+ {
+ throw null;
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ // Doesn't force module-level cancellation token to ref/in/out parameters as these are not commonly used in cancellable APIs.
+ verifier.VerifyMethodBody("C.F", $$"""
+ {
+ // Code size 21 (0x15)
+ .maxstack 2
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point: G({{refKind}} token);
+ IL_000b: ldarg.0
+ IL_000c: ldarga.s V_1
+ IL_000e: call "void C.G({{refKind}} System.Threading.CancellationToken)"
+ IL_0013: nop
+ // sequence point: }
+ IL_0014: ret
+ }
+ """);
+ }
+
+ [Fact]
+ public void ArgumentReplacement_Named()
+ {
+ var source = """
+ using System.Threading;
+ using System.Threading.Tasks;
+
+ class C
+ {
+ void F(CancellationToken token)
+ {
+ G(token: token, a: 1);
+ }
+
+ void G(int a, CancellationToken token)
+ {
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ verifier.VerifyMethodBody("C.F", """
+ {
+ // Code size 25 (0x19)
+ .maxstack 3
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point: G(token: token, a: 1);
+ IL_000b: ldarg.0
+ IL_000c: ldc.i4.1
+ IL_000d: ldsfld "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0012: call "void C.G(int, System.Threading.CancellationToken)"
+ IL_0017: nop
+ // sequence point: }
+ IL_0018: ret
+ }
+ """);
+ }
+
+ [Fact]
+ public void ArgumentReplacement_Optional()
+ {
+ var source = """
+ using System.Threading;
+ using System.Threading.Tasks;
+
+ class C
+ {
+ void F(CancellationToken token)
+ {
+ G(1);
+ }
+
+ void G(int a, CancellationToken token = default)
+ {
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ verifier.VerifyMethodBody("C.F", """
+ {
+ // Code size 25 (0x19)
+ .maxstack 3
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point: G(1);
+ IL_000b: ldarg.0
+ IL_000c: ldc.i4.1
+ IL_000d: ldsfld "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0012: call "void C.G(int, System.Threading.CancellationToken)"
+ IL_0017: nop
+ // sequence point: }
+ IL_0018: ret
+ }
+ """);
+ }
+
+ [Fact]
+ public void ArgumentReplacement_IndirectCall_Positional()
+ {
+ var source = """
+ using System;
+ using System.Threading;
+
+ class C
+ {
+ void F(CancellationToken token)
+ {
+ var g = new Action(G);
+ g(token);
+ }
+
+ void G(CancellationToken token)
+ {
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ verifier.VerifyMethodBody("C.F", $$"""
+ {
+ // Code size 37 (0x25)
+ .maxstack 2
+ .locals init (System.Action V_0) //g
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_0005: call "void System.Threading.CancellationToken.ThrowIfCancellationRequested()"
+ // sequence point: {
+ IL_000a: nop
+ // sequence point: var g = new Action(G);
+ IL_000b: ldarg.0
+ IL_000c: ldftn "void C.G(System.Threading.CancellationToken)"
+ IL_0012: newobj "System.Action..ctor(object, {{NativeIntDisplay}})"
+ IL_0017: stloc.0
+ // sequence point: g(token);
+ IL_0018: ldloc.0
+ IL_0019: ldsfld "System.Threading.CancellationToken .ModuleCancellationToken"
+ IL_001e: callvirt "void System.Action.Invoke(System.Threading.CancellationToken)"
+ IL_0023: nop
+ // sequence point: }
+ IL_0024: ret
+ }
+ """);
+ }
+
+ [Theory]
+ [InlineData("ref")]
+ [InlineData("out")]
+ [InlineData("in")]
+ public void ArgumentReplacement_IndirectCall_RefKinds(string refKind)
+ {
+ var source = $$"""
+ using System.Threading;
+
+ delegate void D({{refKind}} CancellationToken token);
+
+ class C
+ {
+ void F(CancellationToken token)
+ {
+ var g = new D(G);
+ g({{refKind}} token);
+ }
+
+ void G({{refKind}} CancellationToken token)
+ {
+ throw null;
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ // Doesn't force module-level cancellation token to ref/in/out parameters as these are not commonly used in cancellable APIs.
+ AssertNotInstrumentedWithTokenLoad(verifier, "C.F");
+ }
+
+ [Fact]
+ public void ArgumentReplacement_IndirectCall_Named()
+ {
+ var source = """
+ using System.Threading;
+ using System.Threading.Tasks;
+
+ delegate void D(int a, CancellationToken token);
+
+ class C
+ {
+ void F(CancellationToken token)
+ {
+ var g = new D(G);
+ g(token: token, a: 1);
+ }
+
+ void G(int a, CancellationToken token)
+ {
+ }
+ }
+ """;
+
+ var verifier = CompileAndVerify(source);
+
+ verifier.VerifyMethodBody("C.F", $$"""
+ {
+ // Code size 38 (0x26)
+ .maxstack 3
+ .locals init (D V_0) //g
+ // sequence point:
+ IL_0000: ldsflda "System.Threading.CancellationToken