Skip to content

Properly handle byrefs in tailcall helper stubs #41815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2020
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
2 changes: 2 additions & 0 deletions src/coreclr/src/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ DEFINE_FIELD(NULL, VALUE, Value)
DEFINE_CLASS(NULLABLE, System, Nullable`1)

DEFINE_CLASS(BYREFERENCE, System, ByReference`1)
DEFINE_METHOD(BYREFERENCE, CTOR, .ctor, NoSig)
DEFINE_METHOD(BYREFERENCE, GET_VALUE, get_Value, NoSig)
DEFINE_CLASS(SPAN, System, Span`1)
DEFINE_METHOD(SPAN, GET_ITEM, get_Item, IM_Int_RetRefT)
DEFINE_CLASS(READONLY_SPAN, System, ReadOnlySpan`1)
Expand Down
61 changes: 44 additions & 17 deletions src/coreclr/src/vm/tailcallhelp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ MethodDesc* TailCallHelp::CreateStoreArgsStub(TailCallInfo& info)
for (COUNT_T i = 0; i < info.ArgBufLayout.Values.GetCount(); i++)
{
const ArgBufferValue& arg = info.ArgBufLayout.Values[i];
CorElementType ty = arg.TyHnd.GetSignatureCorElementType();

emitOffs(arg.Offset);
pCode->EmitLDARG(i);
Expand Down Expand Up @@ -629,35 +628,63 @@ void TailCallHelp::CreateCallTargetStubSig(const TailCallInfo& info, SigBuilder*
#endif // _DEBUG
}

// Get TypeHandle for ByReference<System.Byte>
static TypeHandle GetByReferenceOfByteType()
{
TypeHandle byteTH(CoreLibBinder::GetElementType(ELEMENT_TYPE_U1));
Instantiation byteInst(&byteTH, 1);
TypeHandle th = TypeHandle(CoreLibBinder::GetClass(CLASS__BYREFERENCE)).Instantiate(byteInst);
return th;
}

// Get MethodDesc* for ByReference<System.Byte>::get_Value
static MethodDesc* GetByReferenceOfByteValueGetter()
{
MethodDesc* getter = CoreLibBinder::GetMethod(METHOD__BYREFERENCE__GET_VALUE);
getter =
MethodDesc::FindOrCreateAssociatedMethodDesc(
getter,
GetByReferenceOfByteType().GetMethodTable(),
false,
Instantiation(),
TRUE);

return getter;
}

// Get MethodDesc* for ByReference<System.Byte>::.ctor
static MethodDesc* GetByReferenceOfByteCtor()
{
MethodDesc* ctor = CoreLibBinder::GetMethod(METHOD__BYREFERENCE__CTOR);
ctor =
MethodDesc::FindOrCreateAssociatedMethodDesc(
ctor,
GetByReferenceOfByteType().GetMethodTable(),
false,
Instantiation(),
TRUE);

return ctor;
}

void TailCallHelp::EmitLoadTyHnd(ILCodeStream* stream, TypeHandle tyHnd)
{
CorElementType ty = tyHnd.GetSignatureCorElementType();
if (tyHnd.IsByRef())
{
// Note: we can use an "untracked" ldind.i here even with byrefs because
// we are loading between two tracked positions.
stream->EmitLDIND_I();
}
stream->EmitCALL(stream->GetToken(GetByReferenceOfByteValueGetter()), 1, 1);
else
{
int token = stream->GetToken(tyHnd);
stream->EmitLDOBJ(token);
}
stream->EmitLDOBJ(stream->GetToken(tyHnd));
}

void TailCallHelp::EmitStoreTyHnd(ILCodeStream* stream, TypeHandle tyHnd)
{
CorElementType ty = tyHnd.GetSignatureCorElementType();
if (tyHnd.IsByRef())
{
// Note: we can use an "untracked" stind.i here even with byrefs because
// we are storing between two tracked positions.
stream->EmitSTIND_I();
stream->EmitNEWOBJ(stream->GetToken(GetByReferenceOfByteCtor()), 1);
stream->EmitSTOBJ(stream->GetToken(GetByReferenceOfByteType()));
}
else
{
int token = stream->GetToken(tyHnd);
stream->EmitSTOBJ(token);
stream->EmitSTOBJ(stream->GetToken(tyHnd));
}
}

Expand Down