2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
27 #include <sys/types.h>
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
38 #include "wine/exception.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
44 static unsigned int page_size;
46 /* filter for page-fault exceptions */
47 static WINE_EXCEPTION_FILTER(page_fault)
49 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
50 return EXCEPTION_EXECUTE_HANDLER;
51 return EXCEPTION_CONTINUE_SEARCH;
55 /***********************************************************************
56 * VirtualAlloc (KERNEL32.@)
57 * Reserves or commits a region of pages in virtual address space
60 * Base address of allocated region of pages
63 LPVOID WINAPI VirtualAlloc(
64 LPVOID addr, /* [in] Address of region to reserve or commit */
65 SIZE_T size, /* [in] Size of region */
66 DWORD type, /* [in] Type of allocation */
67 DWORD protect)/* [in] Type of access protection */
69 return VirtualAllocEx( GetCurrentProcess(), addr, size, type, protect );
73 /***********************************************************************
74 * VirtualAllocEx (KERNEL32.@)
76 * Seems to be just as VirtualAlloc, but with process handle.
78 LPVOID WINAPI VirtualAllocEx(
79 HANDLE hProcess, /* [in] Handle of process to do mem operation */
80 LPVOID addr, /* [in] Address of region to reserve or commit */
81 SIZE_T size, /* [in] Size of region */
82 DWORD type, /* [in] Type of allocation */
83 DWORD protect ) /* [in] Type of access protection */
88 if ((status = NtAllocateVirtualMemory( hProcess, &ret, addr, &size, type, protect )))
90 SetLastError( RtlNtStatusToDosError(status) );
97 /***********************************************************************
98 * VirtualFree (KERNEL32.@)
99 * Release or decommits a region of pages in virtual address space.
105 BOOL WINAPI VirtualFree(
106 LPVOID addr, /* [in] Address of region of committed pages */
107 SIZE_T size, /* [in] Size of region */
108 DWORD type /* [in] Type of operation */
110 return VirtualFreeEx( GetCurrentProcess(), addr, size, type );
114 /***********************************************************************
115 * VirtualFreeEx (KERNEL32.@)
116 * Release or decommits a region of pages in virtual address space.
122 BOOL WINAPI VirtualFreeEx( HANDLE process, LPVOID addr, SIZE_T size, DWORD type )
124 NTSTATUS status = NtFreeVirtualMemory( process, &addr, &size, type );
125 if (status) SetLastError( RtlNtStatusToDosError(status) );
130 /***********************************************************************
131 * VirtualLock (KERNEL32.@)
132 * Locks the specified region of virtual address space
135 * Always returns TRUE
141 BOOL WINAPI VirtualLock( LPVOID addr, /* [in] Address of first byte of range to lock */
142 SIZE_T size ) /* [in] Number of bytes in range to lock */
144 NTSTATUS status = NtLockVirtualMemory( GetCurrentProcess(), &addr, &size, 1 );
145 if (status) SetLastError( RtlNtStatusToDosError(status) );
150 /***********************************************************************
151 * VirtualUnlock (KERNEL32.@)
152 * Unlocks a range of pages in the virtual address space
155 * Always returns TRUE
161 BOOL WINAPI VirtualUnlock( LPVOID addr, /* [in] Address of first byte of range */
162 SIZE_T size ) /* [in] Number of bytes in range */
164 NTSTATUS status = NtUnlockVirtualMemory( GetCurrentProcess(), &addr, &size, 1 );
165 if (status) SetLastError( RtlNtStatusToDosError(status) );
170 /***********************************************************************
171 * VirtualProtect (KERNEL32.@)
172 * Changes the access protection on a region of committed pages
178 BOOL WINAPI VirtualProtect(
179 LPVOID addr, /* [in] Address of region of committed pages */
180 SIZE_T size, /* [in] Size of region */
181 DWORD new_prot, /* [in] Desired access protection */
182 LPDWORD old_prot /* [out] Address of variable to get old protection */
184 return VirtualProtectEx( GetCurrentProcess(), addr, size, new_prot, old_prot );
188 /***********************************************************************
189 * VirtualProtectEx (KERNEL32.@)
190 * Changes the access protection on a region of committed pages in the
191 * virtual address space of a specified process
197 BOOL WINAPI VirtualProtectEx(
198 HANDLE process, /* [in] Handle of process */
199 LPVOID addr, /* [in] Address of region of committed pages */
200 SIZE_T size, /* [in] Size of region */
201 DWORD new_prot, /* [in] Desired access protection */
202 LPDWORD old_prot /* [out] Address of variable to get old protection */ )
204 NTSTATUS status = NtProtectVirtualMemory( process, &addr, &size, new_prot, old_prot );
205 if (status) SetLastError( RtlNtStatusToDosError(status) );
210 /***********************************************************************
211 * VirtualQuery (KERNEL32.@)
212 * Provides info about a range of pages in virtual address space
215 * Number of bytes returned in information buffer
216 * or 0 if addr is >= 0xc0000000 (kernel space).
218 SIZE_T WINAPI VirtualQuery(
219 LPCVOID addr, /* [in] Address of region */
220 PMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
221 SIZE_T len /* [in] Size of buffer */
223 return VirtualQueryEx( GetCurrentProcess(), addr, info, len );
227 /***********************************************************************
228 * VirtualQueryEx (KERNEL32.@)
229 * Provides info about a range of pages in virtual address space of a
233 * Number of bytes returned in information buffer
235 SIZE_T WINAPI VirtualQueryEx(
236 HANDLE process, /* [in] Handle of process */
237 LPCVOID addr, /* [in] Address of region */
238 PMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
239 SIZE_T len /* [in] Size of buffer */ )
244 if ((status = NtQueryVirtualMemory( process, addr, MemoryBasicInformation, info, len, &ret )))
246 SetLastError( RtlNtStatusToDosError(status) );
253 /***********************************************************************
254 * CreateFileMappingA (KERNEL32.@)
255 * Creates a named or unnamed file-mapping object for the specified file
259 * 0: Mapping object does not exist
262 HANDLE WINAPI CreateFileMappingA(
263 HANDLE hFile, /* [in] Handle of file to map */
264 SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
265 DWORD protect, /* [in] Protection for mapping object */
266 DWORD size_high, /* [in] High-order 32 bits of object size */
267 DWORD size_low, /* [in] Low-order 32 bits of object size */
268 LPCSTR name /* [in] Name of file-mapping object */ )
270 WCHAR buffer[MAX_PATH];
272 if (!name) return CreateFileMappingW( hFile, sa, protect, size_high, size_low, NULL );
274 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
276 SetLastError( ERROR_FILENAME_EXCED_RANGE );
279 return CreateFileMappingW( hFile, sa, protect, size_high, size_low, buffer );
283 /***********************************************************************
284 * CreateFileMappingW (KERNEL32.@)
285 * See CreateFileMappingA
287 HANDLE WINAPI CreateFileMappingW( HANDLE hFile, LPSECURITY_ATTRIBUTES sa,
288 DWORD protect, DWORD size_high,
289 DWORD size_low, LPCWSTR name )
291 static const int sec_flags = SEC_FILE | SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_NOCACHE;
294 OBJECT_ATTRIBUTES attr;
295 UNICODE_STRING nameW;
297 DWORD access, sec_type;
300 attr.Length = sizeof(attr);
301 attr.RootDirectory = 0;
302 attr.ObjectName = NULL;
303 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
304 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
305 attr.SecurityQualityOfService = NULL;
309 RtlInitUnicodeString( &nameW, name );
310 attr.ObjectName = &nameW;
313 sec_type = protect & sec_flags;
314 protect &= ~sec_flags;
315 if (!sec_type) sec_type = SEC_COMMIT;
322 access = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ;
325 access = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE;
328 SetLastError( ERROR_INVALID_PARAMETER );
332 if (hFile == INVALID_HANDLE_VALUE)
335 if (!size_low && !size_high)
337 SetLastError( ERROR_INVALID_PARAMETER );
342 size.s.LowPart = size_low;
343 size.s.HighPart = size_high;
345 status = NtCreateSection( &ret, access, &attr, &size, protect, sec_type, hFile );
346 SetLastError( RtlNtStatusToDosError(status) );
351 /***********************************************************************
352 * OpenFileMappingA (KERNEL32.@)
353 * Opens a named file-mapping object.
359 HANDLE WINAPI OpenFileMappingA(
360 DWORD access, /* [in] Access mode */
361 BOOL inherit, /* [in] Inherit flag */
362 LPCSTR name ) /* [in] Name of file-mapping object */
364 WCHAR buffer[MAX_PATH];
366 if (!name) return OpenFileMappingW( access, inherit, NULL );
368 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
370 SetLastError( ERROR_FILENAME_EXCED_RANGE );
373 return OpenFileMappingW( access, inherit, buffer );
377 /***********************************************************************
378 * OpenFileMappingW (KERNEL32.@)
379 * See OpenFileMappingA
381 HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
383 OBJECT_ATTRIBUTES attr;
384 UNICODE_STRING nameW;
390 SetLastError( ERROR_INVALID_PARAMETER );
393 attr.Length = sizeof(attr);
394 attr.RootDirectory = 0;
395 attr.ObjectName = &nameW;
396 attr.Attributes = inherit ? OBJ_INHERIT : 0;
397 attr.SecurityDescriptor = NULL;
398 attr.SecurityQualityOfService = NULL;
399 RtlInitUnicodeString( &nameW, name );
401 if (access == FILE_MAP_COPY) access = FILE_MAP_READ;
403 if ((status = NtOpenSection( &ret, access, &attr )))
405 SetLastError( RtlNtStatusToDosError(status) );
412 /***********************************************************************
413 * MapViewOfFile (KERNEL32.@)
414 * Maps a view of a file into the address space
417 * Starting address of mapped view
420 LPVOID WINAPI MapViewOfFile(
421 HANDLE mapping, /* [in] File-mapping object to map */
422 DWORD access, /* [in] Access mode */
423 DWORD offset_high, /* [in] High-order 32 bits of file offset */
424 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
425 SIZE_T count /* [in] Number of bytes to map */
427 return MapViewOfFileEx( mapping, access, offset_high,
428 offset_low, count, NULL );
432 /***********************************************************************
433 * MapViewOfFileEx (KERNEL32.@)
434 * Maps a view of a file into the address space
437 * Starting address of mapped view
440 LPVOID WINAPI MapViewOfFileEx(
441 HANDLE handle, /* [in] File-mapping object to map */
442 DWORD access, /* [in] Access mode */
443 DWORD offset_high, /* [in] High-order 32 bits of file offset */
444 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
445 SIZE_T count, /* [in] Number of bytes to map */
446 LPVOID addr /* [in] Suggested starting address for mapped view */
449 LARGE_INTEGER offset;
452 offset.s.LowPart = offset_low;
453 offset.s.HighPart = offset_high;
455 if (access & FILE_MAP_WRITE) protect = PAGE_READWRITE;
456 else if (access & FILE_MAP_READ) protect = PAGE_READONLY;
457 else if (access & FILE_MAP_COPY) protect = PAGE_WRITECOPY;
458 else protect = PAGE_NOACCESS;
460 if ((status = NtMapViewOfSection( handle, GetCurrentProcess(), &addr, 0, 0, &offset,
461 &count, ViewShare, 0, protect )))
463 SetLastError( RtlNtStatusToDosError(status) );
470 /***********************************************************************
471 * UnmapViewOfFile (KERNEL32.@)
472 * Unmaps a mapped view of a file.
475 * Should addr be an LPCVOID?
481 BOOL WINAPI UnmapViewOfFile( LPVOID addr ) /* [in] Address where mapped view begins */
483 NTSTATUS status = NtUnmapViewOfSection( GetCurrentProcess(), addr );
484 if (status) SetLastError( RtlNtStatusToDosError(status) );
489 /***********************************************************************
490 * FlushViewOfFile (KERNEL32.@)
491 * Writes to the disk a byte range within a mapped view of a file
497 BOOL WINAPI FlushViewOfFile( LPCVOID base, /* [in] Start address of byte range to flush */
498 SIZE_T size ) /* [in] Number of bytes in range */
500 NTSTATUS status = NtFlushVirtualMemory( GetCurrentProcess(), &base, &size, 0 );
503 if (status == STATUS_NOT_MAPPED_DATA) status = STATUS_SUCCESS;
504 else SetLastError( RtlNtStatusToDosError(status) );
510 /***********************************************************************
511 * IsBadReadPtr (KERNEL32.@)
514 * FALSE: Process has read access to entire block
517 BOOL WINAPI IsBadReadPtr(
518 LPCVOID ptr, /* [in] Address of memory block */
519 UINT size ) /* [in] Size of block */
521 if (!size) return FALSE; /* handle 0 size case w/o reference */
522 if (!page_size) page_size = getpagesize();
525 volatile const char *p = ptr;
529 while (count > page_size)
536 dummy = p[count - 1];
538 __EXCEPT(page_fault) { return TRUE; }
544 /***********************************************************************
545 * IsBadWritePtr (KERNEL32.@)
548 * FALSE: Process has write access to entire block
551 BOOL WINAPI IsBadWritePtr(
552 LPVOID ptr, /* [in] Address of memory block */
553 UINT size ) /* [in] Size of block in bytes */
555 if (!size) return FALSE; /* handle 0 size case w/o reference */
556 if (!page_size) page_size = getpagesize();
559 volatile char *p = ptr;
562 while (count > page_size)
571 __EXCEPT(page_fault) { return TRUE; }
577 /***********************************************************************
578 * IsBadHugeReadPtr (KERNEL32.@)
580 * FALSE: Process has read access to entire block
583 BOOL WINAPI IsBadHugeReadPtr(
584 LPCVOID ptr, /* [in] Address of memory block */
585 UINT size /* [in] Size of block */
587 return IsBadReadPtr( ptr, size );
591 /***********************************************************************
592 * IsBadHugeWritePtr (KERNEL32.@)
594 * FALSE: Process has write access to entire block
597 BOOL WINAPI IsBadHugeWritePtr(
598 LPVOID ptr, /* [in] Address of memory block */
599 UINT size /* [in] Size of block */
601 return IsBadWritePtr( ptr, size );
605 /***********************************************************************
606 * IsBadCodePtr (KERNEL32.@)
609 * FALSE: Process has read access to specified memory
612 BOOL WINAPI IsBadCodePtr( FARPROC ptr ) /* [in] Address of function */
614 return IsBadReadPtr( ptr, 1 );
618 /***********************************************************************
619 * IsBadStringPtrA (KERNEL32.@)
622 * FALSE: Read access to all bytes in string
625 BOOL WINAPI IsBadStringPtrA(
626 LPCSTR str, /* [in] Address of string */
627 UINT max ) /* [in] Maximum size of string */
631 volatile const char *p = str;
632 while (p != str + max) if (!*p++) break;
634 __EXCEPT(page_fault) { return TRUE; }
640 /***********************************************************************
641 * IsBadStringPtrW (KERNEL32.@)
642 * See IsBadStringPtrA
644 BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT max )
648 volatile const WCHAR *p = str;
649 while (p != str + max) if (!*p++) break;
651 __EXCEPT(page_fault) { return TRUE; }