VirtualBox

Changeset 97866 in vbox for trunk/src/VBox/Runtime/common


Ignore:
Timestamp:
Dec 26, 2022 2:14:08 AM (2 years ago)
Author:
vboxsync
Message:

IPRT/nocrt: Added an exception handler to rtVccEh4DoLocalUnwind and implemented rtVccValidateExceptionContextRecord. bugref:10261 ticketref:21303

Location:
trunk/src/VBox/Runtime/common/compiler/vcc
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/compiler/vcc/except-x86-vcc-asm.asm

    r97862 r97866  
    8686BEGINCODE
    8787extern IMPNAME(RtlUnwind@16)
    88 
    89 
    90 
     88extern _rtVccEh4DoLocalUnwindHandler@16
     89
     90
     91;*********************************************************************************************************************************
     92;*  Global Variables                                                                                                             *
     93;*********************************************************************************************************************************
     94
     95;; Delcare rtVccEh4DoLocalUnwindHandler() in except-x86.cpp as a save exception handler.
     96; This adds the symbol table number of the exception handler to the special .sxdata section.
     97safeseh _rtVccEh4DoLocalUnwindHandler@16
     98
     99
     100BEGINCODE
    91101;;
    92102; Calls the filter sub-function for a __finally statement.
  • trunk/src/VBox/Runtime/common/compiler/vcc/except-x86-vcc.cpp

    r97863 r97866  
    3939*   Header Files                                                                                                                 *
    4040*********************************************************************************************************************************/
    41 #include <iprt/win/windows.h>
    42 
     41#include <iprt/nt/nt-and-windows.h>
     42
     43#include "internal/compiler-vcc.h"
    4344#include "except-vcc.h"
     45
     46
     47/*********************************************************************************************************************************
     48*   Structures and Typedefs                                                                                                      *
     49*********************************************************************************************************************************/
     50/**
     51 * Extended exception registration record used by rtVccEh4DoLocalUnwind
     52 * and rtVccEh4DoLocalUnwindHandler.
     53 */
     54typedef struct EH4_LOCAL_UNWIND_XCPT_REG
     55{
     56    /** Security cookie. */
     57    uintptr_t                       uEHCookieFront;
     58    /** The actual registration record.   */
     59    EXCEPTION_REGISTRATION_RECORD   XcptRegRec;
     60    /** @name rtVccEh4DoLocalUnwind parameters
     61     * @{ */
     62    PEH4_XCPT_REG_REC_T             pEh4XcptRegRec;
     63    uint32_t                        uTargetTryLevel;
     64    uint8_t const                  *pbFrame;
     65    /** @} */
     66    /** Security cookie. */
     67    uintptr_t                       uEHCookieBack;
     68} EH4_LOCAL_UNWIND_XCPT_REG;
     69
    4470
    4571
     
    5783DECLASM(void)                   rtVccEh4DoGlobalUnwind(PEXCEPTION_RECORD pXcptRec, PEXCEPTION_REGISTRATION_RECORD pXcptRegRec);
    5884DECLASM(void)                   rtVccEh4DoFinally(PFN_EH4_FINALLY_T pfnFinally, bool fAbend, uint8_t const *pbFrame);
    59 
     85extern "C" EXCEPTION_DISPOSITION __stdcall
     86rtVccEh4DoLocalUnwindHandler(PEXCEPTION_RECORD pXcptRec, PVOID pvEstFrame, PCONTEXT pCpuCtx, PVOID pvDispCtx);
     87
     88
     89#ifdef _MSC_VER
     90# pragma warning(push)
     91# pragma warning(disable:4733)  /* warning C4733: Inline asm assigning to 'FS:0': handler not registered as safe handler */
     92#endif
    6093
    6194/**
     
    72105     * Manually set up exception handler.
    73106     */
    74     /** @todo    */
     107    EH4_LOCAL_UNWIND_XCPT_REG RegRec =
     108    {
     109        __security_cookie ^ (uintptr_t)&RegRec,
     110        {
     111            (EXCEPTION_REGISTRATION_RECORD *)__readfsdword(RT_UOFFSETOF(NT_TIB, ExceptionList)),
     112            rtVccEh4DoLocalUnwindHandler /* safeseh (.sxdata) entry emitted by except-x86-vcc-asm.asm */
     113        },
     114        pEh4XcptRegRec,
     115        uTargetTryLevel,
     116        pbFrame,
     117        __security_cookie ^ (uintptr_t)&RegRec
     118    };
     119    __writefsdword(RT_UOFFSETOF(NT_TIB, ExceptionList), (uintptr_t)&RegRec.XcptRegRec);
    75120
    76121    /*
     
    101146     * Deregister exception handler.
    102147     */
    103     /** @todo */
    104 }
    105 
    106 
     148    __writefsdword(RT_UOFFSETOF(NT_TIB, ExceptionList), (uintptr_t)RegRec.XcptRegRec.Next);
     149}
     150
     151#ifdef _MSC_VER
     152# pragma warning(pop)
     153#endif
     154
     155/**
     156 * Exception handler for rtVccEh4DoLocalUnwind.
     157 */
     158EXCEPTION_DISPOSITION __stdcall
     159rtVccEh4DoLocalUnwindHandler(PEXCEPTION_RECORD pXcptRec, PVOID pvEstFrame, PCONTEXT pCpuCtx, PVOID pvDispCtx)
     160{
     161    EH4_LOCAL_UNWIND_XCPT_REG *pMyRegRec = RT_FROM_MEMBER(pvEstFrame, EH4_LOCAL_UNWIND_XCPT_REG, XcptRegRec);
     162    __security_check_cookie(pMyRegRec->uEHCookieFront ^ (uintptr_t)pMyRegRec);
     163    __security_check_cookie(pMyRegRec->uEHCookieBack  ^ (uintptr_t)pMyRegRec);
     164
     165    /*
     166     * This is a little sketchy as it isn't all that well documented by the OS
     167     * vendor, but if invoked while unwinding, we return ExceptionCollidedUnwind
     168     * and update the *ppDispCtx value to point to the colliding one.
     169     */
     170    if (pXcptRec->ExceptionFlags & (EXCEPTION_UNWINDING | EXCEPTION_EXIT_UNWIND))
     171    {
     172        rtVccEh4DoLocalUnwind(pMyRegRec->pEh4XcptRegRec, pMyRegRec->uTargetTryLevel, pMyRegRec->pbFrame);
     173
     174        PEXCEPTION_REGISTRATION_RECORD *ppDispCtx = (PEXCEPTION_REGISTRATION_RECORD *)pvDispCtx;
     175        *ppDispCtx = &pMyRegRec->XcptRegRec;
     176        return ExceptionCollidedUnwind;
     177    }
     178
     179    /*
     180     * In all other cases we do nothing special.
     181     */
     182    RT_NOREF(pCpuCtx);
     183    return ExceptionContinueSearch;
     184}
     185
     186
     187/**
     188 * This validates the CPU context, may terminate the application if invalid.
     189 */
    107190DECLINLINE(void) rtVccValidateExceptionContextRecord(PCONTEXT pCpuCtx)
    108191{
    109     RT_NOREF(pCpuCtx);
    110     /** @todo  Implement __exception_validate_context_record .*/
     192    if (RT_LIKELY(   !rtVccIsGuardICallChecksActive()
     193                  || rtVccIsPointerOnTheStack(pCpuCtx->Esp)))
     194    { /* likely */ }
     195    else
     196        rtVccCheckContextFailed(pCpuCtx);
    111197}
    112198
  • trunk/src/VBox/Runtime/common/compiler/vcc/loadcfg-vcc.c

    r96407 r97866  
    5656extern uint8_t      __safe_se_handler_count;    /**< Absolute "address" defined by the linker representing the table size. */
    5757#endif
    58 extern uintptr_t    __guard_check_icall_fptr;   /**< nocrt-guard-win.asm */
    5958#ifdef RT_ARCH_AMD64
    6059extern uintptr_t    __guard_dispatch_icall_fptr;/**< nocrt-guard-win.asm */
  • trunk/src/VBox/Runtime/common/compiler/vcc/stacksup-vcc.cpp

    r96580 r97866  
    329329}
    330330
     331
     332void rtVccCheckContextFailed(PCONTEXT pCpuCtx)
     333{
     334#ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
     335    RTAssertMsg2("\n\n!!Context (stack) check failed!!\n\n"
     336                 "PC=%p SP=%p BP=%p\n",
     337# ifdef RT_ARCH_AMD64
     338                 pCpuCtx->Rip, pCpuCtx->Rsp, pCpuCtx->Rbp
     339# elif defined(RT_ARCH_X86)
     340                 pCpuCtx->Eip, pCpuCtx->Esp, pCpuCtx->Ebp
     341# else
     342#  error "unsupported arch"
     343# endif
     344                 );
     345#else
     346    rtNoCrtFatalWriteBegin(RT_STR_TUPLE("\r\n\r\n!!Context (stack) check failed!!\r\n\r\n"
     347                                       "PC="));
     348# ifdef RT_ARCH_AMD64
     349    rtNoCrtFatalWritePtr((void *)pCpuCtx->Rip);
     350# elif defined(RT_ARCH_X86)
     351    rtNoCrtFatalWritePtr((void *)pCpuCtx->Eip);
     352# else
     353#  error "unsupported arch"
     354# endif
     355    rtNoCrtFatalWrite(RT_STR_TUPLE(" SP="));
     356# ifdef RT_ARCH_AMD64
     357    rtNoCrtFatalWritePtr((void *)pCpuCtx->Rsp);
     358# elif defined(RT_ARCH_X86)
     359    rtNoCrtFatalWritePtr((void *)pCpuCtx->Esp);
     360# endif
     361    rtNoCrtFatalWrite(RT_STR_TUPLE(" BP="));
     362# ifdef RT_ARCH_AMD64
     363    rtNoCrtFatalWritePtr((void *)pCpuCtx->Rbp);
     364# elif defined(RT_ARCH_X86)
     365    rtNoCrtFatalWritePtr((void *)pCpuCtx->Ebp);
     366# endif
     367    rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
     368#endif
     369    rtVccFatalSecurityErrorWithCtx(FAST_FAIL_INVALID_SET_OF_CONTEXT, pCpuCtx);
     370}
     371
Note: See TracChangeset for help on using the changeset viewer.

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