VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTMemSafer.cpp@ 59692

Last change on this file since 59692 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: tstRTMemSafer.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTMemSafer* functions.
4 */
5
6/*
7 * Copyright (C) 2012-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/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/memsafer.h>
32
33#include <iprt/asm.h>
34#include <iprt/param.h>
35#include <iprt/rand.h>
36#include <iprt/string.h>
37#include <iprt/test.h>
38#ifdef VBOX
39# include <VBox/sup.h>
40#endif
41
42
43
44static void doMemSaferScramble(RTTEST hTest, void *pvBuf, size_t cbAlloc)
45{
46 /*
47 * Fill it with random bytes and make a reference copy of these.
48 */
49 RTRandBytes(pvBuf, cbAlloc);
50
51 void *pvRef = RTMemDup(pvBuf, cbAlloc);
52 RTTESTI_CHECK_RETV(pvRef);
53
54 /*
55 * Scramble the allocation and check that it no longer matches the refernece bytes.
56 */
57 int rc = RTMemSaferScramble(pvBuf, cbAlloc);
58 if (RT_SUCCESS(rc))
59 {
60 if (!memcmp(pvRef, pvBuf, cbAlloc))
61 RTTestIFailed("Memory blocks must differ (%z bytes, 0x%p vs. 0x%p)!\n",
62 cbAlloc, pvRef, pvBuf);
63 else
64 {
65 /*
66 * Check that unscrambling returns the original content.
67 */
68 rc = RTMemSaferUnscramble(pvBuf, cbAlloc);
69 if (RT_SUCCESS(rc))
70 {
71 if (memcmp(pvRef, pvBuf, cbAlloc))
72 RTTestIFailed("Memory blocks must not differ (%z bytes, 0x%p vs. 0x%p)!\n",
73 cbAlloc, pvRef, pvBuf);
74 }
75 else
76 RTTestIFailed("Unscrambling %z bytes failed with %Rrc!\n", cbAlloc, rc);
77 }
78 }
79 else
80 RTTestIFailed("Scrambling %z bytes failed with %Rrc!\n", cbAlloc, rc);
81
82 RTMemFree(pvRef);
83}
84
85
86static void doMemSaferAllocation(RTTEST hTest)
87{
88 size_t cbAlloc = RTRandS32Ex(1, _1M) * sizeof(uint8_t);
89
90 void *pvBuf = NULL;
91 int rc = RTMemSaferAllocZEx(&pvBuf, cbAlloc, 0);
92 if (RT_SUCCESS(rc))
93 {
94 /* Fill it with random bytes. */
95 RTRandBytes(pvBuf, cbAlloc);
96
97 /* Scrambling test */
98 doMemSaferScramble(hTest, pvBuf, cbAlloc);
99
100 RTMemSaferFree(pvBuf, cbAlloc);
101 }
102 else
103 RTTestIFailed("Allocating %z bytes of secure memory failed with %Rrc\n", cbAlloc, rc);
104}
105
106
107static void doMemRealloc(RTTEST hTest)
108{
109 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%u reallocation, grow by 1 bytes\n", PAGE_SIZE * 2);
110 size_t cbAlloc = RTRandS32Ex(1, _16K);
111 void *pvBuf = NULL;
112 RTTESTI_CHECK_RC_OK_RETV(RTMemSaferAllocZEx(&pvBuf, cbAlloc, 0));
113 for (uint32_t i = 0; i <= PAGE_SIZE * 2; i++)
114 {
115 cbAlloc += 1;
116 RTTESTI_CHECK_RC_OK_RETV(RTMemSaferReallocZEx(cbAlloc - 1, pvBuf, cbAlloc, &pvBuf, 0));
117 memset(pvBuf, i & 0x7f, cbAlloc);
118 }
119 RTMemSaferFree(pvBuf, cbAlloc);
120
121
122 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "100 random reallocations\n");
123 uint8_t chFiller = 0x42;
124 cbAlloc = 0;
125 pvBuf = NULL;
126 for (uint32_t i = 1; i <= 100; i++)
127 {
128 uint32_t cbNew = RTRandS32Ex(1, _16K + (i / 4) * _16K);
129 RTTESTI_CHECK_RC_OK_RETV(RTMemSaferReallocZEx(cbAlloc, pvBuf, cbNew, &pvBuf, 0));
130
131 RTTESTI_CHECK(ASMMemIsAll8(pvBuf, RT_MIN(cbAlloc, cbNew), chFiller) == NULL);
132
133 chFiller += 0x31;
134 memset(pvBuf, chFiller, cbNew);
135 cbAlloc = cbNew;
136 }
137 RTTESTI_CHECK_RC_OK_RETV(RTMemSaferReallocZEx(cbAlloc, pvBuf, 0, &pvBuf, 0));
138 RTTESTI_CHECK(pvBuf == NULL);
139}
140
141
142int main()
143{
144 RTTEST hTest;
145 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTMemSafer", &hTest);
146 if (rcExit != RTEXITCODE_SUCCESS)
147 return rcExit;
148 RTTestBanner(hTest);
149#ifdef VBOX
150 SUPR3Init(NULL);
151#endif
152
153 /*
154 * Not using sub-tests here, just printing progress.
155 */
156 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "20 random allocations\n");
157 for (uint32_t i = 0; i < 20; i++)
158 doMemSaferAllocation(hTest);
159
160 doMemRealloc(hTest);
161
162 return RTTestSummaryAndDestroy(hTest);
163}
164
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette