From add6b2f016c4bf6ae3e0bedeef586460eca0c182 Mon Sep 17 00:00:00 2001 From: Jan Vorlicek Date: Tue, 26 Mar 2024 16:34:02 +0100 Subject: [PATCH] Ensure Lr / Ra context pointers are non-null --- src/coreclr/pal/src/exception/seh-unwind.cpp | 2 ++ src/coreclr/vm/arm/stubs.cpp | 8 ++++---- src/coreclr/vm/arm64/stubs.cpp | 6 ++---- src/coreclr/vm/loongarch64/stubs.cpp | 8 +++----- src/coreclr/vm/riscv64/stubs.cpp | 8 +++----- src/coreclr/vm/stackwalk.cpp | 4 ++-- 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/coreclr/pal/src/exception/seh-unwind.cpp b/src/coreclr/pal/src/exception/seh-unwind.cpp index 5b12af8aa027bc..8ce69ce73f71a5 100644 --- a/src/coreclr/pal/src/exception/seh-unwind.cpp +++ b/src/coreclr/pal/src/exception/seh-unwind.cpp @@ -555,6 +555,7 @@ void GetContextPointers(unw_cursor_t *cursor, unw_context_t *unwContext, KNONVOL GetContextPointer(cursor, unwContext, UNW_ARM_R9, &contextPointers->R9); GetContextPointer(cursor, unwContext, UNW_ARM_R10, &contextPointers->R10); GetContextPointer(cursor, unwContext, UNW_ARM_R11, &contextPointers->R11); + GetContextPointer(cursor, unwContext, UNW_ARM_R14, &contextPointers->Lr); GetContextPointer(cursor, unwContext, UNW_ARM_D8, (SIZE_T **)&contextPointers->D8); GetContextPointer(cursor, unwContext, UNW_ARM_D9, (SIZE_T **)&contextPointers->D9); GetContextPointer(cursor, unwContext, UNW_ARM_D10, (SIZE_T **)&contextPointers->D10); @@ -575,6 +576,7 @@ void GetContextPointers(unw_cursor_t *cursor, unw_context_t *unwContext, KNONVOL GetContextPointer(cursor, unwContext, UNW_AARCH64_X27, (SIZE_T**)&contextPointers->X27); GetContextPointer(cursor, unwContext, UNW_AARCH64_X28, (SIZE_T**)&contextPointers->X28); GetContextPointer(cursor, unwContext, UNW_AARCH64_X29, (SIZE_T**)&contextPointers->Fp); + GetContextPointer(cursor, unwContext, UNW_AARCH64_X30, (SIZE_T**)&contextPointers->Lr); GetContextPointer(cursor, unwContext, UNW_AARCH64_V8, (SIZE_T**)&contextPointers->D8); GetContextPointer(cursor, unwContext, UNW_AARCH64_V9, (SIZE_T**)&contextPointers->D9); GetContextPointer(cursor, unwContext, UNW_AARCH64_V10, (SIZE_T**)&contextPointers->D10); diff --git a/src/coreclr/vm/arm/stubs.cpp b/src/coreclr/vm/arm/stubs.cpp index 1424dcecbd918d..8c85461cde9e63 100644 --- a/src/coreclr/vm/arm/stubs.cpp +++ b/src/coreclr/vm/arm/stubs.cpp @@ -718,7 +718,7 @@ void HelperMethodFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloat pRD->pCurrentContextPointers->R9 = m_MachState._R4_R11[5]; pRD->pCurrentContextPointers->R10 = m_MachState._R4_R11[6]; pRD->pCurrentContextPointers->R11 = m_MachState._R4_R11[7]; - pRD->pCurrentContextPointers->Lr = NULL; + pRD->pCurrentContextPointers->Lr = &pRD->pCurrentContext->Lr; } #ifndef DACCESS_COMPILE @@ -1505,7 +1505,7 @@ void UpdateRegDisplayFromCalleeSavedRegisters(REGDISPLAY * pRD, CalleeSavedRegis pRD->pCurrentContextPointers->R9 = (PDWORD)&pRegs->r9; pRD->pCurrentContextPointers->R10 = (PDWORD)&pRegs->r10; pRD->pCurrentContextPointers->R11 = (PDWORD)&pRegs->r11; - pRD->pCurrentContextPointers->Lr = NULL; + pRD->pCurrentContextPointers->Lr = (PDWORD)&pRegs->r14; } void TransitionFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloats) @@ -1565,7 +1565,7 @@ void FaultingExceptionFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool update pRD->pCurrentContextPointers->R9 = (PDWORD)&m_ctx.R9; pRD->pCurrentContextPointers->R10 = (PDWORD)&m_ctx.R10; pRD->pCurrentContextPointers->R11 = (PDWORD)&m_ctx.R11; - pRD->pCurrentContextPointers->Lr = NULL; + pRD->pCurrentContextPointers->Lr = (PDWORD)&m_ctx.Lr; pRD->IsCallerContextValid = FALSE; pRD->IsCallerSPValid = FALSE; // Don't add usage of this field. This is only temporary. @@ -1706,7 +1706,7 @@ void HijackFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloats) pRD->pCurrentContextPointers->R9 = &m_Args->R9; pRD->pCurrentContextPointers->R10 = &m_Args->R10; pRD->pCurrentContextPointers->R11 = &m_Args->R11; - pRD->pCurrentContextPointers->Lr = NULL; + pRD->pCurrentContextPointers->Lr = &m_Args->Lr; SyncRegDisplayToCurrentContext(pRD); } diff --git a/src/coreclr/vm/arm64/stubs.cpp b/src/coreclr/vm/arm64/stubs.cpp index 03783f016a52d3..fedcfe1979aa26 100644 --- a/src/coreclr/vm/arm64/stubs.cpp +++ b/src/coreclr/vm/arm64/stubs.cpp @@ -545,7 +545,7 @@ void HelperMethodFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloat pRD->pCurrentContextPointers->X27 = m_MachState.ptrX19_X29[8]; pRD->pCurrentContextPointers->X28 = m_MachState.ptrX19_X29[9]; pRD->pCurrentContextPointers->Fp = m_MachState.ptrX19_X29[10]; - pRD->pCurrentContextPointers->Lr = NULL; // Unwind again to get Caller's PC + pRD->pCurrentContextPointers->Lr = &pRD->pCurrentContext->Lr; #endif ClearRegDisplayArgumentAndScratchRegisters(pRD); @@ -629,8 +629,6 @@ void TransitionFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloats) ClearRegDisplayArgumentAndScratchRegisters(pRD); // copy the control registers - pRD->pCurrentContext->Fp = pCalleeSaved->x29; - pRD->pCurrentContext->Lr = pCalleeSaved->x30; pRD->pCurrentContext->Pc = GetReturnAddress(); pRD->pCurrentContext->Sp = this->GetSP(); @@ -825,7 +823,7 @@ void HijackFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloats) pRD->pCurrentContextPointers->X27 = &m_Args->X27; pRD->pCurrentContextPointers->X28 = &m_Args->X28; pRD->pCurrentContextPointers->Fp = &m_Args->X29; - pRD->pCurrentContextPointers->Lr = NULL; + pRD->pCurrentContextPointers->Lr = &m_Args->Lr; SyncRegDisplayToCurrentContext(pRD); diff --git a/src/coreclr/vm/loongarch64/stubs.cpp b/src/coreclr/vm/loongarch64/stubs.cpp index 052d71ebc1e44e..73477d979d0a7c 100644 --- a/src/coreclr/vm/loongarch64/stubs.cpp +++ b/src/coreclr/vm/loongarch64/stubs.cpp @@ -514,7 +514,7 @@ void HelperMethodFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloat pRD->pCurrentContextPointers->S8 = pUnwoundState->ptrCalleeSavedRegisters[8]; pRD->pCurrentContextPointers->Fp = pUnwoundState->ptrCalleeSavedRegisters[9]; pRD->pCurrentContextPointers->Tp = pUnwoundState->ptrCalleeSavedRegisters[10]; - pRD->pCurrentContextPointers->Ra = NULL; + pRD->pCurrentContextPointers->Ra = &pRD->pCurrentContext->Ra; return; } #endif // DACCESS_COMPILE @@ -567,7 +567,7 @@ void HelperMethodFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloat pRD->pCurrentContextPointers->S8 = m_MachState.ptrCalleeSavedRegisters[8]; pRD->pCurrentContextPointers->Fp = m_MachState.ptrCalleeSavedRegisters[9]; pRD->pCurrentContextPointers->Tp = m_MachState.ptrCalleeSavedRegisters[10]; - pRD->pCurrentContextPointers->Ra = NULL; // Unwind again to get Caller's PC + pRD->pCurrentContextPointers->Ra = pRD->pCurrentContext->Ra; #endif ClearRegDisplayArgumentAndScratchRegisters(pRD); } @@ -648,8 +648,6 @@ void TransitionFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloats) ClearRegDisplayArgumentAndScratchRegisters(pRD); // copy the control registers - //pRD->pCurrentContext->Fp = pCalleeSaved->fp;//not needed for duplicated. - //pRD->pCurrentContext->Ra = pCalleeSaved->ra;//not needed for duplicated. pRD->pCurrentContext->Pc = GetReturnAddress(); pRD->pCurrentContext->Sp = this->GetSP(); @@ -857,7 +855,7 @@ void HijackFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloats) pRD->pCurrentContextPointers->S8 = &m_Args->S8; pRD->pCurrentContextPointers->Tp = &m_Args->Tp; pRD->pCurrentContextPointers->Fp = &m_Args->Fp; - pRD->pCurrentContextPointers->Ra = NULL; + pRD->pCurrentContextPointers->Ra = &m_Args->Ra; SyncRegDisplayToCurrentContext(pRD); LOG((LF_GCROOTS, LL_INFO100000, "STACKWALK HijackFrame::UpdateRegDisplay(pc:%p, sp:%p)\n", pRD->ControlPC, pRD->SP)); diff --git a/src/coreclr/vm/riscv64/stubs.cpp b/src/coreclr/vm/riscv64/stubs.cpp index 4ce55a3849e12b..90101d8df475f1 100644 --- a/src/coreclr/vm/riscv64/stubs.cpp +++ b/src/coreclr/vm/riscv64/stubs.cpp @@ -414,7 +414,7 @@ void HelperMethodFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloat pRD->pCurrentContextPointers->S11 = pUnwoundState->ptrCalleeSavedRegisters[11]; pRD->pCurrentContextPointers->Gp = pUnwoundState->ptrCalleeSavedRegisters[12]; pRD->pCurrentContextPointers->Tp = pUnwoundState->ptrCalleeSavedRegisters[13]; - pRD->pCurrentContextPointers->Ra = NULL; + pRD->pCurrentContextPointers->Ra = &pRD->pCurrentContext->Ra; return; } #endif // DACCESS_COMPILE @@ -476,7 +476,7 @@ void HelperMethodFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloat pRD->pCurrentContextPointers->S11 = m_MachState.ptrCalleeSavedRegisters[11]; pRD->pCurrentContextPointers->Gp = m_MachState.ptrCalleeSavedRegisters[12]; pRD->pCurrentContextPointers->Tp = m_MachState.ptrCalleeSavedRegisters[13]; - pRD->pCurrentContextPointers->Ra = NULL; // Unwind again to get Caller's PC + pRD->pCurrentContextPointers->Ra = &pRD->pCurrentContext->Ra; #endif ClearRegDisplayArgumentAndScratchRegisters(pRD); } @@ -562,8 +562,6 @@ void TransitionFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloats) ClearRegDisplayArgumentAndScratchRegisters(pRD); // copy the control registers - //pRD->pCurrentContext->Fp = pCalleeSaved->fp;//not needed for duplicated. - //pRD->pCurrentContext->Ra = pCalleeSaved->ra;//not needed for duplicated. pRD->pCurrentContext->Pc = GetReturnAddress(); pRD->pCurrentContext->Sp = this->GetSP(); @@ -783,7 +781,7 @@ void HijackFrame::UpdateRegDisplay(const PREGDISPLAY pRD, bool updateFloats) pRD->pCurrentContextPointers->Gp = &m_Args->Gp; pRD->pCurrentContextPointers->Tp = &m_Args->Tp; pRD->pCurrentContextPointers->Fp = &m_Args->Fp; - pRD->pCurrentContextPointers->Ra = NULL; + pRD->pCurrentContextPointers->Ra = &m_Args->Ra; SyncRegDisplayToCurrentContext(pRD); LOG((LF_GCROOTS, LL_INFO100000, "STACKWALK HijackFrame::UpdateRegDisplay(pc:%p, sp:%p)\n", pRD->ControlPC, pRD->SP)); diff --git a/src/coreclr/vm/stackwalk.cpp b/src/coreclr/vm/stackwalk.cpp index 2eb5695ce9f62e..1bb11b8b876f4f 100644 --- a/src/coreclr/vm/stackwalk.cpp +++ b/src/coreclr/vm/stackwalk.cpp @@ -1584,7 +1584,7 @@ void StackFrameIterator::SkipTo(StackFrameIterator *pOtherStackFrameIterator) #define CALLEE_SAVED_REGISTER(regname) pRD->pCurrentContext->regname = *pRD->pCurrentContextPointers->regname; ENUM_CALLEE_SAVED_REGISTERS(); #undef CALLEE_SAVED_REGISTER -#define CALLEE_SAVED_REGISTER(regname) pRD->pCurrentContext->regname = pRD->pCurrentContext->regname; +#define CALLEE_SAVED_REGISTER(regname) pRD->pCurrentContext->regname = pOtherRD->pCurrentContext->regname; ENUM_FP_CALLEE_SAVED_REGISTERS(); #undef CALLEE_SAVED_REGISTER pRD->IsCallerContextValid = pOtherRD->IsCallerContextValid; @@ -1596,7 +1596,7 @@ void StackFrameIterator::SkipTo(StackFrameIterator *pOtherStackFrameIterator) #define CALLEE_SAVED_REGISTER(regname) pRD->pCallerContext->regname = *pRD->pCallerContextPointers->regname; ENUM_CALLEE_SAVED_REGISTERS(); #undef CALLEE_SAVED_REGISTER -#define CALLEE_SAVED_REGISTER(regname) pRD->pCallerContext->regname = pRD->pCallerContext->regname; +#define CALLEE_SAVED_REGISTER(regname) pRD->pCallerContext->regname = pOtherRD->pCallerContext->regname; ENUM_FP_CALLEE_SAVED_REGISTERS(); #undef CALLEE_SAVED_REGISTER }