static void doMemRealloc(RTTEST hTest) { RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%u reallocation, grow by 1 bytes\n", PAGE_SIZE * 2); size_t cbAlloc = RTRandS32Ex(1, _16K); void *pvBuf = NULL; RTTESTI_CHECK_RC_OK_RETV(RTMemSaferAllocZEx(&pvBuf, cbAlloc, 0)); for (uint32_t i = 0; i <= PAGE_SIZE * 2; i++) { cbAlloc += 1; RTTESTI_CHECK_RC_OK_RETV(RTMemSaferReallocZEx(cbAlloc - 1, pvBuf, cbAlloc, &pvBuf, 0)); memset(pvBuf, i & 0x7f, cbAlloc); } RTMemSaferFree(pvBuf, cbAlloc); RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "100 random reallocations\n"); uint8_t chFiller = 0x42; cbAlloc = 0; pvBuf = NULL; for (uint32_t i = 1; i <= 100; i++) { uint32_t cbNew = RTRandS32Ex(1, _16K + (i / 4) * _16K); RTTESTI_CHECK_RC_OK_RETV(RTMemSaferReallocZEx(cbAlloc, pvBuf, cbNew, &pvBuf, 0)); RTTESTI_CHECK(ASMMemIsAll8(pvBuf, RT_MIN(cbAlloc, cbNew), chFiller) == NULL); chFiller += 0x31; memset(pvBuf, chFiller, cbNew); cbAlloc = cbNew; } RTTESTI_CHECK_RC_OK_RETV(RTMemSaferReallocZEx(cbAlloc, pvBuf, 0, &pvBuf, 0)); RTTESTI_CHECK(pvBuf == NULL); }
SecretKey::SecretKey(const uint8_t *pbKey, size_t cbKey, bool fKeyBufNonPageable) { m_cRefs = 0; m_fRemoveOnSuspend = false; m_cUsers = 0; m_cbKey = cbKey; int rc = RTMemSaferAllocZEx((void **)&this->m_pbKey, cbKey, fKeyBufNonPageable ? RTMEMSAFER_F_REQUIRE_NOT_PAGABLE : 0); if (RT_SUCCESS(rc)) { memcpy(this->m_pbKey, pbKey, cbKey); /* Scramble content to make retrieving the key more difficult. */ rc = RTMemSaferScramble(this->m_pbKey, cbKey); } else throw rc; }
static void doMemSaferAllocation(RTTEST hTest) { size_t cbAlloc = RTRandS32Ex(1, _1M) * sizeof(uint8_t); void *pvBuf = NULL; int rc = RTMemSaferAllocZEx(&pvBuf, cbAlloc, 0); if (RT_SUCCESS(rc)) { /* Fill it with random bytes. */ RTRandBytes(pvBuf, cbAlloc); /* Scrambling test */ doMemSaferScramble(hTest, pvBuf, cbAlloc); RTMemSaferFree(pvBuf, cbAlloc); } else RTTestIFailed("Allocating %z bytes of secure memory failed with %Rrc\n", cbAlloc, rc); }