Skip to content

Commit

Permalink
JIT: fix non-funclet EH empty finally/fault removal (#109361)
Browse files Browse the repository at this point in the history
For non-funclet EH we don't use the handler index to constrain the placement
of add code descs, but we were still recording the handler index in the ACD.

This lead to some errors in ACD maintenance if there was an EH region
being removed with an ACD in the try and an equivalent one in the handler.

Fixes #108851
  • Loading branch information
AndyAyersMS authored Oct 30, 2024
1 parent a72cfb0 commit 86d2eaa
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/coreclr/jit/fgehopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ PhaseStatus Compiler::fgRemoveEmptyFinally()
AddCodeDscMap* const map = fgGetAddCodeDscMap();
for (AddCodeDsc* const add : AddCodeDscMap::ValueIteration(map))
{
JITDUMP("Considering ");
JITDUMPEXEC(add->Dump());

// Remember the old lookup key
//
AddCodeDscKey oldKey(add);
Expand All @@ -245,7 +248,7 @@ PhaseStatus Compiler::fgRemoveEmptyFinally()
{
bool const removed = map->Remove(oldKey);
assert(removed);
JITDUMP("ACD%u was in EH#%u handler region: removing\n", XTnum);
JITDUMP("ACD%u was in EH#%u handler region: removing\n", add->acdNum, XTnum);
JITDUMPEXEC(add->Dump());
continue;
}
Expand All @@ -258,6 +261,7 @@ PhaseStatus Compiler::fgRemoveEmptyFinally()

if (!inTry || ((unsigned)(add->acdTryIndex - 1) != XTnum))
{
JITDUMP("ACD%u not affected\n", add->acdNum);
continue;
}

Expand Down
9 changes: 6 additions & 3 deletions src/coreclr/jit/flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3420,9 +3420,12 @@ void Compiler::fgAddCodeRef(BasicBlock* srcBlk, SpecialCodeKind kind)
add = new (this, CMK_Unknown) AddCodeDsc;
add->acdDstBlk = nullptr;
add->acdTryIndex = srcBlk->bbTryIndex;
add->acdHndIndex = srcBlk->bbHndIndex;
add->acdKeyDsg = dsg;
add->acdKind = kind;

// For non-funclet EH we don't constrain ACD placement via handler regions
add->acdHndIndex = UsesFunclets() ? srcBlk->bbHndIndex : 0;

add->acdKeyDsg = dsg;
add->acdKind = kind;

// This gets set true in the stack level setter
// if there's still a need for this helper
Expand Down
46 changes: 46 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_108851/Runtime_108851.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Generated by Fuzzlyn v2.4 on 2024-10-14 17:25:44
// Run on X86 Windows
// Seed: 16789241273493732217-vectort,vector128,vector256,x86aes,x86avx,x86avx2,x86avx512bw,x86avx512bwvl,x86avx512cd,x86avx512cdvl,x86avx512dq,x86avx512dqvl,x86avx512f,x86avx512fvl,x86bmi1,x86bmi2,x86fma,x86lzcnt,x86pclmulqdq,x86popcnt,x86sse,x86sse2,x86sse3,x86sse41,x86sse42,x86ssse3,x86x86base
// Reduced from 49.4 KiB to 0.5 KiB in 00:03:38
// Hits JIT assert in Release:
// Assertion failed '!fgRngChkThrowAdded' in 'Program:Main(Fuzzlyn.ExecutionServer.IRuntime)' during 'Calculate stack level slots' (IL size 59; hash 0xade6b36b; FullOpts)
//
// File: D:\a\_work\1\s\src\coreclr\jit\flowgraph.cpp Line: 3655
//
using System;
using System.Numerics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using Xunit;

public class Runtime_108851
{
public static int[] s_21 = new int[1];

[Fact]
public static void Problem()
{
if (!Avx512F.IsSupported)
{
return;
}

var vr1 = Vector128.Create<double>(0);
if (Avx512F.ConvertToUInt32WithTruncation(vr1) >= 2894444111893762202L)
{
try
{
s_21[0] = 0;
}
finally
{
s_21[0] = 0;
}
}

s_21[0] = 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Optimize>True</Optimize>
<NoWarn>0652</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>

0 comments on commit 86d2eaa

Please sign in to comment.