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