1 #include <stdio.h> 2 #include <windows.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 /* This tests the ability of the target win32 to create an anonymous file 7 * mapping, create a mapping view with MapViewOfFile, and then realize the 8 * pages with VirtualAlloc. 9 */ 10 11 int main( int argc, char **argv ) { 12 HANDLE file_view; 13 void *file_map; 14 int *x; 15 16 fprintf( stderr, "%lu: Starting\n", GetCurrentProcessId() ); 17 18 if( argc == 2 ) { 19 #ifdef WIN64 20 file_map = (void *)_atoi64(argv[1]); 21 #else 22 file_map = (void *)UlongToPtr(atoi(argv[1])); 23 #endif 24 } else { 25 file_map = CreateFileMapping( INVALID_HANDLE_VALUE, 26 NULL, 27 PAGE_READWRITE | SEC_RESERVE, 28 0, 0x1000, NULL ); 29 if( !SetHandleInformation( file_map, 30 HANDLE_FLAG_INHERIT, 31 HANDLE_FLAG_INHERIT ) ) { 32 fprintf( stderr, "%lu: Could not make handle inheritable.\n", 33 GetCurrentProcessId() ); 34 return 100; 35 } 36 } 37 38 if( !file_map ) { 39 fprintf( stderr, "%lu: Could not create anonymous file map.\n", 40 GetCurrentProcessId() ); 41 return 1; 42 } 43 44 file_view = MapViewOfFile( file_map, 45 FILE_MAP_WRITE, 46 0, 47 0, 48 0x1000 ); 49 50 if( !file_view ) { 51 fprintf( stderr, "%lu: Could not map view of file.\n", 52 GetCurrentProcessId() ); 53 if (file_map != INVALID_HANDLE_VALUE) 54 CloseHandle(file_map); 55 return 2; 56 } 57 58 if( !VirtualAlloc( file_view, 0x1000, MEM_COMMIT, PAGE_READWRITE ) ) { 59 fprintf( stderr, "%lu: VirtualAlloc failed to realize the page.\n", 60 GetCurrentProcessId() ); 61 if (file_map != INVALID_HANDLE_VALUE) 62 CloseHandle(file_map); 63 return 3; 64 } 65 66 x = (int *)file_view; 67 x[0] = 0x12345678; 68 69 if( x[0] != 0x12345678 ) { 70 fprintf( stderr, "%lu: Can't write to the memory (%08x != 0x12345678)\n", 71 GetCurrentProcessId(), x[0] ); 72 return 4; 73 } 74 75 if( argc == 1 ) { 76 STARTUPINFO si; 77 PROCESS_INFORMATION pi; 78 char cmdline[1000]; 79 80 memset( &si, 0, sizeof( si ) ); 81 memset( &pi, 0, sizeof( pi ) ); 82 83 sprintf(cmdline,"%s %p", argv[0], file_map); 84 CloseHandle(file_map); 85 86 if( !CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, 87 &si, &pi ) ) { 88 fprintf( stderr, "%lu: Could not create child process.\n", 89 GetCurrentProcessId() ); 90 if (pi.hProcess != INVALID_HANDLE_VALUE) 91 CloseHandle(pi.hProcess); 92 93 return 5; 94 } 95 96 if( WaitForSingleObject( pi.hThread, INFINITE ) != WAIT_OBJECT_0 ) { 97 fprintf( stderr, "%lu: Failed to wait for child process to terminate.\n", 98 GetCurrentProcessId() ); 99 if (pi.hProcess != INVALID_HANDLE_VALUE) 100 CloseHandle(pi.hProcess); 101 return 6; 102 } 103 104 if (pi.hProcess != INVALID_HANDLE_VALUE) 105 CloseHandle(pi.hProcess); 106 107 } 108 109 return 0; 110 } 111