Skip to content

Fix loading token for TypeSpec #3189

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 1 commit into from
Jun 24, 2025
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
47 changes: 42 additions & 5 deletions src/CLR/Core/CLR_RT_HeapBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,54 @@ HRESULT CLR_RT_HeapBlock::SetReflection(const CLR_RT_Assembly_Index &assm)
NANOCLR_NOCLEANUP_NOLABEL();
}

HRESULT CLR_RT_HeapBlock::SetReflection(const CLR_RT_TypeSpec_Index &sig)
HRESULT CLR_RT_HeapBlock::SetReflection(const CLR_RT_TypeSpec_Instance &tsInst, const CLR_RT_TypeSpec_Index *caller)
{
NATIVE_PROFILE_CLR_CORE();
NANOCLR_HEADER();

CLR_RT_TypeDescriptor desc{};
m_id.raw = CLR_RT_HEAPBLOCK_RAW_ID(DATATYPE_REFLECTION, 0, 1);
m_data.reflection.kind = REFLECTION_TYPE;

NANOCLR_CHECK_HRESULT(desc.InitializeFromTypeSpec(sig));
// start parsing the signature
CLR_RT_SignatureParser parser;
parser.Initialize_TypeSpec(tsInst.assembly, tsInst.target);

m_id.raw = CLR_RT_HEAPBLOCK_RAW_ID(DATATYPE_REFLECTION, 0, 1);
m_data.reflection = desc.m_reflex;
// read first element
CLR_RT_SignatureParser::Element elem;
if (FAILED(parser.Advance(elem)))
{
NANOCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
}

if (elem.DataType == DATATYPE_VAR || elem.DataType == DATATYPE_MVAR)
{
int gpIndex = elem.GenericParamPosition;

// if the caller's genericType is non‐null, ask the CLR to map !n→actual argument:
if (caller != nullptr && NANOCLR_INDEX_IS_VALID(*caller))
{
CLR_RT_TypeDef_Index tdArg{};
NanoCLRDataType dtArg;

bool ok = g_CLR_RT_TypeSystem.m_assemblies[caller->Assembly() - 1]
->FindGenericParamAtTypeSpec(caller->TypeSpec(), gpIndex, tdArg, dtArg);
Comment on lines +264 to +265
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add bounds checking to prevent array access violation.

The code accesses g_CLR_RT_TypeSystem.m_assemblies[caller->Assembly() - 1] without bounds checking. If Assembly() returns 0, this will cause an underflow and potential memory corruption.

+        if (caller->Assembly() == 0 || caller->Assembly() > g_CLR_RT_TypeSystem.m_assemblies.size())
+        {
+            NANOCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
+        }
         bool ok = g_CLR_RT_TypeSystem.m_assemblies[caller->Assembly() - 1]
                       ->FindGenericParamAtTypeSpec(caller->TypeSpec(), gpIndex, tdArg, dtArg);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
bool ok = g_CLR_RT_TypeSystem.m_assemblies[caller->Assembly() - 1]
->FindGenericParamAtTypeSpec(caller->TypeSpec(), gpIndex, tdArg, dtArg);
if (caller->Assembly() == 0 || caller->Assembly() > g_CLR_RT_TypeSystem.m_assemblies.size())
{
NANOCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
}
bool ok = g_CLR_RT_TypeSystem.m_assemblies[caller->Assembly() - 1]
->FindGenericParamAtTypeSpec(caller->TypeSpec(), gpIndex, tdArg, dtArg);
🤖 Prompt for AI Agents
In src/CLR/Core/CLR_RT_HeapBlock.cpp around lines 264 to 265, add bounds
checking before accessing g_CLR_RT_TypeSystem.m_assemblies with index
caller->Assembly() - 1. Verify that caller->Assembly() is greater than 0 and
less than or equal to the size of m_assemblies to prevent underflow and
out-of-bounds access. Only perform the array access if these conditions are met;
otherwise, handle the error or return safely.

if (ok)
{
m_data.reflection.data.type = tdArg;
m_data.reflection.levels = elem.Levels;
}
Comment on lines +266 to +270
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle failed generic parameter resolution properly.

When FindGenericParamAtTypeSpec returns false (ok == false), the reflection data is left uninitialized, potentially leaving the heap block in an inconsistent state.

         if (ok)
         {
             m_data.reflection.data.type = tdArg;
             m_data.reflection.levels = elem.Levels;
         }
+        else
+        {
+            NANOCLR_SET_AND_LEAVE(CLR_E_TYPE_UNAVAILABLE);
+        }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (ok)
{
m_data.reflection.data.type = tdArg;
m_data.reflection.levels = elem.Levels;
}
if (ok)
{
m_data.reflection.data.type = tdArg;
m_data.reflection.levels = elem.Levels;
}
else
{
NANOCLR_SET_AND_LEAVE(CLR_E_TYPE_UNAVAILABLE);
}
🤖 Prompt for AI Agents
In src/CLR/Core/CLR_RT_HeapBlock.cpp around lines 266 to 270, the code does not
handle the case when FindGenericParamAtTypeSpec returns false, leaving
reflection data uninitialized. Add proper handling for the failure case by
initializing or resetting the reflection data fields to a safe default state to
avoid inconsistent heap block state.

}
else
{
// TODO
_ASSERTE(false);
}
Comment on lines +272 to +276
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove TODO section with always-failing assertion.

The TODO comment with _ASSERTE(false) will always trigger in debug builds when a caller is not provided for generic parameters, making this code path unusable.

-        else
-        {
-            // TODO
-            _ASSERTE(false);
-        }
+        else
+        {
+            // No caller provided for generic parameter resolution
+            NANOCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
+        }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
else
{
// TODO
_ASSERTE(false);
}
else
{
// No caller provided for generic parameter resolution
NANOCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
}
🤖 Prompt for AI Agents
In src/CLR/Core/CLR_RT_HeapBlock.cpp around lines 272 to 276, remove the TODO
comment and the _ASSERTE(false) statement that always triggers in debug builds.
Replace this section with proper handling or a more appropriate fallback to
avoid unusable code paths when a caller is not provided for generic parameters.

}
else
{
m_data.reflection.data.type = elem.Class;
m_data.reflection.levels = elem.Levels;
}

NANOCLR_NOCLEANUP();
}
Expand Down
2 changes: 1 addition & 1 deletion src/CLR/Core/CLR_RT_HeapBlock_Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ HRESULT CLR_RT_HeapBlock_Array::CreateInstance(
}
else if (def.ResolveToken(tk, assm))
{
NANOCLR_CHECK_HRESULT(ref.SetReflection(def));
NANOCLR_CHECK_HRESULT(ref.SetReflection(def, caller->genericType));
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions src/CLR/Core/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3210,13 +3210,13 @@ HRESULT CLR_RT_Thread::Execute_IL(CLR_RT_StackFrame &stackArg)
{
case TBL_TypeSpec:
{
CLR_RT_TypeSpec_Instance sig{};
if (sig.ResolveToken(arg, assm) == false)
CLR_RT_TypeSpec_Instance tsInst{};
if (tsInst.ResolveToken(arg, assm) == false)
{
NANOCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);
}

evalPos[0].SetReflection(sig);
evalPos[0].SetReflection(tsInst, stack->m_call.genericType);
}
break;

Expand Down
16 changes: 9 additions & 7 deletions src/CLR/Diagnostics/Info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ void CLR_RT_Assembly::DumpToken(CLR_UINT32 token, const CLR_RT_TypeSpec_Index *g
}

CLR_RT_TypeSpec_Index tsIdx;
tsIdx.Set(ownerAsm, index);
tsIdx.Set(assemblyIndex, index);

// bind to get the signature blob
CLR_RT_TypeSpec_Instance tsInst{};
Expand Down Expand Up @@ -568,17 +568,19 @@ void CLR_RT_Assembly::DumpToken(CLR_UINT32 token, const CLR_RT_TypeSpec_Index *g
{
CLR_RT_TypeDef_Index tdArg{};
NanoCLRDataType dtArg;

bool ok =
tsInst.assembly->FindGenericParamAtTypeSpec(genericType->TypeSpec(), gpIndex, tdArg, dtArg);
bool ok = g_CLR_RT_TypeSystem.m_assemblies[genericType->Assembly() - 1]
->FindGenericParamAtTypeSpec(genericType->TypeSpec(), gpIndex, tdArg, dtArg);
if (ok)
{
// Print that bound argument (e.g. "I4" or full class name)
char bufArg[256];
char bufArg[256]{};
char *pArg = bufArg;
size_t cbArg = sizeof(bufArg);
g_CLR_RT_TypeSystem.BuildTypeName(tdArg, pArg, cbArg);

g_CLR_RT_TypeSystem
.BuildTypeName(tdArg, pArg, cbArg, CLR_RT_TypeSystem::TYPENAME_FLAGS_FULL, elem.Levels);

CLR_Debug::Printf("%s", bufArg);

break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/CLR/Include/nanoCLR_Runtime__HeapBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ struct CLR_RT_HeapBlock

HRESULT SetReflection(const CLR_RT_ReflectionDef_Index &reflex);
HRESULT SetReflection(const CLR_RT_Assembly_Index &assm);
HRESULT SetReflection(const CLR_RT_TypeSpec_Index &sig);
HRESULT SetReflection(const CLR_RT_TypeSpec_Instance &tsInst, const CLR_RT_TypeSpec_Index *caller);
HRESULT SetReflection(const CLR_RT_TypeDef_Index &cls);
HRESULT SetReflection(const CLR_RT_FieldDef_Index &fd);
HRESULT SetReflection(const CLR_RT_MethodDef_Index &md);
Expand Down