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"
44 #include "wine/debug.h"
46 #include "kernel_private.h"
48 WINE_DECLARE_DEBUG_CHANNEL(seh
);
50 static unsigned int page_size
;
53 /***********************************************************************
54 * VirtualAlloc (KERNEL32.@)
56 * Reserves or commits a region of pages in virtual address space.
59 * addr [I] Address of region to reserve or commit.
60 * size [I] Size of region.
61 * type [I] Type of allocation.
62 * protect [I] Type of access protection.
65 * Success: Base address of allocated region of pages.
68 LPVOID WINAPI
VirtualAlloc( LPVOID addr
, SIZE_T size
, DWORD type
, DWORD protect
)
70 return VirtualAllocEx( GetCurrentProcess(), addr
, size
, type
, protect
);
74 /***********************************************************************
75 * VirtualAllocEx (KERNEL32.@)
77 * Seems to be just as VirtualAlloc, but with process handle.
80 * hProcess [I] Handle to process to do mem operation.
81 * addr [I] Address of region to reserve or commit.
82 * size [I] Size of region.
83 * type [I] Type of allocation.
84 * protect [I] Type of access protection.
88 * Success: Base address of allocated region of pages.
91 LPVOID WINAPI
VirtualAllocEx( HANDLE hProcess
, LPVOID addr
, SIZE_T size
,
92 DWORD type
, DWORD protect
)
97 if ((status
= NtAllocateVirtualMemory( hProcess
, &ret
, 0, &size
, type
, protect
)))
99 SetLastError( RtlNtStatusToDosError(status
) );
106 /***********************************************************************
107 * VirtualFree (KERNEL32.@)
109 * Releases or decommits a region of pages in virtual address space.
112 * addr [I] Address of region of committed pages.
113 * size [I] Size of region.
114 * type [I] Type of operation.
120 BOOL WINAPI
VirtualFree( LPVOID addr
, SIZE_T size
, DWORD type
)
122 return VirtualFreeEx( GetCurrentProcess(), addr
, size
, type
);
126 /***********************************************************************
127 * VirtualFreeEx (KERNEL32.@)
129 * Releases or decommits a region of pages in virtual address space.
132 * process [I] Handle to process.
133 * addr [I] Address of region to free.
134 * size [I] Size of region.
135 * type [I] Type of operation.
141 BOOL WINAPI
VirtualFreeEx( HANDLE process
, LPVOID addr
, SIZE_T size
, DWORD type
)
143 NTSTATUS status
= NtFreeVirtualMemory( process
, &addr
, &size
, type
);
144 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
149 /***********************************************************************
150 * VirtualLock (KERNEL32.@)
152 * Locks the specified region of virtual address space.
155 * addr [I] Address of first byte of range to lock.
156 * size [I] Number of bytes in range to lock.
163 * Always returns TRUE.
166 BOOL WINAPI
VirtualLock( LPVOID addr
, SIZE_T size
)
168 NTSTATUS status
= NtLockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
169 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
174 /***********************************************************************
175 * VirtualUnlock (KERNEL32.@)
177 * Unlocks a range of pages in the virtual address space.
180 * addr [I] Address of first byte of range.
181 * size [I] Number of bytes in range.
188 * Always returns TRUE.
191 BOOL WINAPI
VirtualUnlock( LPVOID addr
, SIZE_T size
)
193 NTSTATUS status
= NtUnlockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
194 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
199 /***********************************************************************
200 * VirtualProtect (KERNEL32.@)
202 * Changes the access protection on a region of committed pages.
205 * addr [I] Address of region of committed pages.
206 * size [I] Size of region.
207 * new_prot [I] Desired access protection.
208 * old_prot [O] Address of variable to get old protection.
214 BOOL WINAPI
VirtualProtect( LPVOID addr
, SIZE_T size
, DWORD new_prot
, LPDWORD old_prot
)
216 return VirtualProtectEx( GetCurrentProcess(), addr
, size
, new_prot
, old_prot
);
220 /***********************************************************************
221 * VirtualProtectEx (KERNEL32.@)
223 * Changes the access protection on a region of committed pages in the
224 * virtual address space of a specified process.
227 * process [I] Handle of process.
228 * addr [I] Address of region of committed pages.
229 * size [I] Size of region.
230 * new_prot [I] Desired access protection.
231 * old_prot [O] Address of variable to get old protection.
237 BOOL WINAPI
VirtualProtectEx( HANDLE process
, LPVOID addr
, SIZE_T size
,
238 DWORD new_prot
, LPDWORD old_prot
)
240 NTSTATUS status
= NtProtectVirtualMemory( process
, &addr
, &size
, new_prot
, old_prot
);
241 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
246 /***********************************************************************
247 * VirtualQuery (KERNEL32.@)
249 * Provides info about a range of pages in virtual address space.
252 * addr [I] Address of region.
253 * info [O] Address of info buffer.
254 * len [I] Size of buffer.
257 * Number of bytes returned in information buffer or 0 if
258 * addr >= 0xc0000000 (kernel space).
260 SIZE_T WINAPI
VirtualQuery( LPCVOID addr
, PMEMORY_BASIC_INFORMATION info
,
263 return VirtualQueryEx( GetCurrentProcess(), addr
, info
, len
);
267 /***********************************************************************
268 * VirtualQueryEx (KERNEL32.@)
270 * Provides info about a range of pages in virtual address space of a
274 * process [I] Handle to process.
275 * addr [I] Address of region.
276 * info [O] Address of info buffer.
277 * len [I] Size of buffer.
280 * Number of bytes returned in information buffer.
282 SIZE_T WINAPI
VirtualQueryEx( HANDLE process
, LPCVOID addr
,
283 PMEMORY_BASIC_INFORMATION info
, SIZE_T len
)
288 if ((status
= NtQueryVirtualMemory( process
, addr
, MemoryBasicInformation
, info
, len
, &ret
)))
290 SetLastError( RtlNtStatusToDosError(status
) );
297 /***********************************************************************
298 * CreateFileMappingA (KERNEL32.@)
300 * Creates a named or unnamed file-mapping object for the specified file.
303 * hFile [I] Handle to the file to map.
304 * sa [I] Optional security attributes.
305 * protect [I] Protection for mapping object.
306 * size_high [I] High-order 32 bits of object size.
307 * size_low [I] Low-order 32 bits of object size.
308 * name [I] Name of file-mapping object.
312 * Failure: NULL. Mapping object does not exist.
314 HANDLE WINAPI
CreateFileMappingA( HANDLE hFile
, SECURITY_ATTRIBUTES
*sa
,
315 DWORD protect
, DWORD size_high
, DWORD size_low
, LPCSTR name
)
317 WCHAR buffer
[MAX_PATH
];
319 if (!name
) return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, NULL
);
321 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
323 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
326 return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, buffer
);
330 /***********************************************************************
331 * CreateFileMappingW (KERNEL32.@)
333 * See CreateFileMappingA.
335 HANDLE WINAPI
CreateFileMappingW( HANDLE hFile
, LPSECURITY_ATTRIBUTES sa
,
336 DWORD protect
, DWORD size_high
,
337 DWORD size_low
, LPCWSTR name
)
339 static const int sec_flags
= SEC_FILE
| SEC_IMAGE
| SEC_RESERVE
| SEC_COMMIT
| SEC_NOCACHE
;
342 OBJECT_ATTRIBUTES attr
;
343 UNICODE_STRING nameW
;
345 DWORD access
, sec_type
;
348 attr
.Length
= sizeof(attr
);
349 attr
.RootDirectory
= 0;
350 attr
.ObjectName
= NULL
;
351 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
|
352 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
353 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
354 attr
.SecurityQualityOfService
= NULL
;
358 RtlInitUnicodeString( &nameW
, name
);
359 attr
.ObjectName
= &nameW
;
360 attr
.RootDirectory
= get_BaseNamedObjects_handle();
363 sec_type
= protect
& sec_flags
;
364 protect
&= ~sec_flags
;
365 if (!sec_type
) sec_type
= SEC_COMMIT
;
370 protect
= PAGE_READONLY
; /* Win9x compatibility */
374 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
;
377 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
;
380 SetLastError( ERROR_INVALID_PARAMETER
);
384 if (hFile
== INVALID_HANDLE_VALUE
)
387 if (!size_low
&& !size_high
)
389 SetLastError( ERROR_INVALID_PARAMETER
);
394 size
.u
.LowPart
= size_low
;
395 size
.u
.HighPart
= size_high
;
397 status
= NtCreateSection( &ret
, access
, &attr
, &size
, protect
, sec_type
, hFile
);
398 if (status
== STATUS_OBJECT_NAME_EXISTS
)
399 SetLastError( ERROR_ALREADY_EXISTS
);
401 SetLastError( RtlNtStatusToDosError(status
) );
406 /***********************************************************************
407 * OpenFileMappingA (KERNEL32.@)
409 * Opens a named file-mapping object.
412 * access [I] Access mode.
413 * inherit [I] Inherit flag.
414 * name [I] Name of file-mapping object.
420 HANDLE WINAPI
OpenFileMappingA( DWORD access
, BOOL inherit
, LPCSTR name
)
422 WCHAR buffer
[MAX_PATH
];
424 if (!name
) return OpenFileMappingW( access
, inherit
, NULL
);
426 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
428 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
431 return OpenFileMappingW( access
, inherit
, buffer
);
435 /***********************************************************************
436 * OpenFileMappingW (KERNEL32.@)
438 * See OpenFileMappingA.
440 HANDLE WINAPI
OpenFileMappingW( DWORD access
, BOOL inherit
, LPCWSTR name
)
442 OBJECT_ATTRIBUTES attr
;
443 UNICODE_STRING nameW
;
449 SetLastError( ERROR_INVALID_PARAMETER
);
452 attr
.Length
= sizeof(attr
);
453 attr
.RootDirectory
= get_BaseNamedObjects_handle();
454 attr
.ObjectName
= &nameW
;
455 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
456 attr
.SecurityDescriptor
= NULL
;
457 attr
.SecurityQualityOfService
= NULL
;
458 RtlInitUnicodeString( &nameW
, name
);
460 if (access
== FILE_MAP_COPY
) access
= FILE_MAP_READ
;
462 if ((status
= NtOpenSection( &ret
, access
, &attr
)))
464 SetLastError( RtlNtStatusToDosError(status
) );
471 /***********************************************************************
472 * MapViewOfFile (KERNEL32.@)
474 * Maps a view of a file into the address space.
477 * mapping [I] File-mapping object to map.
478 * access [I] Access mode.
479 * offset_high [I] High-order 32 bits of file offset.
480 * offset_low [I] Low-order 32 bits of file offset.
481 * count [I] Number of bytes to map.
484 * Success: Starting address of mapped view.
487 LPVOID WINAPI
MapViewOfFile( HANDLE mapping
, DWORD access
,
488 DWORD offset_high
, DWORD offset_low
, SIZE_T count
)
490 return MapViewOfFileEx( mapping
, access
, offset_high
,
491 offset_low
, count
, NULL
);
495 /***********************************************************************
496 * MapViewOfFileEx (KERNEL32.@)
498 * Maps a view of a file into the address space.
501 * handle [I] File-mapping object to map.
502 * access [I] Access mode.
503 * offset_high [I] High-order 32 bits of file offset.
504 * offset_low [I] Low-order 32 bits of file offset.
505 * count [I] Number of bytes to map.
506 * addr [I] Suggested starting address for mapped view.
509 * Success: Starting address of mapped view.
512 LPVOID WINAPI
MapViewOfFileEx( HANDLE handle
, DWORD access
,
513 DWORD offset_high
, DWORD offset_low
, SIZE_T count
, LPVOID addr
)
516 LARGE_INTEGER offset
;
519 offset
.u
.LowPart
= offset_low
;
520 offset
.u
.HighPart
= offset_high
;
522 if (access
& FILE_MAP_WRITE
) protect
= PAGE_READWRITE
;
523 else if (access
& FILE_MAP_READ
) protect
= PAGE_READONLY
;
524 else if (access
& FILE_MAP_COPY
) protect
= PAGE_WRITECOPY
;
525 else protect
= PAGE_NOACCESS
;
527 if ((status
= NtMapViewOfSection( handle
, GetCurrentProcess(), &addr
, 0, 0, &offset
,
528 &count
, ViewShare
, 0, protect
)))
530 SetLastError( RtlNtStatusToDosError(status
) );
537 /***********************************************************************
538 * UnmapViewOfFile (KERNEL32.@)
540 * Unmaps a mapped view of a file.
543 * addr [I] Address where mapped view begins.
550 BOOL WINAPI
UnmapViewOfFile( LPCVOID addr
)
552 NTSTATUS status
= NtUnmapViewOfSection( GetCurrentProcess(), (void *)addr
);
553 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
558 /***********************************************************************
559 * FlushViewOfFile (KERNEL32.@)
561 * Writes to the disk a byte range within a mapped view of a file.
564 * base [I] Start address of byte range to flush.
565 * size [I] Number of bytes in range.
571 BOOL WINAPI
FlushViewOfFile( LPCVOID base
, SIZE_T size
)
573 NTSTATUS status
= NtFlushVirtualMemory( GetCurrentProcess(), &base
, &size
, 0 );
576 if (status
== STATUS_NOT_MAPPED_DATA
) status
= STATUS_SUCCESS
;
577 else SetLastError( RtlNtStatusToDosError(status
) );
583 /***********************************************************************
584 * IsBadReadPtr (KERNEL32.@)
586 * Check for read access on a memory block.
588 * ptr [I] Address of memory block.
589 * size [I] Size of block.
593 * Failure: FALSE. Process has read access to entire block.
595 BOOL WINAPI
IsBadReadPtr( LPCVOID ptr
, UINT size
)
597 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
598 if (!ptr
) return TRUE
;
600 if (!page_size
) page_size
= getpagesize();
603 volatile const char *p
= ptr
;
607 while (count
> page_size
)
614 dummy
= p
[count
- 1];
618 TRACE_(seh
)("%p caused page fault during read\n", ptr
);
626 /***********************************************************************
627 * IsBadWritePtr (KERNEL32.@)
629 * Check for write access on a memory block.
632 * ptr [I] Address of memory block.
633 * size [I] Size of block in bytes.
637 * Failure: FALSE. Process has write access to entire block.
639 BOOL WINAPI
IsBadWritePtr( LPVOID ptr
, UINT size
)
641 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
642 if (!ptr
) return TRUE
;
644 if (!page_size
) page_size
= getpagesize();
647 volatile char *p
= ptr
;
650 while (count
> page_size
)
661 TRACE_(seh
)("%p caused page fault during write\n", ptr
);
669 /***********************************************************************
670 * IsBadHugeReadPtr (KERNEL32.@)
672 * Check for read access on a memory block.
675 * ptr [I] Address of memory block.
676 * size [I] Size of block.
680 * Failure: FALSE. Process has read access to entire block.
682 BOOL WINAPI
IsBadHugeReadPtr( LPCVOID ptr
, UINT size
)
684 return IsBadReadPtr( ptr
, size
);
688 /***********************************************************************
689 * IsBadHugeWritePtr (KERNEL32.@)
691 * Check for write access on a memory block.
694 * ptr [I] Address of memory block.
695 * size [I] Size of block.
699 * Failure: FALSE. Process has write access to entire block.
701 BOOL WINAPI
IsBadHugeWritePtr( LPVOID ptr
, UINT size
)
703 return IsBadWritePtr( ptr
, size
);
707 /***********************************************************************
708 * IsBadCodePtr (KERNEL32.@)
710 * Check for read access on a memory address.
713 * ptr [I] Address of function.
717 * Failure: FALSE. Process has read access to specified memory.
719 BOOL WINAPI
IsBadCodePtr( FARPROC ptr
)
721 return IsBadReadPtr( ptr
, 1 );
725 /***********************************************************************
726 * IsBadStringPtrA (KERNEL32.@)
728 * Check for read access on a range of memory pointed to by a string pointer.
731 * str [I] Address of string.
732 * max [I] Maximum size of string.
736 * Failure: FALSE. Read access to all bytes in string.
738 BOOL WINAPI
IsBadStringPtrA( LPCSTR str
, UINT max
)
740 if (!str
) return TRUE
;
744 volatile const char *p
= str
;
745 while (p
!= str
+ max
) if (!*p
++) break;
749 TRACE_(seh
)("%p caused page fault during read\n", str
);
757 /***********************************************************************
758 * IsBadStringPtrW (KERNEL32.@)
760 * See IsBadStringPtrA.
762 BOOL WINAPI
IsBadStringPtrW( LPCWSTR str
, UINT max
)
764 if (!str
) return TRUE
;
768 volatile const WCHAR
*p
= str
;
769 while (p
!= str
+ max
) if (!*p
++) break;
773 TRACE_(seh
)("%p caused page fault during read\n", str
);