1 | /* $Id: tstRTMemEf.cpp 56290 2015-06-09 14:01:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Testcase for the RTMemEf* functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <iprt/mem.h>
|
---|
31 |
|
---|
32 | #include <iprt/asm.h>
|
---|
33 | #include <iprt/initterm.h>
|
---|
34 | #include <iprt/stream.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /*******************************************************************************
|
---|
39 | * Global Variables *
|
---|
40 | *******************************************************************************/
|
---|
41 | static unsigned g_cErrors = 0;
|
---|
42 |
|
---|
43 |
|
---|
44 | static int tstMemAllocEfAccess()
|
---|
45 | {
|
---|
46 | /* Trivial alloc fence test - allocate a single word and access both
|
---|
47 | * the word after the allocated block and the word before. One of them
|
---|
48 | * will crash no matter whether the fence is at the bottom or on top. */
|
---|
49 | int32_t *p = (int32_t *)RTMemEfAllocNP(sizeof(int32_t), RTMEM_TAG);
|
---|
50 | RTPrintf("tstRTMemAllocEfAccess: allocated int32_t at %#p\n", p);
|
---|
51 | RTPrintf("tstRTMemAllocEfAccess: triggering buffer overrun...\n");
|
---|
52 | ASMProbeReadByte(p + 1);
|
---|
53 | RTPrintf("tstRTMemAllocEfAccess: triggering buffer underrun...\n");
|
---|
54 | ASMProbeReadByte((char *)p - 1);
|
---|
55 |
|
---|
56 | /* Reaching this is a severe error. */
|
---|
57 | return 0;
|
---|
58 | }
|
---|
59 |
|
---|
60 | int main()
|
---|
61 | {
|
---|
62 | RTR3InitExeNoArguments(0);
|
---|
63 | RTPrintf("tstRTMemEf: TESTING...\n");
|
---|
64 |
|
---|
65 | #define CHECK_EXPR(expr) \
|
---|
66 | do { bool const f = !!(expr); if (!f) { RTPrintf("tstRTMemEf(%d): %s!\n", __LINE__, #expr); g_cErrors++; } } while (0)
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Some simple stuff.
|
---|
70 | */
|
---|
71 | {
|
---|
72 | CHECK_EXPR(tstMemAllocEfAccess());
|
---|
73 | }
|
---|
74 |
|
---|
75 | /*
|
---|
76 | * Summary.
|
---|
77 | */
|
---|
78 | if (!g_cErrors)
|
---|
79 | RTPrintf("tstMemAutoPtr: SUCCESS\n");
|
---|
80 | else
|
---|
81 | RTPrintf("tstMemAutoPtr: FAILED - %d errors\n", g_cErrors);
|
---|
82 | return !!g_cErrors;
|
---|
83 | }
|
---|