Skip to content

Commit 19b5f15

Browse files
github-actions[bot]jkotasMichalStrehovsky
authored
[release/6.0] Fix catching of generic exception in crossgened shared generic code (#66207)
* Add regression test for #66005 * Fix * Update src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs Co-authored-by: Michal Strehovský <[email protected]> Co-authored-by: Jan Kotas <[email protected]> Co-authored-by: Michal Strehovský <[email protected]>
1 parent 4050c12 commit 19b5f15

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,8 @@ private bool Get_CORINFO_METHOD_INFO(MethodDesc method, MethodIL methodIL, CORIN
604604
methodInfo->ILCode = (byte*)GetPin(ilCode);
605605
methodInfo->ILCodeSize = (uint)ilCode.Length;
606606
methodInfo->maxStack = (uint)methodIL.MaxStack;
607-
methodInfo->EHcount = (uint)methodIL.GetExceptionRegions().Length;
607+
var exceptionRegions = methodIL.GetExceptionRegions();
608+
methodInfo->EHcount = (uint)exceptionRegions.Length;
608609
methodInfo->options = methodIL.IsInitLocals ? CorInfoOptions.CORINFO_OPT_INIT_LOCALS : (CorInfoOptions)0;
609610

610611
if (method.AcquiresInstMethodTableFromThis())
@@ -623,6 +624,24 @@ private bool Get_CORINFO_METHOD_INFO(MethodDesc method, MethodIL methodIL, CORIN
623624
Get_CORINFO_SIG_INFO(method, sig: &methodInfo->args);
624625
Get_CORINFO_SIG_INFO(methodIL.GetLocals(), &methodInfo->locals);
625626

627+
#if READYTORUN
628+
if ((methodInfo->options & CorInfoOptions.CORINFO_GENERICS_CTXT_MASK) != 0)
629+
{
630+
foreach (var region in exceptionRegions)
631+
{
632+
if (region.Kind == ILExceptionRegionKind.Catch)
633+
{
634+
TypeDesc catchType = (TypeDesc)methodIL.GetObject(region.ClassToken);
635+
if (catchType.IsCanonicalSubtype(CanonicalFormKind.Any))
636+
{
637+
methodInfo->options |= CorInfoOptions.CORINFO_GENERICS_CTXT_KEEP_ALIVE;
638+
break;
639+
}
640+
}
641+
}
642+
}
643+
#endif
644+
626645
return true;
627646
}
628647

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
7+
class Program
8+
{
9+
[MethodImpl(MethodImplOptions.NoInlining)]
10+
static void Test<TException>() where TException : Exception
11+
{
12+
try
13+
{
14+
throw new InvalidOperationException();
15+
}
16+
catch (TException)
17+
{
18+
return;
19+
}
20+
}
21+
22+
static int Main()
23+
{
24+
try
25+
{
26+
Test<InvalidOperationException>();
27+
}
28+
catch
29+
{
30+
return -1;
31+
}
32+
33+
return 100;
34+
}
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<CLRTestPriority>1</CLRTestPriority>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="test66005.cs" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)