2004-10-21 04:59:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* This tests the ability of the target win32 to create an anonymous file
|
|
|
|
* mapping, create a mapping view with MapViewOfFile, and then realize the
|
|
|
|
* pages with VirtualAlloc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int main( int argc, char **argv ) {
|
|
|
|
HANDLE file_view;
|
|
|
|
void *file_map;
|
|
|
|
int *x;
|
|
|
|
|
|
|
|
fprintf( stderr, "%lu: Starting\n", GetCurrentProcessId() );
|
|
|
|
|
|
|
|
if( argc == 2 ) {
|
2005-11-15 02:25:28 +00:00
|
|
|
#ifdef WIN64
|
2010-06-03 20:08:26 +00:00
|
|
|
file_map = (void *)_atoi64(argv[1]);
|
2005-11-15 02:25:28 +00:00
|
|
|
#else
|
|
|
|
file_map = (void *)UlongToPtr(atoi(argv[1]));
|
|
|
|
#endif
|
2004-10-21 04:59:01 +00:00
|
|
|
} else {
|
2005-05-07 21:24:31 +00:00
|
|
|
file_map = CreateFileMapping( INVALID_HANDLE_VALUE,
|
|
|
|
NULL,
|
|
|
|
PAGE_READWRITE | SEC_RESERVE,
|
2004-10-21 04:59:01 +00:00
|
|
|
0, 0x1000, NULL );
|
2005-05-07 21:24:31 +00:00
|
|
|
if( !SetHandleInformation( file_map,
|
|
|
|
HANDLE_FLAG_INHERIT,
|
2004-10-21 04:59:01 +00:00
|
|
|
HANDLE_FLAG_INHERIT ) ) {
|
|
|
|
fprintf( stderr, "%lu: Could not make handle inheritable.\n",
|
|
|
|
GetCurrentProcessId() );
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
}
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:59:01 +00:00
|
|
|
if( !file_map ) {
|
|
|
|
fprintf( stderr, "%lu: Could not create anonymous file map.\n",
|
|
|
|
GetCurrentProcessId() );
|
|
|
|
return 1;
|
|
|
|
}
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:59:01 +00:00
|
|
|
file_view = MapViewOfFile( file_map,
|
|
|
|
FILE_MAP_WRITE,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0x1000 );
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:59:01 +00:00
|
|
|
if( !file_view ) {
|
|
|
|
fprintf( stderr, "%lu: Could not map view of file.\n",
|
|
|
|
GetCurrentProcessId() );
|
2005-11-15 02:25:28 +00:00
|
|
|
if (file_map != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(file_map);
|
2004-10-21 04:59:01 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:59:01 +00:00
|
|
|
if( !VirtualAlloc( file_view, 0x1000, MEM_COMMIT, PAGE_READWRITE ) ) {
|
|
|
|
fprintf( stderr, "%lu: VirtualAlloc failed to realize the page.\n",
|
|
|
|
GetCurrentProcessId() );
|
2005-11-15 02:25:28 +00:00
|
|
|
if (file_map != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(file_map);
|
2004-10-21 04:59:01 +00:00
|
|
|
return 3;
|
|
|
|
}
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:59:01 +00:00
|
|
|
x = (int *)file_view;
|
|
|
|
x[0] = 0x12345678;
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:59:01 +00:00
|
|
|
if( x[0] != 0x12345678 ) {
|
|
|
|
fprintf( stderr, "%lu: Can't write to the memory (%08x != 0x12345678)\n",
|
|
|
|
GetCurrentProcessId(), x[0] );
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( argc == 1 ) {
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
char cmdline[1000];
|
|
|
|
|
|
|
|
memset( &si, 0, sizeof( si ) );
|
|
|
|
memset( &pi, 0, sizeof( pi ) );
|
|
|
|
|
2005-11-15 02:25:28 +00:00
|
|
|
sprintf(cmdline,"%s %p", argv[0], file_map);
|
|
|
|
CloseHandle(file_map);
|
|
|
|
|
2004-10-21 04:59:01 +00:00
|
|
|
if( !CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL,
|
|
|
|
&si, &pi ) ) {
|
|
|
|
fprintf( stderr, "%lu: Could not create child process.\n",
|
|
|
|
GetCurrentProcessId() );
|
2005-11-15 02:25:28 +00:00
|
|
|
if (pi.hProcess != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
|
2004-10-21 04:59:01 +00:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( WaitForSingleObject( pi.hThread, INFINITE ) != WAIT_OBJECT_0 ) {
|
|
|
|
fprintf( stderr, "%lu: Failed to wait for child process to terminate.\n",
|
|
|
|
GetCurrentProcessId() );
|
2005-11-15 02:25:28 +00:00
|
|
|
if (pi.hProcess != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(pi.hProcess);
|
2004-10-21 04:59:01 +00:00
|
|
|
return 6;
|
|
|
|
}
|
2007-10-19 23:01:40 +00:00
|
|
|
|
2005-11-15 02:25:28 +00:00
|
|
|
if (pi.hProcess != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
|
2004-10-21 04:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|