@@ -3164,6 +3164,31 @@ static MethodTable * g_pStreamMT;
31643164static WORD g_slotBeginRead, g_slotEndRead;
31653165static WORD g_slotBeginWrite, g_slotEndWrite;
31663166
3167+ static bool HasOverriddenStreamMethod (MethodTable * pMT, WORD slot)
3168+ {
3169+ CONTRACTL{
3170+ NOTHROW;
3171+ GC_NOTRIGGER;
3172+ MODE_ANY;
3173+ SO_TOLERANT;
3174+ } CONTRACTL_END;
3175+
3176+ PCODE actual = pMT->GetRestoredSlot (slot);
3177+ PCODE base = g_pStreamMT->GetRestoredSlot (slot);
3178+ if (actual == base)
3179+ return false ;
3180+
3181+ if (!g_pStreamMT->IsZapped ())
3182+ {
3183+ // If mscorlib is JITed, the slots can be patched and thus we need to compare the actual MethodDescs
3184+ // to detect match reliably
3185+ if (MethodTable::GetMethodDescForSlotAddress (actual) == MethodTable::GetMethodDescForSlotAddress (base))
3186+ return false ;
3187+ }
3188+
3189+ return true ;
3190+ }
3191+
31673192FCIMPL1 (FC_BOOL_RET, StreamNative::HasOverriddenBeginEndRead, Object *stream)
31683193{
31693194 FCALL_CONTRACT;
@@ -3182,10 +3207,7 @@ FCIMPL1(FC_BOOL_RET, StreamNative::HasOverriddenBeginEndRead, Object *stream)
31823207
31833208 MethodTable * pMT = stream->GetMethodTable ();
31843209
3185- FC_RETURN_BOOL (
3186- pMT->GetRestoredSlot (g_slotBeginRead) != g_pStreamMT->GetRestoredSlot (g_slotBeginRead) ||
3187- pMT->GetRestoredSlot (g_slotEndRead) != g_pStreamMT->GetRestoredSlot (g_slotEndRead)
3188- );
3210+ FC_RETURN_BOOL (HasOverriddenStreamMethod (pMT, g_slotBeginRead) || HasOverriddenStreamMethod (pMT, g_slotEndRead));
31893211}
31903212FCIMPLEND
31913213
@@ -3207,9 +3229,6 @@ FCIMPL1(FC_BOOL_RET, StreamNative::HasOverriddenBeginEndWrite, Object *stream)
32073229
32083230 MethodTable * pMT = stream->GetMethodTable ();
32093231
3210- FC_RETURN_BOOL (
3211- pMT->GetRestoredSlot (g_slotBeginWrite) != g_pStreamMT->GetRestoredSlot (g_slotBeginWrite) ||
3212- pMT->GetRestoredSlot (g_slotEndWrite) != g_pStreamMT->GetRestoredSlot (g_slotEndWrite)
3213- );
3232+ FC_RETURN_BOOL (HasOverriddenStreamMethod (pMT, g_slotBeginWrite) || HasOverriddenStreamMethod (pMT, g_slotEndWrite));
32143233}
32153234FCIMPLEND
0 commit comments