diff --git a/src/coreclr/interpreter/compiler.cpp b/src/coreclr/interpreter/compiler.cpp index 6a8671899c85a3..2ecf807c1d7667 100644 --- a/src/coreclr/interpreter/compiler.cpp +++ b/src/coreclr/interpreter/compiler.cpp @@ -623,6 +623,19 @@ void InterpCompiler::CheckStackHelper(int n) } } +void InterpCompiler::CheckStackExact(int n) +{ + int32_t currentSize = (int32_t)(m_pStackPointer - m_pStackBase); + if (currentSize < n) + { + BADCODE("Stack underflow"); + } + else if (currentSize > n) + { + BADCODE("Stack contains extra data"); + } +} + void InterpCompiler::PushTypeExplicit(StackType stackType, CORINFO_CLASS_HANDLE clsHnd, int size) { EnsureStack(1); @@ -3908,11 +3921,12 @@ void InterpCompiler::GenerateCode(CORINFO_METHOD_INFO* methodInfo) if (retType == InterpTypeVoid) { + CheckStackExact(0); AddIns(INTOP_RET_VOID); } else if (retType == InterpTypeVT) { - CHECK_STACK(1); + CheckStackExact(1); AddIns(INTOP_RET_VT); m_pStackPointer--; int32_t retVar = m_pStackPointer[0].var; @@ -3921,7 +3935,7 @@ void InterpCompiler::GenerateCode(CORINFO_METHOD_INFO* methodInfo) } else { - CHECK_STACK(1); + CheckStackExact(1); AddIns(INTOP_RET); m_pStackPointer--; m_pLastNewIns->SetSVar(m_pStackPointer[0].var); diff --git a/src/coreclr/interpreter/compiler.h b/src/coreclr/interpreter/compiler.h index 54b25ac2857ce4..5b761596393166 100644 --- a/src/coreclr/interpreter/compiler.h +++ b/src/coreclr/interpreter/compiler.h @@ -690,6 +690,7 @@ class InterpCompiler int32_t m_stackCapacity; void CheckStackHelper(int n); + void CheckStackExact(int n); void EnsureStack(int additional); void PushTypeExplicit(StackType stackType, CORINFO_CLASS_HANDLE clsHnd, int size); void PushStackType(StackType stackType, CORINFO_CLASS_HANDLE clsHnd);