From 43d1838e6a05028e9d2880527c9c2b7e35e81ce8 Mon Sep 17 00:00:00 2001 From: David Wrighton Date: Fri, 6 Dec 2024 15:33:02 -0800 Subject: [PATCH] Fix C++/CLI applications which use __declspec(appdomain) (#110367) * Do not eagerly allocate static variable space while loading the assembly in use. This avoids possible recursive loading issues found in C++/CLI codebases. Repurpose the NativeCallingManaged test to subsume this particular regression test case. Fix #110365 * Fix testcase --- src/coreclr/vm/methodtable.cpp | 5 +++- .../IjwNativeCallingManagedDll.cpp | 24 +++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/coreclr/vm/methodtable.cpp b/src/coreclr/vm/methodtable.cpp index e507019450727..fc657cabf1bcf 100644 --- a/src/coreclr/vm/methodtable.cpp +++ b/src/coreclr/vm/methodtable.cpp @@ -4316,7 +4316,10 @@ void MethodTable::DoFullyLoad(Generics::RecursionGraph * const pVisited, const ClassLoader::ValidateMethodsWithCovariantReturnTypes(this); } - if ((level == CLASS_LOADED) && CORDisableJITOptimizations(this->GetModule()->GetDebuggerInfoBits()) && !HasInstantiation()) + if ((level == CLASS_LOADED) && + CORDisableJITOptimizations(this->GetModule()->GetDebuggerInfoBits()) && + !HasInstantiation() && + !GetModule()->GetAssembly()->IsLoading()) // Do not do this during the vtable fixup stage of C++/CLI assembly loading. See https://github.com/dotnet/runtime/issues/110365 { if (g_fEEStarted) { diff --git a/src/tests/Interop/IJW/IjwNativeCallingManagedDll/IjwNativeCallingManagedDll.cpp b/src/tests/Interop/IJW/IjwNativeCallingManagedDll/IjwNativeCallingManagedDll.cpp index 15e7098774db2..e581c7ae185e1 100644 --- a/src/tests/Interop/IJW/IjwNativeCallingManagedDll/IjwNativeCallingManagedDll.cpp +++ b/src/tests/Interop/IJW/IjwNativeCallingManagedDll/IjwNativeCallingManagedDll.cpp @@ -17,10 +17,22 @@ extern "C" DLL_EXPORT int __cdecl NativeEntryPoint() } #pragma managed + +// Needed to provide a regression case for https://github.com/dotnet/runtime/issues/110365 +[assembly:System::Diagnostics::DebuggableAttribute(true, true)]; +[module:System::Diagnostics::DebuggableAttribute(true, true)]; + +public value struct ValueToReturnStorage +{ + int valueToReturn; + bool valueSet; +}; + +// store the value to return in an appdomain local static variable to allow this test to be a regression test for https://github.com/dotnet/runtime/issues/110365 +static __declspec(appdomain) ValueToReturnStorage s_valueToReturnStorage; + public ref class TestClass { -private: - static int s_valueToReturn = 100; public: int ManagedEntryPoint() { @@ -29,12 +41,16 @@ public ref class TestClass static void ChangeReturnedValue(int i) { - s_valueToReturn = i; + s_valueToReturnStorage.valueToReturn = i; + s_valueToReturnStorage.valueSet = true; } static int GetReturnValue() { - return s_valueToReturn; + if (s_valueToReturnStorage.valueSet) + return s_valueToReturnStorage.valueToReturn; + else + return 100; } };