ReactOS  0.4.13-dev-242-g611e6d7
filemap.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Win32 Base API
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: dll/win32/kernel32/client/file/filemap.c
5  * PURPOSE: Handles virtual memory APIs
6  * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #include <k32.h>
12 
13 #define NDEBUG
14 #include <debug.h>
15 
16 /* FUNCTIONS ******************************************************************/
17 
18 /*
19  * @implemented
20  */
21 HANDLE
22 NTAPI
24  IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
25  IN DWORD flProtect,
26  IN DWORD dwMaximumSizeHigh,
27  IN DWORD dwMaximumSizeLow,
29 {
30  /* Call the W(ide) function */
32  lpName,
33  hFile,
34  lpFileMappingAttributes,
35  flProtect,
36  dwMaximumSizeHigh,
37  dwMaximumSizeLow);
38 }
39 
40 /*
41  * @implemented
42  */
43 HANDLE
44 NTAPI
46  LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
47  DWORD flProtect,
48  DWORD dwMaximumSizeHigh,
49  DWORD dwMaximumSizeLow,
51 {
53  HANDLE SectionHandle;
54  OBJECT_ATTRIBUTES LocalAttributes;
56  UNICODE_STRING SectionName;
59  PLARGE_INTEGER SectionSize = NULL;
61 
62  /* Set default access */
64 
65  /* Get the attributes for the actual allocation and cleanup flProtect */
67  flProtect ^= Attributes;
68 
69  /* If the caller didn't say anything, assume SEC_COMMIT */
71 
72  /* Now check if the caller wanted write access */
73  if (flProtect == PAGE_READWRITE)
74  {
75  /* Give it */
77  }
78  else if (flProtect == PAGE_EXECUTE_READWRITE)
79  {
80  /* Give it */
82  }
83  else if (flProtect == PAGE_EXECUTE_READ)
84  {
85  /* Give it */
87  }
88  else if ((flProtect == PAGE_EXECUTE_WRITECOPY) &&
89  (NtCurrentPeb()->OSMajorVersion >= 6))
90  {
91  /* Give it */
93  }
94  else if ((flProtect != PAGE_READONLY) && (flProtect != PAGE_WRITECOPY))
95  {
97  return NULL;
98  }
99 
100  /* Now check if we got a name */
101  if (lpName) RtlInitUnicodeString(&SectionName, lpName);
102 
103  /* Now convert the object attributes */
104  ObjectAttributes = BaseFormatObjectAttributes(&LocalAttributes,
105  lpFileMappingAttributes,
106  lpName ? &SectionName : NULL);
107 
108  /* Check if we got a size */
109  if (dwMaximumSizeLow || dwMaximumSizeHigh)
110  {
111  /* Use a LARGE_INTEGER and convert */
112  SectionSize = &LocalSize;
113  SectionSize->LowPart = dwMaximumSizeLow;
114  SectionSize->HighPart = dwMaximumSizeHigh;
115  }
116 
117  /* Make sure the handle is valid */
119  {
120  /* It's not, we'll only go on if we have a size */
121  hFile = NULL;
122  if (!SectionSize)
123  {
124  /* No size, so this isn't a valid non-mapped section */
126  return NULL;
127  }
128  }
129 
130  /* Now create the actual section */
131  Status = NtCreateSection(&SectionHandle,
134  SectionSize,
135  flProtect,
136  Attributes,
137  hFile);
138  if (!NT_SUCCESS(Status))
139  {
140  /* We failed */
142  return NULL;
143  }
144  else if (Status == STATUS_OBJECT_NAME_EXISTS)
145  {
147  }
148  else
149  {
151  }
152 
153  /* Return the section */
154  return SectionHandle;
155 }
156 
157 /*
158  * @implemented
159  */
160 LPVOID
161 NTAPI
162 MapViewOfFileEx(HANDLE hFileMappingObject,
163  DWORD dwDesiredAccess,
164  DWORD dwFileOffsetHigh,
165  DWORD dwFileOffsetLow,
166  SIZE_T dwNumberOfBytesToMap,
167  LPVOID lpBaseAddress)
168 {
172  ULONG Protect;
173  LPVOID ViewBase;
174 
175  /* Convert the offset */
176  SectionOffset.LowPart = dwFileOffsetLow;
177  SectionOffset.HighPart = dwFileOffsetHigh;
178 
179  /* Save the size and base */
180  ViewBase = lpBaseAddress;
181  ViewSize = dwNumberOfBytesToMap;
182 
183  /* Convert flags to NT Protection Attributes */
184  if (dwDesiredAccess == FILE_MAP_COPY)
185  {
187  }
188  else if (dwDesiredAccess & FILE_MAP_WRITE)
189  {
190  Protect = (dwDesiredAccess & FILE_MAP_EXECUTE) ?
192  }
193  else if (dwDesiredAccess & FILE_MAP_READ)
194  {
195  Protect = (dwDesiredAccess & FILE_MAP_EXECUTE) ?
197  }
198  else
199  {
201  }
202 
203  /* Map the section */
204  Status = NtMapViewOfSection(hFileMappingObject,
206  &ViewBase,
207  0,
208  0,
209  &SectionOffset,
210  &ViewSize,
211  ViewShare,
212  0,
213  Protect);
214  if (!NT_SUCCESS(Status))
215  {
216  /* We failed */
218  return NULL;
219  }
220 
221  /* Return the base */
222  return ViewBase;
223 }
224 
225 /*
226  * @implemented
227  */
228 LPVOID
229 NTAPI
230 MapViewOfFile(HANDLE hFileMappingObject,
231  DWORD dwDesiredAccess,
232  DWORD dwFileOffsetHigh,
233  DWORD dwFileOffsetLow,
234  SIZE_T dwNumberOfBytesToMap)
235 {
236  /* Call the extended API */
237  return MapViewOfFileEx(hFileMappingObject,
238  dwDesiredAccess,
239  dwFileOffsetHigh,
240  dwFileOffsetLow,
241  dwNumberOfBytesToMap,
242  NULL);
243 }
244 
245 /*
246  * @implemented
247  */
248 BOOL
249 NTAPI
250 UnmapViewOfFile(LPCVOID lpBaseAddress)
251 {
253 
254  /* Unmap the section */
255  Status = NtUnmapViewOfSection(NtCurrentProcess(), (PVOID)lpBaseAddress);
256  if (!NT_SUCCESS(Status))
257  {
258  /* Check if the pages were protected */
260  {
261  /* Flush the region if it was a "secure memory cache" */
262  if (RtlFlushSecureMemoryCache((PVOID)lpBaseAddress, 0))
263  {
264  /* Now try to unmap again */
265  Status = NtUnmapViewOfSection(NtCurrentProcess(), (PVOID)lpBaseAddress);
266  if (NT_SUCCESS(Status)) return TRUE;
267  }
268  }
269 
270  /* We failed */
272  return FALSE;
273  }
274 
275  /* Otherwise, return success */
276  return TRUE;
277 }
278 
279 /*
280  * @implemented
281  */
282 HANDLE
283 NTAPI
284 OpenFileMappingA(IN DWORD dwDesiredAccess,
286  IN LPCSTR lpName)
287 {
288  ConvertOpenWin32AnsiObjectApiToUnicodeApi(FileMapping, dwDesiredAccess, bInheritHandle, lpName);
289 }
290 
291 /*
292  * @implemented
293  */
294  /* FIXME: Convert to the new macros */
295 HANDLE
296 NTAPI
297 OpenFileMappingW(IN DWORD dwDesiredAccess,
299  IN LPCWSTR lpName)
300 {
302  HANDLE SectionHandle;
305 
306  /* We need a name */
307  if (!lpName)
308  {
309  /* Otherwise, fail */
311  return NULL;
312  }
313 
314  /* Convert attributes */
317  &UnicodeName,
318  (bInheritHandle ? OBJ_INHERIT : 0),
320  NULL);
321 
322  /* Convert COPY to READ */
323  if (dwDesiredAccess == FILE_MAP_COPY)
324  {
325  /* Fixup copy */
326  dwDesiredAccess = SECTION_MAP_READ;
327  }
328  else if (dwDesiredAccess & FILE_MAP_EXECUTE)
329  {
330  /* Fixup execute */
331  dwDesiredAccess = (dwDesiredAccess & ~FILE_MAP_EXECUTE) | SECTION_MAP_EXECUTE;
332  }
333 
334  /* Open the section */
335  Status = NtOpenSection(&SectionHandle, dwDesiredAccess, &ObjectAttributes);
336  if (!NT_SUCCESS(Status))
337  {
338  /* We failed */
340  return NULL;
341  }
342 
343  /* Otherwise, return the handle */
344  return SectionHandle;
345 }
346 
347 /*
348  * @implemented
349  */
350 BOOL
351 NTAPI
352 FlushViewOfFile(IN LPCVOID lpBaseAddress,
353  IN SIZE_T dwNumberOfBytesToFlush)
354 {
356  PVOID BaseAddress = (PVOID)lpBaseAddress;
357  SIZE_T NumberOfBytesToFlush = dwNumberOfBytesToFlush;
359 
360  /* Flush the view */
362  &BaseAddress,
363  &NumberOfBytesToFlush,
364  &IoStatusBlock);
366  {
367  /* We failed */
369  return FALSE;
370  }
371 
372  /* Return success */
373  return TRUE;
374 }
375 
376 /* EOF */
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
LPVOID NTAPI MapViewOfFileEx(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap, LPVOID lpBaseAddress)
Definition: filemap.c:162
#define IN
Definition: typedefs.h:38
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3780
#define TRUE
Definition: types.h:120
#define STATUS_NOT_MAPPED_DATA
Definition: udferr_usr.h:157
#define SEC_LARGE_PAGES
Definition: mmtypes.h:102
#define ERROR_SUCCESS
Definition: deptool.c:10
NTSTATUS NTAPI NtCreateSection(OUT PHANDLE SectionHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN PLARGE_INTEGER MaximumSize OPTIONAL, IN ULONG SectionPageProtection OPTIONAL, IN ULONG AllocationAttributes, IN HANDLE FileHandle OPTIONAL)
Definition: section.c:3371
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG _In_ ULONG Protect
Definition: zwfuncs.h:214
#define SECTION_QUERY
Definition: nt_native.h:1287
#define STATUS_OBJECT_NAME_EXISTS
Definition: ntstatus.h:114
LONG NTSTATUS
Definition: precomp.h:26
IN PDCB IN POEM_STRING IN PUNICODE_STRING UnicodeName
Definition: fatprocs.h:1294
_In_ LPCSTR lpName
Definition: winbase.h:2729
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
SIZE_T NTAPI LocalSize(HLOCAL hMem)
Definition: heapmem.c:1777
#define SEC_NOCACHE
Definition: mmtypes.h:100
HANDLE NTAPI CreateFileMappingW(HANDLE hFile, LPSECURITY_ATTRIBUTES lpFileMappingAttributes, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCWSTR lpName)
Definition: filemap.c:45
#define PAGE_EXECUTE_WRITECOPY
Definition: nt_native.h:1309
#define SEC_COMMIT
Definition: mmtypes.h:99
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
static BOOL bInheritHandle
Definition: pipe.c:82
unsigned int BOOL
Definition: ntddk_ex.h:94
#define ConvertWin32AnsiObjectApiToUnicodeApi(obj, name,...)
Definition: base_x.h:54
#define SEC_RESERVE
Definition: nt_native.h:1323
NTSTATUS NTAPI NtMapViewOfSection(IN HANDLE SectionHandle, IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, IN SIZE_T CommitSize, IN OUT PLARGE_INTEGER SectionOffset OPTIONAL, IN OUT PSIZE_T ViewSize, IN SECTION_INHERIT InheritDisposition, IN ULONG AllocationType, IN ULONG Protect)
Definition: section.c:3552
NTSTATUS NTAPI NtFlushVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToFlush, OUT PIO_STATUS_BLOCK IoStatusBlock)
Definition: virtual.c:3910
NTSTATUS NTAPI NtOpenSection(OUT PHANDLE SectionHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes)
Definition: section.c:3499
#define PAGE_NOACCESS
Definition: nt_native.h:1302
smooth NULL
Definition: ftsmooth.c:416
#define FILE_MAP_READ
Definition: compat.h:427
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
const char * LPCSTR
Definition: xmlstorage.h:183
void * PVOID
Definition: retypes.h:9
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:404
#define SECTION_MAP_WRITE
Definition: nt_native.h:1288
#define NtCurrentProcess()
Definition: nt_native.h:1657
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define OBJ_INHERIT
Definition: winternl.h:225
#define FILE_MAP_WRITE
Definition: winbase.h:154
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
BOOL NTAPI UnmapViewOfFile(LPCVOID lpBaseAddress)
Definition: filemap.c:250
#define FILE_MAP_COPY
Definition: winbase.h:153
#define SECTION_MAP_READ
Definition: compat.h:128
ULONG LowPart
Definition: typedefs.h:104
#define ConvertOpenWin32AnsiObjectApiToUnicodeApi(obj, acc, inh, name)
Definition: base_x.h:86
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:291
HANDLE NTAPI OpenFileMappingW(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName)
Definition: filemap.c:297
_In_ HANDLE hFile
Definition: mswsock.h:90
POBJECT_ATTRIBUTES WINAPI BaseFormatObjectAttributes(OUT POBJECT_ATTRIBUTES ObjectAttributes, IN PSECURITY_ATTRIBUTES SecurityAttributes OPTIONAL, IN PUNICODE_STRING ObjectName)
Definition: utils.c:304
Status
Definition: gdiplustypes.h:24
HANDLE WINAPI BaseGetNamedObjectDirectory(VOID)
Definition: utils.c:63
ULONG_PTR SIZE_T
Definition: typedefs.h:78
_Must_inspect_result_ _In_ USHORT _In_ PHIDP_PREPARSED_DATA _Out_writes_to_ LengthAttributes PHIDP_EXTENDED_ATTRIBUTES Attributes
Definition: hidpi.h:348
BOOL NTAPI FlushViewOfFile(IN LPCVOID lpBaseAddress, IN SIZE_T dwNumberOfBytesToFlush)
Definition: filemap.c:352
#define NtCurrentPeb()
Definition: FLS.c:19
HANDLE NTAPI OpenFileMappingA(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCSTR lpName)
Definition: filemap.c:284
_In_ PIO_STACK_LOCATION _Inout_ PFILE_OBJECT _Inout_ PVCB _Outptr_result_maybenull_ PDCB _In_ PDCB _In_ PDIRENT _In_ ULONG _In_ ULONG _In_ PUNICODE_STRING _In_ PACCESS_MASK DesiredAccess
Definition: create.c:4157
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define PAGE_READONLY
Definition: compat.h:127
#define PAGE_WRITECOPY
Definition: nt_native.h:1305
#define SEC_FILE
Definition: mmtypes.h:95
CONST void * LPCVOID
Definition: windef.h:191
BOOLEAN NTAPI RtlFlushSecureMemoryCache(IN PVOID MemoryCache, IN OPTIONAL SIZE_T MemoryLength)
Definition: security.c:830
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
#define STANDARD_RIGHTS_REQUIRED
Definition: nt_native.h:63
unsigned int ULONG
Definition: retypes.h:1
#define SECTION_MAP_EXECUTE
Definition: nt_native.h:1290
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define SEC_IMAGE
Definition: mmtypes.h:96
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define PAGE_EXECUTE_READ
Definition: nt_native.h:1307
LPVOID NTAPI MapViewOfFile(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap)
Definition: filemap.c:230
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
#define FILE_MAP_EXECUTE
Definition: winbase.h:157
ULONG ACCESS_MASK
Definition: nt_native.h:40
HANDLE NTAPI CreateFileMappingA(IN HANDLE hFile, IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes, IN DWORD flProtect, IN DWORD dwMaximumSizeHigh, IN DWORD dwMaximumSizeLow, IN LPCSTR lpName)
Definition: filemap.c:23
#define PAGE_READWRITE
Definition: nt_native.h:1304