Skip to content

Commit

Permalink
JIT: if IR and stack can't provide a ref class handle, use object (do…
Browse files Browse the repository at this point in the history
…tnet#239)

In some cases we may end up in `lvaSetClass` without a valid ref class handle
from either the IR or the stack. Use the handle for `object` as a conservative
fallback.

Closes dotnet/coreclr#27923.
AndyAyersMS authored Nov 26, 2019
1 parent ff2c9c0 commit ffdb313
Showing 3 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/coreclr/src/jit/lclvars.cpp
Original file line number Diff line number Diff line change
@@ -2666,7 +2666,7 @@ void Compiler::lvaSetClass(unsigned varNum, CORINFO_CLASS_HANDLE clsHnd, bool is
// Notes:
// Preferentially uses the tree's type, when available. Since not all
// tree kinds can track ref types, the stack type is used as a
// fallback.
// fallback. If there is no stack type, then the class is set to object.

void Compiler::lvaSetClass(unsigned varNum, GenTree* tree, CORINFO_CLASS_HANDLE stackHnd)
{
@@ -2682,6 +2682,10 @@ void Compiler::lvaSetClass(unsigned varNum, GenTree* tree, CORINFO_CLASS_HANDLE
{
lvaSetClass(varNum, stackHnd);
}
else
{
lvaSetClass(varNum, impGetObjectClass());
}
}

//------------------------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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;

class Writer
{
public object Data { get; set; }
public int Position { get; set; }

[MethodImpl(MethodImplOptions.NoInlining)]
Writer()
{
Data = new int[] { 100, -1, -2, -3 };
Position = 4;
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static ArraySegment<byte> Test()
{
var writer = new Writer();
object temp = writer.Data;
byte[] data = Unsafe.As<object, byte[]>(ref temp);
return new ArraySegment<byte>(data, 0, writer.Position);
}

public static int Main()
{
var x = Test();
return x[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>

0 comments on commit ffdb313

Please sign in to comment.