Skip to content

Commit 1cead3e

Browse files
committed
Always zero-init if object contains pointers (dotnet#100265)
1 parent a494d22 commit 1cead3e

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/coreclr/nativeaot/Runtime/gcrhenv.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ Object* GcAllocInternal(MethodTable *pEEType, uint32_t uFlags, uintptr_t numElem
129129
ASSERT(!pThread->IsDoNotTriggerGcSet());
130130
ASSERT(pThread->IsCurrentThreadInCooperativeMode());
131131

132+
if (pEEType->ContainsPointers())
133+
{
134+
uFlags |= GC_ALLOC_CONTAINS_REF;
135+
uFlags &= ~GC_ALLOC_ZEROING_OPTIONAL;
136+
}
137+
132138
size_t cbSize = pEEType->get_BaseSize();
133139

134140
if (pEEType->HasComponentSize())

src/libraries/System.Runtime/tests/System/GCTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,8 @@ private unsafe static void AllocateArrayPinned_ManagedValueType_CanRoundtripThro
11001100
byte* pointer = (byte*)Unsafe.AsPointer(ref array[0]);
11011101
var size = Unsafe.SizeOf<EmbeddedValueType<string>>();
11021102

1103+
GC.Collect();
1104+
11031105
for(int i = 0; i < length; ++i)
11041106
{
11051107
int idx = rng.Next(length);

0 commit comments

Comments
 (0)