From 8e2091f18f1c123e0af0a6db8adac7f8fc6378bc Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Mon, 4 Nov 2024 14:09:15 -0800 Subject: [PATCH] Disable GS cookie checks for LightUnwind LightUnwind does not track sufficient context to compute GS cookie address Fixes #109242 --- src/coreclr/inc/eetwain.h | 2 ++ src/coreclr/vm/eetwain.cpp | 9 ++++++++ src/coreclr/vm/exceptionhandling.cpp | 1 + src/coreclr/vm/stackwalk.cpp | 1 + .../coreclr/GitHub_109242/test109242.cs | 21 +++++++++++++++++++ .../coreclr/GitHub_109242/test109242.csproj | 12 +++++++++++ 6 files changed, 46 insertions(+) create mode 100644 src/tests/Regressions/coreclr/GitHub_109242/test109242.cs create mode 100644 src/tests/Regressions/coreclr/GitHub_109242/test109242.csproj diff --git a/src/coreclr/inc/eetwain.h b/src/coreclr/inc/eetwain.h index c7b1be02e5c638..4ee7b9a7b84b6e 100644 --- a/src/coreclr/inc/eetwain.h +++ b/src/coreclr/inc/eetwain.h @@ -273,6 +273,7 @@ virtual GenericParamContextType GetParamContextType(PREGDISPLAY pContext, */ virtual void * GetGSCookieAddr(PREGDISPLAY pContext, EECodeInfo * pCodeInfo, + unsigned flags, CodeManState * pState) = 0; #ifndef USE_GC_INFO_DECODER @@ -541,6 +542,7 @@ PTR_VOID GetExactGenericsToken(SIZE_T baseStackSlot, virtual void * GetGSCookieAddr(PREGDISPLAY pContext, EECodeInfo * pCodeInfo, + unsigned flags, CodeManState * pState); diff --git a/src/coreclr/vm/eetwain.cpp b/src/coreclr/vm/eetwain.cpp index 5746c44de4a770..618077b1b105c7 100644 --- a/src/coreclr/vm/eetwain.cpp +++ b/src/coreclr/vm/eetwain.cpp @@ -1952,6 +1952,7 @@ PTR_VOID EECodeManager::GetExactGenericsToken(SIZE_T baseStackSlot, void * EECodeManager::GetGSCookieAddr(PREGDISPLAY pContext, EECodeInfo * pCodeInfo, + unsigned flags, CodeManState * pState) { CONTRACTL { @@ -1969,6 +1970,14 @@ void * EECodeManager::GetGSCookieAddr(PREGDISPLAY pContext, } #endif +#ifdef HAS_LIGHTUNWIND + // LightUnwind does not track sufficient context to compute GS cookie address + if (flags & LightUnwind) + { + return NULL; + } +#endif + #ifndef USE_GC_INFO_DECODER _ASSERTE(sizeof(CodeManStateBuf) <= sizeof(pState->stateBuf)); diff --git a/src/coreclr/vm/exceptionhandling.cpp b/src/coreclr/vm/exceptionhandling.cpp index f9426d46f0029b..264a29d17bf32c 100644 --- a/src/coreclr/vm/exceptionhandling.cpp +++ b/src/coreclr/vm/exceptionhandling.cpp @@ -2054,6 +2054,7 @@ CLRUnwindStatus ExceptionTracker::ProcessOSExceptionNotification( { pGSCookie = (GSCookie*)cfThisFrame.GetCodeManager()->GetGSCookieAddr(cfThisFrame.pRD, &cfThisFrame.codeInfo, + 0 /* CodeManFlags */, &cfThisFrame.codeManState); if (pGSCookie) { diff --git a/src/coreclr/vm/stackwalk.cpp b/src/coreclr/vm/stackwalk.cpp index b333249637d37e..fcf0a2e7f64853 100644 --- a/src/coreclr/vm/stackwalk.cpp +++ b/src/coreclr/vm/stackwalk.cpp @@ -3185,6 +3185,7 @@ void StackFrameIterator::PreProcessingForManagedFrames(void) m_pCachedGSCookie = (GSCookie*)m_crawl.GetCodeManager()->GetGSCookieAddr( m_crawl.pRD, &m_crawl.codeInfo, + m_codeManFlags, &m_crawl.codeManState); #endif // !DACCESS_COMPILE diff --git a/src/tests/Regressions/coreclr/GitHub_109242/test109242.cs b/src/tests/Regressions/coreclr/GitHub_109242/test109242.cs new file mode 100644 index 00000000000000..080744316bdd73 --- /dev/null +++ b/src/tests/Regressions/coreclr/GitHub_109242/test109242.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +using System; +using System.Reflection; +using Xunit; + +public class Test109242 +{ + [Fact] + public static void TestEntryPoint() + { + unsafe + { + void* p = stackalloc byte[Random.Shared.Next(100)]; + GC.KeepAlive(((IntPtr)p).ToString()); + } + + Assembly.Load("System.Runtime"); + } +} + diff --git a/src/tests/Regressions/coreclr/GitHub_109242/test109242.csproj b/src/tests/Regressions/coreclr/GitHub_109242/test109242.csproj new file mode 100644 index 00000000000000..d087213b695b82 --- /dev/null +++ b/src/tests/Regressions/coreclr/GitHub_109242/test109242.csproj @@ -0,0 +1,12 @@ + + + 1 + true + + + + + + + +