Skip to content

Forbid creation of non-faulting null-check nodes. #77078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9940,8 +9940,8 @@ var_types Compiler::gtTypeForNullCheck(GenTree* tree)
// gtChangeOperToNullCheck: helper to change tree oper to a NULLCHECK.
//
// Arguments:
// tree - the node to change;
// basicBlock - basic block of the node.
// tree - the node to change;
// block - basic block of the node.
//
// Notes:
// the function should not be called after lowering for platforms that do not support
Expand All @@ -9953,6 +9953,8 @@ void Compiler::gtChangeOperToNullCheck(GenTree* tree, BasicBlock* block)
assert(tree->OperIs(GT_FIELD, GT_IND, GT_OBJ, GT_BLK));
tree->ChangeOper(GT_NULLCHECK);
tree->ChangeType(gtTypeForNullCheck(tree));
assert(fgAddrCouldBeNull(tree->gtGetOp1()));
tree->gtFlags |= GTF_EXCEPT;
block->bbFlags |= BBF_HAS_NULLCHECK;
optMethodFlags |= OMF_HAS_NULLCHECK;
}
Expand Down
19 changes: 16 additions & 3 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16047,11 +16047,24 @@ void Compiler::gtExtractSideEffList(GenTree* expr,
{
if (m_compiler->gtNodeHasSideEffects(node, m_flags))
{
Append(node);
if (node->OperIsBlk() && !node->OperIsStoreBlk())
{
JITDUMP("Replace an unused OBJ/BLK node [%06d] with a NULLCHECK\n", dspTreeID(node));
m_compiler->gtChangeOperToNullCheck(node, m_compiler->compCurBB);
// Check for a guaranteed non-faulting IND, and create a NOP node instead of a NULLCHECK in that
// case.
if (m_compiler->fgAddrCouldBeNull(node->AsBlk()->Addr()))
{
Append(node);
JITDUMP("Replace an unused OBJ/BLK node [%06d] with a NULLCHECK\n", dspTreeID(node));
m_compiler->gtChangeOperToNullCheck(node, m_compiler->compCurBB);
}
else
{
JITDUMP("Dropping non-faulting OBJ/BLK node [%06d]\n", dspTreeID(node));
}
}
else
{
Append(node);
}
return Compiler::WALK_SKIP_SUBTREES;
}
Expand Down
10 changes: 9 additions & 1 deletion src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8251,7 +8251,15 @@ void Compiler::impImportBlockCode(BasicBlock* block)
// via an underlying address, just null check the address.
if (op1->OperIs(GT_FIELD, GT_IND, GT_OBJ))
{
gtChangeOperToNullCheck(op1, block);
GenTree* addr = op1->gtGetOp1();
if ((addr != nullptr) && fgAddrCouldBeNull(addr))
{
gtChangeOperToNullCheck(op1, block);
}
else
{
op1 = gtNewNothingNode();
}
}
else
{
Expand Down
22 changes: 17 additions & 5 deletions src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7279,13 +7279,16 @@ void Lowering::LowerIndir(GenTreeIndir* ind)
#if defined(TARGET_ARM64)
// Verify containment safety before creating an LEA that must be contained.
//
const bool isContainable = IsSafeToContainMem(ind, ind->Addr());
const bool isContainable = (ind->Addr() != nullptr) && IsSafeToContainMem(ind, ind->Addr());
#else
const bool isContainable = true;
#endif

TryCreateAddrMode(ind->Addr(), isContainable, ind);
ContainCheckIndir(ind);
if (!ind->OperIs(GT_NOP))
{
TryCreateAddrMode(ind->Addr(), isContainable, ind);
ContainCheckIndir(ind);
}

#ifdef TARGET_XARCH
if (ind->OperIs(GT_NULLCHECK) || ind->IsUnusedValue())
Expand Down Expand Up @@ -7333,14 +7336,23 @@ void Lowering::TransformUnusedIndirection(GenTreeIndir* ind, Compiler* comp, Bas
//
assert(ind->OperIs(GT_NULLCHECK, GT_IND, GT_BLK, GT_OBJ));

GenTree* const addr = ind->Addr();
if (!comp->fgAddrCouldBeNull(addr))
{
addr->SetUnusedValue();
ind->gtBashToNOP();
JITDUMP("bash an unused indir [%06u] to NOP.\n", comp->dspTreeID(ind));
return;
}

ind->ChangeType(comp->gtTypeForNullCheck(ind));

#if defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64)
bool useNullCheck = true;
#elif TARGET_ARM
#elif defined(TARGET_ARM)
bool useNullCheck = false;
#else // TARGET_XARCH
bool useNullCheck = !ind->Addr()->isContained();
bool useNullCheck = !addr->isContained();
ind->ClearDontExtend();
#endif // !TARGET_XARCH

Expand Down