summaryrefslogtreecommitdiffstats
path: root/src/VBox/ValidationKit/utils/nt/ntFlushVirtualMemory.cpp
blob: cb7394984f90bc99242896785673530e3418eda5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* $Id: ntFlushVirtualMemory.cpp $ */
/** @file
 * Memory mapped files testcase - NT.
 */

/*
 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/nt/nt.h>

#include <iprt/alloca.h>
#include <iprt/file.h>
#include <iprt/getopt.h>
#include <iprt/initterm.h>
#include <iprt/mem.h>
#include <iprt/message.h>
#include <iprt/path.h>
#include <iprt/stream.h>
#include <iprt/string.h>
#include <iprt/thread.h>
#include <iprt/err.h>
#include <iprt/x86.h>


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
/** Create page signature. */
#define MAKE_PAGE_SIGNATURE(a_iPage) ((a_iPage) | UINT32_C(0x42000000) )

/** Number history entries on the page. */
#define NUM_ROUND_HISTORY 16


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
/** How chatty we should be. */
static uint32_t g_cVerbosity = 0;


/**
 * Checks if the on-disk file matches our expectations.
 *
 * @returns IPRT status code, fully bitched.
 * @param   pszFilename         The name of the file.
 * @param   pu32BufChk          Buffer to read the file into
 * @param   pu32BufOrg          Expected file content.
 * @param   cbBuf               The buffer size.
 * @param   iRound              The update round.
 */
static int CheckFile(const char *pszFilename, uint32_t *pu32BufChk, uint32_t const *pu32BufOrg, size_t cbBuf, uint32_t iRound)
{
    /*
     * Open and read the file into memory.
     */
    HANDLE hFile;
    int rc = RTNtPathOpen(pszFilename,
                          GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
                          FILE_ATTRIBUTE_NORMAL,
                          FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                          FILE_OPEN,
                          FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_NO_INTERMEDIATE_BUFFERING,
                          OBJ_CASE_INSENSITIVE,
                          &hFile,
                          NULL);
    if (RT_SUCCESS(rc))
    {
        IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
        NTSTATUS rcNt = NtReadFile(hFile, NULL /*hEvent*/, NULL /*pfnApc*/,  NULL /*pvApcCtx*/,
                                   &Ios, pu32BufChk, (ULONG)cbBuf, NULL /*poffFile*/, NULL /*pvKey*/);
        if (NT_SUCCESS(rcNt) || Ios.Information != cbBuf)
        {
            /*
             * See if the content of the file matches our expectations.
             */
            if (memcmp(pu32BufChk, pu32BufOrg, cbBuf) == 0)
            { /* matches - likely */ }
            else
            {
                RTMsgError("Round %u: Buffer mismatch!\n", iRound);

                /* Try figure where the differences are. */
                size_t const cPages        = cbBuf / X86_PAGE_SIZE;
                size_t const cItemsPerPage = X86_PAGE_SIZE / sizeof(pu32BufOrg[0]);
                for (uint32_t iPage = 0; iPage < cPages; iPage++)
                    for (uint32_t iItem = 0; iItem < cItemsPerPage; iItem++)
                    {
                        uint32_t uValue    = pu32BufChk[iPage * cItemsPerPage + iItem];
                        uint32_t uExpected = pu32BufOrg[iPage * cItemsPerPage + iItem];
                        if (uValue != uExpected)
                            RTMsgError("Round %u: page #%u, index #%u: %#x, expected %#x\n",
                                       iRound, iPage, iItem, uValue, uExpected);
                    }

                rc = VERR_MISMATCH;
            }
        }
        else if (NT_SUCCESS(rcNt))
        {
            RTMsgError("Round %u: NtReadFile return %zu bytes intead of %zu!\n", iRound, Ios.Information, cbBuf);
            rc = VERR_READ_ERROR;
        }
        else
        {
            RTMsgError("Round %u: NtReadFile(%#x) failed: %#x (%#x)\n", iRound, cbBuf, rcNt, Ios.Status);
            rc = RTErrConvertFromNtStatus(rcNt);
        }

        /*
         * Close the file and return.
         */
        rcNt = NtClose(hFile);
        if (!NT_SUCCESS(rcNt))
        {
            RTMsgError("Round %u: NtCloseFile() failed: %#x\n", iRound, rcNt);
            rc = RTErrConvertFromNtStatus(rcNt);
        }
    }
    else
        RTMsgError("Round %u: RTNtPathOpen() failed: %Rrc\n", iRound, rc);
    return rc;
}


/**
 * Manually checks whether the buffer matches up to our expectations.
 *
 * @returns IPRT status code, fully bitched.
 * @param   pu32Buf             The buffer/mapping to check.
 * @param   cbBuf               The buffer size.
 * @param   iRound              The update round.
 * @param   cFlushesLeft        Number of flushes left in the round.
 */
static int CheckBuffer(uint32_t const *pu32Buf, size_t cbBuf, uint32_t iRound, uint32_t cFlushesLeft)
{
    size_t const cPages        = cbBuf / X86_PAGE_SIZE;
    size_t const cItemsPerPage = X86_PAGE_SIZE / sizeof(pu32Buf[0]);
    size_t const offPage       = iRound & (NUM_ROUND_HISTORY - 1);
    uint32_t const uValue      = iRound | (cFlushesLeft << 20);
//RTPrintf("debug: CheckBuffer: %p %u/%u\n", pu32Buf, iRound, cFlushesLeft);

    for (uint32_t iPage = 0; iPage < cPages; iPage++)
    {
        uint32_t uActual = pu32Buf[iPage * cItemsPerPage + offPage];
        if (uActual != uValue)
        {
            RTMsgError("Round %u/%u: page #%u: last entry is corrupted: %#x, expected %#x\n",
                       iRound, cFlushesLeft, iPage, uActual, uValue);
            return VERR_MISMATCH;
        }

        uActual = pu32Buf[iPage * cItemsPerPage + cItemsPerPage - 1];
        if (uActual != MAKE_PAGE_SIGNATURE(iPage))
        {
            RTMsgError("Round %u/%u: page #%u magic corrupted: %#x, expected %#x\n",
                       iRound, cFlushesLeft, iPage, uActual, MAKE_PAGE_SIGNATURE(iPage));
            return VERR_INVALID_MAGIC;
        }
    }

    /*
     * Check previous rounds.
     */
    for (uint32_t cRoundsAgo = 1; cRoundsAgo < NUM_ROUND_HISTORY - 1 && cRoundsAgo <= iRound; cRoundsAgo++)
    {
        uint32_t        iOldRound  = iRound - cRoundsAgo;
        size_t const    offOldPage = iOldRound & (NUM_ROUND_HISTORY - 1);
        for (uint32_t iPage = 0; iPage < cPages; iPage++)
        {
            uint32_t    uActual    = pu32Buf[iPage * cItemsPerPage + offOldPage];
            if (uActual != iOldRound)
            {
                RTMsgError("Round %u/%u: page #%u: entry from %u rounds ago is corrupted: %#x, expected %#x\n",
                           iRound, cFlushesLeft, iPage, cRoundsAgo, uActual, uValue);
                return VERR_MISMATCH;
            }
        }
    }

    return VINF_SUCCESS;
}


/**
 * Updates the buffer.
 *
 * @param   pu32Buf             The buffer/mapping to update.
 * @param   cbBuf               The buffer size.
 * @param   iRound              The update round.
 * @param   cFlushesLeft        Number of flushes left in this round.
 */
static void UpdateBuffer(uint32_t *pu32Buf, size_t cbBuf, uint32_t iRound, uint32_t cFlushesLeft)
{
    size_t const cPages        = cbBuf / X86_PAGE_SIZE;
    size_t const cItemsPerPage = X86_PAGE_SIZE / sizeof(pu32Buf[0]);
    size_t const offPage       = iRound & (NUM_ROUND_HISTORY - 1);
    uint32_t const uValue      = iRound | (cFlushesLeft << 20);
//RTPrintf("debug: UpdateBuffer: %p %u/%u\n", pu32Buf, iRound, cFlushesLeft);

    for (uint32_t iPage = 0; iPage < cPages; iPage++)
        pu32Buf[iPage * cItemsPerPage + offPage] = uValue;
}



/**
 * Modifies the file via memory mapping.
 *
 * @returns IPRT status code, fully bitched.
 * @param   pszFilename         The file we're using as a test bed.
 * @param   pu32BufOrg          The sane copy of the file that gets updated in
 *                              parallel.
 * @param   cbBuf               The size of the file and bufer.
 * @param   iRound              The current round number.
 * @param   fCheckFirst         Whether to read from the mapping the mapping
 *                              before dirtying it the first time around.
 * @param   fCheckAfterFlush    Whether to read from the mapping the mapping
 *                              before dirtying it after a flush.
 * @param   cFlushes            How many times we modify the mapping and flush
 *                              it before one final modification and unmapping.
 * @param   fLargePages         Whether to use large pages.
 */
static int MakeModifications(const char *pszFilename, uint32_t *pu32BufOrg, size_t cbBuf, uint32_t iRound,
                             bool fCheckFirst, bool fCheckAfterFlush, uint32_t cFlushes, bool fLargePages)
{

    HANDLE hFile = RTNT_INVALID_HANDLE_VALUE;
    int rc = RTNtPathOpen(pszFilename,
                          GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
                          FILE_ATTRIBUTE_NORMAL,
                          FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                          FILE_OPEN,
                          FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_NO_INTERMEDIATE_BUFFERING,
                          OBJ_CASE_INSENSITIVE,
                          &hFile,
                          NULL);
    if (RT_SUCCESS(rc))
    {
        HANDLE hSection;
        NTSTATUS rcNt = NtCreateSection(&hSection,
                                        SECTION_ALL_ACCESS,
                                        NULL, /*pObjAttrs*/
                                        NULL, /*pcbMax*/
                                        PAGE_READWRITE,
                                        SEC_COMMIT,
                                        hFile);
        NtClose(hFile);
        if (NT_SUCCESS(rcNt))
        {
            PVOID  pvMapping = NULL;
            SIZE_T cbMapping = 0;
            rcNt = NtMapViewOfSection(hSection, NtCurrentProcess(),
                                      &pvMapping,
                                      0, /* ZeroBits */
                                      0, /* CommitSize */
                                      NULL, /* SectionOffset */
                                      &cbMapping,
                                      ViewUnmap,
                                      fLargePages ? MEM_LARGE_PAGES : 0,
                                      PAGE_READWRITE);
            if (NT_SUCCESS(rcNt))
            {
                /*
                 * Make the modifications.
                 */
                if (g_cVerbosity >= 2)
                    RTPrintf("debug: pvMapping=%p LB %#x\n", pvMapping, cbBuf);

                for (uint32_t iInner = 0;; iInner++)
                {
                    if (iInner ? fCheckAfterFlush : fCheckFirst)
                    {
                        if (iInner == 0)
                            rc = CheckBuffer((uint32_t *)pvMapping, cbBuf, iRound - 1, 0);
                        else
                            rc = CheckBuffer((uint32_t *)pvMapping, cbBuf, iRound, cFlushes - iInner + 1);
                        if (RT_FAILURE(rc))
                        {
                            RTMsgError("Round %u/%u: NtUnmapViewOfSection failed: %#x\n", iRound, rcNt);
                            break;
                        }
                    }

                    UpdateBuffer((uint32_t *)pvMapping, cbBuf, iRound, cFlushes - iInner);
                    UpdateBuffer(pu32BufOrg, cbBuf, iRound, cFlushes - iInner);

                    if (iInner >= cFlushes)
                        break;

                    IO_STATUS_BLOCK Ios        = RTNT_IO_STATUS_BLOCK_INITIALIZER;
                    SIZE_T          cbBuf2     = cbBuf;
                    PVOID           pvMapping2 = pvMapping;
                    rcNt = NtFlushVirtualMemory(NtCurrentProcess(), &pvMapping2, &cbBuf2, &Ios);
                    if (!NT_SUCCESS(rcNt))
                    {
                        RTMsgError("Round %u: NtFlushVirtualMemory failed: %#x\n", iRound, rcNt);
                        rc = RTErrConvertFromNtStatus(rcNt);
                        break;
                    }
                }

                /*
                 * Cleanup.
                 */
                rcNt = NtUnmapViewOfSection(NtCurrentProcess(), pvMapping);
                if (!NT_SUCCESS(rcNt))
                {
                    RTMsgError("Round %u: NtUnmapViewOfSection failed: %#x\n", iRound, rcNt);
                    rc = RTErrConvertFromNtStatus(rcNt);
                }
            }
            else
            {
                RTMsgError("Round %u: NtMapViewOfSection failed: %#x\n", iRound, rcNt);
                rc = RTErrConvertFromNtStatus(rcNt);
            }

            rcNt = NtClose(hSection);
            if (!NT_SUCCESS(rcNt))
            {
                RTMsgError("Round %u: NtClose(hSection) failed: %#x\n", iRound, rcNt);
                rc = RTErrConvertFromNtStatus(rcNt);
            }
        }
        else
        {
            RTMsgError("Round %u: NtCreateSection failed: %#x\n", iRound, rcNt);
            rc = RTErrConvertFromNtStatus(rcNt);
        }
    }
    else
        RTMsgError("Round %u: Error opening file '%s' for memory mapping: %Rrc\n", iRound, pszFilename, rc);
    return rc;
}


int main(int argc, char **argv)
{
    /*
     * Init IPRT.
     */
    int rc = RTR3InitExe(argc, &argv, 0);
    if (RT_FAILURE(rc))
        return RTMsgInitFailure(rc);

    /*
     * Parse arguments.
     */
    const char *pszFilename = NULL;
    uint32_t    cRounds     = 4096;
    uint32_t    cPages      = 128;
    bool        fLargePages = false;

    static const RTGETOPTDEF s_aOptions[] =
    {
        { "--rounds",       'r',  RTGETOPT_REQ_UINT32 },
        { "--pages",        'p',  RTGETOPT_REQ_UINT32 },
        { "--filename",     'f',  RTGETOPT_REQ_STRING },
        { "--large-pages",  'l',  RTGETOPT_REQ_NOTHING },
        { "--quiet",        'q',  RTGETOPT_REQ_NOTHING },
        { "--verbose",      'v',  RTGETOPT_REQ_NOTHING },
    };

    RTGETOPTSTATE State;
    RTGetOptInit(&State, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
    RTGETOPTUNION ValueUnion;
    int chOpt;
    while ((chOpt = RTGetOpt(&State, &ValueUnion)) != 0)
    {
        switch (chOpt)
        {
            case 'r': cRounds = ValueUnion.u32; break;
            case 'p': cPages = ValueUnion.u32; break;
            case 'f': pszFilename = ValueUnion.psz; break;
            case 'l': fLargePages = true; break;
            case 'q': g_cVerbosity = 0; break;
            case 'v': g_cVerbosity += 1; break;
            case 'h':
                RTPrintf("usage: ntFlushVirtualMemory [-c <times>] [-p <pages>] [-l|--large-pages] [-f <filename>]\n"
                         "\n"
                         "Aims at testing memory mapped files on NT w/ NtFlushVirtualMemory / FlushViewOfFile.\n");
                return 0;

            default:
                return RTGetOptPrintError(chOpt, &ValueUnion);
        }
    }

    /*
     * Allocate buffers and initialize the original with page numbers.
     *
     * We keep a original copy that gets updated in parallel to the memory
     * mapping, allowing for simple file initialization and memcpy checking.
     *
     * The second buffer is for reading the file from disk and check.
     */
    size_t const cbBuf = cPages * X86_PAGE_SIZE;
    size_t const cItemsPerPage = X86_PAGE_SIZE / sizeof(uint32_t);
    uint32_t *pu32BufOrg = (uint32_t *)RTMemPageAllocZ(cbBuf);
    uint32_t *pu32BufChk = (uint32_t *)RTMemPageAllocZ(cbBuf);
    if (pu32BufOrg == NULL || pu32BufChk == NULL)
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to allocate two %zu sized buffers!\n", cbBuf);

    for (uint32_t iPage = 0; iPage < cPages; iPage++)
        pu32BufOrg[iPage * cItemsPerPage + cItemsPerPage - 1] = MAKE_PAGE_SIGNATURE(iPage);

    rc = CheckBuffer(pu32BufOrg, cbBuf, 0, 0);
    if (RT_FAILURE(rc))
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Internal error: CheckBuffer failed on virgin buffer: %Rrc\n", rc);

    /*
     * Open the file and write out the orignal one.
     */
    RTFILE hFile;
    if (!pszFilename)
    {
        char *pszBuf = (char *)alloca(RTPATH_MAX);
        rc = RTFileOpenTemp(&hFile, pszBuf, RTPATH_MAX, RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE);
        if (RT_FAILURE(rc))
            return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to create temporary file: %Rrc\n", rc);
        pszFilename = pszBuf;
    }
    else
    {
        rc = RTFileOpen(&hFile, pszFilename, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE);
        if (RT_FAILURE(rc))
            return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to open '%s': %Rrc\n", rc);
    }

    RTEXITCODE rcExit = RTEXITCODE_SUCCESS;

    rc = RTFileWrite(hFile, pu32BufOrg, cbBuf, NULL);
    if (RT_SUCCESS(rc))
    {
        RTFileClose(hFile);

        /*
         * Do the rounds.  We count from 1 here to make verifying the previous round simpler.
         */
        for (uint32_t iRound = 1; iRound <= cRounds; iRound++)
        {
            rc = MakeModifications(pszFilename, pu32BufOrg, cbBuf, iRound,
                                   ((iRound >> 5) & 1) == 1, ((iRound >> 5) & 3) == 3, (iRound >> 3) & 31, fLargePages);
            if (RT_SUCCESS(rc))
            {
                rc = CheckBuffer(pu32BufOrg, cbBuf, iRound, 0);
                if (RT_SUCCESS(rc))
                {
                    rc = CheckFile(pszFilename, pu32BufChk, pu32BufOrg, cbBuf, iRound);
                    if (RT_SUCCESS(rc))
                        continue;
                }
            }
            break;
        }
    }
    else
    {
        rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error writing initial %zu bytes to '%s': %Rrc\n", cbBuf, rc);
        RTFileClose(hFile);
    }
    RTFileDelete(pszFilename);
    return rcExit;
}