VirtualBox

Ignore:
Timestamp:
Mar 1, 2024 12:42:36 AM (14 months ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
161981
Message:

VMM/IEM: Obfuscate most variable indexes we pass around in strict builds so we easily catch register/variable index mixups. bugref:10371

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/VMM/include/IEMN8veRecompiler.h

    r103614 r103622  
    794794    /** The argument number if argument, UINT8_MAX if regular variable. */
    795795    uint8_t             uArgNo;
    796     /** If referenced, the index of the variable referencing this one, otherwise
    797      *  UINT8_MAX.  A referenced variable must only be placed on the stack and
    798      * must be either kIemNativeVarKind_Stack or kIemNativeVarKind_Immediate. */
     796    /** If referenced, the index (unpacked) of the variable referencing this one,
     797     * otherwise UINT8_MAX.  A referenced variable must only be placed on the stack
     798     * and must be either kIemNativeVarKind_Stack or kIemNativeVarKind_Immediate. */
    799799    uint8_t             idxReferrerVar;
    800800    /** Guest register being shadowed here, kIemNativeGstReg_End(/UINT8_MAX) if not.
     
    809809        /** kIemNativeVarKind_Immediate: The immediate value. */
    810810        uint64_t            uValue;
    811         /** kIemNativeVarKind_VarRef: The index of the variable being referenced. */
     811        /** kIemNativeVarKind_VarRef: The index (unpacked) of the variable being referenced. */
    812812        uint8_t             idxRefVar;
    813813        /** kIemNativeVarKind_GstRegRef: The guest register being referrenced. */
     
    821821    } u;
    822822} IEMNATIVEVAR;
     823/** Pointer to a variable or argument. */
     824typedef IEMNATIVEVAR *PIEMNATIVEVAR;
     825/** Pointer to a const variable or argument. */
     826typedef IEMNATIVEVAR const *PCIEMNATIVEVAR;
    823827
    824828/** What is being kept in a host register. */
     
    878882    /** What is being kept in this register. */
    879883    IEMNATIVEWHAT   enmWhat;
    880     /** Variable index if holding a variable, otherwise UINT8_MAX. */
     884    /** Variable index (packed) if holding a variable, otherwise UINT8_MAX. */
    881885    uint8_t         idxVar;
    882886    /** Stack slot assigned by iemNativeVarSaveVolatileRegsPreHlpCall and freed
     
    906910    union
    907911    {
    908         /** Index of variable arguments, UINT8_MAX if not valid. */
     912        /** Index of variable (unpacked) arguments, UINT8_MAX if not valid. */
    909913        uint8_t                 aidxArgVars[8];
    910914        /** For more efficient resetting. */
     
    933937/** Pointer to const core state. */
    934938typedef IEMNATIVECORESTATE const *PCIEMNATIVECORESTATE;
     939
     940/** @def IEMNATIVE_VAR_IDX_UNPACK
     941 * @returns Index into IEMNATIVECORESTATE::aVars.
     942 * @param   a_idxVar    Variable index w/ magic (in strict builds).
     943 */
     944/** @def IEMNATIVE_VAR_IDX_PACK
     945 * @returns Variable index w/ magic (in strict builds).
     946 * @param   a_idxVar    Index into IEMNATIVECORESTATE::aVars.
     947 */
     948#ifdef VBOX_STRICT
     949# define IEMNATIVE_VAR_IDX_UNPACK(a_idxVar) ((a_idxVar) & IEMNATIVE_VAR_IDX_MASK)
     950# define IEMNATIVE_VAR_IDX_PACK(a_idxVar)   ((a_idxVar) | IEMNATIVE_VAR_IDX_MAGIC)
     951# define IEMNATIVE_VAR_IDX_MAGIC            UINT8_C(0xd0)
     952# define IEMNATIVE_VAR_IDX_MAGIC_MASK       UINT8_C(0xf0)
     953# define IEMNATIVE_VAR_IDX_MASK             UINT8_C(0x0f)
     954#else
     955# define IEMNATIVE_VAR_IDX_UNPACK(a_idxVar) (a_idxVar)
     956# define IEMNATIVE_VAR_IDX_PACK(a_idxVar)   (a_idxVar)
     957#endif
    935958
    936959
     
    11811204                                                                            IEMNATIVEGSTREG enmGstReg);
    11821205
    1183 DECL_HIDDEN_THROW(uint8_t)  iemNativeRegAllocVar(PIEMRECOMPILERSTATE pReNative, uint32_t *poff, uint8_t idxVar);
    11841206DECL_HIDDEN_THROW(uint32_t) iemNativeRegAllocArgs(PIEMRECOMPILERSTATE pReNative, uint32_t off, uint8_t cArgs);
    11851207DECL_HIDDEN_THROW(uint8_t)  iemNativeRegAssignRc(PIEMRECOMPILERSTATE pReNative, uint8_t idxHstReg);
     
    12601282 * Checks that a variable index is valid.
    12611283 */
    1262 #define IEMNATIVE_ASSERT_VAR_IDX(a_pReNative, a_idxVar) \
     1284#ifdef IEMNATIVE_VAR_IDX_MAGIC
     1285# define IEMNATIVE_ASSERT_VAR_IDX(a_pReNative, a_idxVar) \
     1286    AssertMsg(   ((a_idxVar) & IEMNATIVE_VAR_IDX_MAGIC_MASK) == IEMNATIVE_VAR_IDX_MAGIC \
     1287              && (unsigned)IEMNATIVE_VAR_IDX_UNPACK(a_idxVar) < RT_ELEMENTS((a_pReNative)->Core.aVars) \
     1288              && ((a_pReNative)->Core.bmVars & RT_BIT_32(IEMNATIVE_VAR_IDX_UNPACK(a_idxVar))), \
     1289              ("%s=%#x\n", #a_idxVar, a_idxVar))
     1290#else
     1291# define IEMNATIVE_ASSERT_VAR_IDX(a_pReNative, a_idxVar) \
    12631292    AssertMsg(   (unsigned)(a_idxVar) < RT_ELEMENTS((a_pReNative)->Core.aVars) \
    12641293              && ((a_pReNative)->Core.bmVars & RT_BIT_32(a_idxVar)), ("%s=%d\n", #a_idxVar, a_idxVar))
     1294#endif
    12651295
    12661296/**
     
    12691299 * This also adds a RT_NOREF of a_idxVar.
    12701300 */
    1271 #define IEMNATIVE_ASSERT_ARG_VAR_IDX(a_pReNative, a_idxVar, a_uArgNo) do { \
     1301#ifdef IEMNATIVE_VAR_IDX_MAGIC
     1302# define IEMNATIVE_ASSERT_ARG_VAR_IDX(a_pReNative, a_idxVar, a_uArgNo) do { \
     1303        RT_NOREF_PV(a_idxVar); \
     1304        AssertMsg(   ((a_idxVar) & IEMNATIVE_VAR_IDX_MAGIC_MASK) == IEMNATIVE_VAR_IDX_MAGIC \
     1305                  && (unsigned)IEMNATIVE_VAR_IDX_UNPACK(a_idxVar) < RT_ELEMENTS((a_pReNative)->Core.aVars) \
     1306                  && ((a_pReNative)->Core.bmVars & RT_BIT_32(IEMNATIVE_VAR_IDX_UNPACK(a_idxVar))) \
     1307                  && (a_pReNative)->Core.aVars[IEMNATIVE_VAR_IDX_UNPACK(a_idxVar)].uArgNo == (a_uArgNo), \
     1308                  ("%s=%d; uArgNo=%d, expected %u\n", #a_idxVar, a_idxVar, \
     1309                   (a_pReNative)->Core.aVars[RT_MIN(IEMNATIVE_VAR_IDX_UNPACK(a_idxVar), \
     1310                                                    RT_ELEMENTS((a_pReNative)->Core.aVars)) - 1].uArgNo, \
     1311                   a_uArgNo)); \
     1312    } while (0)
     1313#else
     1314# define IEMNATIVE_ASSERT_ARG_VAR_IDX(a_pReNative, a_idxVar, a_uArgNo) do { \
    12721315        RT_NOREF_PV(a_idxVar); \
    12731316        AssertMsg(   (unsigned)(a_idxVar) < RT_ELEMENTS((a_pReNative)->Core.aVars) \
     
    12751318                  && (a_pReNative)->Core.aVars[a_idxVar].uArgNo == (a_uArgNo) \
    12761319                  , ("%s=%d; uArgNo=%d, expected %u\n", #a_idxVar, a_idxVar, \
    1277                      (a_pReNative)->Core.aVars[RT_MAX(a_idxVar, RT_ELEMENTS((a_pReNative)->Core.aVars)) - 1].uArgNo, a_uArgNo)); \
     1320                     (a_pReNative)->Core.aVars[RT_MIN(a_idxVar, RT_ELEMENTS((a_pReNative)->Core.aVars)) - 1].uArgNo, a_uArgNo)); \
    12781321    } while (0)
     1322#endif
     1323
     1324
     1325/**
     1326 * Checks that a variable has the expected size.
     1327 */
     1328#define IEMNATIVE_ASSERT_VAR_SIZE(a_pReNative, a_idxVar, a_cbVar) \
     1329    AssertMsg((a_pReNative)->Core.aVars[IEMNATIVE_VAR_IDX_UNPACK(a_idxVar)].cbVar == (a_cbVar), \
     1330              ("%s=%#x: cbVar=%#x, expected %#x!\n", #a_idxVar, a_idxVar, \
     1331              (a_pReNative)->Core.aVars[IEMNATIVE_VAR_IDX_UNPACK(a_idxVar)].cbVar == (a_cbVar)))
     1332
    12791333
    12801334/**
     
    12991353{
    13001354    IEMNATIVE_ASSERT_VAR_IDX(pReNative, idxVar);
    1301     Assert(pReNative->Core.aVars[idxVar].fRegAcquired);
    1302     pReNative->Core.aVars[idxVar].fRegAcquired = false;
     1355    Assert(pReNative->Core.aVars[IEMNATIVE_VAR_IDX_UNPACK(idxVar)].fRegAcquired);
     1356    pReNative->Core.aVars[IEMNATIVE_VAR_IDX_UNPACK(idxVar)].fRegAcquired = false;
    13031357}
    13041358
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette