From d6b890c05dd50d1d8fd57b41db6b32c9bccc047a Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Mon, 5 Aug 2024 09:54:18 -0700 Subject: [PATCH] Backport copy constructor fix for unsafe value types --- src/coreclr/vm/jitinterface.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/coreclr/vm/jitinterface.cpp b/src/coreclr/vm/jitinterface.cpp index 67e127bf91ea62..774dffe355909d 100644 --- a/src/coreclr/vm/jitinterface.cpp +++ b/src/coreclr/vm/jitinterface.cpp @@ -3898,8 +3898,18 @@ uint32_t CEEInfo::getClassAttribsInternal (CORINFO_CLASS_HANDLE clsHnd) if (pMT->IsByRefLike()) ret |= CORINFO_FLG_BYREF_LIKE; - if (pClass->IsUnsafeValueClass()) + // In Reverse P/Invoke stubs, we are generating the code + // and we are not generating the code patterns that the GS checks + // are meant to catch. + // As a result, we can skip setting this flag. + // We do this as the GS checks (emitted when this flag is set) + // can break C++/CLI's copy-constructor semantics by missing copies. + if (pClass->IsUnsafeValueClass() + && !(m_pMethodBeingCompiled->IsILStub() + && dac_cast(m_pMethodBeingCompiled)->GetILStubType() == DynamicMethodDesc::StubNativeToCLRInterop)) + { ret |= CORINFO_FLG_UNSAFE_VALUECLASS; + } } if (pClass->HasExplicitFieldOffsetLayout() && pClass->HasOverlaidField()) ret |= CORINFO_FLG_OVERLAPPING_FIELDS;