VirtualBox

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

Last change on this file since 104626 was 103005, checked in by vboxsync, 11 months ago

iprt/asm.h,*: Split out the ASMMem* and related stuff into a separate header, asm-mem.h, so that we can get the RT_ASM_PAGE_SIZE stuff out of the way.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/* $Id: tstRTMemSafer.cpp 103005 2024-01-23 23:55:58Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTMemSafer* functions.
4 */
5
6/*
7 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/memsafer.h>
42
43#include <iprt/asm-mem.h>
44#include <iprt/param.h>
45#include <iprt/rand.h>
46#include <iprt/string.h>
47#include <iprt/test.h>
48#if defined(VBOX) && (defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64))
49# include <VBox/sup.h>
50#endif
51
52
53
54static void doMemSaferScramble(RTTEST hTest, void *pvBuf, size_t cbAlloc)
55{
56 RT_NOREF_PV(hTest);
57
58 /*
59 * Fill it with random bytes and make a reference copy of these.
60 */
61 RTRandBytes(pvBuf, cbAlloc);
62
63 void *pvRef = RTMemDup(pvBuf, cbAlloc);
64 RTTESTI_CHECK_RETV(pvRef);
65
66 /*
67 * Scramble the allocation and check that it no longer matches the refernece bytes.
68 */
69 int rc = RTMemSaferScramble(pvBuf, cbAlloc);
70 if (RT_SUCCESS(rc))
71 {
72 if (!memcmp(pvRef, pvBuf, cbAlloc))
73 RTTestIFailed("Memory blocks must differ (%z bytes, 0x%p vs. 0x%p)!\n",
74 cbAlloc, pvRef, pvBuf);
75 else
76 {
77 /*
78 * Check that unscrambling returns the original content.
79 */
80 rc = RTMemSaferUnscramble(pvBuf, cbAlloc);
81 if (RT_SUCCESS(rc))
82 {
83 if (memcmp(pvRef, pvBuf, cbAlloc))
84 RTTestIFailed("Memory blocks must not differ (%z bytes, 0x%p vs. 0x%p)!\n",
85 cbAlloc, pvRef, pvBuf);
86 }
87 else
88 RTTestIFailed("Unscrambling %z bytes failed with %Rrc!\n", cbAlloc, rc);
89 }
90 }
91 else
92 RTTestIFailed("Scrambling %z bytes failed with %Rrc!\n", cbAlloc, rc);
93
94 RTMemFree(pvRef);
95}
96
97
98static void doMemSaferAllocation(RTTEST hTest)
99{
100 size_t cbAlloc = RTRandS32Ex(1, _1M) * sizeof(uint8_t);
101
102 void *pvBuf = NULL;
103 int rc = RTMemSaferAllocZEx(&pvBuf, cbAlloc, 0);
104 if (RT_SUCCESS(rc))
105 {
106 /* Fill it with random bytes. */
107 RTRandBytes(pvBuf, cbAlloc);
108
109 /* Scrambling test */
110 doMemSaferScramble(hTest, pvBuf, cbAlloc);
111
112 RTMemSaferFree(pvBuf, cbAlloc);
113 }
114 else
115 RTTestIFailed("Allocating %z bytes of secure memory failed with %Rrc\n", cbAlloc, rc);
116}
117
118
119static void doMemRealloc(RTTEST hTest)
120{
121 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%u reallocation, grow by 1 bytes\n", PAGE_SIZE * 2);
122 size_t cbAlloc = RTRandS32Ex(1, _16K);
123 void *pvBuf = NULL;
124 RTTESTI_CHECK_RC_OK_RETV(RTMemSaferAllocZEx(&pvBuf, cbAlloc, 0));
125 for (uint32_t i = 0; i <= PAGE_SIZE * 2; i++)
126 {
127 cbAlloc += 1;
128 RTTESTI_CHECK_RC_OK_RETV(RTMemSaferReallocZEx(cbAlloc - 1, pvBuf, cbAlloc, &pvBuf, 0));
129 memset(pvBuf, i & 0x7f, cbAlloc);
130 }
131 RTMemSaferFree(pvBuf, cbAlloc);
132
133
134 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "100 random reallocations\n");
135 uint8_t chFiller = 0x42;
136 cbAlloc = 0;
137 pvBuf = NULL;
138 for (uint32_t i = 1; i <= 100; i++)
139 {
140 uint32_t cbNew = RTRandS32Ex(1, _16K + (i / 4) * _16K);
141 RTTESTI_CHECK_RC_OK_RETV(RTMemSaferReallocZEx(cbAlloc, pvBuf, cbNew, &pvBuf, 0));
142
143 RTTESTI_CHECK(ASMMemIsAllU8(pvBuf, RT_MIN(cbAlloc, cbNew), chFiller));
144
145 chFiller += 0x31;
146 memset(pvBuf, chFiller, cbNew);
147 cbAlloc = cbNew;
148 }
149 RTTESTI_CHECK_RC_OK_RETV(RTMemSaferReallocZEx(cbAlloc, pvBuf, 0, &pvBuf, 0));
150 RTTESTI_CHECK(pvBuf == NULL);
151}
152
153
154int main()
155{
156 RTTEST hTest;
157 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTMemSafer", &hTest);
158 if (rcExit != RTEXITCODE_SUCCESS)
159 return rcExit;
160 RTTestBanner(hTest);
161#if defined(VBOX) && (defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64))
162 SUPR3Init(NULL);
163#endif
164
165 /*
166 * Not using sub-tests here, just printing progress.
167 */
168 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "20 random allocations\n");
169 for (uint32_t i = 0; i < 20; i++)
170 doMemSaferAllocation(hTest);
171
172 doMemRealloc(hTest);
173
174 return RTTestSummaryAndDestroy(hTest);
175}
176
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