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

Commit efcf98f

Browse files
authored
Port of dotnet/runtime#1059 to 3.1 branch (#27986)
This is the fix for #27924. This is a GC hole bug that was found externally, #27590. The cause is that the JIT was using the target type of the subtract when it needed to make a copy of the source, but it needs to use the source type. ## Customer Impact Corruption of state that is non-deterministic and hard to track down. ## Regression? Not a recent regression, but exposed by Unsafe.ByteOffset. ## Testing The fix has been verified in the runtime repo. ## Risk Low: The fix is straightfoward and only impacts 3 lines of code.
1 parent a86e825 commit efcf98f

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

src/jit/codegenxarch.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,9 +954,10 @@ void CodeGen::genCodeForBinary(GenTreeOp* treeNode)
954954
// reg3 = reg3 op reg2
955955
else
956956
{
957-
inst_RV_RV(ins_Copy(targetType), targetReg, op1reg, targetType);
957+
var_types op1Type = op1->TypeGet();
958+
inst_RV_RV(ins_Copy(op1Type), targetReg, op1reg, op1Type);
958959
regSet.verifyRegUsed(targetReg);
959-
gcInfo.gcMarkRegPtrVal(targetReg, targetType);
960+
gcInfo.gcMarkRegPtrVal(targetReg, op1Type);
960961
dst = treeNode;
961962
src = op2;
962963
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.Threading;
7+
using System.Runtime.CompilerServices;
8+
using System.Collections.Generic;
9+
using System.Threading.Tasks;
10+
11+
class Program
12+
{
13+
static int returnVal = 100;
14+
static byte[][] s = new byte[1000][];
15+
16+
static void Work()
17+
{
18+
for (uint i = 0; i < 1000000; i++)
19+
{
20+
var a = s[i++ % s.Length];
21+
22+
ref byte p = ref a[0];
23+
ref byte q = ref a[1];
24+
25+
if (Unsafe.ByteOffset(ref p, ref q) != new IntPtr(1))
26+
{
27+
Console.WriteLine("ERROR: i = " + i);
28+
returnVal = -1;
29+
}
30+
p = 1; q = 2;
31+
}
32+
}
33+
34+
static int Main(string[] args)
35+
{
36+
for(int i = 0; i < s.Length; i++) s[i] = new byte[2];
37+
38+
List<Task> tasks = new List<Task>();
39+
for(int i = 0; i < 5; i++)
40+
{
41+
tasks.Add(Task.Run(Work));
42+
}
43+
44+
Random r = new Random();
45+
for (uint i = 0; i < 10000; i++)
46+
{
47+
s[r.Next(s.Length)] = new byte[3 + r.Next(100)];
48+
}
49+
Task t = Task.WhenAll(tasks);
50+
t.Wait();
51+
return returnVal;
52+
}
53+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
8+
<SchemaVersion>2.0</SchemaVersion>
9+
<ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid>
10+
<OutputType>Exe</OutputType>
11+
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
12+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
13+
</PropertyGroup>
14+
<!-- Default configurations to help VS understand the configurations -->
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "></PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "></PropertyGroup>
17+
<ItemGroup>
18+
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
19+
<Visible>False</Visible>
20+
</CodeAnalysisDependentAssemblyPaths>
21+
</ItemGroup>
22+
<PropertyGroup>
23+
<DebugType></DebugType>
24+
<Optimize>True</Optimize>
25+
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
26+
</PropertyGroup>
27+
<ItemGroup>
28+
<Compile Include="$(MSBuildProjectName).cs" />
29+
</ItemGroup>
30+
<PropertyGroup>
31+
<CLRTestBatchPreCommands><![CDATA[
32+
$(CLRTestBatchPreCommands)
33+
set COMPlus_GcStress=0xc
34+
]]></CLRTestBatchPreCommands>
35+
<BashCLRTestPreCommands><![CDATA[
36+
$(BashCLRTestPreCommands)
37+
export COMPlus_GcStress=0xc
38+
]]></BashCLRTestPreCommands>
39+
</PropertyGroup>
40+
<ItemGroup>
41+
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
42+
</ItemGroup>
43+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
44+
<PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "></PropertyGroup>
45+
</Project>
46+

0 commit comments

Comments
 (0)