Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 21cfdb6

Browse files
Anipikjkotas
authored andcommitted
Respect STA/MTAThread attributes (#15512)
* Apartment state set for main method * Default apartment state is Unknown * Removed Extra validation of token * Removed legacy apartment code
1 parent bb3aaf3 commit 21cfdb6

File tree

7 files changed

+21
-48
lines changed

7 files changed

+21
-48
lines changed

src/inc/clrconfigvalues.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,6 @@ RETAIL_CONFIG_STRING_INFO(INTERNAL_EventNameFilter, W("EventNameFilter"), "")
937937
CONFIG_DWORD_INFO_DIRECT_ACCESS(INTERNAL_ExposeExceptionsInCOM, W("ExposeExceptionsInCOM"), "")
938938
RETAIL_CONFIG_DWORD_INFO(EXTERNAL_ComInsteadOfManagedRemoting, W("PreferComInsteadOfManagedRemoting"), 0, "When communicating with a cross app domain CCW, use COM instead of managed remoting.")
939939
CONFIG_DWORD_INFO(INTERNAL_GenerateStubForHost, W("GenerateStubForHost"), 0, "Forces the host hook stub to be built for all unmanaged calls, even when not running hosted.")
940-
RETAIL_CONFIG_DWORD_INFO_DIRECT_ACCESS(EXTERNAL_legacyApartmentInitPolicy, W("legacyApartmentInitPolicy"), "")
941940
RETAIL_CONFIG_DWORD_INFO_DIRECT_ACCESS(EXTERNAL_legacyComHierarchyVisibility, W("legacyComHierarchyVisibility"), "")
942941
RETAIL_CONFIG_DWORD_INFO_DIRECT_ACCESS(EXTERNAL_legacyComVTableLayout, W("legacyComVTableLayout"), "")
943942
RETAIL_CONFIG_DWORD_INFO_DIRECT_ACCESS(EXTERNAL_newComVTableLayout, W("newComVTableLayout"), "")

src/vm/appdomain.cpp

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,39 +3081,10 @@ Thread::ApartmentState SystemDomain::GetEntryPointThreadAptState(IMDInternalImpo
30813081
return Thread::AS_Unknown;
30823082
}
30833083

3084-
void SystemDomain::SetThreadAptState (IMDInternalImport* pScope, Thread::ApartmentState state)
3084+
void SystemDomain::SetThreadAptState (Thread::ApartmentState state)
30853085
{
30863086
STANDARD_VM_CONTRACT;
30873087

3088-
BOOL fIsLegacy = FALSE;
3089-
3090-
// Check for legacy behavior regarding COM Apartment state of the main thread.
3091-
3092-
#define METAMODEL_MAJOR_VER_WITH_NEW_BEHAVIOR 2
3093-
#define METAMODEL_MINOR_VER_WITH_NEW_BEHAVIOR 0
3094-
3095-
LPCSTR pVer;
3096-
IfFailThrow(pScope->GetVersionString(&pVer));
3097-
3098-
// Does this look like a version?
3099-
if (pVer != NULL)
3100-
{
3101-
// Is it 'vN.' where N is a digit?
3102-
if ((pVer[0] == 'v' || pVer[0] == 'V') &&
3103-
IS_DIGIT(pVer[1]) &&
3104-
(pVer[2] == '.') )
3105-
{
3106-
// Looks like a version. Is it lesser than v2.0 major version where we start using new behavior?
3107-
fIsLegacy = DIGIT_TO_INT(pVer[1]) < METAMODEL_MAJOR_VER_WITH_NEW_BEHAVIOR;
3108-
}
3109-
}
3110-
3111-
if (!fIsLegacy && g_pConfig != NULL)
3112-
{
3113-
fIsLegacy = g_pConfig->LegacyApartmentInitPolicy();
3114-
}
3115-
3116-
31173088
Thread* pThread = GetThread();
31183089
_ASSERTE(pThread);
31193090

@@ -3122,14 +3093,8 @@ void SystemDomain::SetThreadAptState (IMDInternalImport* pScope, Thread::Apartme
31223093
Thread::ApartmentState pState = pThread->SetApartment(Thread::AS_InSTA, TRUE);
31233094
_ASSERTE(pState == Thread::AS_InSTA);
31243095
}
3125-
else if ((state == Thread::AS_InMTA) || (!fIsLegacy))
3096+
else if (state == Thread::AS_InMTA)
31263097
{
3127-
// If either MTAThreadAttribute is specified or (if no attribute is specified and we are not
3128-
// running in legacy mode), then
3129-
// we will set the apartment state to MTA. The reason for this is to ensure the apartment
3130-
// state is consistent and reliably set. Without this, the apartment state for the main
3131-
// thread would be undefined and would actually be dependent on if the assembly was
3132-
// ngen'd, which other type were loaded, etc.
31333098
Thread::ApartmentState pState = pThread->SetApartment(Thread::AS_InMTA, TRUE);
31343099
_ASSERTE(pState == Thread::AS_InMTA);
31353100
}

src/vm/appdomain.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4267,7 +4267,7 @@ class SystemDomain : public BaseDomain
42674267

42684268
#if defined(FEATURE_COMINTEROP_APARTMENT_SUPPORT) && !defined(CROSSGEN_COMPILE)
42694269
static Thread::ApartmentState GetEntryPointThreadAptState(IMDInternalImport* pScope, mdMethodDef mdMethod);
4270-
static void SetThreadAptState(IMDInternalImport* pScope, Thread::ApartmentState state);
4270+
static void SetThreadAptState(Thread::ApartmentState state);
42714271
#endif
42724272
static BOOL SetGlobalSharePolicyUsingAttribute(IMDInternalImport* pScope, mdMethodDef mdMethod);
42734273

src/vm/assembly.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,22 @@ INT32 Assembly::ExecuteMainMethod(PTRARRAYREF *stringArgs, BOOL waitForOtherThre
17901790
GCX_COOP();
17911791

17921792
pMeth = GetEntryPoint();
1793+
17931794
if (pMeth) {
1795+
{
1796+
#ifdef FEATURE_COMINTEROP
1797+
GCX_PREEMP();
1798+
1799+
Thread::ApartmentState state = Thread::AS_Unknown;
1800+
state = SystemDomain::GetEntryPointThreadAptState(pMeth->GetMDImport(), pMeth->GetMemberDef());
1801+
1802+
// If the entry point has an explicit thread apartment state, set it
1803+
// before running the AppDomainManager initialization code.
1804+
if (state == Thread::AS_InSTA || state == Thread::AS_InMTA)
1805+
SystemDomain::SetThreadAptState(state);
1806+
#endif // FEATURE_COMINTEROP
1807+
}
1808+
17941809
RunMainPre();
17951810

17961811

src/vm/comsynchronizable.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,13 +1109,10 @@ FCIMPL1(void, ThreadNative::StartupSetApartmentState, ThreadBaseObject* pThisUNS
11091109
// Assert that the thread hasn't been started yet.
11101110
_ASSERTE(Thread::TS_Unstarted & thread->GetSnapshotState());
11111111

1112-
if ((g_pConfig != NULL) && !g_pConfig->LegacyApartmentInitPolicy())
1112+
Thread::ApartmentState as = thread->GetExplicitApartment();
1113+
if (as == Thread::AS_Unknown)
11131114
{
1114-
Thread::ApartmentState as = thread->GetExplicitApartment();
1115-
if (as == Thread::AS_Unknown)
1116-
{
1117-
thread->SetApartment(Thread::AS_InMTA, TRUE);
1118-
}
1115+
thread->SetApartment(Thread::AS_InMTA, TRUE);
11191116
}
11201117

11211118
HELPER_METHOD_FRAME_END();

src/vm/eeconfig.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ HRESULT EEConfig::Init()
223223

224224
fLegacyNullReferenceExceptionPolicy = false;
225225
fLegacyUnhandledExceptionPolicy = false;
226-
fLegacyApartmentInitPolicy = false;
227226
fLegacyComHierarchyVisibility = false;
228227
fLegacyComVTableLayout = false;
229228
fNewComVTableLayout = false;

src/vm/eeconfig.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ class EEConfig
317317
bool LegacyNullReferenceExceptionPolicy(void) const {LIMITED_METHOD_CONTRACT; return fLegacyNullReferenceExceptionPolicy; }
318318
bool LegacyUnhandledExceptionPolicy(void) const {LIMITED_METHOD_CONTRACT; return fLegacyUnhandledExceptionPolicy; }
319319

320-
bool LegacyApartmentInitPolicy(void) const {LIMITED_METHOD_CONTRACT; return fLegacyApartmentInitPolicy; }
321320
bool LegacyComHierarchyVisibility(void) const {LIMITED_METHOD_CONTRACT; return fLegacyComHierarchyVisibility; }
322321
bool LegacyComVTableLayout(void) const {LIMITED_METHOD_CONTRACT; return fLegacyComVTableLayout; }
323322
bool NewComVTableLayout(void) const {LIMITED_METHOD_CONTRACT; return fNewComVTableLayout; }
@@ -867,7 +866,6 @@ class EEConfig
867866
bool fLegacyCorruptedStateExceptionsPolicy;
868867
#endif // FEATURE_CORRUPTING_EXCEPTIONS
869868

870-
bool fLegacyApartmentInitPolicy; // Old nondeterministic COM apartment initialization switch
871869
bool fLegacyComHierarchyVisibility; // Old behavior allowing QIs for classes with invisible parents
872870
bool fLegacyComVTableLayout; // Old behavior passing out IClassX interface for IUnknown and IDispatch.
873871
bool fNewComVTableLayout; // New behavior passing out Basic interface for IUnknown and IDispatch.

0 commit comments

Comments
 (0)