tstRTMemSafer.cpp revision 9236bebed5c5902b30061ea6378bba30ef0577cb
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync/* $Id$ */
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync/** @file
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * IPRT Testcase - RTMemSafer* functions.
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync */
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync/*
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * Copyright (C) 2012 Oracle Corporation
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync *
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * available from http://www.virtualbox.org. This file is free software;
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * you can redistribute it and/or modify it under the terms of the GNU
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * General Public License (GPL) as published by the Free Software
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync *
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * The contents of this file may alternatively be used under the terms
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * of the Common Development and Distribution License Version 1.0
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * VirtualBox OSE distribution, in which case the provisions of the
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * CDDL are applicable instead of those of the GPL.
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync *
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * You may elect to license modified versions of this file under the
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync * terms and conditions of either the GPL or the CDDL or both.
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync */
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync/*******************************************************************************
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync* Header Files *
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync*******************************************************************************/
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync#include <iprt/path.h>
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync#include <iprt/rand.h>
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync#include <iprt/string.h>
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync#include <iprt/stream.h>
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync#include <iprt/initterm.h>
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync#include <iprt/param.h>
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync#include <iprt/memsafer.h>
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync#include <iprt/test.h>
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync/*******************************************************************************
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync* Global Variables *
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync*******************************************************************************/
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsyncstatic void doMemSaferScramble(RTTEST hTest, void *pvBuf, size_t cbAlloc)
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync{
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Testing scrambling (%u bytes) ...\n", cbAlloc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTRandBytes(pvBuf, cbAlloc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync void *pvRef = RTMemDup(pvBuf, cbAlloc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync if (!pvRef)
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync {
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTTestIFailed("No memory for reference buffer (%z bytes)\n", cbAlloc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync return;
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync }
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync int rc = RTMemSaferScramble(pvBuf, cbAlloc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync if (RT_SUCCESS(rc))
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync {
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync if (!memcmp(pvRef, pvBuf, cbAlloc))
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTTestIFailed("Memory blocks must differ (%z bytes, 0x%p vs. 0x%p)!\n",
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync cbAlloc, pvRef, pvBuf);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync else
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync {
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync /* Test unscrambling. */
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync rc = RTMemSaferUnscramble(pvBuf, cbAlloc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync if (RT_SUCCESS(rc))
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync {
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync if (memcmp(pvRef, pvBuf, cbAlloc))
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTTestIFailed("Memory blocks must not differ (%z bytes, 0x%p vs. 0x%p)!\n",
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync cbAlloc, pvRef, pvBuf);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync }
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync else
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTTestIFailed("Unscrambling %z bytes failed with %Rrc!\n", cbAlloc, rc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync }
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync }
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync else
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTTestIFailed("Scrambling %z bytes failed with %Rrc!\n", cbAlloc, rc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTMemFree(pvRef);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync}
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsyncstatic void doMemSaferAllocation(RTTEST hTest)
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync{
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync size_t cbAlloc = RTRandS32Ex(1, _1M) * sizeof(uint8_t);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Testing allocation of secure memory (%u bytes) ...\n", cbAlloc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync void *pvBuf = NULL;
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync int rc = RTMemSaferAllocZEx(&pvBuf, cbAlloc, RTMEMSAFER_ALLOC_EX_FLAGS_DEFAULT);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync if (RT_SUCCESS(rc))
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync {
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync /* Try to access memory. */
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTRandBytes(pvBuf, cbAlloc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync /* Scrambling test */
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync doMemSaferScramble(hTest, pvBuf, cbAlloc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync#if 0
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync /* Try to access memory after the allocation, should crash. */
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync size_t cbAllocAligned = RT_ALIGN_Z(cbAlloc, PAGE_SIZE);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync *((uint8_t *)pvBuf + cbAllocAligned) = 0xcc;
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync#endif
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTMemSaferFree(pvBuf, cbAlloc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync }
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync else
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTTestIFailed("Allocating %z bytes of secure memory failed with %Rrc\n", cbAlloc, rc);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync}
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsyncint main()
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync{
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTTEST hTest;
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTEXITCODE rcExit = RTTestInitAndCreate("memsafer", &hTest);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync if (rcExit != RTEXITCODE_SUCCESS)
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync return rcExit;
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync RTTestBanner(hTest);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync doMemSaferAllocation(hTest);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync return RTTestSummaryAndDestroy(hTest);
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync}
9236bebed5c5902b30061ea6378bba30ef0577cbvboxsync