Skip to content

Commit 0f0753d

Browse files
authored
Revert potential UB due to aliasing + more WB removals (#111733)
1 parent d5c8265 commit 0f0753d

File tree

5 files changed

+41
-32
lines changed

5 files changed

+41
-32
lines changed

src/coreclr/jit/assertionprop.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5258,30 +5258,6 @@ static GCInfo::WriteBarrierForm GetWriteBarrierForm(Compiler* comp, ValueNum vn)
52585258
return GetWriteBarrierForm(comp, funcApp.m_args[0]);
52595259
}
52605260
}
5261-
if (funcApp.m_func == VNF_InitVal)
5262-
{
5263-
unsigned lclNum = vnStore->CoercedConstantValue<unsigned>(funcApp.m_args[0]);
5264-
assert(lclNum != BAD_VAR_NUM);
5265-
CORINFO_CLASS_HANDLE srcCls = NO_CLASS_HANDLE;
5266-
5267-
if (comp->compMethodHasRetVal() && (lclNum == comp->info.compRetBuffArg))
5268-
{
5269-
// See if the address is in current method's return buffer
5270-
// while the return type is a byref-like type.
5271-
srcCls = comp->info.compMethodInfo->args.retTypeClass;
5272-
}
5273-
else if (lclNum == comp->info.compThisArg)
5274-
{
5275-
// Same for implicit "this" parameter
5276-
assert(!comp->info.compIsStatic);
5277-
srcCls = comp->info.compClassHnd;
5278-
}
5279-
5280-
if ((srcCls != NO_CLASS_HANDLE) && comp->eeIsByrefLike(srcCls))
5281-
{
5282-
return GCInfo::WriteBarrierForm::WBF_NoBarrier;
5283-
}
5284-
}
52855261
}
52865262
return GCInfo::WriteBarrierForm::WBF_BarrierUnknown;
52875263
}

src/coreclr/jit/importer.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9548,6 +9548,21 @@ void Compiler::impImportBlockCode(BasicBlock* block)
95489548
{
95499549
indirFlags |= GTF_IND_TGT_HEAP;
95509550
}
9551+
else if ((lclTyp == TYP_STRUCT) && (fieldInfo.structType != NO_CLASS_HANDLE) &&
9552+
eeIsByrefLike(fieldInfo.structType))
9553+
{
9554+
// Field's type is a byref-like struct -> address is not on the heap.
9555+
indirFlags |= GTF_IND_TGT_NOT_HEAP;
9556+
}
9557+
else
9558+
{
9559+
// Field's owner is a byref-like struct -> address is not on the heap.
9560+
CORINFO_CLASS_HANDLE fldOwner = info.compCompHnd->getFieldClass(resolvedToken.hField);
9561+
if ((fldOwner != NO_CLASS_HANDLE) && eeIsByrefLike(fldOwner))
9562+
{
9563+
indirFlags |= GTF_IND_TGT_NOT_HEAP;
9564+
}
9565+
}
95519566

95529567
assert(varTypeIsI(op1));
95539568
op1 = (lclTyp == TYP_STRUCT) ? gtNewStoreBlkNode(layout, op1, op2, indirFlags)->AsIndir()

src/coreclr/jit/morph.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7755,14 +7755,23 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
77557755
break;
77567756

77577757
case GT_STOREIND:
7758-
if (op1->OperIs(GT_FIELD_ADDR) && varTypeIsGC(tree))
7758+
if (varTypeIsGC(tree))
77597759
{
7760-
CORINFO_FIELD_HANDLE fieldHandle = op1->AsFieldAddr()->gtFldHnd;
7761-
if (eeIsByrefLike(info.compCompHnd->getFieldClass(fieldHandle)))
7760+
GenTree* addr = op1;
7761+
// If we're storing a reference to a field (GT_FIELD_ADDR), let's check if the field's owner is a
7762+
// byref-like struct.
7763+
while ((addr != nullptr) && addr->OperIs(GT_FIELD_ADDR))
77627764
{
7763-
JITDUMP("Marking [%06u] STOREIND as GTF_IND_TGT_NOT_HEAP: field's owner is a byref-like struct\n",
7765+
CORINFO_FIELD_HANDLE fieldHandle = addr->AsFieldAddr()->gtFldHnd;
7766+
if (eeIsByrefLike(info.compCompHnd->getFieldClass(fieldHandle)))
7767+
{
7768+
JITDUMP(
7769+
"Marking [%06u] STOREIND as GTF_IND_TGT_NOT_HEAP: field's owner is a byref-like struct\n",
77647770
dspTreeID(tree));
7765-
tree->gtFlags |= GTF_IND_TGT_NOT_HEAP;
7771+
tree->gtFlags |= GTF_IND_TGT_NOT_HEAP;
7772+
break;
7773+
}
7774+
addr = addr->AsFieldAddr()->GetFldObj();
77667775
}
77677776
}
77687777
break;

src/coreclr/jit/morphblock.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,10 @@ GenTree* MorphCopyBlockHelper::CopyFieldByField()
13631363
if (m_store->OperIs(GT_STORE_BLK, GT_STOREIND))
13641364
{
13651365
indirFlags = m_store->gtFlags & (GTF_IND_TGT_NOT_HEAP | GTF_IND_TGT_HEAP);
1366+
if (m_store->OperIs(GT_STORE_BLK) && m_store->AsBlk()->GetLayout()->IsStackOnly(m_comp))
1367+
{
1368+
indirFlags |= GTF_IND_TGT_NOT_HEAP;
1369+
}
13661370
}
13671371
dstFldStore = m_comp->gtNewStoreIndNode(srcType, fldAddr, srcFld, indirFlags);
13681372
}

src/coreclr/jit/promotiondecomposition.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,21 @@ class DecompositionPlan
523523
target_ssize_t addrBaseOffs = 0;
524524
FieldSeq* addrBaseOffsFldSeq = nullptr;
525525
GenTreeFlags indirFlags = GTF_EMPTY;
526-
526+
GenTreeFlags flagsToPropagate = GTF_IND_COPYABLE_FLAGS;
527527
if (m_store->OperIs(GT_STORE_BLK))
528528
{
529+
flagsToPropagate |= GTF_IND_TGT_NOT_HEAP | GTF_IND_TGT_HEAP;
529530
addr = m_store->AsIndir()->Addr();
530-
indirFlags = m_store->gtFlags & GTF_IND_COPYABLE_FLAGS;
531+
indirFlags = m_store->gtFlags & flagsToPropagate;
532+
if (m_store->AsBlk()->GetLayout()->IsStackOnly(m_compiler))
533+
{
534+
indirFlags |= GTF_IND_TGT_NOT_HEAP;
535+
}
531536
}
532537
else if (m_src->OperIs(GT_BLK))
533538
{
534539
addr = m_src->AsIndir()->Addr();
535-
indirFlags = m_src->gtFlags & GTF_IND_COPYABLE_FLAGS;
540+
indirFlags = m_src->gtFlags & flagsToPropagate;
536541
}
537542

538543
int numAddrUses = 0;

0 commit comments

Comments
 (0)