93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync/* $Id$ */
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync/** @file
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * IPRT - Testcase for the RTMemEf* functions.
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync */
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync/*
c7814cf6e1240a519cbec0441e033d0e2470ed00vboxsync * Copyright (C) 2010-2012 Oracle Corporation
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync *
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * available from http://www.virtualbox.org. This file is free software;
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * you can redistribute it and/or modify it under the terms of the GNU
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * General Public License (GPL) as published by the Free Software
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync *
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * The contents of this file may alternatively be used under the terms
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * of the Common Development and Distribution License Version 1.0
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * VirtualBox OSE distribution, in which case the provisions of the
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * CDDL are applicable instead of those of the GPL.
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync *
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * You may elect to license modified versions of this file under the
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * terms and conditions of either the GPL or the CDDL or both.
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync */
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync/*******************************************************************************
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync* Header Files *
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync*******************************************************************************/
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync#include <iprt/mem.h>
b1fbae26d6a78376edb37e05a7874f3901ee304dvboxsync
b1fbae26d6a78376edb37e05a7874f3901ee304dvboxsync#include <iprt/asm.h>
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync#include <iprt/initterm.h>
b1fbae26d6a78376edb37e05a7874f3901ee304dvboxsync#include <iprt/stream.h>
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync#include <iprt/string.h>
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync/*******************************************************************************
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync* Global Variables *
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync*******************************************************************************/
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsyncstatic unsigned g_cErrors = 0;
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsyncstatic int tstMemAllocEfAccess()
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync{
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync /* Trivial alloc fence test - allocate a single word and access both
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * the word after the allocated block and the word before. One of them
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * will crash no matter whether the fence is at the bottom or on top. */
9d577a7b721cf6430727d2bc2577642310642366vboxsync int32_t *p = (int32_t *)RTMemEfAllocNP(sizeof(int32_t), RTMEM_TAG);
e5f7384e62bc82ad4610e1dd35fe4ca59016814evboxsync RTPrintf("tstRTMemAllocEfAccess: allocated int32_t at %#p\n", p);
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync RTPrintf("tstRTMemAllocEfAccess: triggering buffer overrun...\n");
b1fbae26d6a78376edb37e05a7874f3901ee304dvboxsync ASMProbeReadByte(p + 1);
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync RTPrintf("tstRTMemAllocEfAccess: triggering buffer underrun...\n");
b1fbae26d6a78376edb37e05a7874f3901ee304dvboxsync ASMProbeReadByte((char *)p - 1);
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync /* Reaching this is a severe error. */
e5f7384e62bc82ad4610e1dd35fe4ca59016814evboxsync return 0;
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync}
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsyncint main()
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync{
230bd8589bba39933ac5ec21482d6186d675e604vboxsync RTR3InitExeNoArguments(0);
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync RTPrintf("tstRTMemEf: TESTING...\n");
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync#define CHECK_EXPR(expr) \
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync do { bool const f = !!(expr); if (!f) { RTPrintf("tstRTMemEf(%d): %s!\n", __LINE__, #expr); g_cErrors++; } } while (0)
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync /*
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * Some simple stuff.
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync */
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync {
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync CHECK_EXPR(tstMemAllocEfAccess());
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync }
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync /*
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync * Summary.
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync */
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync if (!g_cErrors)
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync RTPrintf("tstMemAutoPtr: SUCCESS\n");
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync else
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync RTPrintf("tstMemAutoPtr: FAILED - %d errors\n", g_cErrors);
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync return !!g_cErrors;
93916c9acad52e266a71eac82da1da3c2d23d4dbvboxsync}