ReactOS  0.4.13-dev-92-gf251225
filemap.c File Reference
#include <k32.h>
#include <debug.h>
Include dependency graph for filemap.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

HANDLE NTAPI CreateFileMappingA (IN HANDLE hFile, IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes, IN DWORD flProtect, IN DWORD dwMaximumSizeHigh, IN DWORD dwMaximumSizeLow, IN LPCSTR lpName)
 
HANDLE NTAPI CreateFileMappingW (HANDLE hFile, LPSECURITY_ATTRIBUTES lpFileMappingAttributes, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCWSTR lpName)
 
LPVOID NTAPI MapViewOfFileEx (HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap, LPVOID lpBaseAddress)
 
LPVOID NTAPI MapViewOfFile (HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap)
 
BOOL NTAPI UnmapViewOfFile (LPCVOID lpBaseAddress)
 
HANDLE NTAPI OpenFileMappingA (IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCSTR lpName)
 
HANDLE NTAPI OpenFileMappingW (IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName)
 
BOOL NTAPI FlushViewOfFile (IN LPCVOID lpBaseAddress, IN SIZE_T dwNumberOfBytesToFlush)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file filemap.c.

Function Documentation

◆ CreateFileMappingA()

HANDLE NTAPI CreateFileMappingA ( IN HANDLE  hFile,
IN LPSECURITY_ATTRIBUTES  lpFileMappingAttributes,
IN DWORD  flProtect,
IN DWORD  dwMaximumSizeHigh,
IN DWORD  dwMaximumSizeLow,
IN LPCSTR  lpName 
)

◆ CreateFileMappingW()

HANDLE NTAPI CreateFileMappingW ( HANDLE  hFile,
LPSECURITY_ATTRIBUTES  lpFileMappingAttributes,
DWORD  flProtect,
DWORD  dwMaximumSizeHigh,
DWORD  dwMaximumSizeLow,
LPCWSTR  lpName 
)

Definition at line 45 of file filemap.c.

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 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#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
#define SECTION_QUERY
Definition: nt_native.h:1287
#define STATUS_OBJECT_NAME_EXISTS
Definition: ntstatus.h:114
LONG NTSTATUS
Definition: precomp.h:26
_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
#define PAGE_EXECUTE_WRITECOPY
Definition: nt_native.h:1309
#define SEC_COMMIT
Definition: mmtypes.h:99
#define SEC_RESERVE
Definition: nt_native.h:1323
smooth NULL
Definition: ftsmooth.c:416
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define SECTION_MAP_WRITE
Definition: nt_native.h:1288
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:409
#define SECTION_MAP_READ
Definition: compat.h:128
ULONG LowPart
Definition: typedefs.h:104
_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
_Must_inspect_result_ _In_ USHORT _In_ PHIDP_PREPARSED_DATA _Out_writes_to_ LengthAttributes PHIDP_EXTENDED_ATTRIBUTES Attributes
Definition: hidpi.h:348
#define NtCurrentPeb()
Definition: FLS.c:19
_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
#define PAGE_READONLY
Definition: compat.h:127
#define PAGE_WRITECOPY
Definition: nt_native.h:1305
#define SEC_FILE
Definition: mmtypes.h:95
#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 PAGE_EXECUTE_READ
Definition: nt_native.h:1307
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
ULONG ACCESS_MASK
Definition: nt_native.h:40
#define PAGE_READWRITE
Definition: nt_native.h:1304

◆ FlushViewOfFile()

BOOL NTAPI FlushViewOfFile ( IN LPCVOID  lpBaseAddress,
IN SIZE_T  dwNumberOfBytesToFlush 
)

Definition at line 352 of file filemap.c.

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 }
#define TRUE
Definition: types.h:120
#define STATUS_NOT_MAPPED_DATA
Definition: udferr_usr.h:157
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtFlushVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToFlush, OUT PIO_STATUS_BLOCK IoStatusBlock)
Definition: virtual.c:3910
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
void * PVOID
Definition: retypes.h:9
#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
Status
Definition: gdiplustypes.h:24
ULONG_PTR SIZE_T
Definition: typedefs.h:78
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75

Referenced by DPLAYX_ConstructData().

◆ MapViewOfFile()

LPVOID NTAPI MapViewOfFile ( HANDLE  hFileMappingObject,
DWORD  dwDesiredAccess,
DWORD  dwFileOffsetHigh,
DWORD  dwFileOffsetLow,
SIZE_T  dwNumberOfBytesToMap 
)

Definition at line 230 of file filemap.c.

235 {
236  /* Call the extended API */
237  return MapViewOfFileEx(hFileMappingObject,
238  dwDesiredAccess,
239  dwFileOffsetHigh,
240  dwFileOffsetLow,
241  dwNumberOfBytesToMap,
242  NULL);
243 }
LPVOID NTAPI MapViewOfFileEx(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap, LPVOID lpBaseAddress)
Definition: filemap.c:162
smooth NULL
Definition: ftsmooth.c:416

◆ MapViewOfFileEx()

LPVOID NTAPI MapViewOfFileEx ( HANDLE  hFileMappingObject,
DWORD  dwDesiredAccess,
DWORD  dwFileOffsetHigh,
DWORD  dwFileOffsetLow,
SIZE_T  dwNumberOfBytesToMap,
LPVOID  lpBaseAddress 
)

Definition at line 162 of file filemap.c.

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 }
_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
LONG NTSTATUS
Definition: precomp.h:26
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
#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
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:404
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define FILE_MAP_WRITE
Definition: winbase.h:154
#define FILE_MAP_COPY
Definition: winbase.h:153
Status
Definition: gdiplustypes.h:24
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define PAGE_READONLY
Definition: compat.h:127
#define PAGE_WRITECOPY
Definition: nt_native.h:1305
_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
unsigned int ULONG
Definition: retypes.h:1
#define PAGE_EXECUTE_READ
Definition: nt_native.h:1307
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
#define FILE_MAP_EXECUTE
Definition: winbase.h:157
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by DPLAYX_ConstructData(), and MapViewOfFile().

◆ OpenFileMappingA()

HANDLE NTAPI OpenFileMappingA ( IN DWORD  dwDesiredAccess,
IN BOOL  bInheritHandle,
IN LPCSTR  lpName 
)

Definition at line 284 of file filemap.c.

287 {
288  ConvertOpenWin32AnsiObjectApiToUnicodeApi(FileMapping, dwDesiredAccess, bInheritHandle, lpName);
289 }
_In_ LPCSTR lpName
Definition: winbase.h:2729
static BOOL bInheritHandle
Definition: pipe.c:82
#define ConvertOpenWin32AnsiObjectApiToUnicodeApi(obj, acc, inh, name)
Definition: base_x.h:86

Referenced by doChild(), test_CreateFileMapping(), test_filemap_security(), and test_MapViewOfFile().

◆ OpenFileMappingW()

HANDLE NTAPI OpenFileMappingW ( IN DWORD  dwDesiredAccess,
IN BOOL  bInheritHandle,
IN LPCWSTR  lpName 
)

Definition at line 297 of file filemap.c.

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 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
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
static BOOL bInheritHandle
Definition: pipe.c:82
NTSTATUS NTAPI NtOpenSection(OUT PHANDLE SectionHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes)
Definition: section.c:3499
smooth NULL
Definition: ftsmooth.c:416
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define OBJ_INHERIT
Definition: winternl.h:225
#define SetLastError(x)
Definition: compat.h:409
#define FILE_MAP_COPY
Definition: winbase.h:153
#define SECTION_MAP_READ
Definition: compat.h:128
Status
Definition: gdiplustypes.h:24
HANDLE WINAPI BaseGetNamedObjectDirectory(VOID)
Definition: utils.c:63
#define SECTION_MAP_EXECUTE
Definition: nt_native.h:1290
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define FILE_MAP_EXECUTE
Definition: winbase.h:157

Referenced by cache_container_map_index(), main(), map_memory(), and OutputDebugStringA().

◆ UnmapViewOfFile()

BOOL NTAPI UnmapViewOfFile ( LPCVOID  lpBaseAddress)

Definition at line 250 of file filemap.c.

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 }
NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, IN PVOID BaseAddress)
Definition: section.c:3780
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:291
Status
Definition: gdiplustypes.h:24
BOOLEAN NTAPI RtlFlushSecureMemoryCache(IN PVOID MemoryCache, IN OPTIONAL SIZE_T MemoryLength)
Definition: security.c:830