From 90c417a923c1afc59ce03aeac2b640c51461353b Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Wed, 18 Oct 2023 15:31:03 -0700 Subject: [PATCH] JIT: remove incorrect type deduction for an Unsafe.As case (#93694) Thanks to @SingleAccretion for the fix. The JIT was assuming that an indirectly accessed value type field had the type of the field, but that might not be the case if the field was accessed via `Unsafe.As`. Fix this by limiting type deduction from these indirectly accessed fields to only ref type fields. Closes #93650. --- src/coreclr/jit/gentree.cpp | 4 +- .../JitBlue/Runtime_93650/Runtime_93650.cs | 42 +++++++++++++++++++ .../Runtime_93650/Runtime_93650.csproj | 8 ++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_93650/Runtime_93650.cs create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_93650/Runtime_93650.csproj diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index 122a8e35f1ee4..585ffa8680210 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -18752,10 +18752,12 @@ CORINFO_CLASS_HANDLE Compiler::gtGetFieldClassHandle(CORINFO_FIELD_HANDLE fieldH { JITDUMP("Field's current class not available\n"); } + + return fieldClass; } } - return fieldClass; + return NO_CLASS_HANDLE; } //------------------------------------------------------------------------ diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_93650/Runtime_93650.cs b/src/tests/JIT/Regression/JitBlue/Runtime_93650/Runtime_93650.cs new file mode 100644 index 0000000000000..b424dd6c2f3c9 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_93650/Runtime_93650.cs @@ -0,0 +1,42 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.CompilerServices; +using System.Text; +using Xunit; + +public struct Holder +{ + internal StringBuilder.AppendInterpolatedStringHandler _h; + public Holder() => _h = new(0, 0, new()); + + internal StringBuilder GetBuilder() => Unsafe.As(ref _h); +} + +public static class Runtime_93650 +{ + static int N = 1; + + [Fact] + public static int Problem() + { + var sb = new Holder(); + for (int i = 0; i < N; i++) + { + var s = Bind(ref sb); + if (s.Length != 0) + { + Console.WriteLine("FAILED: StringBuilder.ToString() returned: " + s); + return -1; + } + } + + return 100; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static string Bind(ref Holder parameters) => GetString(parameters.GetBuilder()); + + public static string GetString(StringBuilder sb) => sb.ToString(); +} diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_93650/Runtime_93650.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_93650/Runtime_93650.csproj new file mode 100644 index 0000000000000..15edd99711a1a --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_93650/Runtime_93650.csproj @@ -0,0 +1,8 @@ + + + True + + + + + \ No newline at end of file