VirtualBox

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

Last change on this file was 108650, checked in by vboxsync, 6 weeks ago

Runtime/testcase/tstRTMemSafer.cpp: Make it work for hosts where we don't know the host page size when building, bugref:10391

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

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