third_party/virtualbox/src/VBox/HostDrivers/Support/posix/SUPR3HardenedMain-posix.cpp
/**
 * Allocates executable patch memory with the given constraints.
 *
 * @returns VBox status code.
 * @param   cb                  Size of the patch memory in bytes.
 * @param   pvHint              Where to try allocating nearby.
 * @param   fRipRelAddr         Flag whether the executable memory must be within
 *                              2GB before or after the hint as it will contain
 *                              instructions using RIP relative addressing
 */
Allocates executable patch memory with the given constraints.
@returns VBox status code.
@param   cb                  Size of the patch memory in bytes.
@param   pvHint              Where to try allocating nearby.
@param   fRipRelAddr         Flag whether the executable memory must be within
2GB before or after the hint as it will contain
instructions using RIP relative addressing
Allocates executable patch memory with the given constraints . @returns VBox status code . @param cb Size of the patch memory in bytes . @param pvHint Where to try allocating nearby . @param fRipRelAddr Flag whether the executable memory must be within 2GB before or after the hint as it will contain instructions using RIP relative addressing
static uint8_t *supR3HardenedMainPosixExecMemAlloc(size_t cb, void *pvHint, bool fRipRelAddr)
{
    AssertReturn(cb < _1K, NULL);
    if (!g_pbExecMemory)
    {
        g_pbExecMemory = (uint8_t *)mmap(pvHint, DLOPEN_PATCH_MEMORY_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
                                         MAP_SHARED | MAP_ANONYMOUS, -1, 0);
        g_offExecMemory = 0;
        if (g_pbExecMemory == MAP_FAILED)
            return NULL;
        memset(g_pbExecMemory, 0xcc, DLOPEN_PATCH_MEMORY_SIZE);
    }
    if (g_offExecMemory + cb >= DLOPEN_PATCH_MEMORY_SIZE)
        return NULL;
    uint8_t *pb = &g_pbExecMemory[g_offExecMemory];
    if (fRipRelAddr)
    {
        uintptr_t uPtrHint     = (uintptr_t)pvHint;
        uintptr_t uPtrPatchMem = (uintptr_t)pb;
        uintptr_t cbDistance   = uPtrHint < uPtrPatchMem
                               ? uPtrPatchMem - uPtrHint
                               : uPtrHint - uPtrPatchMem;
        if (cbDistance >= _2G - _4K)
            return NULL;
    }
    g_offExecMemory = RT_ALIGN_32(g_offExecMemory + cb, 16);
    return pb;
}
static uint8_t * supR3HardenedMainPosixExecMemAlloc ( size_t cb , void * pvHint , bool fRipRelAddr ) { AssertReturn ( cb < _1K , NULL ) ; if ( ! g_pbExecMemory ) { g_pbExecMemory = ( uint8_t * ) mmap ( pvHint , DLOPEN_PATCH_MEMORY_SIZE , PROT_READ | PROT_WRITE | PROT_EXEC , MAP_SHARED | MAP_ANONYMOUS , -1 , 0 ) ; g_offExecMemory = 0 ; if ( g_pbExecMemory == MAP_FAILED ) return NULL ; memset ( g_pbExecMemory , 0xcc , DLOPEN_PATCH_MEMORY_SIZE ) ; } if ( g_offExecMemory + cb >= DLOPEN_PATCH_MEMORY_SIZE ) return NULL ; uint8_t * pb = & g_pbExecMemory [ g_offExecMemory ] ; if ( fRipRelAddr ) { uintptr_t uPtrHint = ( uintptr_t ) pvHint ; uintptr_t uPtrPatchMem = ( uintptr_t ) pb ; uintptr_t cbDistance = uPtrHint < uPtrPatchMem ? uPtrPatchMem - uPtrHint : uPtrHint - uPtrPatchMem ; if ( cbDistance >= _2G - _4K ) return NULL ; } g_offExecMemory = RT_ALIGN_32 ( g_offExecMemory + cb , 16 ) ; return pb ; }
Allocates executable patch memory with the given constraints.
Allocates executable patch memory with the given constraints .
/* Lazy allocation of exectuable memory. */ /* Check that we allocated within 2GB of the hint. */