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>
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
36 #define WIN32_NO_STATUS
42 #include "wine/exception.h"
43 #include "wine/debug.h"
45 #include "kernel_private.h"
47 WINE_DECLARE_DEBUG_CHANNEL(seh
);
49 static unsigned int page_size
;
52 /***********************************************************************
53 * VirtualAlloc (KERNEL32.@)
55 * Reserves or commits a region of pages in virtual address space.
58 * addr [I] Address of region to reserve or commit.
59 * size [I] Size of region.
60 * type [I] Type of allocation.
61 * protect [I] Type of access protection.
64 * Success: Base address of allocated region of pages.
67 LPVOID WINAPI
VirtualAlloc( LPVOID addr
, SIZE_T size
, DWORD type
, DWORD protect
)
69 return VirtualAllocEx( GetCurrentProcess(), addr
, size
, type
, protect
);
73 /***********************************************************************
74 * VirtualAllocEx (KERNEL32.@)
76 * Seems to be just as VirtualAlloc, but with process handle.
79 * hProcess [I] Handle to process to do mem operation.
80 * addr [I] Address of region to reserve or commit.
81 * size [I] Size of region.
82 * type [I] Type of allocation.
83 * protect [I] Type of access protection.
87 * Success: Base address of allocated region of pages.
90 LPVOID WINAPI
VirtualAllocEx( HANDLE hProcess
, LPVOID addr
, SIZE_T size
,
91 DWORD type
, DWORD protect
)
96 if ((status
= NtAllocateVirtualMemory( hProcess
, &ret
, 0, &size
, type
, protect
)))
98 SetLastError( RtlNtStatusToDosError(status
) );
105 /***********************************************************************
106 * VirtualFree (KERNEL32.@)
108 * Releases or decommits a region of pages in virtual address space.
111 * addr [I] Address of region of committed pages.
112 * size [I] Size of region.
113 * type [I] Type of operation.
119 BOOL WINAPI
VirtualFree( LPVOID addr
, SIZE_T size
, DWORD type
)
121 return VirtualFreeEx( GetCurrentProcess(), addr
, size
, type
);
125 /***********************************************************************
126 * VirtualFreeEx (KERNEL32.@)
128 * Releases or decommits a region of pages in virtual address space.
131 * process [I] Handle to process.
132 * addr [I] Address of region to free.
133 * size [I] Size of region.
134 * type [I] Type of operation.
140 BOOL WINAPI
VirtualFreeEx( HANDLE process
, LPVOID addr
, SIZE_T size
, DWORD type
)
142 NTSTATUS status
= NtFreeVirtualMemory( process
, &addr
, &size
, type
);
143 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
148 /***********************************************************************
149 * VirtualLock (KERNEL32.@)
151 * Locks the specified region of virtual address space.
154 * addr [I] Address of first byte of range to lock.
155 * size [I] Number of bytes in range to lock.
162 * Always returns TRUE.
165 BOOL WINAPI
VirtualLock( LPVOID addr
, SIZE_T size
)
167 NTSTATUS status
= NtLockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
168 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
173 /***********************************************************************
174 * VirtualUnlock (KERNEL32.@)
176 * Unlocks a range of pages in the virtual address space.
179 * addr [I] Address of first byte of range.
180 * size [I] Number of bytes in range.
187 * Always returns TRUE.
190 BOOL WINAPI
VirtualUnlock( LPVOID addr
, SIZE_T size
)
192 NTSTATUS status
= NtUnlockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
193 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
198 /***********************************************************************
199 * VirtualProtect (KERNEL32.@)
201 * Changes the access protection on a region of committed pages.
204 * addr [I] Address of region of committed pages.
205 * size [I] Size of region.
206 * new_prot [I] Desired access protection.
207 * old_prot [O] Address of variable to get old protection.
213 BOOL WINAPI
VirtualProtect( LPVOID addr
, SIZE_T size
, DWORD new_prot
, LPDWORD old_prot
)
215 return VirtualProtectEx( GetCurrentProcess(), addr
, size
, new_prot
, old_prot
);
219 /***********************************************************************
220 * VirtualProtectEx (KERNEL32.@)
222 * Changes the access protection on a region of committed pages in the
223 * virtual address space of a specified process.
226 * process [I] Handle of process.
227 * addr [I] Address of region of committed pages.
228 * size [I] Size of region.
229 * new_prot [I] Desired access protection.
230 * old_prot [O] Address of variable to get old protection.
236 BOOL WINAPI
VirtualProtectEx( HANDLE process
, LPVOID addr
, SIZE_T size
,
237 DWORD new_prot
, LPDWORD old_prot
)
239 NTSTATUS status
= NtProtectVirtualMemory( process
, &addr
, &size
, new_prot
, old_prot
);
240 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
245 /***********************************************************************
246 * VirtualQuery (KERNEL32.@)
248 * Provides info about a range of pages in virtual address space.
251 * addr [I] Address of region.
252 * info [O] Address of info buffer.
253 * len [I] Size of buffer.
256 * Number of bytes returned in information buffer or 0 if
257 * addr >= 0xc0000000 (kernel space).
259 SIZE_T WINAPI
VirtualQuery( LPCVOID addr
, PMEMORY_BASIC_INFORMATION info
,
262 return VirtualQueryEx( GetCurrentProcess(), addr
, info
, len
);
266 /***********************************************************************
267 * VirtualQueryEx (KERNEL32.@)
269 * Provides info about a range of pages in virtual address space of a
273 * process [I] Handle to process.
274 * addr [I] Address of region.
275 * info [O] Address of info buffer.
276 * len [I] Size of buffer.
279 * Number of bytes returned in information buffer.
281 SIZE_T WINAPI
VirtualQueryEx( HANDLE process
, LPCVOID addr
,
282 PMEMORY_BASIC_INFORMATION info
, SIZE_T len
)
287 if ((status
= NtQueryVirtualMemory( process
, addr
, MemoryBasicInformation
, info
, len
, &ret
)))
289 SetLastError( RtlNtStatusToDosError(status
) );
296 /***********************************************************************
297 * CreateFileMappingA (KERNEL32.@)
299 * Creates a named or unnamed file-mapping object for the specified file.
302 * hFile [I] Handle to the file to map.
303 * sa [I] Optional security attributes.
304 * protect [I] Protection for mapping object.
305 * size_high [I] High-order 32 bits of object size.
306 * size_low [I] Low-order 32 bits of object size.
307 * name [I] Name of file-mapping object.
311 * Failure: NULL. Mapping object does not exist.
313 HANDLE WINAPI
CreateFileMappingA( HANDLE hFile
, SECURITY_ATTRIBUTES
*sa
,
314 DWORD protect
, DWORD size_high
, DWORD size_low
, LPCSTR name
)
316 WCHAR buffer
[MAX_PATH
];
318 if (!name
) return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, NULL
);
320 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
322 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
325 return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, buffer
);
329 /***********************************************************************
330 * CreateFileMappingW (KERNEL32.@)
332 * See CreateFileMappingA.
334 HANDLE WINAPI
CreateFileMappingW( HANDLE hFile
, LPSECURITY_ATTRIBUTES sa
,
335 DWORD protect
, DWORD size_high
,
336 DWORD size_low
, LPCWSTR name
)
338 static const int sec_flags
= SEC_FILE
| SEC_IMAGE
| SEC_RESERVE
| SEC_COMMIT
| SEC_NOCACHE
;
341 OBJECT_ATTRIBUTES attr
;
342 UNICODE_STRING nameW
;
344 DWORD access
, sec_type
;
347 attr
.Length
= sizeof(attr
);
348 attr
.RootDirectory
= 0;
349 attr
.ObjectName
= NULL
;
350 attr
.Attributes
= OBJ_OPENIF
| ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
351 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
352 attr
.SecurityQualityOfService
= NULL
;
356 RtlInitUnicodeString( &nameW
, name
);
357 attr
.ObjectName
= &nameW
;
358 attr
.RootDirectory
= get_BaseNamedObjects_handle();
361 sec_type
= protect
& sec_flags
;
362 protect
&= ~sec_flags
;
363 if (!sec_type
) sec_type
= SEC_COMMIT
;
368 protect
= PAGE_READONLY
; /* Win9x compatibility */
372 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
;
375 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
;
378 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_EXECUTE
;
380 case PAGE_EXECUTE_READ
:
381 case PAGE_EXECUTE_WRITECOPY
:
382 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
;
384 case PAGE_EXECUTE_READWRITE
:
385 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
;
388 SetLastError( ERROR_INVALID_PARAMETER
);
392 if (hFile
== INVALID_HANDLE_VALUE
)
395 if (!size_low
&& !size_high
)
397 SetLastError( ERROR_INVALID_PARAMETER
);
402 size
.u
.LowPart
= size_low
;
403 size
.u
.HighPart
= size_high
;
405 status
= NtCreateSection( &ret
, access
, &attr
, &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
= FILE_MAP_READ
;
470 if ((status
= NtOpenSection( &ret
, access
, &attr
)))
472 SetLastError( RtlNtStatusToDosError(status
) );
479 /***********************************************************************
480 * MapViewOfFile (KERNEL32.@)
482 * Maps a view of a file into the address space.
485 * mapping [I] File-mapping object to map.
486 * access [I] Access mode.
487 * offset_high [I] High-order 32 bits of file offset.
488 * offset_low [I] Low-order 32 bits of file offset.
489 * count [I] Number of bytes to map.
492 * Success: Starting address of mapped view.
495 LPVOID WINAPI
MapViewOfFile( HANDLE mapping
, DWORD access
,
496 DWORD offset_high
, DWORD offset_low
, SIZE_T count
)
498 return MapViewOfFileEx( mapping
, access
, offset_high
,
499 offset_low
, count
, NULL
);
503 /***********************************************************************
504 * MapViewOfFileEx (KERNEL32.@)
506 * Maps a view of a file into the address space.
509 * handle [I] File-mapping object to map.
510 * access [I] Access mode.
511 * offset_high [I] High-order 32 bits of file offset.
512 * offset_low [I] Low-order 32 bits of file offset.
513 * count [I] Number of bytes to map.
514 * addr [I] Suggested starting address for mapped view.
517 * Success: Starting address of mapped view.
520 LPVOID WINAPI
MapViewOfFileEx( HANDLE handle
, DWORD access
,
521 DWORD offset_high
, DWORD offset_low
, SIZE_T count
, LPVOID addr
)
524 LARGE_INTEGER offset
;
527 offset
.u
.LowPart
= offset_low
;
528 offset
.u
.HighPart
= offset_high
;
530 if (access
& FILE_MAP_WRITE
) protect
= PAGE_READWRITE
;
531 else if (access
& FILE_MAP_READ
) protect
= PAGE_READONLY
;
532 else if (access
& FILE_MAP_COPY
) protect
= PAGE_WRITECOPY
;
533 else protect
= PAGE_NOACCESS
;
535 if ((status
= NtMapViewOfSection( handle
, GetCurrentProcess(), &addr
, 0, 0, &offset
,
536 &count
, ViewShare
, 0, protect
)))
538 SetLastError( RtlNtStatusToDosError(status
) );
545 /***********************************************************************
546 * UnmapViewOfFile (KERNEL32.@)
548 * Unmaps a mapped view of a file.
551 * addr [I] Address where mapped view begins.
558 BOOL WINAPI
UnmapViewOfFile( LPCVOID addr
)
560 NTSTATUS status
= NtUnmapViewOfSection( GetCurrentProcess(), (void *)addr
);
561 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
566 /***********************************************************************
567 * FlushViewOfFile (KERNEL32.@)
569 * Writes to the disk a byte range within a mapped view of a file.
572 * base [I] Start address of byte range to flush.
573 * size [I] Number of bytes in range.
579 BOOL WINAPI
FlushViewOfFile( LPCVOID base
, SIZE_T size
)
581 NTSTATUS status
= NtFlushVirtualMemory( GetCurrentProcess(), &base
, &size
, 0 );
584 if (status
== STATUS_NOT_MAPPED_DATA
) status
= STATUS_SUCCESS
;
585 else SetLastError( RtlNtStatusToDosError(status
) );
591 /***********************************************************************
592 * IsBadReadPtr (KERNEL32.@)
594 * Check for read access on a memory block.
596 * ptr [I] Address of memory block.
597 * size [I] Size of block.
601 * Failure: FALSE. Process has read access to entire block.
603 BOOL WINAPI
IsBadReadPtr( LPCVOID ptr
, UINT size
)
605 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
606 if (!ptr
) return TRUE
;
608 if (!page_size
) page_size
= getpagesize();
611 volatile const char *p
= ptr
;
615 while (count
> page_size
)
622 dummy
= p
[count
- 1];
626 TRACE_(seh
)("%p caused page fault during read\n", ptr
);
634 /***********************************************************************
635 * IsBadWritePtr (KERNEL32.@)
637 * Check for write access on a memory block.
640 * ptr [I] Address of memory block.
641 * size [I] Size of block in bytes.
645 * Failure: FALSE. Process has write access to entire block.
647 BOOL WINAPI
IsBadWritePtr( LPVOID ptr
, UINT size
)
649 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
650 if (!ptr
) return TRUE
;
652 if (!page_size
) page_size
= getpagesize();
655 volatile char *p
= ptr
;
658 while (count
> page_size
)
669 TRACE_(seh
)("%p caused page fault during write\n", ptr
);
677 /***********************************************************************
678 * IsBadHugeReadPtr (KERNEL32.@)
680 * Check for read access on a memory block.
683 * ptr [I] Address of memory block.
684 * size [I] Size of block.
688 * Failure: FALSE. Process has read access to entire block.
690 BOOL WINAPI
IsBadHugeReadPtr( LPCVOID ptr
, UINT size
)
692 return IsBadReadPtr( ptr
, size
);
696 /***********************************************************************
697 * IsBadHugeWritePtr (KERNEL32.@)
699 * Check for write access on a memory block.
702 * ptr [I] Address of memory block.
703 * size [I] Size of block.
707 * Failure: FALSE. Process has write access to entire block.
709 BOOL WINAPI
IsBadHugeWritePtr( LPVOID ptr
, UINT size
)
711 return IsBadWritePtr( ptr
, size
);
715 /***********************************************************************
716 * IsBadCodePtr (KERNEL32.@)
718 * Check for read access on a memory address.
721 * ptr [I] Address of function.
725 * Failure: FALSE. Process has read access to specified memory.
727 BOOL WINAPI
IsBadCodePtr( FARPROC ptr
)
729 return IsBadReadPtr( ptr
, 1 );
733 /***********************************************************************
734 * IsBadStringPtrA (KERNEL32.@)
736 * Check for read access on a range of memory pointed to by a string pointer.
739 * str [I] Address of string.
740 * max [I] Maximum size of string.
744 * Failure: FALSE. Read access to all bytes in string.
746 BOOL WINAPI
IsBadStringPtrA( LPCSTR str
, UINT max
)
748 if (!str
) return TRUE
;
752 volatile const char *p
= str
;
753 while (p
!= str
+ max
) if (!*p
++) break;
757 TRACE_(seh
)("%p caused page fault during read\n", str
);
765 /***********************************************************************
766 * IsBadStringPtrW (KERNEL32.@)
768 * See IsBadStringPtrA.
770 BOOL WINAPI
IsBadStringPtrW( LPCWSTR str
, UINT max
)
772 if (!str
) return TRUE
;
776 volatile const WCHAR
*p
= str
;
777 while (p
!= str
+ max
) if (!*p
++) break;
781 TRACE_(seh
)("%p caused page fault during read\n", str
);