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>
36 #include "wine/exception.h"
37 #include "msvcrt/excpt.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
42 static unsigned int page_size
;
44 /* filter for page-fault exceptions */
45 static WINE_EXCEPTION_FILTER(page_fault
)
47 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
48 return EXCEPTION_EXECUTE_HANDLER
;
49 return EXCEPTION_CONTINUE_SEARCH
;
53 /***********************************************************************
54 * VirtualAlloc (KERNEL32.@)
55 * Reserves or commits a region of pages in virtual address space
58 * Base address of allocated region of pages
61 LPVOID WINAPI
VirtualAlloc(
62 LPVOID addr
, /* [in] Address of region to reserve or commit */
63 SIZE_T size
, /* [in] Size of region */
64 DWORD type
, /* [in] Type of allocation */
65 DWORD protect
)/* [in] Type of access protection */
67 return VirtualAllocEx( GetCurrentProcess(), addr
, size
, type
, protect
);
71 /***********************************************************************
72 * VirtualAllocEx (KERNEL32.@)
74 * Seems to be just as VirtualAlloc, but with process handle.
76 LPVOID WINAPI
VirtualAllocEx(
77 HANDLE hProcess
, /* [in] Handle of process to do mem operation */
78 LPVOID addr
, /* [in] Address of region to reserve or commit */
79 SIZE_T size
, /* [in] Size of region */
80 DWORD type
, /* [in] Type of allocation */
81 DWORD protect
) /* [in] Type of access protection */
86 if ((status
= NtAllocateVirtualMemory( hProcess
, &ret
, addr
, &size
, type
, protect
)))
88 SetLastError( RtlNtStatusToDosError(status
) );
95 /***********************************************************************
96 * VirtualFree (KERNEL32.@)
97 * Release or decommits a region of pages in virtual address space.
103 BOOL WINAPI
VirtualFree(
104 LPVOID addr
, /* [in] Address of region of committed pages */
105 SIZE_T size
, /* [in] Size of region */
106 DWORD type
/* [in] Type of operation */
108 return VirtualFreeEx( GetCurrentProcess(), addr
, size
, type
);
112 /***********************************************************************
113 * VirtualFreeEx (KERNEL32.@)
114 * Release or decommits a region of pages in virtual address space.
120 BOOL WINAPI
VirtualFreeEx( HANDLE process
, LPVOID addr
, SIZE_T size
, DWORD type
)
122 NTSTATUS status
= NtFreeVirtualMemory( process
, &addr
, &size
, type
);
123 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
128 /***********************************************************************
129 * VirtualLock (KERNEL32.@)
130 * Locks the specified region of virtual address space
133 * Always returns TRUE
139 BOOL WINAPI
VirtualLock( LPVOID addr
, /* [in] Address of first byte of range to lock */
140 SIZE_T size
) /* [in] Number of bytes in range to lock */
142 NTSTATUS status
= NtLockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
143 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
148 /***********************************************************************
149 * VirtualUnlock (KERNEL32.@)
150 * Unlocks a range of pages in the virtual address space
153 * Always returns TRUE
159 BOOL WINAPI
VirtualUnlock( LPVOID addr
, /* [in] Address of first byte of range */
160 SIZE_T size
) /* [in] Number of bytes in range */
162 NTSTATUS status
= NtUnlockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
163 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
168 /***********************************************************************
169 * VirtualProtect (KERNEL32.@)
170 * Changes the access protection on a region of committed pages
176 BOOL WINAPI
VirtualProtect(
177 LPVOID addr
, /* [in] Address of region of committed pages */
178 SIZE_T size
, /* [in] Size of region */
179 DWORD new_prot
, /* [in] Desired access protection */
180 LPDWORD old_prot
/* [out] Address of variable to get old protection */
182 return VirtualProtectEx( GetCurrentProcess(), addr
, size
, new_prot
, old_prot
);
186 /***********************************************************************
187 * VirtualProtectEx (KERNEL32.@)
188 * Changes the access protection on a region of committed pages in the
189 * virtual address space of a specified process
195 BOOL WINAPI
VirtualProtectEx(
196 HANDLE process
, /* [in] Handle of process */
197 LPVOID addr
, /* [in] Address of region of committed pages */
198 SIZE_T size
, /* [in] Size of region */
199 DWORD new_prot
, /* [in] Desired access protection */
200 LPDWORD old_prot
/* [out] Address of variable to get old protection */ )
202 NTSTATUS status
= NtProtectVirtualMemory( process
, &addr
, &size
, new_prot
, old_prot
);
203 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
208 /***********************************************************************
209 * VirtualQuery (KERNEL32.@)
210 * Provides info about a range of pages in virtual address space
213 * Number of bytes returned in information buffer
214 * or 0 if addr is >= 0xc0000000 (kernel space).
216 SIZE_T WINAPI
VirtualQuery(
217 LPCVOID addr
, /* [in] Address of region */
218 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
219 SIZE_T len
/* [in] Size of buffer */
221 return VirtualQueryEx( GetCurrentProcess(), addr
, info
, len
);
225 /***********************************************************************
226 * VirtualQueryEx (KERNEL32.@)
227 * Provides info about a range of pages in virtual address space of a
231 * Number of bytes returned in information buffer
233 SIZE_T WINAPI
VirtualQueryEx(
234 HANDLE process
, /* [in] Handle of process */
235 LPCVOID addr
, /* [in] Address of region */
236 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
237 SIZE_T len
/* [in] Size of buffer */ )
242 if ((status
= NtQueryVirtualMemory( process
, addr
, MemoryBasicInformation
, info
, len
, &ret
)))
244 SetLastError( RtlNtStatusToDosError(status
) );
251 /***********************************************************************
252 * CreateFileMappingA (KERNEL32.@)
253 * Creates a named or unnamed file-mapping object for the specified file
257 * 0: Mapping object does not exist
260 HANDLE WINAPI
CreateFileMappingA(
261 HANDLE hFile
, /* [in] Handle of file to map */
262 SECURITY_ATTRIBUTES
*sa
, /* [in] Optional security attributes*/
263 DWORD protect
, /* [in] Protection for mapping object */
264 DWORD size_high
, /* [in] High-order 32 bits of object size */
265 DWORD size_low
, /* [in] Low-order 32 bits of object size */
266 LPCSTR name
/* [in] Name of file-mapping object */ )
268 WCHAR buffer
[MAX_PATH
];
270 if (!name
) return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, NULL
);
272 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
274 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
277 return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, buffer
);
281 /***********************************************************************
282 * CreateFileMappingW (KERNEL32.@)
283 * See CreateFileMappingA
285 HANDLE WINAPI
CreateFileMappingW( HANDLE hFile
, LPSECURITY_ATTRIBUTES sa
,
286 DWORD protect
, DWORD size_high
,
287 DWORD size_low
, LPCWSTR name
)
289 static const int sec_flags
= SEC_FILE
| SEC_IMAGE
| SEC_RESERVE
| SEC_COMMIT
| SEC_NOCACHE
;
292 OBJECT_ATTRIBUTES attr
;
293 UNICODE_STRING nameW
;
295 DWORD access
, sec_type
;
298 attr
.Length
= sizeof(attr
);
299 attr
.RootDirectory
= 0;
300 attr
.ObjectName
= NULL
;
301 attr
.Attributes
= (sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0;
302 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
303 attr
.SecurityQualityOfService
= NULL
;
307 RtlInitUnicodeString( &nameW
, name
);
308 attr
.ObjectName
= &nameW
;
311 sec_type
= protect
& sec_flags
;
312 protect
&= ~sec_flags
;
313 if (!sec_type
) sec_type
= SEC_COMMIT
;
320 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
;
323 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
;
326 SetLastError( ERROR_INVALID_PARAMETER
);
330 if (hFile
== INVALID_HANDLE_VALUE
)
333 if (!size_low
&& !size_high
)
335 SetLastError( ERROR_INVALID_PARAMETER
);
340 size
.s
.LowPart
= size_low
;
341 size
.s
.HighPart
= size_high
;
343 status
= NtCreateSection( &ret
, access
, &attr
, &size
, protect
, sec_type
, hFile
);
344 SetLastError( RtlNtStatusToDosError(status
) );
349 /***********************************************************************
350 * OpenFileMappingA (KERNEL32.@)
351 * Opens a named file-mapping object.
357 HANDLE WINAPI
OpenFileMappingA(
358 DWORD access
, /* [in] Access mode */
359 BOOL inherit
, /* [in] Inherit flag */
360 LPCSTR name
) /* [in] Name of file-mapping object */
362 WCHAR buffer
[MAX_PATH
];
364 if (!name
) return OpenFileMappingW( access
, inherit
, NULL
);
366 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
368 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
371 return OpenFileMappingW( access
, inherit
, buffer
);
375 /***********************************************************************
376 * OpenFileMappingW (KERNEL32.@)
377 * See OpenFileMappingA
379 HANDLE WINAPI
OpenFileMappingW( DWORD access
, BOOL inherit
, LPCWSTR name
)
381 OBJECT_ATTRIBUTES attr
;
382 UNICODE_STRING nameW
;
388 SetLastError( ERROR_INVALID_PARAMETER
);
391 attr
.Length
= sizeof(attr
);
392 attr
.RootDirectory
= 0;
393 attr
.ObjectName
= &nameW
;
394 attr
.Attributes
= inherit
? OBJ_INHERIT
: 0;
395 attr
.SecurityDescriptor
= NULL
;
396 attr
.SecurityQualityOfService
= NULL
;
397 RtlInitUnicodeString( &nameW
, name
);
399 if (access
== FILE_MAP_COPY
) access
= FILE_MAP_READ
;
401 if ((status
= NtOpenSection( &ret
, access
, &attr
)))
403 SetLastError( RtlNtStatusToDosError(status
) );
410 /***********************************************************************
411 * MapViewOfFile (KERNEL32.@)
412 * Maps a view of a file into the address space
415 * Starting address of mapped view
418 LPVOID WINAPI
MapViewOfFile(
419 HANDLE mapping
, /* [in] File-mapping object to map */
420 DWORD access
, /* [in] Access mode */
421 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
422 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
423 SIZE_T count
/* [in] Number of bytes to map */
425 return MapViewOfFileEx( mapping
, access
, offset_high
,
426 offset_low
, count
, NULL
);
430 /***********************************************************************
431 * MapViewOfFileEx (KERNEL32.@)
432 * Maps a view of a file into the address space
435 * Starting address of mapped view
438 LPVOID WINAPI
MapViewOfFileEx(
439 HANDLE handle
, /* [in] File-mapping object to map */
440 DWORD access
, /* [in] Access mode */
441 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
442 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
443 SIZE_T count
, /* [in] Number of bytes to map */
444 LPVOID addr
/* [in] Suggested starting address for mapped view */
447 LARGE_INTEGER offset
;
450 offset
.s
.LowPart
= offset_low
;
451 offset
.s
.HighPart
= offset_high
;
453 if (access
& FILE_MAP_WRITE
) protect
= PAGE_READWRITE
;
454 else if (access
& FILE_MAP_READ
) protect
= PAGE_READONLY
;
455 else if (access
& FILE_MAP_COPY
) protect
= PAGE_WRITECOPY
;
456 else protect
= PAGE_NOACCESS
;
458 if ((status
= NtMapViewOfSection( handle
, GetCurrentProcess(), &addr
, 0, 0, &offset
,
459 &count
, ViewShare
, 0, protect
)))
461 SetLastError( RtlNtStatusToDosError(status
) );
468 /***********************************************************************
469 * UnmapViewOfFile (KERNEL32.@)
470 * Unmaps a mapped view of a file.
473 * Should addr be an LPCVOID?
479 BOOL WINAPI
UnmapViewOfFile( LPVOID addr
) /* [in] Address where mapped view begins */
481 NTSTATUS status
= NtUnmapViewOfSection( GetCurrentProcess(), addr
);
482 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
487 /***********************************************************************
488 * FlushViewOfFile (KERNEL32.@)
489 * Writes to the disk a byte range within a mapped view of a file
495 BOOL WINAPI
FlushViewOfFile( LPCVOID base
, /* [in] Start address of byte range to flush */
496 SIZE_T size
) /* [in] Number of bytes in range */
498 NTSTATUS status
= NtFlushVirtualMemory( GetCurrentProcess(), &base
, &size
, 0 );
501 if (status
== STATUS_NOT_MAPPED_DATA
) status
= STATUS_SUCCESS
;
502 else SetLastError( RtlNtStatusToDosError(status
) );
508 /***********************************************************************
509 * IsBadReadPtr (KERNEL32.@)
512 * FALSE: Process has read access to entire block
515 BOOL WINAPI
IsBadReadPtr(
516 LPCVOID ptr
, /* [in] Address of memory block */
517 UINT size
) /* [in] Size of block */
519 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
520 if (!page_size
) page_size
= getpagesize();
523 volatile const char *p
= ptr
;
527 while (count
> page_size
)
534 dummy
= p
[count
- 1];
536 __EXCEPT(page_fault
) { return TRUE
; }
542 /***********************************************************************
543 * IsBadWritePtr (KERNEL32.@)
546 * FALSE: Process has write access to entire block
549 BOOL WINAPI
IsBadWritePtr(
550 LPVOID ptr
, /* [in] Address of memory block */
551 UINT size
) /* [in] Size of block in bytes */
553 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
554 if (!page_size
) page_size
= getpagesize();
557 volatile char *p
= ptr
;
560 while (count
> page_size
)
569 __EXCEPT(page_fault
) { return TRUE
; }
575 /***********************************************************************
576 * IsBadHugeReadPtr (KERNEL32.@)
578 * FALSE: Process has read access to entire block
581 BOOL WINAPI
IsBadHugeReadPtr(
582 LPCVOID ptr
, /* [in] Address of memory block */
583 UINT size
/* [in] Size of block */
585 return IsBadReadPtr( ptr
, size
);
589 /***********************************************************************
590 * IsBadHugeWritePtr (KERNEL32.@)
592 * FALSE: Process has write access to entire block
595 BOOL WINAPI
IsBadHugeWritePtr(
596 LPVOID ptr
, /* [in] Address of memory block */
597 UINT size
/* [in] Size of block */
599 return IsBadWritePtr( ptr
, size
);
603 /***********************************************************************
604 * IsBadCodePtr (KERNEL32.@)
607 * FALSE: Process has read access to specified memory
610 BOOL WINAPI
IsBadCodePtr( FARPROC ptr
) /* [in] Address of function */
612 return IsBadReadPtr( ptr
, 1 );
616 /***********************************************************************
617 * IsBadStringPtrA (KERNEL32.@)
620 * FALSE: Read access to all bytes in string
623 BOOL WINAPI
IsBadStringPtrA(
624 LPCSTR str
, /* [in] Address of string */
625 UINT max
) /* [in] Maximum size of string */
629 volatile const char *p
= str
;
630 while (p
!= str
+ max
) if (!*p
++) break;
632 __EXCEPT(page_fault
) { return TRUE
; }
638 /***********************************************************************
639 * IsBadStringPtrW (KERNEL32.@)
640 * See IsBadStringPtrA
642 BOOL WINAPI
IsBadStringPtrW( LPCWSTR str
, UINT max
)
646 volatile const WCHAR
*p
= str
;
647 while (p
!= str
+ max
) if (!*p
++) break;
649 __EXCEPT(page_fault
) { return TRUE
; }