Skip to content

Commit b2c99a0

Browse files
authored
Fix GitHub_25027 in the interpreter by verifying stack depth at CEE_RET (#118976)
1 parent 674d359 commit b2c99a0

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/coreclr/interpreter/compiler.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,19 @@ void InterpCompiler::CheckStackHelper(int n)
623623
}
624624
}
625625

626+
void InterpCompiler::CheckStackExact(int n)
627+
{
628+
int32_t currentSize = (int32_t)(m_pStackPointer - m_pStackBase);
629+
if (currentSize < n)
630+
{
631+
BADCODE("Stack underflow");
632+
}
633+
else if (currentSize > n)
634+
{
635+
BADCODE("Stack contains extra data");
636+
}
637+
}
638+
626639
void InterpCompiler::PushTypeExplicit(StackType stackType, CORINFO_CLASS_HANDLE clsHnd, int size)
627640
{
628641
EnsureStack(1);
@@ -3921,11 +3934,12 @@ void InterpCompiler::GenerateCode(CORINFO_METHOD_INFO* methodInfo)
39213934

39223935
if (retType == InterpTypeVoid)
39233936
{
3937+
CheckStackExact(0);
39243938
AddIns(INTOP_RET_VOID);
39253939
}
39263940
else if (retType == InterpTypeVT)
39273941
{
3928-
CHECK_STACK(1);
3942+
CheckStackExact(1);
39293943
AddIns(INTOP_RET_VT);
39303944
m_pStackPointer--;
39313945
int32_t retVar = m_pStackPointer[0].var;
@@ -3934,7 +3948,7 @@ void InterpCompiler::GenerateCode(CORINFO_METHOD_INFO* methodInfo)
39343948
}
39353949
else
39363950
{
3937-
CHECK_STACK(1);
3951+
CheckStackExact(1);
39383952
AddIns(INTOP_RET);
39393953
m_pStackPointer--;
39403954
m_pLastNewIns->SetSVar(m_pStackPointer[0].var);

src/coreclr/interpreter/compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ class InterpCompiler
690690
int32_t m_stackCapacity;
691691

692692
void CheckStackHelper(int n);
693+
void CheckStackExact(int n);
693694
void EnsureStack(int additional);
694695
void PushTypeExplicit(StackType stackType, CORINFO_CLASS_HANDLE clsHnd, int size);
695696
void PushStackType(StackType stackType, CORINFO_CLASS_HANDLE clsHnd);

0 commit comments

Comments
 (0)