Skip to content

Commit

Permalink
JIT: update byref null check logic to handle field chains (dotnet/cor…
Browse files Browse the repository at this point in the history
…eclr#23850)

The jit was not properly accumulating offsets when figuring out if a byref
should be null checked.

Use a non-null MorphAddressContext as indication that GT_FIELD and GT_IND
nodes are actually part of an ongoing address computation.

During field morphing propagate the current address context to the child node,
instead of starting a new one.

Fixes dotnet/coreclr#23791.


Commit migrated from dotnet/coreclr@72d4912
  • Loading branch information
AndyAyersMS authored Apr 12, 2019
1 parent e9c7126 commit eda59a1
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/coreclr/src/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6607,7 +6607,8 @@ GenTree* Compiler::fgMorphField(GenTree* tree, MorphAddrContext* mac)
}
noway_assert(tree->gtOper == GT_IND);

GenTree* res = fgMorphSmpOp(tree);
// Pass down the current mac; if non null we are computing an address
GenTree* res = fgMorphSmpOp(tree, mac);

if (fldOffset == 0 && res->OperGet() == GT_IND)
{
Expand Down Expand Up @@ -11886,6 +11887,10 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
switch (tree->gtOper)
{
case GT_ADDR:
// A non-null mac here implies this node is part of an address computation.
// If so, we need to pass the existing mac down to the child node.
//
// Otherwise, use a new mac.
if (subMac1 == nullptr)
{
subMac1 = &subIndMac1;
Expand All @@ -11901,7 +11906,15 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
case GT_BLK:
case GT_DYN_BLK:
case GT_IND:
subMac1 = &subIndMac1;
// A non-null mac here implies this node is part of an address computation (the tree parent is
// GT_ADDR).
// If so, we need to pass the existing mac down to the child node.
//
// Otherwise, use a new mac.
if (subMac1 == nullptr)
{
subMac1 = &subIndMac1;
}
break;
default:
break;
Expand Down Expand Up @@ -11936,16 +11949,16 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
}
}

// If gtOp1 is a GT_FIELD, we need to pass down the mac if
// its parent is GT_ADDR, since the address of the field
// If op1 is a GT_FIELD or indir, we need to pass down the mac if
// its parent is GT_ADDR, since the address of op1
// is part of an ongoing address computation. Otherwise
// op1 represents the value of the field and so any address
// calculations it does are in a new context.
if ((op1->gtOper == GT_FIELD) && (tree->gtOper != GT_ADDR))
if (((op1->gtOper == GT_FIELD) || op1->OperIsIndir()) && (tree->gtOper != GT_ADDR))
{
subMac1 = nullptr;

// The impact of this field's value to any ongoing
// The impact of op1's value to any ongoing
// address computation is handled below when looking
// at op2.
}
Expand Down Expand Up @@ -12046,11 +12059,11 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
break;
}

// If gtOp2 is a GT_FIELD, we must be taking its value,
// If op2 is a GT_FIELD or indir, we must be taking its value,
// so it should evaluate its address in a new context.
if (op2->gtOper == GT_FIELD)
if ((op2->gtOper == GT_FIELD) || op2->OperIsIndir())
{
// The impact of this field's value to any ongoing
// The impact of op2's value to any ongoing
// address computation is handled above when looking
// at op1.
mac = nullptr;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

using System;
using System.Runtime.CompilerServices;

// The jit should null check 'this' in NextElement

unsafe struct GitHub_23791
{
fixed byte A[10];

[MethodImpl(MethodImplOptions.NoInlining)]
byte NextElement(int i) => A[1+i];

static int Main()
{
int result = -1;
GitHub_23791* x = null;
bool threw = true;

try
{
byte t = x->NextElement(100000);
threw = false;
}
catch (NullReferenceException)
{
result = 100;
}

if (!threw)
{
Console.WriteLine($"FAIL: did not throw an exception");
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{2649FAFE-07BF-4F93-8120-BA9A69285ABB}</ProjectGuid>
<OutputType>Exe</OutputType>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
</PropertyGroup>
<!-- Default configurations to help VS understand the configurations -->
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "></PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "></PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
<Visible>False</Visible>
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
<PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "></PropertyGroup>
</Project>

0 comments on commit eda59a1

Please sign in to comment.