Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions src/coreclr/tools/ILVerification/ILImporter.Verify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,22 @@ void ImportReturn()

var declaredReturnType = _method.Signature.ReturnType;

if (declaredReturnType.IsVoid)
// For async methods, unwrap Task/ValueTask return types
TypeDesc expectedReturnType = declaredReturnType;
if (_method.IsAsync)
{
if (IsTaskOrValueTaskType(declaredReturnType, out TypeDesc unwrappedType))
{
expectedReturnType = unwrappedType;
}
else
{
// Async methods must return Task or ValueTask
VerificationError(VerifierError.StackUnexpected);
}
}

if (expectedReturnType.IsVoid)
{
Debug.Assert(_stackTop >= 0);

Expand All @@ -1891,11 +1906,49 @@ void ImportReturn()
Check(_stackTop == 1, VerifierError.ReturnEmpty);

var actualReturnType = Pop();
CheckIsAssignable(actualReturnType, StackValue.CreateFromType(declaredReturnType));
CheckIsAssignable(actualReturnType, StackValue.CreateFromType(expectedReturnType));

Check((!expectedReturnType.IsByRef && !expectedReturnType.IsByRefLike) || actualReturnType.IsPermanentHome, VerifierError.ReturnPtrToStack);
}
}
}

bool IsTaskOrValueTaskType(TypeDesc type, out TypeDesc unwrappedType)
{
unwrappedType = null;

if (type is not MetadataType metadataType)
return false;

if (!metadataType.Namespace.SequenceEqual("System.Threading.Tasks"u8))
return false;

// Check for Task (non-generic)
if (metadataType.Name.SequenceEqual("Task"u8) && !metadataType.HasInstantiation)
{
unwrappedType = _typeSystemContext.GetWellKnownType(WellKnownType.Void);
return true;
}

Check((!declaredReturnType.IsByRef && !declaredReturnType.IsByRefLike) || actualReturnType.IsPermanentHome, VerifierError.ReturnPtrToStack);
// Check for ValueTask (non-generic)
if (metadataType.Name.SequenceEqual("ValueTask"u8) && !metadataType.HasInstantiation)
{
unwrappedType = _typeSystemContext.GetWellKnownType(WellKnownType.Void);
return true;
}

// Check for Task<T> and ValueTask<T>
if (metadataType.HasInstantiation && metadataType.Instantiation.Length == 1)
{
if (metadataType.Name.SequenceEqual("Task`1"u8) ||
metadataType.Name.SequenceEqual("ValueTask`1"u8))
{
unwrappedType = metadataType.Instantiation[0];
return true;
}
}

return false;
}

void ImportFallthrough(BasicBlock next)
Expand Down
159 changes: 159 additions & 0 deletions src/tests/ilverify/ILTests/RuntimeAsyncTests.il
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

.assembly extern System.Runtime
{
}

.assembly RuntimeAsyncTests
{
}

.class public auto ansi beforefieldinit RuntimeAsyncTestsType
extends [System.Runtime]System.Object
{
// Valid async method returning Task<int> with int on stack
.method public hidebysig instance class [System.Runtime]System.Threading.Tasks.Task`1<int32> AsyncTaskInt_Valid() cil managed async
{
ldc.i4.0
ret
}

// Valid async method returning ValueTask<int> with int on stack
.method public hidebysig instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask`1<int32> AsyncValueTaskInt_Valid() cil managed async
{
ldc.i4.0
ret
}

// Valid async method returning ValueTask<bool> with bool (i4) on stack
.method public hidebysig instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask`1<bool> AsyncValueTaskBool_Valid() cil managed async
{
ldc.i4.1
ret
}

// Valid async method returning ValueTask<object> with object on stack
.method public hidebysig instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask`1<object> AsyncValueTaskObject_Valid() cil managed async
{
ldnull
ret
}

// Invalid: async method with wrong stack state - empty stack for ValueTask<int>
.method public hidebysig instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask`1<int32> AsyncEmptyStack_Invalid_ReturnMissing() cil managed async
{
ret
}

// Invalid: async method with wrong type on stack
.method public hidebysig instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask`1<int32> AsyncWrongType_Invalid_StackUnexpected() cil managed async
{
ldnull
ret
}

// Invalid: async method with extra items on stack
.method public hidebysig instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask`1<int32> AsyncExtraStack_Invalid_ReturnEmpty() cil managed async
{
ldc.i4.0
ldc.i4.1
ret
}

// Invalid: async method with ValueTask return should have empty stack
.method public hidebysig instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask AsyncReturnsInt_Invalid_ReturnVoid() cil managed async
{
ldc.i4.0
ret
}

// Invalid: method with async flag but returning integer type
.method public hidebysig instance int32 NonAsyncReturnType_Invalid_StackUnexpected() cil managed async
{
ldc.i4.0
ret
}

// Valid: async method returning Task with nothing on stack
.method public hidebysig instance class [System.Runtime]System.Threading.Tasks.Task AsyncTask_Valid() cil managed async
{
ret
}

// Valid: async method returning ValueTask with nothing on stack
.method public hidebysig instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask AsyncValueTask_Valid() cil managed async
{
ret
}

// Invalid: async method returning from try block (not allowed)
.method public hidebysig instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask`1<int32> AsyncReturnFromTry_Invalid_ReturnFromTry() cil managed async
{
.try
{
ldc.i4.0
ret
}
catch [System.Runtime]System.Object
{
pop
leave.s lbl_ret
}

lbl_ret:
ldc.i4.1
ret
}

// Invalid: async method returning from catch block (not allowed)
.method public hidebysig instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask`1<int32> AsyncReturnFromCatch_Invalid_ReturnFromHandler() cil managed async
{
.try
{
leave.s lbl_ret
}
catch [System.Runtime]System.Object
{
pop
ldc.i4.0
ret
}

lbl_ret:
ldc.i4.1
ret
}

// Invalid: async method returning from filter (still not allowed)
.method public hidebysig instance valuetype [System.Runtime]System.Threading.Tasks.ValueTask`1<int32> AsyncReturnFromFilter_Invalid_ReturnFromFilter() cil managed async
{
.try
{
leave.s lbl_ret
}
filter
{
pop
ldc.i4.0
ret

endfilter
}
{
pop
leave.s lbl_ret
}

lbl_ret:
ldc.i4.1
ret
}

.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
ldarg.0
call instance void [System.Runtime]System.Object::.ctor()
ret
}
}
3 changes: 3 additions & 0 deletions src/tests/ilverify/ILTests/RuntimeAsyncTests.ilproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="ILTests.targets" />
</Project>
Loading