Skip to content

Fix locals initialization of generic instance type #3188

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
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
33 changes: 16 additions & 17 deletions src/CLR/Core/Execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1915,7 +1915,6 @@ HRESULT CLR_RT_ExecutionEngine::InitializeLocals(
{
NanoCLRDataType dt = DATATYPE_VOID;
CLR_RT_TypeDef_Index cls;
CLR_RT_TypeSpec_Index typeSpecIndex;
CLR_UINT32 levels = 0;
NanoCLRDataType dtModifier = DATATYPE_VOID;

Expand Down Expand Up @@ -1963,10 +1962,17 @@ HRESULT CLR_RT_ExecutionEngine::InitializeLocals(
// otherwise the comparison won't be possible
sig--;

if (!assembly->FindTypeSpec(sig, typeSpecIndex))
{
NANOCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
}
// Parse the TypeSpec signature to get the instantiated element
CLR_RT_SignatureParser sp;
sp.Initialize_TypeSpec(assembly, sig);

CLR_RT_SignatureParser::Element element;
NANOCLR_CHECK_HRESULT(sp.Advance(element));

// element.Class and element.DataType represent the T
cls = element.Class;
dt = element.DataType;

goto done;

case DATATYPE_VAR:
Expand All @@ -1977,7 +1983,7 @@ HRESULT CLR_RT_ExecutionEngine::InitializeLocals(
// parse the locals-signature to extract that T
CLR_RT_SignatureParser parser;
parser.Initialize_MethodLocals(assembly, methodDef);
CLR_RT_SignatureParser::Element element;
CLR_RT_SignatureParser::Element sigElement;

// ensure we don’t walk past the available generic parameters
const int maxParams = methodDefInstance.target->genericParamCount;
Expand All @@ -1987,17 +1993,17 @@ HRESULT CLR_RT_ExecutionEngine::InitializeLocals(
}

// advance into the VAR entry
parser.Advance(element);
parser.Advance(sigElement);

// walk forward to the Nth generic-parameter
for (int i = 0; i < genericParamPosition; i++)
{
parser.Advance(element);
parser.Advance(sigElement);
}

// element.Class and element.DataType represent the T
cls = element.Class;
dt = element.DataType;
cls = sigElement.Class;
dt = sigElement.DataType;

goto done;
}
Expand Down Expand Up @@ -2081,13 +2087,6 @@ HRESULT CLR_RT_ExecutionEngine::InitializeLocals(
NANOCLR_CHECK_HRESULT(NewObject(*locals, inst));
}
}
else if (dt == DATATYPE_GENERICINST)
{
// locals for generic instances are always OBJECT type
dt = DATATYPE_OBJECT;
locals->SetDataId(CLR_RT_HEAPBLOCK_RAW_ID(dt, CLR_RT_HeapBlock::HB_Alive, 1));
locals->ClearData();
}
else
{
if (c_CLR_RT_DataTypeLookup[dt].m_flags & CLR_RT_DataTypeLookup::c_Reference)
Expand Down
58 changes: 3 additions & 55 deletions src/CLR/Core/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2569,33 +2569,7 @@ HRESULT CLR_RT_Thread::Execute_IL(CLR_RT_StackFrame &stackArg)
CLR_RT_HeapBlock *obj = &evalPos[0];
NanoCLRDataType dt = obj->DataType();

// If it's a byref, it must be a struct instance on the stack/heap
bool instanceIsByRef =
(obj->DataType() == DATATYPE_BYREF) || (obj->DataType() == DATATYPE_ARRAY_BYREF);

if (instanceIsByRef)
{
// extra check for DATATYPE_DATETIME and DATATYPE_TIMESPAN (special cases)
if (obj->Dereference()->DataType() == DATATYPE_DATETIME ||
obj->Dereference()->DataType() == DATATYPE_TIMESPAN)
{
NANOCLR_CHECK_HRESULT(CLR_RT_TypeDescriptor::ExtractObjectAndDataType(obj, dt));
}
else
{
// we already have a pointer to the raw struct
dt = DATATYPE_VALUETYPE;

obj = obj->Dereference();
FAULT_ON_NULL(obj);
}
}
else
{
// ordinary object/array
FAULT_ON_NULL(obj);
NANOCLR_CHECK_HRESULT(CLR_RT_TypeDescriptor::ExtractObjectAndDataType(obj, dt));
}
NANOCLR_CHECK_HRESULT(CLR_RT_TypeDescriptor::ExtractObjectAndDataType(obj, dt));

switch (dt)
{
Expand Down Expand Up @@ -2691,35 +2665,9 @@ HRESULT CLR_RT_Thread::Execute_IL(CLR_RT_StackFrame &stackArg)
}

CLR_RT_HeapBlock *obj = &evalPos[1];
NanoCLRDataType dt;

// If it's a byref, it must be a struct instance on the stack/heap
bool instanceIsByRef =
(obj->DataType() == DATATYPE_BYREF) || (obj->DataType() == DATATYPE_ARRAY_BYREF);

if (instanceIsByRef)
{
// extra check for DATATYPE_DATETIME and DATATYPE_TIMESPAN (special cases)
if (obj->Dereference()->DataType() == DATATYPE_DATETIME ||
obj->Dereference()->DataType() == DATATYPE_TIMESPAN)
{
NANOCLR_CHECK_HRESULT(CLR_RT_TypeDescriptor::ExtractObjectAndDataType(obj, dt));
}
else
{
// we already have a pointer to the raw struct
dt = DATATYPE_VALUETYPE;
NanoCLRDataType dt = obj->DataType();

// follow the byref so obj really points at the struct
obj = obj->Dereference();
}
}
else
{
// ordinary object/array
FAULT_ON_NULL(obj);
NANOCLR_CHECK_HRESULT(CLR_RT_TypeDescriptor::ExtractObjectAndDataType(obj, dt));
}
NANOCLR_CHECK_HRESULT(CLR_RT_TypeDescriptor::ExtractObjectAndDataType(obj, dt));

switch (dt)
{
Expand Down