1 | /* $Id: tstRTMemSafer.cpp 52018 2014-07-14 19:44:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTMemSafer* functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 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 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/path.h>
|
---|
32 | #include <iprt/rand.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/stream.h>
|
---|
35 | #include <iprt/initterm.h>
|
---|
36 | #include <iprt/param.h>
|
---|
37 | #include <iprt/memsafer.h>
|
---|
38 | #include <iprt/test.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Global Variables *
|
---|
43 | *******************************************************************************/
|
---|
44 |
|
---|
45 | static void doMemSaferScramble(RTTEST hTest, void *pvBuf, size_t cbAlloc)
|
---|
46 | {
|
---|
47 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Testing scrambling (%u bytes) ...\n", cbAlloc);
|
---|
48 |
|
---|
49 | RTRandBytes(pvBuf, cbAlloc);
|
---|
50 |
|
---|
51 | void *pvRef = RTMemDup(pvBuf, cbAlloc);
|
---|
52 | if (!pvRef)
|
---|
53 | {
|
---|
54 | RTTestIFailed("No memory for reference buffer (%z bytes)\n", cbAlloc);
|
---|
55 | return;
|
---|
56 | }
|
---|
57 |
|
---|
58 | int rc = RTMemSaferScramble(pvBuf, cbAlloc);
|
---|
59 | if (RT_SUCCESS(rc))
|
---|
60 | {
|
---|
61 | if (!memcmp(pvRef, pvBuf, cbAlloc))
|
---|
62 | RTTestIFailed("Memory blocks must differ (%z bytes, 0x%p vs. 0x%p)!\n",
|
---|
63 | cbAlloc, pvRef, pvBuf);
|
---|
64 | else
|
---|
65 | {
|
---|
66 | /* Test unscrambling. */
|
---|
67 | rc = RTMemSaferUnscramble(pvBuf, cbAlloc);
|
---|
68 | if (RT_SUCCESS(rc))
|
---|
69 | {
|
---|
70 | if (memcmp(pvRef, pvBuf, cbAlloc))
|
---|
71 | RTTestIFailed("Memory blocks must not differ (%z bytes, 0x%p vs. 0x%p)!\n",
|
---|
72 | cbAlloc, pvRef, pvBuf);
|
---|
73 | }
|
---|
74 | else
|
---|
75 | RTTestIFailed("Unscrambling %z bytes failed with %Rrc!\n", cbAlloc, rc);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | else
|
---|
79 | RTTestIFailed("Scrambling %z bytes failed with %Rrc!\n", cbAlloc, rc);
|
---|
80 |
|
---|
81 | RTMemFree(pvRef);
|
---|
82 | }
|
---|
83 |
|
---|
84 | static void doMemSaferAllocation(RTTEST hTest)
|
---|
85 | {
|
---|
86 | size_t cbAlloc = RTRandS32Ex(1, _1M) * sizeof(uint8_t);
|
---|
87 |
|
---|
88 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Testing allocation of secure memory (%u bytes) ...\n", cbAlloc);
|
---|
89 | void *pvBuf = NULL;
|
---|
90 | int rc = RTMemSaferAllocZEx(&pvBuf, cbAlloc, RTMEMSAFER_ALLOC_EX_FLAGS_DEFAULT);
|
---|
91 | if (RT_SUCCESS(rc))
|
---|
92 | {
|
---|
93 | /* Try to access memory. */
|
---|
94 | RTRandBytes(pvBuf, cbAlloc);
|
---|
95 |
|
---|
96 | /* Scrambling test */
|
---|
97 | doMemSaferScramble(hTest, pvBuf, cbAlloc);
|
---|
98 |
|
---|
99 | #if 0
|
---|
100 | /* Try to access memory after the allocation, should crash. */
|
---|
101 | size_t cbAllocAligned = RT_ALIGN_Z(cbAlloc, PAGE_SIZE);
|
---|
102 | *((uint8_t *)pvBuf + cbAllocAligned) = 0xcc;
|
---|
103 | #endif
|
---|
104 | RTMemSaferFree(pvBuf, cbAlloc);
|
---|
105 | }
|
---|
106 | else
|
---|
107 | RTTestIFailed("Allocating %z bytes of secure memory failed with %Rrc\n", cbAlloc, rc);
|
---|
108 | }
|
---|
109 |
|
---|
110 | int main()
|
---|
111 | {
|
---|
112 | RTTEST hTest;
|
---|
113 | RTEXITCODE rcExit = RTTestInitAndCreate("memsafer", &hTest);
|
---|
114 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
115 | return rcExit;
|
---|
116 | RTTestBanner(hTest);
|
---|
117 |
|
---|
118 | doMemSaferAllocation(hTest);
|
---|
119 |
|
---|
120 | return RTTestSummaryAndDestroy(hTest);
|
---|
121 | }
|
---|
122 |
|
---|