Index: /trunk/src/VBox/VMM/VMMAll/IEMAllN8veRecompiler.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMAll/IEMAllN8veRecompiler.cpp	(revision 101534)
+++ /trunk/src/VBox/VMM/VMMAll/IEMAllN8veRecompiler.cpp	(revision 101535)
@@ -2960,4 +2960,5 @@
 #  error "Port me!"
 # endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -2989,4 +2990,5 @@
     pbCodeBuf[off++] = 0x0b;                    /* or edx, eax */
     pbCodeBuf[off++] = X86_MODRM_MAKE(X86_MOD_REG, X86_GREG_xDX, X86_GREG_xAX);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
     /* Jump to non-zero status return path. */
@@ -3015,4 +3017,5 @@
 # error "port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -3201,25 +3204,8 @@
      * Make the call.
      */
-#ifdef RT_ARCH_AMD64
-    off = iemNativeEmitLoadGprImm64(pReNative, off, X86_GREG_xAX, pfnCImpl);
-
-    uint8_t *pbCodeBuf = iemNativeInstrBufEnsure(pReNative, off, 2);
-    AssertReturn(pbCodeBuf, UINT32_MAX);
-    pbCodeBuf[off++] = 0xff;                    /* call rax */
-    pbCodeBuf[off++] = X86_MODRM_MAKE(X86_MOD_REG, 2, X86_GREG_xAX);
-
-# if defined(VBOXSTRICTRC_STRICT_ENABLED) && defined(RT_OS_WINDOWS)
+    off = iemNativeEmitCallImm(pReNative, off, pfnCImpl);
+
+#if defined(RT_ARCH_AMD64) && defined(VBOXSTRICTRC_STRICT_ENABLED) && defined(RT_OS_WINDOWS)
     off = iemNativeEmitLoadGprByBpU32(pReNative, off, X86_GREG_xAX, IEMNATIVE_FP_OFF_IN_SHADOW_ARG0); /* rcStrict (see above) */
-# endif
-
-#elif defined(RT_ARCH_ARM64)
-    off = iemNativeEmitLoadGprImm64(pReNative, off, IEMNATIVE_REG_FIXED_TMP0, pfnCImpl);
-
-    uint32_t *pu32CodeBuf = iemNativeInstrBufEnsure(pReNative, off, 1);
-    AssertReturn(pu32CodeBuf, UINT32_MAX);
-    pu32CodeBuf[off++] = Armv8A64MkInstrBlr(IEMNATIVE_REG_FIXED_TMP0);
-
-#else
-# error "Port me!"
 #endif
 
@@ -3347,11 +3333,4 @@
         /* iemNativeHlpExecStatusCodeFiddling(PVMCPUCC pVCpu, int rc, uint8_t idxInstr) */
 #ifdef RT_ARCH_AMD64
-        /*
-         * AMD64:
-         */
-        uint8_t *pbCodeBuf = iemNativeInstrBufEnsure(pReNative, off, 20);
-        AssertReturn(pbCodeBuf, UINT32_MAX);
-
-        /* Call helper and jump to return point. */
 # ifdef RT_OS_WINDOWS
 #  ifdef IEMNATIVE_WITH_INSTRUCTION_COUNTING
@@ -3371,15 +3350,8 @@
 # endif
 
-#elif defined(RT_ARCH_ARM64)
-        /*
-         * ARM64:
-         */
+#else
         off = iemNativeEmitLoadGprFromGpr(pReNative, off, IEMNATIVE_CALL_ARG1_GREG, IEMNATIVE_CALL_RET_GREG);
         off = iemNativeEmitLoadGprFromGpr(pReNative, off, IEMNATIVE_CALL_ARG0_GREG, IEMNATIVE_REG_FIXED_PVMCPU);
         /* IEMNATIVE_CALL_ARG2_GREG is already set. */
-        off = iemNativeEmitLoadGprImm64(pReNative, off, IEMNATIVE_REG_FIXED_TMP0, (uintptr_t)iemNativeHlpExecStatusCodeFiddling);
-        AssertReturn(off != UINT32_MAX, UINT32_MAX);
-#else
-# error "port me"
 #endif
 
@@ -3477,4 +3449,5 @@
 # error "port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
     return iemNativeEmitRcFiddling(pReNative, off, idxReturn);
@@ -3583,4 +3556,5 @@
 # error "port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
Index: /trunk/src/VBox/VMM/include/IEMN8veRecompiler.h
===================================================================
--- /trunk/src/VBox/VMM/include/IEMN8veRecompiler.h	(revision 101534)
+++ /trunk/src/VBox/VMM/include/IEMN8veRecompiler.h	(revision 101535)
@@ -473,5 +473,10 @@
      * IEMNATIVEINSTR units. */
     uint32_t                    cInstrBufAlloc;
-    uint32_t                    uPadding; /* We don't keep track of this here... */
+#ifdef VBOX_STRICT
+    /** Strict: How far the last iemNativeInstrBufEnsure() checked. */
+    uint32_t                    offInstrBufChecked;
+#else
+    uint32_t                    uPadding; /* We don't keep track of the size here... */
+#endif
     /** Fixed temporary code buffer for native recompilation. */
     PIEMNATIVEINSTR             pInstrBuf;
@@ -595,8 +600,21 @@
 DECL_FORCE_INLINE(PIEMNATIVEINSTR) iemNativeInstrBufEnsure(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint32_t cInstrReq)
 {
-    if (RT_LIKELY(off + (uint64_t)cInstrReq <= pReNative->cInstrBufAlloc))
+    uint64_t const offChecked = off + (uint64_t)cInstrReq;
+    if (RT_LIKELY(offChecked <= pReNative->cInstrBufAlloc))
+    {
+#ifdef VBOX_STRICT
+        pReNative->offInstrBufChecked = offChecked;
+#endif
         return pReNative->pInstrBuf;
+    }
     return iemNativeInstrBufEnsureSlow(pReNative, off, cInstrReq);
 }
+
+/**
+ * Checks that we didn't exceed the space requested in the last
+ * iemNativeInstrBufEnsure() call. */
+#define IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(a_pReNative, a_off) \
+    AssertMsg((a_off) <= (a_pReNative)->offInstrBufChecked, \
+              ("off=%#x offInstrBufChecked=%#x\n", (a_off), (a_pReNative)->offInstrBufChecked))
 
 
@@ -636,4 +654,5 @@
 # error "port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -663,5 +682,5 @@
 # error "port me"
 #endif
-    RT_NOREF(pReNative);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -760,4 +779,5 @@
 # error "port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -791,4 +811,5 @@
 # error "port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -853,4 +874,5 @@
         pu32CodeBuf[off++] = Armv8A64MkInstrStLdRegIdx(enmOperation, iGpr, IEMNATIVE_REG_FIXED_PVMCPU, IEMNATIVE_REG_FIXED_TMP);
     }
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -873,4 +895,5 @@
     pbCodeBuf[off++] = 0x8b;
     off = iemNativeEmitGprByVCpuDisp(pbCodeBuf,off,iGpr, offVCpu);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
 #elif RT_ARCH_ARM64
@@ -898,4 +921,5 @@
     pbCodeBuf[off++] = 0x8b;
     off = iemNativeEmitGprByVCpuDisp(pbCodeBuf, off, iGpr, offVCpu);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
 #elif RT_ARCH_ARM64
@@ -924,4 +948,5 @@
     pbCodeBuf[off++] = 0xb7;
     off = iemNativeEmitGprByVCpuDisp(pbCodeBuf, off, iGpr, offVCpu);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
 #elif RT_ARCH_ARM64
@@ -950,4 +975,5 @@
     pbCodeBuf[off++] = 0xb6;
     off = iemNativeEmitGprByVCpuDisp(pbCodeBuf, off, iGpr, offVCpu);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
 #elif RT_ARCH_ARM64
@@ -976,4 +1002,5 @@
     pbCodeBuf[off++] = 0x89;
     off = iemNativeEmitGprByVCpuDisp(pbCodeBuf,off,iGpr, offVCpu);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
 #elif RT_ARCH_ARM64
@@ -1000,4 +1027,5 @@
     pbCodeBuf[off++] = 0x89;
     off = iemNativeEmitGprByVCpuDisp(pbCodeBuf, off, iGpr, offVCpu);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
 #elif RT_ARCH_ARM64
@@ -1025,4 +1053,5 @@
     pbCodeBuf[off++] = 0x89;
     off = iemNativeEmitGprByVCpuDisp(pbCodeBuf, off, iGpr, offVCpu);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
 #elif RT_ARCH_ARM64
@@ -1049,4 +1078,5 @@
     pbCodeBuf[off++] = 0x88;
     off = iemNativeEmitGprByVCpuDisp(pbCodeBuf, off, iGpr, offVCpu);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
 #elif RT_ARCH_ARM64
@@ -1087,4 +1117,5 @@
 # error "port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1094,5 +1125,6 @@
  * Common bit of iemNativeEmitLoadGprByBp and friends.
  */
-DECL_FORCE_INLINE(uint32_t) iemNativeEmitGprByBpDisp(uint8_t *pbCodeBuf, uint32_t off, uint8_t iGprReg, int32_t offDisp)
+DECL_FORCE_INLINE(uint32_t) iemNativeEmitGprByBpDisp(uint8_t *pbCodeBuf, uint32_t off, uint8_t iGprReg, int32_t offDisp,
+                                                     PIEMRECOMPILERSTATE pReNativeAssert)
 {
     if (offDisp < 128 && offDisp >= -128)
@@ -1109,4 +1141,5 @@
         pbCodeBuf[off++] = RT_BYTE4((uint32_t)offDisp);
     }
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNativeAssert, off); RT_NOREF(pReNativeAssert);
     return off;
 }
@@ -1128,5 +1161,5 @@
         pbCodeBuf[off++] = X86_OP_REX_W | X86_OP_REX_R;
     pbCodeBuf[off++] = 0x8b;
-    return iemNativeEmitGprByBpDisp(pbCodeBuf, off, iGprDst, offDisp);
+    return iemNativeEmitGprByBpDisp(pbCodeBuf, off, iGprDst, offDisp, pReNative);
 }
 #endif
@@ -1145,5 +1178,5 @@
         pbCodeBuf[off++] = X86_OP_REX_R;
     pbCodeBuf[off++] = 0x8b;
-    return iemNativeEmitGprByBpDisp(pbCodeBuf, off, iGprDst, offDisp);
+    return iemNativeEmitGprByBpDisp(pbCodeBuf, off, iGprDst, offDisp, pReNative);
 }
 #endif
@@ -1164,5 +1197,5 @@
         pbCodeBuf[off++] = X86_OP_REX_W | X86_OP_REX_R;
     pbCodeBuf[off++] = 0x8d;
-    return iemNativeEmitGprByBpDisp(pbCodeBuf, off, iGprDst, offDisp);
+    return iemNativeEmitGprByBpDisp(pbCodeBuf, off, iGprDst, offDisp, pReNative);
 }
 #endif
@@ -1185,5 +1218,5 @@
         pbCodeBuf[off++] = X86_OP_REX_W | X86_OP_REX_R;
     pbCodeBuf[off++] = 0x89;
-    return iemNativeEmitGprByBpDisp(pbCodeBuf, off, iGprSrc, offDisp);
+    return iemNativeEmitGprByBpDisp(pbCodeBuf, off, iGprSrc, offDisp, pReNative);
 
 #elif defined(RT_ARCH_ARM64)
@@ -1212,4 +1245,5 @@
                                                        IEMNATIVE_REG_FIXED_TMP0, kArmv8A64InstrLdStExtend_Sxtw);
     }
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 
@@ -1253,4 +1287,5 @@
         pbCodeBuf[off++] = RT_BYTE3(uImm64);
         pbCodeBuf[off++] = RT_BYTE4(uImm64);
+        IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
         return off;
     }
@@ -1291,4 +1326,5 @@
         pbCodeBuf[off++] = RT_BYTE4(iSubtrahend);
     }
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1319,4 +1355,5 @@
 # error "Port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1356,4 +1393,5 @@
 # error "Port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1395,4 +1433,5 @@
 # error "Port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1465,4 +1504,5 @@
 # error "Port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1518,4 +1558,5 @@
 # error "Port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1549,4 +1590,5 @@
 # error "Port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1575,4 +1617,5 @@
 # error "Port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1591,4 +1634,5 @@
     pu32CodeBuf[off++] = Armv8A64MkInstrAddSubReg(true /*fSub*/, ARMV8_A64_REG_XZR /*iRegResult*/, iGprLeft, iGprRight,
                                                   f64Bit, true /*fSetFlags*/, cShift, enmShift);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1609,4 +1653,5 @@
     pbCodeBuf[off++] = 0x3b;
     pbCodeBuf[off++] = X86_MODRM_MAKE(X86_MOD_REG, iGprLeft & 7, iGprRight & 7);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
 #elif defined(RT_ARCH_ARM64)
@@ -1635,4 +1680,5 @@
     pbCodeBuf[off++] = 0x3b;
     pbCodeBuf[off++] = X86_MODRM_MAKE(X86_MOD_REG, iGprLeft & 7, iGprRight & 7);
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
 
 #elif defined(RT_ARCH_ARM64)
@@ -1700,4 +1746,5 @@
 # error "Port me!"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1754,4 +1801,5 @@
 # error "Port me!"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1868,4 +1916,5 @@
 # error "Port me!"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
@@ -1993,4 +2042,5 @@
 # error "port me"
 #endif
+    IEMNATIVE_ASSERT_INSTR_BUF_ENSURE(pReNative, off);
     return off;
 }
