Skip to content
Closed

test #127490

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

Large diffs are not rendered by default.

40 changes: 19 additions & 21 deletions src/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,15 @@ public static MethodIL EmitTaskReturningThunk(MethodDesc taskReturningMethod, Me

ILLocalVariable returnTaskLocal = emitter.NewLocal(returnType);

MetadataType asyncHelpersType = context.SystemModule.GetKnownType("System.Runtime.CompilerServices"u8, "AsyncHelpers"u8);
TypeDesc stackStateType = asyncHelpersType.GetKnownNestedType("RuntimeAsyncStackState"u8);
ILLocalVariable stackStateLocal = emitter.NewLocal(stackStateType);
TypeDesc awaitStateType = asyncHelpersType.GetKnownNestedType("RuntimeAsyncAwaitState"u8);
ILLocalVariable refAwaitStateLocal = emitter.NewLocal(awaitStateType.MakeByRefType());
TypeDesc executionAndSyncBlockStoreType = context.SystemModule.GetKnownType("System.Runtime.CompilerServices"u8, "ExecutionAndSyncBlockStore"u8);
ILLocalVariable executionAndSyncBlockStoreLocal = emitter.NewLocal(executionAndSyncBlockStoreType);

ILCodeLabel returnTaskLabel = emitter.NewCodeLabel();
ILCodeLabel suspendedLabel = emitter.NewCodeLabel();
ILCodeLabel finishedLabel = emitter.NewCodeLabel();

codestream.Emit(ILOpcode.ldsflda, emitter.NewToken(asyncHelpersType.GetKnownField("t_runtimeAsyncAwaitState"u8)));
codestream.EmitStLoc(refAwaitStateLocal);

codestream.EmitLdLoc(refAwaitStateLocal);
codestream.EmitLdLoca(stackStateLocal);
codestream.Emit(ILOpcode.call, emitter.NewToken(awaitStateType.GetKnownMethod("Push"u8, null)));
codestream.EmitLdLoca(executionAndSyncBlockStoreLocal);
codestream.Emit(ILOpcode.call, emitter.NewToken(executionAndSyncBlockStoreType.GetKnownMethod("Push"u8, null)));

ILExceptionRegionBuilder tryFinallyRegion = emitter.NewFinallyRegion();
{
Expand Down Expand Up @@ -97,7 +90,9 @@ public static MethodIL EmitTaskReturningThunk(MethodDesc taskReturningMethod, Me
codestream.EmitStLoc(logicalResultLocal);
}

MethodDesc asyncCallContinuationMd = asyncHelpersType.GetKnownMethod("AsyncCallContinuation"u8, null);
MethodDesc asyncCallContinuationMd = context.SystemModule
.GetKnownType("System.Runtime.CompilerServices"u8, "AsyncHelpers"u8)
.GetKnownMethod("AsyncCallContinuation"u8, null);

codestream.Emit(ILOpcode.call, emitter.NewToken(asyncCallContinuationMd));

Expand Down Expand Up @@ -166,7 +161,8 @@ public static MethodIL EmitTaskReturningThunk(MethodDesc taskReturningMethod, Me
parameters: new[] { exceptionType }
);

fromExceptionMd = asyncHelpersType
fromExceptionMd = context.SystemModule
.GetKnownType("System.Runtime.CompilerServices"u8, "AsyncHelpers"u8)
.GetKnownMethod(isValueTask ? "ValueTaskFromException"u8 : "TaskFromException"u8, fromExceptionSignature)
.MakeInstantiatedMethod(new Instantiation(logicalReturnType));
}
Expand All @@ -179,7 +175,8 @@ public static MethodIL EmitTaskReturningThunk(MethodDesc taskReturningMethod, Me
parameters: new[] { exceptionType }
);

fromExceptionMd = asyncHelpersType
fromExceptionMd = context.SystemModule
.GetKnownType("System.Runtime.CompilerServices"u8, "AsyncHelpers"u8)
.GetKnownMethod(isValueTask ? "ValueTaskFromException"u8 : "TaskFromException"u8, fromExceptionSignature);
}

Expand All @@ -198,10 +195,11 @@ public static MethodIL EmitTaskReturningThunk(MethodDesc taskReturningMethod, Me
MethodSignatureFlags.Static,
genericParameterCount: 1,
returnType: ((MetadataType)returnType.GetTypeDefinition()).MakeInstantiatedType(context.GetSignatureVariable(0, true)),
parameters: [awaitStateType.MakeByRefType()]
parameters: Array.Empty<TypeDesc>()
);

finalizeTaskReturningThunkMd = asyncHelpersType
finalizeTaskReturningThunkMd = context.SystemModule
.GetKnownType("System.Runtime.CompilerServices"u8, "AsyncHelpers"u8)
.GetKnownMethod(isValueTask ? "FinalizeValueTaskReturningThunk"u8 : "FinalizeTaskReturningThunk"u8, finalizeReturningThunkSignature)
.MakeInstantiatedMethod(new Instantiation(logicalReturnType));
}
Expand All @@ -211,14 +209,14 @@ public static MethodIL EmitTaskReturningThunk(MethodDesc taskReturningMethod, Me
MethodSignatureFlags.Static,
genericParameterCount: 0,
returnType: returnType,
parameters: [awaitStateType.MakeByRefType()]
parameters: Array.Empty<TypeDesc>()
);

finalizeTaskReturningThunkMd = asyncHelpersType
finalizeTaskReturningThunkMd = context.SystemModule
.GetKnownType("System.Runtime.CompilerServices"u8, "AsyncHelpers"u8)
.GetKnownMethod(isValueTask ? "FinalizeValueTaskReturningThunk"u8 : "FinalizeTaskReturningThunk"u8, finalizeReturningThunkSignature);
}

codestream.EmitLdLoc(refAwaitStateLocal);
codestream.Emit(ILOpcode.call, emitter.NewToken(finalizeTaskReturningThunkMd));
codestream.EmitStLoc(returnTaskLocal);
codestream.Emit(ILOpcode.leave, returnTaskLabel);
Expand All @@ -229,8 +227,8 @@ public static MethodIL EmitTaskReturningThunk(MethodDesc taskReturningMethod, Me
{
codestream.BeginHandler(tryFinallyRegion);

codestream.EmitLdLoc(refAwaitStateLocal);
codestream.Emit(ILOpcode.call, emitter.NewToken(awaitStateType.GetKnownMethod("Pop"u8, null)));
codestream.EmitLdLoca(executionAndSyncBlockStoreLocal);
codestream.Emit(ILOpcode.call, emitter.NewToken(executionAndSyncBlockStoreType.GetKnownMethod("Pop"u8, null)));
codestream.Emit(ILOpcode.endfinally);
codestream.EndHandler(tryFinallyRegion);
}
Expand Down
34 changes: 11 additions & 23 deletions src/coreclr/vm/asyncthunks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ void MethodDesc::EmitTaskReturningThunk(MethodDesc* pAsyncCallVariant, MetaSig&

// Emits roughly the following code:
//
// RuntimeAsyncStackState stackState;
// ref RuntimeAsyncAwaitState awaitState = ref AsyncHelpers.t_runtimeAsyncAwaitState;
// awaitState.Push(&stackState);
//
// ExecutionAndSyncBlockStore store = default;
// store.Push();
// try
// {
// try
Expand All @@ -106,7 +104,7 @@ void MethodDesc::EmitTaskReturningThunk(MethodDesc* pAsyncCallVariant, MetaSig&
// if (AsyncHelpers.AsyncCallContinuation() == null)
// return Task.FromResult(result);
//
// return FinalizeTaskReturningThunk(ref awaitState);
// return FinalizeTaskReturningThunk();
// }
// catch (Exception ex)
// {
Expand All @@ -115,7 +113,7 @@ void MethodDesc::EmitTaskReturningThunk(MethodDesc* pAsyncCallVariant, MetaSig&
// }
// finally
// {
// awaitState.Pop();
// store.Pop();
// }

ILCodeStream* pCode = pSL->NewCodeStream(ILStubLinker::kDispatch);
Expand All @@ -134,24 +132,15 @@ void MethodDesc::EmitTaskReturningThunk(MethodDesc* pAsyncCallVariant, MetaSig&

LocalDesc returnLocalDesc(thTaskRet);
DWORD returnTaskLocal = pCode->NewLocal(returnLocalDesc);

LocalDesc stackStateLocalDesc(TypeHandle(CoreLibBinder::GetClass(CLASS__RUNTIME_ASYNC_STACK_STATE)));
DWORD stackStateLocal = pCode->NewLocal(stackStateLocalDesc);

LocalDesc refAwaitStateLocalDesc(TypeHandle(CoreLibBinder::GetClass(CLASS__RUNTIME_ASYNC_AWAIT_STATE)));
refAwaitStateLocalDesc.MakeByRef();
DWORD refAwaitStateLocal = pCode->NewLocal(refAwaitStateLocalDesc);
LocalDesc executionAndSyncBlockStoreLocalDesc(CoreLibBinder::GetClass(CLASS__EXECUTIONANDSYNCBLOCKSTORE));
DWORD executionAndSyncBlockStoreLocal = pCode->NewLocal(executionAndSyncBlockStoreLocalDesc);

ILCodeLabel* returnTaskLabel = pCode->NewCodeLabel();
ILCodeLabel* suspendedLabel = pCode->NewCodeLabel();
ILCodeLabel* finishedLabel = pCode->NewCodeLabel();

pCode->EmitLDSFLDA(pCode->GetToken(CoreLibBinder::GetField(FIELD__ASYNC_HELPERS__TLS_RUNTIME_ASYNC_AWAIT_STATE)));
pCode->EmitSTLOC(refAwaitStateLocal);

pCode->EmitLDLOC(refAwaitStateLocal);
pCode->EmitLDLOCA(stackStateLocal);
pCode->EmitCALL(METHOD__RUNTIME_ASYNC_AWAIT_STATE__PUSH, 2, 0);
pCode->EmitLDLOCA(executionAndSyncBlockStoreLocal);
pCode->EmitCALL(pCode->GetToken(CoreLibBinder::GetMethod(METHOD__EXECUTIONANDSYNCBLOCKSTORE__PUSH)), 1, 0);

{
pCode->BeginTryBlock();
Expand Down Expand Up @@ -263,8 +252,7 @@ void MethodDesc::EmitTaskReturningThunk(MethodDesc* pAsyncCallVariant, MetaSig&
md = CoreLibBinder::GetMethod(METHOD__ASYNC_HELPERS__FINALIZE_TASK_RETURNING_THUNK);
finalizeTaskReturningThunkToken = pCode->GetToken(md);
}
pCode->EmitLDLOC(refAwaitStateLocal);
pCode->EmitCALL(finalizeTaskReturningThunkToken, 1, 1);
pCode->EmitCALL(finalizeTaskReturningThunkToken, 0, 1);
pCode->EmitSTLOC(returnTaskLocal);
pCode->EmitLEAVE(returnTaskLabel);

Expand All @@ -273,8 +261,8 @@ void MethodDesc::EmitTaskReturningThunk(MethodDesc* pAsyncCallVariant, MetaSig&
//
{
pCode->BeginFinallyBlock();
pCode->EmitLDLOC(refAwaitStateLocal);
pCode->EmitCALL(METHOD__RUNTIME_ASYNC_AWAIT_STATE__POP, 1, 0);
pCode->EmitLDLOCA(executionAndSyncBlockStoreLocal);
pCode->EmitCALL(pCode->GetToken(CoreLibBinder::GetMethod(METHOD__EXECUTIONANDSYNCBLOCKSTORE__POP)), 1, 0);
pCode->EmitENDFINALLY();
pCode->EndFinallyBlock();
}
Expand Down
23 changes: 10 additions & 13 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,10 @@ DEFINE_CLASS(RESOURCE_MANAGER, Resources, ResourceManager)
DEFINE_CLASS(RTFIELD, Reflection, RtFieldInfo)
DEFINE_METHOD(RTFIELD, GET_FIELDESC, GetFieldDesc, IM_RetIntPtr)

DEFINE_CLASS(EXECUTIONANDSYNCBLOCKSTORE, CompilerServices, ExecutionAndSyncBlockStore)
DEFINE_METHOD(EXECUTIONANDSYNCBLOCKSTORE, PUSH, Push, NoSig)
DEFINE_METHOD(EXECUTIONANDSYNCBLOCKSTORE, POP, Pop, NoSig)

DEFINE_CLASS(RUNTIME_HELPERS, CompilerServices, RuntimeHelpers)
DEFINE_METHOD(RUNTIME_HELPERS, IS_BITWISE_EQUATABLE, IsBitwiseEquatable, NoSig)
DEFINE_METHOD(RUNTIME_HELPERS, GET_RAW_DATA, GetRawData, NoSig)
Expand All @@ -707,10 +711,10 @@ DEFINE_METHOD(ASYNC_HELPERS, ALLOC_CONTINUATION, AllocContinuation,
DEFINE_METHOD(ASYNC_HELPERS, ALLOC_CONTINUATION_METHOD, AllocContinuationMethod, NoSig)
DEFINE_METHOD(ASYNC_HELPERS, ALLOC_CONTINUATION_CLASS, AllocContinuationClass, NoSig)

DEFINE_METHOD(ASYNC_HELPERS, FINALIZE_TASK_RETURNING_THUNK, FinalizeTaskReturningThunk, SM_RefRuntimeAsyncAwaitState_RetTask)
DEFINE_METHOD(ASYNC_HELPERS, FINALIZE_TASK_RETURNING_THUNK_1, FinalizeTaskReturningThunk, GM_RefRuntimeAsyncAwaitState_RetTaskOfT)
DEFINE_METHOD(ASYNC_HELPERS, FINALIZE_VALUETASK_RETURNING_THUNK, FinalizeValueTaskReturningThunk, SM_RefRuntimeAsyncAwaitState_RetValueTask)
DEFINE_METHOD(ASYNC_HELPERS, FINALIZE_VALUETASK_RETURNING_THUNK_1, FinalizeValueTaskReturningThunk, GM_RefRuntimeAsyncAwaitState_RetValueTaskOfT)
DEFINE_METHOD(ASYNC_HELPERS, FINALIZE_TASK_RETURNING_THUNK, FinalizeTaskReturningThunk, SM_RetTask)
DEFINE_METHOD(ASYNC_HELPERS, FINALIZE_TASK_RETURNING_THUNK_1, FinalizeTaskReturningThunk, GM_RetTaskOfT)
DEFINE_METHOD(ASYNC_HELPERS, FINALIZE_VALUETASK_RETURNING_THUNK, FinalizeValueTaskReturningThunk, SM_RetValueTask)
DEFINE_METHOD(ASYNC_HELPERS, FINALIZE_VALUETASK_RETURNING_THUNK_1, FinalizeValueTaskReturningThunk, GM_RetValueTaskOfT)

DEFINE_METHOD(ASYNC_HELPERS, TASK_FROM_EXCEPTION, TaskFromException, SM_Exception_RetTask)
DEFINE_METHOD(ASYNC_HELPERS, TASK_FROM_EXCEPTION_1, TaskFromException, GM_Exception_RetTaskOfT)
Expand All @@ -728,18 +732,11 @@ DEFINE_METHOD(ASYNC_HELPERS, RESTORE_CONTEXTS, RestoreContexts, No
DEFINE_METHOD(ASYNC_HELPERS, RESTORE_CONTEXTS_ON_SUSPENSION, RestoreContextsOnSuspension, NoSig)
DEFINE_METHOD(ASYNC_HELPERS, ASYNC_CALL_CONTINUATION, AsyncCallContinuation, NoSig)
DEFINE_METHOD(ASYNC_HELPERS, TAIL_AWAIT, TailAwait, NoSig)
DEFINE_FIELD(ASYNC_HELPERS, TLS_RUNTIME_ASYNC_AWAIT_STATE, t_runtimeAsyncAwaitState)

#ifdef FEATURE_INTERPRETER
DEFINE_METHOD(ASYNC_HELPERS, RESUME_INTERPRETER_CONTINUATION, ResumeInterpreterContinuation, NoSig)
#endif

DEFINE_CLASS(RUNTIME_ASYNC_AWAIT_STATE, CompilerServices, AsyncHelpers+RuntimeAsyncAwaitState)
DEFINE_METHOD(RUNTIME_ASYNC_AWAIT_STATE, PUSH, Push, NoSig)
DEFINE_METHOD(RUNTIME_ASYNC_AWAIT_STATE, POP, Pop, NoSig)

DEFINE_CLASS(RUNTIME_ASYNC_STACK_STATE, CompilerServices, AsyncHelpers+RuntimeAsyncStackState)

DEFINE_CLASS_U(CompilerServices, Continuation, ContinuationObject)
DEFINE_FIELD_U(Next, ContinuationObject, Next)
DEFINE_FIELD_U(ResumeInfo, ContinuationObject, ResumeInfo)
Expand Down Expand Up @@ -981,11 +978,11 @@ DEFINE_FIELD(EXECUTIONCONTEXT, DEFAULT_FLOW_SUPPRESSED, DefaultFlowSu
DEFINE_CLASS(DIRECTONTHREADLOCALDATA, Threading, Thread+DirectOnThreadLocalData)

DEFINE_CLASS(THREAD, Threading, Thread)
DEFINE_METHOD(THREAD, START_CALLBACK, StartCallback, SM_PtrThread_RetVoid)
DEFINE_METHOD(THREAD, START_CALLBACK, StartCallback, SM_PtrThread_RetVoid)
DEFINE_METHOD(THREAD, POLLGC, PollGC, NoSig)
DEFINE_METHOD(THREAD, ON_THREAD_EXITING, OnThreadExited, SM_PtrThread_PtrException_RetVoid)
#ifdef FOR_ILLINK
DEFINE_METHOD(THREAD, CTOR, .ctor, IM_RetVoid)
DEFINE_METHOD(THREAD, CTOR, .ctor, IM_RetVoid)
#endif // FOR_ILLINK

#ifdef FEATURE_OBJCMARSHAL
Expand Down
6 changes: 2 additions & 4 deletions src/coreclr/vm/metasig.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,8 @@ DEFINE_METASIG(SM(PtrByte_RetStr, P(b), s))
DEFINE_METASIG(SM(Str_RetPtrByte, s, P(b)))
DEFINE_METASIG(SM(PtrByte_RetVoid, P(b), v))

DEFINE_METASIG_T(SM(RefRuntimeAsyncAwaitState_RetTask, r(g(RUNTIME_ASYNC_AWAIT_STATE)), C(TASK)))
DEFINE_METASIG_T(SM(RefRuntimeAsyncAwaitState_RetValueTask, r(g(RUNTIME_ASYNC_AWAIT_STATE)), g(VALUETASK)))
DEFINE_METASIG_T(GM(RefRuntimeAsyncAwaitState_RetTaskOfT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, r(g(RUNTIME_ASYNC_AWAIT_STATE)), GI(C(TASK_1), 1, M(0))))
DEFINE_METASIG_T(GM(RefRuntimeAsyncAwaitState_RetValueTaskOfT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, r(g(RUNTIME_ASYNC_AWAIT_STATE)), GI(g(VALUETASK_1), 1, M(0))))
DEFINE_METASIG_T(GM(RetTaskOfT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, , GI(C(TASK_1), 1, M(0))))
DEFINE_METASIG_T(GM(RetValueTaskOfT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, , GI(g(VALUETASK_1), 1, M(0))))

// Undefine macros in case we include the file again in the compilation unit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ public static partial class AsyncHelpers
[BypassReadyToRun]
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.Async)]
[StackTraceHidden]
public static unsafe void AwaitAwaiter<TAwaiter>(TAwaiter awaiter) where TAwaiter : INotifyCompletion
public static void AwaitAwaiter<TAwaiter>(TAwaiter awaiter) where TAwaiter : INotifyCompletion
{
ref RuntimeAsyncAwaitState state = ref t_runtimeAsyncAwaitState;
Continuation? sentinelContinuation = state.SentinelContinuation ??= new Continuation();
state.StackState->Notifier = awaiter;
Continuation? sentinelContinuation = state.SentinelContinuation;
if (sentinelContinuation == null)
state.SentinelContinuation = sentinelContinuation = new Continuation();

state.Notifier = awaiter;
state.CaptureContexts();
AsyncSuspend(sentinelContinuation);
}
Expand All @@ -45,11 +48,14 @@ public static unsafe void AwaitAwaiter<TAwaiter>(TAwaiter awaiter) where TAwaite
[BypassReadyToRun]
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.Async)]
[StackTraceHidden]
public static unsafe void UnsafeAwaitAwaiter<TAwaiter>(TAwaiter awaiter) where TAwaiter : ICriticalNotifyCompletion
public static void UnsafeAwaitAwaiter<TAwaiter>(TAwaiter awaiter) where TAwaiter : ICriticalNotifyCompletion
{
ref RuntimeAsyncAwaitState state = ref t_runtimeAsyncAwaitState;
Continuation? sentinelContinuation = state.SentinelContinuation ??= new Continuation();
state.StackState->CriticalNotifier = awaiter;
Continuation? sentinelContinuation = state.SentinelContinuation;
if (sentinelContinuation == null)
state.SentinelContinuation = sentinelContinuation = new Continuation();

state.CriticalNotifier = awaiter;
state.CaptureContexts();
AsyncSuspend(sentinelContinuation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ CustomQueryInterfaceResult ICustomQueryInterface.GetInterface(ref Guid iid, out
try
{
Guid iidMetaDataImport = typeof(IMetaDataImport).GUID;
if (_legacyModulePointer != 0 && Marshal.QueryInterface(_legacyModulePointer, iidMetaDataImport, out nint ppMdi) >= 0)
if (_legacyModulePointer != 0 && _legacyModulePointer != 1 && Marshal.QueryInterface(_legacyModulePointer, iidMetaDataImport, out nint ppMdi) >= 0)
{
legacyImport = ComInterfaceMarshaller<IMetaDataImport>.ConvertToManaged((void*)ppMdi);
Marshal.Release(ppMdi);
Expand Down
Loading