Skip to content

Commit 1a9df91

Browse files
Fix dataflow analysis of lambdas in async methods (#122450)
When we enter ILLinker part of the ILC codebase, we need to switch to the mode where async variants don't exist and async methods have mismatched bodies. There was a spot where we didn't do the switch here: https://github.com/dotnet/runtime/blob/d31e5990b896447bfc3dbe98cfe6ec3b169a4896/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs#L939-L949 The fix is to do the switch early in `FlowAnnotations` so that the `ILProvider` `FlowAnnotations` use (and other parts take advantage of) is already ILLinkerified. Fixes the situation shown in the regression test (we were analyzing the IL of the async variant of `RuntimeAsyncWithLambda` and missed analyzing the lambda completely).
1 parent 512c425 commit 1a9df91

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/FlowAnnotations.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public sealed partial class FlowAnnotations
3636

3737
public FlowAnnotations(Logger logger, ILProvider ilProvider, CompilerGeneratedState compilerGeneratedState)
3838
{
39+
ilProvider = new AsyncMaskingILProvider(ilProvider);
40+
3941
_hashtable = new TypeAnnotationsHashtable(logger, ilProvider, compilerGeneratedState);
4042
_logger = logger;
4143
ILProvider = ilProvider;
@@ -299,7 +301,7 @@ private sealed class TypeAnnotationsHashtable : LockFreeReaderHashtable<TypeDesc
299301
private readonly CompilerGeneratedState _compilerGeneratedState;
300302

301303
public TypeAnnotationsHashtable(Logger logger, ILProvider ilProvider, CompilerGeneratedState compilerGeneratedState) =>
302-
(_logger, _ilProvider, _compilerGeneratedState) = (logger, new AsyncMaskingILProvider(ilProvider), compilerGeneratedState);
304+
(_logger, _ilProvider, _compilerGeneratedState) = (logger, ilProvider, compilerGeneratedState);
303305

304306
private static DynamicallyAccessedMemberTypes GetMemberTypesForDynamicallyAccessedMembersAttribute(MetadataReader reader, CustomAttributeHandleCollection customAttributeHandles)
305307
{

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/MethodBodyScanner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal abstract partial class MethodBodyScanner
5555
protected MethodBodyScanner(FlowAnnotations annotations)
5656
{
5757
_annotations = annotations;
58-
InterproceduralStateLattice = new InterproceduralStateLattice(new AsyncMaskingILProvider(annotations.ILProvider), default, default);
58+
InterproceduralStateLattice = new InterproceduralStateLattice(annotations.ILProvider, default, default);
5959
}
6060

6161
protected virtual void WarnAboutInvalidILInMethod(MethodIL method, int ilOffset)

src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/RuntimeAsyncMethods.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow
1414
[SkipKeptItemsValidation]
1515
[SetupCompileArgument("/features:runtime-async=on")]
1616
[SetupCompileArgument("/nowarn:SYSLIB5007")]
17+
[ExpectedNoWarnings]
1718
public class RuntimeAsyncMethods
1819
{
1920
public static async Task Main()
@@ -26,6 +27,7 @@ public static async Task Main()
2627
await RuntimeAsyncReturningAnnotatedType();
2728
await RuntimeAsyncWithCorrectParameter(null);
2829
await RuntimeAsyncWithLocalAll();
30+
await RuntimeAsyncWithLambda();
2931
}
3032

3133
static async Task BasicRuntimeAsyncMethod()
@@ -103,5 +105,16 @@ static async Task RuntimeAsyncWithLocalAll()
103105
await Task.Delay(1);
104106
t.RequiresAll();
105107
}
108+
109+
class TypeWithRucMethod
110+
{
111+
[RequiresUnreferencedCode("RUC")]
112+
public static void RucMethod() { }
113+
}
114+
115+
static async Task RuntimeAsyncWithLambda()
116+
{
117+
await Task.Run([ExpectedWarning("IL2026", nameof(TypeWithRucMethod.RucMethod))] () => typeof(TypeWithRucMethod).GetMethods());
118+
}
106119
}
107120
}

0 commit comments

Comments
 (0)