Fix ifConversion hoisting of invalid managed references#124810
Fix ifConversion hoisting of invalid managed references#124810
Conversation
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
…refs/refs) Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
|
@copilot that is likely not enough. Since byref constructions currently don't light up any side-effects, we have to scan the whole tree for any TYP_BYREF typed node (and, presumably, GT_INT_CNS with TYP_REF type). Also, please check if optimizebools phase can possibly produce these. |
… root Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Updated in 02e3143. The fix now adds a Regarding |
Description
ifConversioncan speculatively hoist potentially invalid byrefs into branchless code (cmov/csel), which may crash the GC. The root cause is that byref constructions don't light up any side-effect flags, so the existingvarTypeIsIntegralOrI()check on the root node is insufficient — intermediate nodes in the operand tree may produce invalid GC-tracked references that would be unconditionally evaluated.Before fix — both sides are unconditionally evaluated:
After fix — branches preserved, invalid byref never materialized:
Changes
src/coreclr/jit/ifconversion.cpp: Added aContainsGCTypestatic helper that recursively scans the entire operand tree for anyTYP_BYREForTYP_REFtyped node. Applied it to the value trees in bothGT_STORE_LCL_VAR(onop1) andGT_RETURN(onretVal) paths inIfConvertCheckStmts, preventing if-conversion when GC types are found anywhere in the value tree.src/tests/JIT/opt/IfConversion/ByrefConversion.cs: Regression test withX64-NOT: cmovandARM64-NOT: cselFileCheck assertions.The
optimizeboolsphase was also checked and is not affected — it only combines boolean branch conditions with AND/OR operators, it does not produce GT_SELECT or conditional moves.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.