Do not devirtualize indirect calls (#13561)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Fri, 25 Aug 2017 16:28:21 +0000 (09:28 -0700)
committerGitHub <noreply@github.com>
Fri, 25 Aug 2017 16:28:21 +0000 (09:28 -0700)
I'm seeing the affected code take the `impDevirtualizeCall` code path
with `CT_INDIRECT` calls. `gtCallMethHnd` is a `GenTreePtr` in that case
(it's a union) and passing that as as `CORINFO_METHOD_HANDLE` leads
to bad things.

src/jit/flowgraph.cpp
tests/src/JIT/Regression/JitBlue/GitHub_13561/GitHub_13561.cs [new file with mode: 0644]
tests/src/JIT/Regression/JitBlue/GitHub_13561/GitHub_13561.csproj [new file with mode: 0644]

index 21deb78..0f5f3db 100644 (file)
@@ -21809,8 +21809,8 @@ Compiler::fgWalkResult Compiler::fgUpdateInlineReturnExpressionPlaceHolder(GenTr
 
         if ((parentTree != nullptr) && (parentTree->gtOper == GT_CALL))
         {
-            GenTreeCall* call          = parentTree->AsCall();
-            bool         tryLateDevirt = call->IsVirtual() && (call->gtCallObjp == tree);
+            GenTreeCall* call  = parentTree->AsCall();
+            bool tryLateDevirt = call->IsVirtual() && (call->gtCallObjp == tree) && (call->gtCallType == CT_USER_FUNC);
 
 #ifdef DEBUG
             tryLateDevirt = tryLateDevirt && (JitConfig.JitEnableLateDevirtualization() == 1);
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_13561/GitHub_13561.cs b/tests/src/JIT/Regression/JitBlue/GitHub_13561/GitHub_13561.cs
new file mode 100644 (file)
index 0000000..d5cbb92
--- /dev/null
@@ -0,0 +1,61 @@
+// 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.Collections.Generic;
+
+internal static class Program
+{
+    class MemberInfo { }
+
+    class PropertyInfo : MemberInfo {  }
+
+    class CustomAttributeData
+    {
+        public Attribute Instantiate() => new CLSCompliantAttribute(false);
+    }
+
+    private static IEnumerable<CustomAttributeData> GetMatchingCustomAttributes(this MemberInfo element, Type optionalAttributeTypeFilter, bool inherit, bool skipTypeValidation = false)
+    {
+        {
+            PropertyInfo propertyInfo = element as PropertyInfo;
+            if (propertyInfo != null)
+                yield return new CustomAttributeData();
+        }
+
+        if (element == null)
+            throw new ArgumentNullException();
+
+        throw new NotSupportedException(); // Shouldn't get here.
+    }
+
+    private static IEnumerable<TOut> Select<TIn, TOut>(this IEnumerable<TIn> source, Func<TIn, TOut> transform)
+    {
+        foreach (var s in source)
+            yield return transform(s);
+    }
+
+    private static IEnumerable<T> GetCustomAttributes<T>(this MemberInfo element, bool inherit) where T : Attribute
+    {
+        IEnumerable<CustomAttributeData> matches = element.GetMatchingCustomAttributes(typeof(T), inherit, skipTypeValidation: true);
+        return matches.Select(m => (T)(m.Instantiate()));
+    }
+
+    private static AttributeType GetCustomAttribute<AttributeType>(PropertyInfo propInfo)
+        where AttributeType : Attribute
+    {
+        AttributeType result = null;
+        foreach (var attrib in propInfo.GetCustomAttributes<AttributeType>(false))
+        {
+            result = attrib;
+            break;
+        }
+        return result;
+    }
+
+    private static int Main(string[] args)
+    {
+        return GetCustomAttribute<Attribute>(new PropertyInfo()) != null ? 100 : -1;
+    }
+}
diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_13561/GitHub_13561.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_13561/GitHub_13561.csproj
new file mode 100644 (file)
index 0000000..c09ad11
--- /dev/null
@@ -0,0 +1,25 @@
+<?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>
+    <AssemblyName>$(MSBuildProjectName)</AssemblyName>
+    <ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup>
+    <DebugType></DebugType>
+    <Optimize>True</Optimize>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>