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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
28 #include <sys/types.h>
34 #define WIN32_NO_STATUS
35 #define NONAMELESSUNION
42 #include "wine/exception.h"
43 #include "wine/debug.h"
45 #include "kernel_private.h"
47 WINE_DECLARE_DEBUG_CHANNEL(seh
);
48 WINE_DECLARE_DEBUG_CHANNEL(file
);
51 /***********************************************************************
52 * VirtualAlloc (KERNEL32.@)
54 * Reserves or commits a region of pages in virtual address space.
57 * addr [I] Address of region to reserve or commit.
58 * size [I] Size of region.
59 * type [I] Type of allocation.
60 * protect [I] Type of access protection.
63 * Success: Base address of allocated region of pages.
66 LPVOID WINAPI
VirtualAlloc( LPVOID addr
, SIZE_T size
, DWORD type
, DWORD protect
)
68 return VirtualAllocEx( GetCurrentProcess(), addr
, size
, type
, protect
);
72 /***********************************************************************
73 * VirtualAllocEx (KERNEL32.@)
75 * Seems to be just as VirtualAlloc, but with process handle.
78 * hProcess [I] Handle to process to do mem operation.
79 * addr [I] Address of region to reserve or commit.
80 * size [I] Size of region.
81 * type [I] Type of allocation.
82 * protect [I] Type of access protection.
86 * Success: Base address of allocated region of pages.
89 LPVOID WINAPI
VirtualAllocEx( HANDLE hProcess
, LPVOID addr
, SIZE_T size
,
90 DWORD type
, DWORD protect
)
95 if ((status
= NtAllocateVirtualMemory( hProcess
, &ret
, 0, &size
, type
, protect
)))
97 SetLastError( RtlNtStatusToDosError(status
) );
104 /***********************************************************************
105 * VirtualFree (KERNEL32.@)
107 * Releases or decommits a region of pages in virtual address space.
110 * addr [I] Address of region of committed pages.
111 * size [I] Size of region.
112 * type [I] Type of operation.
118 BOOL WINAPI
VirtualFree( LPVOID addr
, SIZE_T size
, DWORD type
)
120 return VirtualFreeEx( GetCurrentProcess(), addr
, size
, type
);
124 /***********************************************************************
125 * VirtualFreeEx (KERNEL32.@)
127 * Releases or decommits a region of pages in virtual address space.
130 * process [I] Handle to process.
131 * addr [I] Address of region to free.
132 * size [I] Size of region.
133 * type [I] Type of operation.
139 BOOL WINAPI
VirtualFreeEx( HANDLE process
, LPVOID addr
, SIZE_T size
, DWORD type
)
141 NTSTATUS status
= NtFreeVirtualMemory( process
, &addr
, &size
, type
);
142 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
147 /***********************************************************************
148 * VirtualLock (KERNEL32.@)
150 * Locks the specified region of virtual address space.
153 * addr [I] Address of first byte of range to lock.
154 * size [I] Number of bytes in range to lock.
161 * Always returns TRUE.
164 BOOL WINAPI
VirtualLock( LPVOID addr
, SIZE_T size
)
166 NTSTATUS status
= NtLockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
167 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
172 /***********************************************************************
173 * VirtualUnlock (KERNEL32.@)
175 * Unlocks a range of pages in the virtual address space.
178 * addr [I] Address of first byte of range.
179 * size [I] Number of bytes in range.
186 * Always returns TRUE.
189 BOOL WINAPI
VirtualUnlock( LPVOID addr
, SIZE_T size
)
191 NTSTATUS status
= NtUnlockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
192 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
197 /***********************************************************************
198 * VirtualProtect (KERNEL32.@)
200 * Changes the access protection on a region of committed pages.
203 * addr [I] Address of region of committed pages.
204 * size [I] Size of region.
205 * new_prot [I] Desired access protection.
206 * old_prot [O] Address of variable to get old protection.
212 BOOL WINAPI
VirtualProtect( LPVOID addr
, SIZE_T size
, DWORD new_prot
, LPDWORD old_prot
)
214 return VirtualProtectEx( GetCurrentProcess(), addr
, size
, new_prot
, old_prot
);
218 /***********************************************************************
219 * VirtualProtectEx (KERNEL32.@)
221 * Changes the access protection on a region of committed pages in the
222 * virtual address space of a specified process.
225 * process [I] Handle of process.
226 * addr [I] Address of region of committed pages.
227 * size [I] Size of region.
228 * new_prot [I] Desired access protection.
229 * old_prot [O] Address of variable to get old protection.
235 BOOL WINAPI
VirtualProtectEx( HANDLE process
, LPVOID addr
, SIZE_T size
,
236 DWORD new_prot
, LPDWORD old_prot
)
238 NTSTATUS status
= NtProtectVirtualMemory( process
, &addr
, &size
, new_prot
, old_prot
);
239 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
244 /***********************************************************************
245 * VirtualQuery (KERNEL32.@)
247 * Provides info about a range of pages in virtual address space.
250 * addr [I] Address of region.
251 * info [O] Address of info buffer.
252 * len [I] Size of buffer.
255 * Number of bytes returned in information buffer or 0 if
256 * addr >= 0xc0000000 (kernel space).
258 SIZE_T WINAPI
VirtualQuery( LPCVOID addr
, PMEMORY_BASIC_INFORMATION info
,
261 return VirtualQueryEx( GetCurrentProcess(), addr
, info
, len
);
265 /***********************************************************************
266 * VirtualQueryEx (KERNEL32.@)
268 * Provides info about a range of pages in virtual address space of a
272 * process [I] Handle to process.
273 * addr [I] Address of region.
274 * info [O] Address of info buffer.
275 * len [I] Size of buffer.
278 * Number of bytes returned in information buffer.
280 SIZE_T WINAPI
VirtualQueryEx( HANDLE process
, LPCVOID addr
,
281 PMEMORY_BASIC_INFORMATION info
, SIZE_T len
)
286 if ((status
= NtQueryVirtualMemory( process
, addr
, MemoryBasicInformation
, info
, len
, &ret
)))
288 SetLastError( RtlNtStatusToDosError(status
) );
295 /***********************************************************************
296 * CreateFileMappingA (KERNEL32.@)
298 * Creates a named or unnamed file-mapping object for the specified file.
301 * hFile [I] Handle to the file to map.
302 * sa [I] Optional security attributes.
303 * protect [I] Protection for mapping object.
304 * size_high [I] High-order 32 bits of object size.
305 * size_low [I] Low-order 32 bits of object size.
306 * name [I] Name of file-mapping object.
310 * Failure: NULL. Mapping object does not exist.
312 HANDLE WINAPI
CreateFileMappingA( HANDLE hFile
, SECURITY_ATTRIBUTES
*sa
,
313 DWORD protect
, DWORD size_high
, DWORD size_low
, LPCSTR name
)
315 WCHAR buffer
[MAX_PATH
];
317 if (!name
) return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, NULL
);
319 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
321 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
324 return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, buffer
);
328 /***********************************************************************
329 * CreateFileMappingW (KERNEL32.@)
331 * See CreateFileMappingA.
333 HANDLE WINAPI
CreateFileMappingW( HANDLE hFile
, LPSECURITY_ATTRIBUTES sa
,
334 DWORD protect
, DWORD size_high
,
335 DWORD size_low
, LPCWSTR name
)
337 static const int sec_flags
= SEC_FILE
| SEC_IMAGE
| SEC_RESERVE
| SEC_COMMIT
| SEC_NOCACHE
;
341 DWORD access
, sec_type
;
344 sec_type
= protect
& sec_flags
;
345 protect
&= ~sec_flags
;
346 if (!sec_type
) sec_type
= SEC_COMMIT
;
348 /* Win9x compatibility */
349 if (!protect
&& (GetVersion() & 0x80000000)) protect
= PAGE_READONLY
;
355 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
;
358 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
;
360 case PAGE_EXECUTE_READ
:
361 case PAGE_EXECUTE_WRITECOPY
:
362 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
;
364 case PAGE_EXECUTE_READWRITE
:
365 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
;
368 SetLastError( ERROR_INVALID_PARAMETER
);
372 if (hFile
== INVALID_HANDLE_VALUE
)
375 if (!size_low
&& !size_high
)
377 SetLastError( ERROR_INVALID_PARAMETER
);
382 size
.u
.LowPart
= size_low
;
383 size
.u
.HighPart
= size_high
;
387 OBJECT_ATTRIBUTES attr
;
388 UNICODE_STRING nameW
;
390 attr
.Length
= sizeof(attr
);
391 attr
.RootDirectory
= 0;
392 attr
.ObjectName
= NULL
;
393 attr
.Attributes
= OBJ_OPENIF
| ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
394 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
395 attr
.SecurityQualityOfService
= NULL
;
398 RtlInitUnicodeString( &nameW
, name
);
399 attr
.ObjectName
= &nameW
;
400 attr
.RootDirectory
= get_BaseNamedObjects_handle();
402 status
= NtCreateSection( &ret
, access
, &attr
, &size
, protect
, sec_type
, hFile
);
404 else status
= NtCreateSection( &ret
, access
, NULL
, &size
, protect
, sec_type
, hFile
);
406 if (status
== STATUS_OBJECT_NAME_EXISTS
)
407 SetLastError( ERROR_ALREADY_EXISTS
);
409 SetLastError( RtlNtStatusToDosError(status
) );
414 /***********************************************************************
415 * OpenFileMappingA (KERNEL32.@)
417 * Opens a named file-mapping object.
420 * access [I] Access mode.
421 * inherit [I] Inherit flag.
422 * name [I] Name of file-mapping object.
428 HANDLE WINAPI
OpenFileMappingA( DWORD access
, BOOL inherit
, LPCSTR name
)
430 WCHAR buffer
[MAX_PATH
];
432 if (!name
) return OpenFileMappingW( access
, inherit
, NULL
);
434 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
436 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
439 return OpenFileMappingW( access
, inherit
, buffer
);
443 /***********************************************************************
444 * OpenFileMappingW (KERNEL32.@)
446 * See OpenFileMappingA.
448 HANDLE WINAPI
OpenFileMappingW( DWORD access
, BOOL inherit
, LPCWSTR name
)
450 OBJECT_ATTRIBUTES attr
;
451 UNICODE_STRING nameW
;
457 SetLastError( ERROR_INVALID_PARAMETER
);
460 attr
.Length
= sizeof(attr
);
461 attr
.RootDirectory
= get_BaseNamedObjects_handle();
462 attr
.ObjectName
= &nameW
;
463 attr
.Attributes
= inherit
? OBJ_INHERIT
: 0;
464 attr
.SecurityDescriptor
= NULL
;
465 attr
.SecurityQualityOfService
= NULL
;
466 RtlInitUnicodeString( &nameW
, name
);
468 if (access
== FILE_MAP_COPY
) access
= SECTION_MAP_READ
;
469 access
|= SECTION_QUERY
;
471 if (GetVersion() & 0x80000000)
473 /* win9x doesn't do access checks, so try with full access first */
474 if (!NtOpenSection( &ret
, access
| SECTION_MAP_READ
| SECTION_MAP_WRITE
, &attr
)) return ret
;
477 if ((status
= NtOpenSection( &ret
, access
, &attr
)))
479 SetLastError( RtlNtStatusToDosError(status
) );
486 /***********************************************************************
487 * MapViewOfFile (KERNEL32.@)
489 * Maps a view of a file into the address space.
492 * mapping [I] File-mapping object to map.
493 * access [I] Access mode.
494 * offset_high [I] High-order 32 bits of file offset.
495 * offset_low [I] Low-order 32 bits of file offset.
496 * count [I] Number of bytes to map.
499 * Success: Starting address of mapped view.
502 LPVOID WINAPI
MapViewOfFile( HANDLE mapping
, DWORD access
,
503 DWORD offset_high
, DWORD offset_low
, SIZE_T count
)
505 return MapViewOfFileEx( mapping
, access
, offset_high
,
506 offset_low
, count
, NULL
);
510 /***********************************************************************
511 * MapViewOfFileEx (KERNEL32.@)
513 * Maps a view of a file into the address space.
516 * handle [I] File-mapping object to map.
517 * access [I] Access mode.
518 * offset_high [I] High-order 32 bits of file offset.
519 * offset_low [I] Low-order 32 bits of file offset.
520 * count [I] Number of bytes to map.
521 * addr [I] Suggested starting address for mapped view.
524 * Success: Starting address of mapped view.
527 LPVOID WINAPI
MapViewOfFileEx( HANDLE handle
, DWORD access
,
528 DWORD offset_high
, DWORD offset_low
, SIZE_T count
, LPVOID addr
)
531 LARGE_INTEGER offset
;
535 offset
.u
.LowPart
= offset_low
;
536 offset
.u
.HighPart
= offset_high
;
538 exec
= access
& FILE_MAP_EXECUTE
;
539 access
&= ~FILE_MAP_EXECUTE
;
541 if (access
== FILE_MAP_COPY
)
542 protect
= exec
? PAGE_EXECUTE_WRITECOPY
: PAGE_WRITECOPY
;
543 else if (access
& FILE_MAP_WRITE
)
544 protect
= exec
? PAGE_EXECUTE_READWRITE
: PAGE_READWRITE
;
545 else if (access
& FILE_MAP_READ
)
546 protect
= exec
? PAGE_EXECUTE_READ
: PAGE_READONLY
;
547 else protect
= PAGE_NOACCESS
;
549 if ((status
= NtMapViewOfSection( handle
, GetCurrentProcess(), &addr
, 0, 0, &offset
,
550 &count
, ViewShare
, 0, protect
)) < 0)
552 SetLastError( RtlNtStatusToDosError(status
) );
559 /***********************************************************************
560 * UnmapViewOfFile (KERNEL32.@)
562 * Unmaps a mapped view of a file.
565 * addr [I] Address where mapped view begins.
572 BOOL WINAPI
UnmapViewOfFile( LPCVOID addr
)
574 NTSTATUS status
= NtUnmapViewOfSection( GetCurrentProcess(), (void *)addr
);
575 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
580 /***********************************************************************
581 * FlushViewOfFile (KERNEL32.@)
583 * Writes to the disk a byte range within a mapped view of a file.
586 * base [I] Start address of byte range to flush.
587 * size [I] Number of bytes in range.
593 BOOL WINAPI
FlushViewOfFile( LPCVOID base
, SIZE_T size
)
595 NTSTATUS status
= NtFlushVirtualMemory( GetCurrentProcess(), &base
, &size
, 0 );
598 if (status
== STATUS_NOT_MAPPED_DATA
) status
= STATUS_SUCCESS
;
599 else SetLastError( RtlNtStatusToDosError(status
) );
605 /***********************************************************************
606 * GetWriteWatch (KERNEL32.@)
608 UINT WINAPI
GetWriteWatch( DWORD flags
, LPVOID base
, SIZE_T size
, LPVOID
*addresses
,
609 ULONG_PTR
*count
, ULONG
*granularity
)
613 status
= NtGetWriteWatch( GetCurrentProcess(), flags
, base
, size
, addresses
, count
, granularity
);
614 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
615 return status
? ~0u : 0;
619 /***********************************************************************
620 * ResetWriteWatch (KERNEL32.@)
622 UINT WINAPI
ResetWriteWatch( LPVOID base
, SIZE_T size
)
626 status
= NtResetWriteWatch( GetCurrentProcess(), base
, size
);
627 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
628 return status
? ~0u : 0;
632 /***********************************************************************
633 * IsBadReadPtr (KERNEL32.@)
635 * Check for read access on a memory block.
637 * ptr [I] Address of memory block.
638 * size [I] Size of block.
642 * Failure: FALSE. Process has read access to entire block.
644 BOOL WINAPI
IsBadReadPtr( LPCVOID ptr
, UINT size
)
646 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
647 if (!ptr
) return TRUE
;
650 volatile const char *p
= ptr
;
651 char dummy
__attribute__((unused
));
654 while (count
> system_info
.PageSize
)
657 p
+= system_info
.PageSize
;
658 count
-= system_info
.PageSize
;
661 dummy
= p
[count
- 1];
665 TRACE_(seh
)("%p caused page fault during read\n", ptr
);
673 /***********************************************************************
674 * IsBadWritePtr (KERNEL32.@)
676 * Check for write access on a memory block.
679 * ptr [I] Address of memory block.
680 * size [I] Size of block in bytes.
684 * Failure: FALSE. Process has write access to entire block.
686 BOOL WINAPI
IsBadWritePtr( LPVOID ptr
, UINT size
)
688 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
689 if (!ptr
) return TRUE
;
692 volatile char *p
= ptr
;
695 while (count
> system_info
.PageSize
)
698 p
+= system_info
.PageSize
;
699 count
-= system_info
.PageSize
;
706 TRACE_(seh
)("%p caused page fault during write\n", ptr
);
714 /***********************************************************************
715 * IsBadHugeReadPtr (KERNEL32.@)
717 * Check for read access on a memory block.
720 * ptr [I] Address of memory block.
721 * size [I] Size of block.
725 * Failure: FALSE. Process has read access to entire block.
727 BOOL WINAPI
IsBadHugeReadPtr( LPCVOID ptr
, UINT size
)
729 return IsBadReadPtr( ptr
, size
);
733 /***********************************************************************
734 * IsBadHugeWritePtr (KERNEL32.@)
736 * Check for write access on a memory block.
739 * ptr [I] Address of memory block.
740 * size [I] Size of block.
744 * Failure: FALSE. Process has write access to entire block.
746 BOOL WINAPI
IsBadHugeWritePtr( LPVOID ptr
, UINT size
)
748 return IsBadWritePtr( ptr
, size
);
752 /***********************************************************************
753 * IsBadCodePtr (KERNEL32.@)
755 * Check for read access on a memory address.
758 * ptr [I] Address of function.
762 * Failure: FALSE. Process has read access to specified memory.
764 BOOL WINAPI
IsBadCodePtr( FARPROC ptr
)
766 return IsBadReadPtr( ptr
, 1 );
770 /***********************************************************************
771 * IsBadStringPtrA (KERNEL32.@)
773 * Check for read access on a range of memory pointed to by a string pointer.
776 * str [I] Address of string.
777 * max [I] Maximum size of string.
781 * Failure: FALSE. Read access to all bytes in string.
783 BOOL WINAPI
IsBadStringPtrA( LPCSTR str
, UINT max
)
785 if (!str
) return TRUE
;
789 volatile const char *p
= str
;
790 while (p
!= str
+ max
) if (!*p
++) break;
794 TRACE_(seh
)("%p caused page fault during read\n", str
);
802 /***********************************************************************
803 * IsBadStringPtrW (KERNEL32.@)
805 * See IsBadStringPtrA.
807 BOOL WINAPI
IsBadStringPtrW( LPCWSTR str
, UINT max
)
809 if (!str
) return TRUE
;
813 volatile const WCHAR
*p
= str
;
814 while (p
!= str
+ max
) if (!*p
++) break;
818 TRACE_(seh
)("%p caused page fault during read\n", str
);
825 /***********************************************************************
826 * K32GetMappedFileNameA (KERNEL32.@)
828 DWORD WINAPI
K32GetMappedFileNameA(HANDLE process
, LPVOID lpv
, LPSTR file_name
, DWORD size
)
830 FIXME_(file
)("(%p, %p, %p, %d): stub\n", process
, lpv
, file_name
, size
);
832 if (file_name
&& size
)
838 /***********************************************************************
839 * K32GetMappedFileNameW (KERNEL32.@)
841 DWORD WINAPI
K32GetMappedFileNameW(HANDLE process
, LPVOID lpv
, LPWSTR file_name
, DWORD size
)
843 FIXME_(file
)("(%p, %p, %p, %d): stub\n", process
, lpv
, file_name
, size
);
845 if (file_name
&& size
)
851 /***********************************************************************
852 * K32EnumPageFilesA (KERNEL32.@)
854 BOOL WINAPI
K32EnumPageFilesA( PENUM_PAGE_FILE_CALLBACKA callback
, LPVOID context
)
856 FIXME_(file
)("(%p, %p) stub\n", callback
, context
);
860 /***********************************************************************
861 * K32EnumPageFilesW (KERNEL32.@)
863 BOOL WINAPI
K32EnumPageFilesW( PENUM_PAGE_FILE_CALLBACKW callback
, LPVOID context
)
865 FIXME_(file
)("(%p, %p) stub\n", callback
, context
);
869 /***********************************************************************
870 * K32GetWsChanges (KERNEL32.@)
872 BOOL WINAPI
K32GetWsChanges(HANDLE process
, PPSAPI_WS_WATCH_INFORMATION watchinfo
, DWORD size
)
876 TRACE_(seh
)("(%p, %p, %d)\n", process
, watchinfo
, size
);
878 status
= NtQueryInformationProcess( process
, ProcessWorkingSetWatch
, watchinfo
, size
, NULL
);
882 SetLastError( RtlNtStatusToDosError( status
) );
888 /***********************************************************************
889 * K32InitializeProcessForWsWatch (KERNEL32.@)
891 BOOL WINAPI
K32InitializeProcessForWsWatch(HANDLE process
)
893 FIXME_(seh
)("(process=%p): stub\n", process
);