Skip to content

Commit e8083cd

Browse files
lewingCopilot
andcommitted
JIT: zero-init untracked GC locals stored after a throwing tree on all targets
Move the fix from IsPotentialGCSafePoint into optRemoveRedundantZeroInits and make it target-independent. A GC local reported as an untracked slot is reported whenever its frame is, including at a throw helper call that a tree before the local's first store may make. Natively, an address-exposed object local stored after a bounds check was reported uninitialized at the RNGCHKFAIL call, and the test now crashes without the fix on osx-arm64 even without GC stress. Wasm reports every GC local on the frame as untracked, so its codegen is unchanged from the previous commits. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 45f42362-f012-4030-8643-26f4c1fbf2e1
1 parent c937908 commit e8083cd

3 files changed

Lines changed: 51 additions & 16 deletions

File tree

‎src/coreclr/jit/compiler.hpp‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,16 +3319,6 @@ inline bool Compiler::IsPotentialGCSafePoint(GenTree* tree) const
33193319
}
33203320
}
33213321

3322-
#ifdef TARGET_WASM
3323-
// On wasm, GC locals on the frame are reported as untracked slots, including for aborted frames,
3324-
// and a tree that may throw can call a throw helper (see StackLevelSetter::MayUseThrowHelperBlock)
3325-
// before a local's first store.
3326-
if ((tree->gtFlags & GTF_EXCEPT) != 0)
3327-
{
3328-
return true;
3329-
}
3330-
#endif // TARGET_WASM
3331-
33323322
// TYP_STRUCT-typed stores might be converted into calls (with gc safe points) in Lower.
33333323
// This is quite a conservative fix as it's hard to prove Lower won't do it at this point.
33343324
if (tree->OperIsLocalStore())

‎src/coreclr/jit/optimizer.cpp‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5696,6 +5696,8 @@ typedef JitHashTable<unsigned, JitSmallPrimitiveKeyFuncs<unsigned>, unsigned> Lc
56965696
// the store is to a local (and not a field),
56975697
// the local is not lvLiveInOutOfHndlr or no exceptions can be thrown between the prolog and the store,
56985698
// either the local has no gc pointers or there are no gc-safe points between the prolog and the store,
5699+
// and, if the local is reported as an untracked gc slot, no exceptions can be thrown between the prolog
5700+
// and the store,
56995701
// then the local is marked with lvHasExplicitInit which tells the codegen not to insert zero initialization
57005702
// for this local in the prolog.
57015703
//
@@ -5714,6 +5716,7 @@ void Compiler::optRemoveRedundantZeroInits()
57145716
BitVec zeroInitLocals = BitVecOps::MakeEmpty(&bitVecTraits);
57155717
bool hasGCSafePoint = false;
57165718
bool hasImplicitControlFlow = false;
5719+
bool mayThrow = false;
57175720

57185721
assert(fgNodeThreading == NodeThreading::AllTrees);
57195722

@@ -5764,7 +5767,9 @@ void Compiler::optRemoveRedundantZeroInits()
57645767
Statement* next = stmt->GetNextStmt();
57655768
for (GenTree* const tree : stmt->TreeList())
57665769
{
5767-
hasImplicitControlFlow |= hasEHSuccs && ((tree->gtFlags & GTF_EXCEPT) != 0);
5770+
const bool treeMayThrow = (tree->gtFlags & GTF_EXCEPT) != 0;
5771+
hasImplicitControlFlow |= hasEHSuccs && treeMayThrow;
5772+
mayThrow |= treeMayThrow;
57685773
hasGCSafePoint |= IsPotentialGCSafePoint(tree);
57695774

57705775
switch (tree->gtOper)
@@ -5922,7 +5927,16 @@ void Compiler::optRemoveRedundantZeroInits()
59225927
// insert a call to CORINFO_HELP_INIT_PINVOKE_FRAME but that is not a gc-safe point.
59235928
assert(s_helperCallProperties.IsNoGC(CORINFO_HELP_INIT_PINVOKE_FRAME));
59245929

5925-
if (!lclDsc->HasGCPtr() || (!GetInterruptible() && !hasGCSafePoint))
5930+
#ifdef TARGET_WASM
5931+
// Wasm reports all gc locals on the frame as untracked slots.
5932+
const bool reportedAsUntracked = true;
5933+
#else
5934+
const bool reportedAsUntracked = !lclDsc->lvTracked;
5935+
#endif
5936+
// An untracked gc slot is reported whenever its frame is, including from
5937+
// a throw helper that a tree before this store may call.
5938+
if (!lclDsc->HasGCPtr() ||
5939+
(!GetInterruptible() && !hasGCSafePoint && !(mayThrow && reportedAsUntracked)))
59265940
{
59275941
// The local hasn't been used and won't be reported to the gc between
59285942
// the prolog and this explicit initialization. Therefore, it doesn't

‎src/tests/JIT/Regression_ro_2/Runtime_135966.cs‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
using System.Runtime.CompilerServices;
66
using Xunit;
77

8-
// Victim's null check throws before its GC ref local is stored, so a GC while the
9-
// exception is dispatched must not see DirtyStack's garbage in that local.
8+
// Each victim throws before its GC ref local is first stored, so a GC while the throw
9+
// helper runs or the exception is dispatched must not see DirtyStack's garbage in it.
1010
public class Runtime_135966
1111
{
1212
private class Holder
@@ -23,21 +23,37 @@ private struct Garbage
2323
[Fact]
2424
public static void TestEntryPoint()
2525
{
26+
int[] array = new int[1];
27+
2628
for (int i = 0; i < 10; i++)
2729
{
2830
DirtyStack();
2931

3032
bool caught = false;
3133
try
3234
{
33-
Victim(null);
35+
NullCheckVictim(null);
3436
}
3537
catch (NullReferenceException) when (Collect())
3638
{
3739
caught = true;
3840
}
3941

4042
Assert.True(caught);
43+
44+
DirtyStack();
45+
46+
caught = false;
47+
try
48+
{
49+
BoundsCheckVictim(array, 5);
50+
}
51+
catch (IndexOutOfRangeException) when (Collect())
52+
{
53+
caught = true;
54+
}
55+
56+
Assert.True(caught);
4157
}
4258
}
4359

@@ -56,18 +72,33 @@ private static void Consume(Span<nint> span)
5672
}
5773

5874
[MethodImpl(MethodImplOptions.NoInlining)]
59-
private static object Victim(Holder holder)
75+
private static object NullCheckVictim(Holder holder)
6076
{
6177
object value = holder.Value;
6278
KeepAlive(value);
6379
return value;
6480
}
6581

82+
// The local is address-exposed, so it is an untracked GC slot on every target.
83+
[MethodImpl(MethodImplOptions.NoInlining)]
84+
private static object BoundsCheckVictim(int[] array, int index)
85+
{
86+
int element = array[index];
87+
object value = array;
88+
KeepAlive(ref value);
89+
return element == 0 ? value : null;
90+
}
91+
6692
[MethodImpl(MethodImplOptions.NoInlining)]
6793
private static void KeepAlive(object value)
6894
{
6995
}
7096

97+
[MethodImpl(MethodImplOptions.NoInlining)]
98+
private static void KeepAlive(ref object value)
99+
{
100+
}
101+
71102
private static bool Collect()
72103
{
73104
GC.Collect();

0 commit comments

Comments
 (0)