@@ -2708,9 +2708,9 @@ ValueNum ValueNumStore::VNForCast(VNFunc func, ValueNum castToVN, ValueNum objVN
27082708 bool isExact;
27092709 bool isNonNull;
27102710 CORINFO_CLASS_HANDLE castFrom = GetObjectType(objVN, &isExact, &isNonNull);
2711- CORINFO_CLASS_HANDLE castTo;
2711+ CORINFO_CLASS_HANDLE castTo = NO_CLASS_HANDLE ;
27122712 if ((castFrom != NO_CLASS_HANDLE) &&
2713- EmbeddedHandleMapLookup(ConstantValue<ssize_t>(castToVN), (ssize_t*)&castTo))
2713+ EmbeddedHandleMapLookup(ConstantValue<ssize_t>(castToVN), (ssize_t*)&castTo) && (castTo != NO_CLASS_HANDLE) )
27142714 {
27152715 TypeCompareState castResult = m_pComp->info.compCompHnd->compareTypesForCast(castFrom, castTo);
27162716 if (castResult == TypeCompareState::Must)
@@ -13738,9 +13738,12 @@ bool Compiler::fgValueNumberSpecialIntrinsic(GenTreeCall* call)
1373813738 break;
1373913739 }
1374013740
13741- ValueNum clsVN = typeHandleFuncApp.m_args[0];
13742- ssize_t clsHandle;
13743- if (!vnStore->EmbeddedHandleMapLookup(vnStore->ConstantValue<ssize_t>(clsVN), &clsHandle))
13741+ ValueNum clsVN = typeHandleFuncApp.m_args[0];
13742+ ssize_t clsHandle = 0;
13743+
13744+ // NOTE: EmbeddedHandleMapLookup may return 0 for non-0 embedded handle
13745+ if (!vnStore->EmbeddedHandleMapLookup(vnStore->ConstantValue<ssize_t>(clsVN), &clsHandle) &&
13746+ (clsHandle != 0))
1374413747 {
1374513748 break;
1374613749 }
@@ -15152,9 +15155,12 @@ CORINFO_CLASS_HANDLE ValueNumStore::GetObjectType(ValueNum vn, bool* pIsExact, b
1515215155 const VNFunc func = funcApp.m_func;
1515315156 if ((func == VNF_CastClass) || (func == VNF_IsInstanceOf) || (func == VNF_JitNew))
1515415157 {
15155- ssize_t clsHandle;
15156- ValueNum clsVN = funcApp.m_args[0];
15157- if (IsVNTypeHandle(clsVN) && EmbeddedHandleMapLookup(ConstantValue<ssize_t>(clsVN), &clsHandle))
15158+ ssize_t clsHandle = 0;
15159+ ValueNum clsVN = funcApp.m_args[0];
15160+
15161+ // NOTE: EmbeddedHandleMapLookup may return 0 for non-0 embedded handle
15162+ if (IsVNTypeHandle(clsVN) && EmbeddedHandleMapLookup(ConstantValue<ssize_t>(clsVN), &clsHandle) &&
15163+ (clsHandle != 0))
1515815164 {
1515915165 // JitNew returns an exact and non-null obj, castclass and isinst do not have this guarantee.
1516015166 *pIsNonNull = func == VNF_JitNew;
0 commit comments