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

Commit e259e0a

Browse files
kouveljkotas
authored andcommitted
Fix GC reproting for by-ref-like structs (#16231)
Port of a portion of #16231 to release/2.1. Fixes https://github.com/dotnet/coreclr/issues/16044: - Divide by pointer size was removed to produce the proper offset
1 parent c878a62 commit e259e0a

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

src/vm/object.inl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ inline void FindByRefPointerOffsetsInByRefLikeObject(PTR_MethodTable pMT, SIZE_T
389389
continue;
390390
}
391391

392-
SIZE_T fieldStartIndex = pFD->GetOffset() / sizeof(void *);
393-
FindByRefPointerOffsetsInByRefLikeObject(pFieldMT, baseOffset + fieldStartIndex, processPointerOffset);
392+
FindByRefPointerOffsetsInByRefLikeObject(pFieldMT, baseOffset + pFD->GetOffset(), processPointerOffset);
394393
}
395394
}
396395

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Threading;
8+
9+
ref struct MyStruct<A, B>
10+
{
11+
static Random r = new Random();
12+
13+
Span<int> s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;
14+
15+
public static void Test(int depth)
16+
{
17+
MyStruct<A, B> u;
18+
u.s1 = My.g[r.Next(My.g.Length)];
19+
u.s2 = My.g[r.Next(My.g.Length)];
20+
u.s3 = My.g[r.Next(My.g.Length)];
21+
u.s4 = My.g[r.Next(My.g.Length)];
22+
u.s5 = My.g[r.Next(My.g.Length)];
23+
u.s6 = My.g[r.Next(My.g.Length)];
24+
u.s6 = My.g[r.Next(My.g.Length)];
25+
u.s7 = My.g[r.Next(My.g.Length)];
26+
u.s8 = My.g[r.Next(My.g.Length)];
27+
u.s9 = My.g[r.Next(My.g.Length)];
28+
u.s10 = My.g[r.Next(My.g.Length)];
29+
Test(depth, u);
30+
}
31+
32+
public static void Test(int depth, MyStruct<A, B> u)
33+
{
34+
int x1 = u.s1.Length + u.s2.Length + u.s3.Length + u.s4.Length + u.s5.Length +
35+
u.s6.Length + u.s7.Length + u.s8.Length + u.s9.Length + u.s10.Length;
36+
int x2 = u.s1[0] + u.s2[0] + u.s3[0] + u.s4[0] + u.s5[0] +
37+
u.s6[0] + u.s7[0] + u.s8[0] + u.s9[0] + u.s10[0];
38+
if (x1 != x2)
39+
throw new InvalidOperationException();
40+
41+
if (depth-- == 0)
42+
return;
43+
MyStruct<KeyValuePair<A, B>, B>.Test(depth);
44+
MyStruct<A, KeyValuePair<B, A>>.Test(depth);
45+
}
46+
}
47+
48+
class My
49+
{
50+
static void Stress()
51+
{
52+
for (; ; )
53+
{
54+
GC.Collect();
55+
Thread.Sleep(1);
56+
}
57+
}
58+
59+
static void Churn()
60+
{
61+
Random r = new Random();
62+
for (; ; )
63+
{
64+
var a = new int[1 + r.Next(100)];
65+
a[0] = a.Length;
66+
g[r.Next(g.Length)] = a;
67+
}
68+
}
69+
70+
public static int[][] g = new int[10000][];
71+
72+
static int Main()
73+
{
74+
int[] empty = new int[] { 1 };
75+
for (int i = 0; i < g.Length; i++)
76+
g[i] = empty;
77+
78+
var t = new Thread(Stress);
79+
t.IsBackground = true;
80+
t.Start();
81+
t = new Thread(Churn);
82+
t.IsBackground = true;
83+
t.Start();
84+
85+
int result = 100; // pass
86+
try
87+
{
88+
MyStruct<int, uint>.Test(3);
89+
}
90+
catch (InvalidOperationException)
91+
{
92+
result = 1; // fail
93+
}
94+
return result;
95+
}
96+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{3801DEB6-DDA1-4E94-B96F-9C04CC083DCC}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
12+
<LangVersion>latest</LangVersion>
13+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
14+
<CLRTestKind>BuildAndRun</CLRTestKind>
15+
<CLRTestPriority>1</CLRTestPriority>
16+
<GCStressIncompatible>true</GCStressIncompatible>
17+
</PropertyGroup>
18+
<!-- Default configurations to help VS understand the configurations -->
19+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "></PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "></PropertyGroup>
21+
<ItemGroup>
22+
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
23+
<Visible>False</Visible>
24+
</CodeAnalysisDependentAssemblyPaths>
25+
</ItemGroup>
26+
<ItemGroup>
27+
<!-- Add Compile Object Here -->
28+
<Compile Include="RefStructWithSpan.cs" />
29+
</ItemGroup>
30+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
31+
</Project>

0 commit comments

Comments
 (0)