| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*============================================================= |
| 6 | ** |
| 7 | ** Source: MapViewOfFile.c |
| 8 | ** |
| 9 | ** Purpose: Positivve test the MapViewOfFile API. |
| 10 | ** Mapping a pagefile allocation into memory |
| 11 | ** |
| 12 | ** Depends: CreateFileMappingW, |
| 13 | ** UnmapViewOfFile |
| 14 | ** CloseHandle. |
| 15 | ** |
| 16 | |
| 17 | ** |
| 18 | **============================================================*/ |
| 19 | #include <palsuite.h> |
| 20 | |
| 21 | int __cdecl main(int argc, char *argv[]) |
| 22 | { |
| 23 | |
| 24 | const int MAPPINGSIZE = 2048; |
| 25 | HANDLE hFileMapping; |
| 26 | LPVOID lpMapViewAddress; |
| 27 | char *p; |
| 28 | int i; |
| 29 | |
| 30 | /* Initialize the PAL environment. |
| 31 | */ |
| 32 | if(0 != PAL_Initialize(argc, argv)) |
| 33 | { |
| 34 | return FAIL; |
| 35 | } |
| 36 | |
| 37 | hFileMapping = CreateFileMappingW(INVALID_HANDLE_VALUE, |
| 38 | NULL, |
| 39 | PAGE_READWRITE, |
| 40 | 0, |
| 41 | MAPPINGSIZE, |
| 42 | NULL); |
| 43 | |
| 44 | if (hFileMapping == NULL) { |
| 45 | Trace("ERROR:%u: CreateFileMappingW() failed\n" , GetLastError()); |
| 46 | Fail("" ); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | lpMapViewAddress = MapViewOfFile( |
| 51 | hFileMapping, |
| 52 | FILE_MAP_WRITE, /* access code */ |
| 53 | 0, /* high order offset */ |
| 54 | 0, /* low order offset */ |
| 55 | MAPPINGSIZE); /* number of bytes for map */ |
| 56 | |
| 57 | if(NULL == lpMapViewAddress) |
| 58 | { |
| 59 | Trace("ERROR:%u: MapViewOfFile() failed.\n" , |
| 60 | GetLastError()); |
| 61 | CloseHandle(hFileMapping); |
| 62 | Fail("" ); |
| 63 | } |
| 64 | |
| 65 | p = (char *)lpMapViewAddress; |
| 66 | for (i=0; i<MAPPINGSIZE; ++i) { |
| 67 | /* Confirm that the memory is zero-initialized */ |
| 68 | if (p[i] != 0) |
| 69 | { |
| 70 | Fail("MapViewOfFile() of pagefile didn't return 0-filled data " |
| 71 | "(Offset %d has value 0x%x)\n" , i, p[i]); |
| 72 | } |
| 73 | /* Confirm that it is writable */ |
| 74 | *(char *)lpMapViewAddress = 0xcc; |
| 75 | } |
| 76 | |
| 77 | /* Clean-up and Terminate the PAL. |
| 78 | */ |
| 79 | CloseHandle(hFileMapping); |
| 80 | UnmapViewOfFile(lpMapViewAddress); |
| 81 | PAL_Terminate(); |
| 82 | return PASS; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | |