VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/nt/ntFlushVirtualMemory.cpp

Last change on this file was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.5 KB
Line 
1/* $Id: ntFlushVirtualMemory.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * Memory mapped files testcase - NT.
4 */
5
6/*
7 * Copyright (C) 2007-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/nt/nt.h>
42
43#include <iprt/alloca.h>
44#include <iprt/file.h>
45#include <iprt/getopt.h>
46#include <iprt/initterm.h>
47#include <iprt/mem.h>
48#include <iprt/message.h>
49#include <iprt/path.h>
50#include <iprt/stream.h>
51#include <iprt/string.h>
52#include <iprt/thread.h>
53#include <iprt/err.h>
54#include <iprt/x86.h>
55
56
57/*********************************************************************************************************************************
58* Defined Constants And Macros *
59*********************************************************************************************************************************/
60/** Create page signature. */
61#define MAKE_PAGE_SIGNATURE(a_iPage) ((a_iPage) | UINT32_C(0x42000000) )
62
63/** Number history entries on the page. */
64#define NUM_ROUND_HISTORY 16
65
66
67/*********************************************************************************************************************************
68* Global Variables *
69*********************************************************************************************************************************/
70/** How chatty we should be. */
71static uint32_t g_cVerbosity = 0;
72
73
74/**
75 * Checks if the on-disk file matches our expectations.
76 *
77 * @returns IPRT status code, fully bitched.
78 * @param pszFilename The name of the file.
79 * @param pu32BufChk Buffer to read the file into
80 * @param pu32BufOrg Expected file content.
81 * @param cbBuf The buffer size.
82 * @param iRound The update round.
83 */
84static int CheckFile(const char *pszFilename, uint32_t *pu32BufChk, uint32_t const *pu32BufOrg, size_t cbBuf, uint32_t iRound)
85{
86 /*
87 * Open and read the file into memory.
88 */
89 HANDLE hFile;
90 int rc = RTNtPathOpen(pszFilename,
91 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
92 FILE_ATTRIBUTE_NORMAL,
93 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
94 FILE_OPEN,
95 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_NO_INTERMEDIATE_BUFFERING,
96 OBJ_CASE_INSENSITIVE,
97 &hFile,
98 NULL);
99 if (RT_SUCCESS(rc))
100 {
101 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
102 NTSTATUS rcNt = NtReadFile(hFile, NULL /*hEvent*/, NULL /*pfnApc*/, NULL /*pvApcCtx*/,
103 &Ios, pu32BufChk, (ULONG)cbBuf, NULL /*poffFile*/, NULL /*pvKey*/);
104 if (NT_SUCCESS(rcNt) || Ios.Information != cbBuf)
105 {
106 /*
107 * See if the content of the file matches our expectations.
108 */
109 if (memcmp(pu32BufChk, pu32BufOrg, cbBuf) == 0)
110 { /* matches - likely */ }
111 else
112 {
113 RTMsgError("Round %u: Buffer mismatch!\n", iRound);
114
115 /* Try figure where the differences are. */
116 size_t const cPages = cbBuf / X86_PAGE_SIZE;
117 size_t const cItemsPerPage = X86_PAGE_SIZE / sizeof(pu32BufOrg[0]);
118 for (uint32_t iPage = 0; iPage < cPages; iPage++)
119 for (uint32_t iItem = 0; iItem < cItemsPerPage; iItem++)
120 {
121 uint32_t uValue = pu32BufChk[iPage * cItemsPerPage + iItem];
122 uint32_t uExpected = pu32BufOrg[iPage * cItemsPerPage + iItem];
123 if (uValue != uExpected)
124 RTMsgError("Round %u: page #%u, index #%u: %#x, expected %#x\n",
125 iRound, iPage, iItem, uValue, uExpected);
126 }
127
128 rc = VERR_MISMATCH;
129 }
130 }
131 else if (NT_SUCCESS(rcNt))
132 {
133 RTMsgError("Round %u: NtReadFile return %zu bytes intead of %zu!\n", iRound, Ios.Information, cbBuf);
134 rc = VERR_READ_ERROR;
135 }
136 else
137 {
138 RTMsgError("Round %u: NtReadFile(%#x) failed: %#x (%#x)\n", iRound, cbBuf, rcNt, Ios.Status);
139 rc = RTErrConvertFromNtStatus(rcNt);
140 }
141
142 /*
143 * Close the file and return.
144 */
145 rcNt = NtClose(hFile);
146 if (!NT_SUCCESS(rcNt))
147 {
148 RTMsgError("Round %u: NtCloseFile() failed: %#x\n", iRound, rcNt);
149 rc = RTErrConvertFromNtStatus(rcNt);
150 }
151 }
152 else
153 RTMsgError("Round %u: RTNtPathOpen() failed: %Rrc\n", iRound, rc);
154 return rc;
155}
156
157
158/**
159 * Manually checks whether the buffer matches up to our expectations.
160 *
161 * @returns IPRT status code, fully bitched.
162 * @param pu32Buf The buffer/mapping to check.
163 * @param cbBuf The buffer size.
164 * @param iRound The update round.
165 * @param cFlushesLeft Number of flushes left in the round.
166 */
167static int CheckBuffer(uint32_t const *pu32Buf, size_t cbBuf, uint32_t iRound, uint32_t cFlushesLeft)
168{
169 size_t const cPages = cbBuf / X86_PAGE_SIZE;
170 size_t const cItemsPerPage = X86_PAGE_SIZE / sizeof(pu32Buf[0]);
171 size_t const offPage = iRound & (NUM_ROUND_HISTORY - 1);
172 uint32_t const uValue = iRound | (cFlushesLeft << 20);
173//RTPrintf("debug: CheckBuffer: %p %u/%u\n", pu32Buf, iRound, cFlushesLeft);
174
175 for (uint32_t iPage = 0; iPage < cPages; iPage++)
176 {
177 uint32_t uActual = pu32Buf[iPage * cItemsPerPage + offPage];
178 if (uActual != uValue)
179 {
180 RTMsgError("Round %u/%u: page #%u: last entry is corrupted: %#x, expected %#x\n",
181 iRound, cFlushesLeft, iPage, uActual, uValue);
182 return VERR_MISMATCH;
183 }
184
185 uActual = pu32Buf[iPage * cItemsPerPage + cItemsPerPage - 1];
186 if (uActual != MAKE_PAGE_SIGNATURE(iPage))
187 {
188 RTMsgError("Round %u/%u: page #%u magic corrupted: %#x, expected %#x\n",
189 iRound, cFlushesLeft, iPage, uActual, MAKE_PAGE_SIGNATURE(iPage));
190 return VERR_INVALID_MAGIC;
191 }
192 }
193
194 /*
195 * Check previous rounds.
196 */
197 for (uint32_t cRoundsAgo = 1; cRoundsAgo < NUM_ROUND_HISTORY - 1 && cRoundsAgo <= iRound; cRoundsAgo++)
198 {
199 uint32_t iOldRound = iRound - cRoundsAgo;
200 size_t const offOldPage = iOldRound & (NUM_ROUND_HISTORY - 1);
201 for (uint32_t iPage = 0; iPage < cPages; iPage++)
202 {
203 uint32_t uActual = pu32Buf[iPage * cItemsPerPage + offOldPage];
204 if (uActual != iOldRound)
205 {
206 RTMsgError("Round %u/%u: page #%u: entry from %u rounds ago is corrupted: %#x, expected %#x\n",
207 iRound, cFlushesLeft, iPage, cRoundsAgo, uActual, uValue);
208 return VERR_MISMATCH;
209 }
210 }
211 }
212
213 return VINF_SUCCESS;
214}
215
216
217/**
218 * Updates the buffer.
219 *
220 * @param pu32Buf The buffer/mapping to update.
221 * @param cbBuf The buffer size.
222 * @param iRound The update round.
223 * @param cFlushesLeft Number of flushes left in this round.
224 */
225static void UpdateBuffer(uint32_t *pu32Buf, size_t cbBuf, uint32_t iRound, uint32_t cFlushesLeft)
226{
227 size_t const cPages = cbBuf / X86_PAGE_SIZE;
228 size_t const cItemsPerPage = X86_PAGE_SIZE / sizeof(pu32Buf[0]);
229 size_t const offPage = iRound & (NUM_ROUND_HISTORY - 1);
230 uint32_t const uValue = iRound | (cFlushesLeft << 20);
231//RTPrintf("debug: UpdateBuffer: %p %u/%u\n", pu32Buf, iRound, cFlushesLeft);
232
233 for (uint32_t iPage = 0; iPage < cPages; iPage++)
234 pu32Buf[iPage * cItemsPerPage + offPage] = uValue;
235}
236
237
238
239/**
240 * Modifies the file via memory mapping.
241 *
242 * @returns IPRT status code, fully bitched.
243 * @param pszFilename The file we're using as a test bed.
244 * @param pu32BufOrg The sane copy of the file that gets updated in
245 * parallel.
246 * @param cbBuf The size of the file and bufer.
247 * @param iRound The current round number.
248 * @param fCheckFirst Whether to read from the mapping the mapping
249 * before dirtying it the first time around.
250 * @param fCheckAfterFlush Whether to read from the mapping the mapping
251 * before dirtying it after a flush.
252 * @param cFlushes How many times we modify the mapping and flush
253 * it before one final modification and unmapping.
254 * @param fLargePages Whether to use large pages.
255 */
256static int MakeModifications(const char *pszFilename, uint32_t *pu32BufOrg, size_t cbBuf, uint32_t iRound,
257 bool fCheckFirst, bool fCheckAfterFlush, uint32_t cFlushes, bool fLargePages)
258{
259
260 HANDLE hFile = RTNT_INVALID_HANDLE_VALUE;
261 int rc = RTNtPathOpen(pszFilename,
262 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
263 FILE_ATTRIBUTE_NORMAL,
264 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
265 FILE_OPEN,
266 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_NO_INTERMEDIATE_BUFFERING,
267 OBJ_CASE_INSENSITIVE,
268 &hFile,
269 NULL);
270 if (RT_SUCCESS(rc))
271 {
272 HANDLE hSection;
273 NTSTATUS rcNt = NtCreateSection(&hSection,
274 SECTION_ALL_ACCESS,
275 NULL, /*pObjAttrs*/
276 NULL, /*pcbMax*/
277 PAGE_READWRITE,
278 SEC_COMMIT,
279 hFile);
280 NtClose(hFile);
281 if (NT_SUCCESS(rcNt))
282 {
283 PVOID pvMapping = NULL;
284 SIZE_T cbMapping = 0;
285 rcNt = NtMapViewOfSection(hSection, NtCurrentProcess(),
286 &pvMapping,
287 0, /* ZeroBits */
288 0, /* CommitSize */
289 NULL, /* SectionOffset */
290 &cbMapping,
291 ViewUnmap,
292 fLargePages ? MEM_LARGE_PAGES : 0,
293 PAGE_READWRITE);
294 if (NT_SUCCESS(rcNt))
295 {
296 /*
297 * Make the modifications.
298 */
299 if (g_cVerbosity >= 2)
300 RTPrintf("debug: pvMapping=%p LB %#x\n", pvMapping, cbBuf);
301
302 for (uint32_t iInner = 0;; iInner++)
303 {
304 if (iInner ? fCheckAfterFlush : fCheckFirst)
305 {
306 if (iInner == 0)
307 rc = CheckBuffer((uint32_t *)pvMapping, cbBuf, iRound - 1, 0);
308 else
309 rc = CheckBuffer((uint32_t *)pvMapping, cbBuf, iRound, cFlushes - iInner + 1);
310 if (RT_FAILURE(rc))
311 {
312 RTMsgError("Round %u/%u: NtUnmapViewOfSection failed: %#x\n", iRound, rcNt);
313 break;
314 }
315 }
316
317 UpdateBuffer((uint32_t *)pvMapping, cbBuf, iRound, cFlushes - iInner);
318 UpdateBuffer(pu32BufOrg, cbBuf, iRound, cFlushes - iInner);
319
320 if (iInner >= cFlushes)
321 break;
322
323 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
324 SIZE_T cbBuf2 = cbBuf;
325 PVOID pvMapping2 = pvMapping;
326 rcNt = NtFlushVirtualMemory(NtCurrentProcess(), &pvMapping2, &cbBuf2, &Ios);
327 if (!NT_SUCCESS(rcNt))
328 {
329 RTMsgError("Round %u: NtFlushVirtualMemory failed: %#x\n", iRound, rcNt);
330 rc = RTErrConvertFromNtStatus(rcNt);
331 break;
332 }
333 }
334
335 /*
336 * Cleanup.
337 */
338 rcNt = NtUnmapViewOfSection(NtCurrentProcess(), pvMapping);
339 if (!NT_SUCCESS(rcNt))
340 {
341 RTMsgError("Round %u: NtUnmapViewOfSection failed: %#x\n", iRound, rcNt);
342 rc = RTErrConvertFromNtStatus(rcNt);
343 }
344 }
345 else
346 {
347 RTMsgError("Round %u: NtMapViewOfSection failed: %#x\n", iRound, rcNt);
348 rc = RTErrConvertFromNtStatus(rcNt);
349 }
350
351 rcNt = NtClose(hSection);
352 if (!NT_SUCCESS(rcNt))
353 {
354 RTMsgError("Round %u: NtClose(hSection) failed: %#x\n", iRound, rcNt);
355 rc = RTErrConvertFromNtStatus(rcNt);
356 }
357 }
358 else
359 {
360 RTMsgError("Round %u: NtCreateSection failed: %#x\n", iRound, rcNt);
361 rc = RTErrConvertFromNtStatus(rcNt);
362 }
363 }
364 else
365 RTMsgError("Round %u: Error opening file '%s' for memory mapping: %Rrc\n", iRound, pszFilename, rc);
366 return rc;
367}
368
369
370int main(int argc, char **argv)
371{
372 /*
373 * Init IPRT.
374 */
375 int rc = RTR3InitExe(argc, &argv, 0);
376 if (RT_FAILURE(rc))
377 return RTMsgInitFailure(rc);
378
379 /*
380 * Parse arguments.
381 */
382 const char *pszFilename = NULL;
383 uint32_t cRounds = 4096;
384 uint32_t cPages = 128;
385 bool fLargePages = false;
386
387 static const RTGETOPTDEF s_aOptions[] =
388 {
389 { "--rounds", 'r', RTGETOPT_REQ_UINT32 },
390 { "--pages", 'p', RTGETOPT_REQ_UINT32 },
391 { "--filename", 'f', RTGETOPT_REQ_STRING },
392 { "--large-pages", 'l', RTGETOPT_REQ_NOTHING },
393 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
394 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
395 };
396
397 RTGETOPTSTATE State;
398 RTGetOptInit(&State, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
399 RTGETOPTUNION ValueUnion;
400 int chOpt;
401 while ((chOpt = RTGetOpt(&State, &ValueUnion)) != 0)
402 {
403 switch (chOpt)
404 {
405 case 'r': cRounds = ValueUnion.u32; break;
406 case 'p': cPages = ValueUnion.u32; break;
407 case 'f': pszFilename = ValueUnion.psz; break;
408 case 'l': fLargePages = true; break;
409 case 'q': g_cVerbosity = 0; break;
410 case 'v': g_cVerbosity += 1; break;
411 case 'h':
412 RTPrintf("usage: ntFlushVirtualMemory [-c <times>] [-p <pages>] [-l|--large-pages] [-f <filename>]\n"
413 "\n"
414 "Aims at testing memory mapped files on NT w/ NtFlushVirtualMemory / FlushViewOfFile.\n");
415 return 0;
416
417 default:
418 return RTGetOptPrintError(chOpt, &ValueUnion);
419 }
420 }
421
422 /*
423 * Allocate buffers and initialize the original with page numbers.
424 *
425 * We keep a original copy that gets updated in parallel to the memory
426 * mapping, allowing for simple file initialization and memcpy checking.
427 *
428 * The second buffer is for reading the file from disk and check.
429 */
430 size_t const cbBuf = cPages * X86_PAGE_SIZE;
431 size_t const cItemsPerPage = X86_PAGE_SIZE / sizeof(uint32_t);
432 uint32_t *pu32BufOrg = (uint32_t *)RTMemPageAllocZ(cbBuf);
433 uint32_t *pu32BufChk = (uint32_t *)RTMemPageAllocZ(cbBuf);
434 if (pu32BufOrg == NULL || pu32BufChk == NULL)
435 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to allocate two %zu sized buffers!\n", cbBuf);
436
437 for (uint32_t iPage = 0; iPage < cPages; iPage++)
438 pu32BufOrg[iPage * cItemsPerPage + cItemsPerPage - 1] = MAKE_PAGE_SIGNATURE(iPage);
439
440 rc = CheckBuffer(pu32BufOrg, cbBuf, 0, 0);
441 if (RT_FAILURE(rc))
442 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Internal error: CheckBuffer failed on virgin buffer: %Rrc\n", rc);
443
444 /*
445 * Open the file and write out the orignal one.
446 */
447 RTFILE hFile;
448 if (!pszFilename)
449 {
450 char *pszBuf = (char *)alloca(RTPATH_MAX);
451 rc = RTFileOpenTemp(&hFile, pszBuf, RTPATH_MAX, RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE);
452 if (RT_FAILURE(rc))
453 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to create temporary file: %Rrc\n", rc);
454 pszFilename = pszBuf;
455 }
456 else
457 {
458 rc = RTFileOpen(&hFile, pszFilename, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE);
459 if (RT_FAILURE(rc))
460 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to open '%s': %Rrc\n", rc);
461 }
462
463 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
464
465 rc = RTFileWrite(hFile, pu32BufOrg, cbBuf, NULL);
466 if (RT_SUCCESS(rc))
467 {
468 RTFileClose(hFile);
469
470 /*
471 * Do the rounds. We count from 1 here to make verifying the previous round simpler.
472 */
473 for (uint32_t iRound = 1; iRound <= cRounds; iRound++)
474 {
475 rc = MakeModifications(pszFilename, pu32BufOrg, cbBuf, iRound,
476 ((iRound >> 5) & 1) == 1, ((iRound >> 5) & 3) == 3, (iRound >> 3) & 31, fLargePages);
477 if (RT_SUCCESS(rc))
478 {
479 rc = CheckBuffer(pu32BufOrg, cbBuf, iRound, 0);
480 if (RT_SUCCESS(rc))
481 {
482 rc = CheckFile(pszFilename, pu32BufChk, pu32BufOrg, cbBuf, iRound);
483 if (RT_SUCCESS(rc))
484 continue;
485 }
486 }
487 break;
488 }
489 }
490 else
491 {
492 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error writing initial %zu bytes to '%s': %Rrc\n", cbBuf, rc);
493 RTFileClose(hFile);
494 }
495 RTFileDelete(pszFilename);
496 return rcExit;
497}
498
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