ReactOS 0.4.15-dev-8138-g1e75ea8
map_dup_inherit.c
Go to the documentation of this file.
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
11int 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 {
26 NULL,
28 0, 0x1000, NULL );
29 if( !SetHandleInformation( file_map,
32 fprintf( stderr, "%lu: Could not make handle inheritable.\n",
34 return 100;
35 }
36 }
37
38 if( !file_map ) {
39 fprintf( stderr, "%lu: Could not create anonymous file map.\n",
41 return 1;
42 }
43
44 file_view = MapViewOfFile( file_map,
46 0,
47 0,
48 0x1000 );
49
50 if( !file_view ) {
51 fprintf( stderr, "%lu: Could not map view of file.\n",
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",
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;
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
87 &si, &pi ) ) {
88 fprintf( stderr, "%lu: Could not create child process.\n",
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",
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}
static int argc
Definition: ServiceArgs.c:12
__int64 CDECL _atoi64(const char *nptr)
Definition: atoi64.c:18
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:739
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MapViewOfFile
Definition: compat.h:745
BOOL WINAPI SetHandleInformation(IN HANDLE hObject, IN DWORD dwMask, IN DWORD dwFlags)
Definition: handle.c:78
int main()
Definition: test.c:6
#define INFINITE
Definition: serial.h:102
#define UlongToPtr(u)
Definition: config.h:106
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
#define sprintf(buf, format,...)
Definition: sprintf.c:55
static refpint_t pi[]
Definition: server.c:96
#define argv
Definition: mplay32.c:18
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define SEC_RESERVE
Definition: nt_native.h:1323
#define MEM_COMMIT
Definition: nt_native.h:1313
#define memset(x, y, z)
Definition: compat.h:39
TCHAR * cmdline
Definition: stretchblt.cpp:32
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
LPVOID NTAPI VirtualAlloc(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flAllocationType, IN DWORD flProtect)
Definition: virtmem.c:65
#define CreateProcess
Definition: winbase.h:3758
#define FILE_MAP_WRITE
Definition: winbase.h:154
#define CreateFileMapping
Definition: winbase.h:3750
#define HANDLE_FLAG_INHERIT
Definition: winbase.h:264
DWORD WINAPI GetCurrentProcessId(void)
Definition: proc.c:1158
#define WAIT_OBJECT_0
Definition: winbase.h:406