Skip to content

Commit

Permalink
Fix C++/CLI applications which use __declspec(appdomain) (#110367)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
davidwrighton authored Dec 6, 2024
1 parent fdb68dd commit 43d1838
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/coreclr/vm/methodtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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;
}
};

Expand Down

0 comments on commit 43d1838

Please sign in to comment.