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"
28 #include <sys/types.h>
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
41 #include "wine/exception.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
46 WINE_DECLARE_DEBUG_CHANNEL(seh
);
48 static unsigned int page_size
;
50 /* filter for page-fault exceptions */
51 static WINE_EXCEPTION_FILTER(page_fault
)
53 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
54 return EXCEPTION_EXECUTE_HANDLER
;
55 return EXCEPTION_CONTINUE_SEARCH
;
59 /***********************************************************************
60 * VirtualAlloc (KERNEL32.@)
61 * Reserves or commits a region of pages in virtual address space
64 * Base address of allocated region of pages
67 LPVOID WINAPI
VirtualAlloc(
68 LPVOID addr
, /* [in] Address of region to reserve or commit */
69 SIZE_T size
, /* [in] Size of region */
70 DWORD type
, /* [in] Type of allocation */
71 DWORD protect
)/* [in] Type of access protection */
73 return VirtualAllocEx( GetCurrentProcess(), addr
, size
, type
, protect
);
77 /***********************************************************************
78 * VirtualAllocEx (KERNEL32.@)
80 * Seems to be just as VirtualAlloc, but with process handle.
83 * Base address of allocated region of pages
86 LPVOID WINAPI
VirtualAllocEx(
87 HANDLE hProcess
, /* [in] Handle of process to do mem operation */
88 LPVOID addr
, /* [in] Address of region to reserve or commit */
89 SIZE_T size
, /* [in] Size of region */
90 DWORD type
, /* [in] Type of allocation */
91 DWORD protect
) /* [in] Type of access protection */
96 if ((status
= NtAllocateVirtualMemory( hProcess
, &ret
, 0, &size
, type
, protect
)))
98 SetLastError( RtlNtStatusToDosError(status
) );
105 /***********************************************************************
106 * VirtualFree (KERNEL32.@)
107 * Release or decommits a region of pages in virtual address space.
113 BOOL WINAPI
VirtualFree(
114 LPVOID addr
, /* [in] Address of region of committed pages */
115 SIZE_T size
, /* [in] Size of region */
116 DWORD type
/* [in] Type of operation */
118 return VirtualFreeEx( GetCurrentProcess(), addr
, size
, type
);
122 /***********************************************************************
123 * VirtualFreeEx (KERNEL32.@)
124 * Release or decommits a region of pages in virtual address space.
130 BOOL WINAPI
VirtualFreeEx( HANDLE process
, LPVOID addr
, SIZE_T size
, DWORD type
)
132 NTSTATUS status
= NtFreeVirtualMemory( process
, &addr
, &size
, type
);
133 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
138 /***********************************************************************
139 * VirtualLock (KERNEL32.@)
140 * Locks the specified region of virtual address space
143 * Always returns TRUE
149 BOOL WINAPI
VirtualLock( LPVOID addr
, /* [in] Address of first byte of range to lock */
150 SIZE_T size
) /* [in] Number of bytes in range to lock */
152 NTSTATUS status
= NtLockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
153 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
158 /***********************************************************************
159 * VirtualUnlock (KERNEL32.@)
160 * Unlocks a range of pages in the virtual address space
163 * Always returns TRUE
169 BOOL WINAPI
VirtualUnlock( LPVOID addr
, /* [in] Address of first byte of range */
170 SIZE_T size
) /* [in] Number of bytes in range */
172 NTSTATUS status
= NtUnlockVirtualMemory( GetCurrentProcess(), &addr
, &size
, 1 );
173 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
178 /***********************************************************************
179 * VirtualProtect (KERNEL32.@)
180 * Changes the access protection on a region of committed pages
186 BOOL WINAPI
VirtualProtect(
187 LPVOID addr
, /* [in] Address of region of committed pages */
188 SIZE_T size
, /* [in] Size of region */
189 DWORD new_prot
, /* [in] Desired access protection */
190 LPDWORD old_prot
/* [out] Address of variable to get old protection */
192 return VirtualProtectEx( GetCurrentProcess(), addr
, size
, new_prot
, old_prot
);
196 /***********************************************************************
197 * VirtualProtectEx (KERNEL32.@)
198 * Changes the access protection on a region of committed pages in the
199 * virtual address space of a specified process
205 BOOL WINAPI
VirtualProtectEx(
206 HANDLE process
, /* [in] Handle of process */
207 LPVOID addr
, /* [in] Address of region of committed pages */
208 SIZE_T size
, /* [in] Size of region */
209 DWORD new_prot
, /* [in] Desired access protection */
210 LPDWORD old_prot
/* [out] Address of variable to get old protection */ )
212 NTSTATUS status
= NtProtectVirtualMemory( process
, &addr
, &size
, new_prot
, old_prot
);
213 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
218 /***********************************************************************
219 * VirtualQuery (KERNEL32.@)
220 * Provides info about a range of pages in virtual address space
223 * Number of bytes returned in information buffer
224 * or 0 if addr is >= 0xc0000000 (kernel space).
226 SIZE_T WINAPI
VirtualQuery(
227 LPCVOID addr
, /* [in] Address of region */
228 PMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
229 SIZE_T len
/* [in] Size of buffer */
231 return VirtualQueryEx( GetCurrentProcess(), addr
, info
, len
);
235 /***********************************************************************
236 * VirtualQueryEx (KERNEL32.@)
237 * Provides info about a range of pages in virtual address space of a
241 * Number of bytes returned in information buffer
243 SIZE_T WINAPI
VirtualQueryEx(
244 HANDLE process
, /* [in] Handle of process */
245 LPCVOID addr
, /* [in] Address of region */
246 PMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
247 SIZE_T len
/* [in] Size of buffer */ )
252 if ((status
= NtQueryVirtualMemory( process
, addr
, MemoryBasicInformation
, info
, len
, &ret
)))
254 SetLastError( RtlNtStatusToDosError(status
) );
261 /***********************************************************************
262 * CreateFileMappingA (KERNEL32.@)
263 * Creates a named or unnamed file-mapping object for the specified file
267 * 0: Mapping object does not exist
270 HANDLE WINAPI
CreateFileMappingA(
271 HANDLE hFile
, /* [in] Handle of file to map */
272 SECURITY_ATTRIBUTES
*sa
, /* [in] Optional security attributes*/
273 DWORD protect
, /* [in] Protection for mapping object */
274 DWORD size_high
, /* [in] High-order 32 bits of object size */
275 DWORD size_low
, /* [in] Low-order 32 bits of object size */
276 LPCSTR name
/* [in] Name of file-mapping object */ )
278 WCHAR buffer
[MAX_PATH
];
280 if (!name
) return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, NULL
);
282 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
284 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
287 return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, buffer
);
291 /***********************************************************************
292 * CreateFileMappingW (KERNEL32.@)
293 * See CreateFileMappingA.
295 HANDLE WINAPI
CreateFileMappingW( HANDLE hFile
, LPSECURITY_ATTRIBUTES sa
,
296 DWORD protect
, DWORD size_high
,
297 DWORD size_low
, LPCWSTR name
)
299 static const int sec_flags
= SEC_FILE
| SEC_IMAGE
| SEC_RESERVE
| SEC_COMMIT
| SEC_NOCACHE
;
302 OBJECT_ATTRIBUTES attr
;
303 UNICODE_STRING nameW
;
305 DWORD access
, sec_type
;
308 attr
.Length
= sizeof(attr
);
309 attr
.RootDirectory
= 0;
310 attr
.ObjectName
= NULL
;
311 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
|
312 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
313 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
314 attr
.SecurityQualityOfService
= NULL
;
318 RtlInitUnicodeString( &nameW
, name
);
319 attr
.ObjectName
= &nameW
;
322 sec_type
= protect
& sec_flags
;
323 protect
&= ~sec_flags
;
324 if (!sec_type
) sec_type
= SEC_COMMIT
;
329 protect
= PAGE_READONLY
; /* Win9x compatibility */
333 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
;
336 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
;
339 SetLastError( ERROR_INVALID_PARAMETER
);
343 if (hFile
== INVALID_HANDLE_VALUE
)
346 if (!size_low
&& !size_high
)
348 SetLastError( ERROR_INVALID_PARAMETER
);
353 size
.u
.LowPart
= size_low
;
354 size
.u
.HighPart
= size_high
;
356 status
= NtCreateSection( &ret
, access
, &attr
, &size
, protect
, sec_type
, hFile
);
357 if (status
== STATUS_OBJECT_NAME_EXISTS
)
358 SetLastError( ERROR_ALREADY_EXISTS
);
360 SetLastError( RtlNtStatusToDosError(status
) );
365 /***********************************************************************
366 * OpenFileMappingA (KERNEL32.@)
367 * Opens a named file-mapping object.
373 HANDLE WINAPI
OpenFileMappingA(
374 DWORD access
, /* [in] Access mode */
375 BOOL inherit
, /* [in] Inherit flag */
376 LPCSTR name
) /* [in] Name of file-mapping object */
378 WCHAR buffer
[MAX_PATH
];
380 if (!name
) return OpenFileMappingW( access
, inherit
, NULL
);
382 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
384 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
387 return OpenFileMappingW( access
, inherit
, buffer
);
391 /***********************************************************************
392 * OpenFileMappingW (KERNEL32.@)
393 * See OpenFileMappingA.
395 HANDLE WINAPI
OpenFileMappingW( DWORD access
, BOOL inherit
, LPCWSTR name
)
397 OBJECT_ATTRIBUTES attr
;
398 UNICODE_STRING nameW
;
404 SetLastError( ERROR_INVALID_PARAMETER
);
407 attr
.Length
= sizeof(attr
);
408 attr
.RootDirectory
= 0;
409 attr
.ObjectName
= &nameW
;
410 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
411 attr
.SecurityDescriptor
= NULL
;
412 attr
.SecurityQualityOfService
= NULL
;
413 RtlInitUnicodeString( &nameW
, name
);
415 if (access
== FILE_MAP_COPY
) access
= FILE_MAP_READ
;
417 if ((status
= NtOpenSection( &ret
, access
, &attr
)))
419 SetLastError( RtlNtStatusToDosError(status
) );
426 /***********************************************************************
427 * MapViewOfFile (KERNEL32.@)
428 * Maps a view of a file into the address space
431 * Starting address of mapped view
434 LPVOID WINAPI
MapViewOfFile(
435 HANDLE mapping
, /* [in] File-mapping object to map */
436 DWORD access
, /* [in] Access mode */
437 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
438 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
439 SIZE_T count
/* [in] Number of bytes to map */
441 return MapViewOfFileEx( mapping
, access
, offset_high
,
442 offset_low
, count
, NULL
);
446 /***********************************************************************
447 * MapViewOfFileEx (KERNEL32.@)
448 * Maps a view of a file into the address space
451 * Starting address of mapped view
454 LPVOID WINAPI
MapViewOfFileEx(
455 HANDLE handle
, /* [in] File-mapping object to map */
456 DWORD access
, /* [in] Access mode */
457 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
458 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
459 SIZE_T count
, /* [in] Number of bytes to map */
460 LPVOID addr
/* [in] Suggested starting address for mapped view */
463 LARGE_INTEGER offset
;
466 offset
.u
.LowPart
= offset_low
;
467 offset
.u
.HighPart
= offset_high
;
469 if (access
& FILE_MAP_WRITE
) protect
= PAGE_READWRITE
;
470 else if (access
& FILE_MAP_READ
) protect
= PAGE_READONLY
;
471 else if (access
& FILE_MAP_COPY
) protect
= PAGE_WRITECOPY
;
472 else protect
= PAGE_NOACCESS
;
474 if ((status
= NtMapViewOfSection( handle
, GetCurrentProcess(), &addr
, 0, 0, &offset
,
475 &count
, ViewShare
, 0, protect
)))
477 SetLastError( RtlNtStatusToDosError(status
) );
484 /***********************************************************************
485 * UnmapViewOfFile (KERNEL32.@)
486 * Unmaps a mapped view of a file.
489 * Should addr be an LPCVOID?
495 BOOL WINAPI
UnmapViewOfFile( LPVOID addr
) /* [in] Address where mapped view begins */
497 NTSTATUS status
= NtUnmapViewOfSection( GetCurrentProcess(), addr
);
498 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
503 /***********************************************************************
504 * FlushViewOfFile (KERNEL32.@)
505 * Writes to the disk a byte range within a mapped view of a file
511 BOOL WINAPI
FlushViewOfFile( LPCVOID base
, /* [in] Start address of byte range to flush */
512 SIZE_T size
) /* [in] Number of bytes in range */
514 NTSTATUS status
= NtFlushVirtualMemory( GetCurrentProcess(), &base
, &size
, 0 );
517 if (status
== STATUS_NOT_MAPPED_DATA
) status
= STATUS_SUCCESS
;
518 else SetLastError( RtlNtStatusToDosError(status
) );
524 /***********************************************************************
525 * IsBadReadPtr (KERNEL32.@)
527 * Check for read access on a memory block.
530 * FALSE: Process has read access to entire block
533 BOOL WINAPI
IsBadReadPtr(
534 LPCVOID ptr
, /* [in] Address of memory block */
535 UINT size
) /* [in] Size of block */
537 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
538 if (!ptr
) return TRUE
;
540 if (!page_size
) page_size
= getpagesize();
543 volatile const char *p
= ptr
;
547 while (count
> page_size
)
554 dummy
= p
[count
- 1];
558 TRACE_(seh
)("%p caused page fault during read\n", ptr
);
566 /***********************************************************************
567 * IsBadWritePtr (KERNEL32.@)
569 * Check for write access on a memory block.
572 * FALSE: Process has write access to entire block
575 BOOL WINAPI
IsBadWritePtr(
576 LPVOID ptr
, /* [in] Address of memory block */
577 UINT size
) /* [in] Size of block in bytes */
579 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
580 if (!ptr
) return TRUE
;
582 if (!page_size
) page_size
= getpagesize();
585 volatile char *p
= ptr
;
588 while (count
> page_size
)
599 TRACE_(seh
)("%p caused page fault during write\n", ptr
);
607 /***********************************************************************
608 * IsBadHugeReadPtr (KERNEL32.@)
610 * Check for read access on a memory block.
613 * FALSE: Process has read access to entire block
616 BOOL WINAPI
IsBadHugeReadPtr(
617 LPCVOID ptr
, /* [in] Address of memory block */
618 UINT size
/* [in] Size of block */
620 return IsBadReadPtr( ptr
, size
);
624 /***********************************************************************
625 * IsBadHugeWritePtr (KERNEL32.@)
627 * Check for write access on a memory block.
630 * FALSE: Process has write access to entire block
633 BOOL WINAPI
IsBadHugeWritePtr(
634 LPVOID ptr
, /* [in] Address of memory block */
635 UINT size
/* [in] Size of block */
637 return IsBadWritePtr( ptr
, size
);
641 /***********************************************************************
642 * IsBadCodePtr (KERNEL32.@)
644 * Check for read access on a memory address.
647 * FALSE: Process has read access to specified memory
650 BOOL WINAPI
IsBadCodePtr( FARPROC ptr
) /* [in] Address of function */
652 return IsBadReadPtr( ptr
, 1 );
656 /***********************************************************************
657 * IsBadStringPtrA (KERNEL32.@)
659 * Check for read access on a range of memory pointed to by a string pointer.
662 * FALSE: Read access to all bytes in string
665 BOOL WINAPI
IsBadStringPtrA(
666 LPCSTR str
, /* [in] Address of string */
667 UINT max
) /* [in] Maximum size of string */
669 if (!str
) return TRUE
;
673 volatile const char *p
= str
;
674 while (p
!= str
+ max
) if (!*p
++) break;
678 TRACE_(seh
)("%p caused page fault during read\n", str
);
686 /***********************************************************************
687 * IsBadStringPtrW (KERNEL32.@)
688 * See IsBadStringPtrA.
690 BOOL WINAPI
IsBadStringPtrW( LPCWSTR str
, UINT max
)
692 if (!str
) return TRUE
;
696 volatile const WCHAR
*p
= str
;
697 while (p
!= str
+ max
) if (!*p
++) break;
701 TRACE_(seh
)("%p caused page fault during read\n", str
);