ntdll: Implement ProcessImageFileNameWin32 in NtQueryInformationProcess.
[wine.git] / dlls / kernel32 / virtual.c
blobc7a15cdcbbf80cbead804fe255283ec4998e85f2
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <fcntl.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
33 #include "ntstatus.h"
34 #define WIN32_NO_STATUS
35 #define NONAMELESSUNION
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winnls.h"
39 #include "winternl.h"
40 #include "winerror.h"
41 #include "psapi.h"
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.
56 * PARAMS
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.
62 * RETURNS
63 * Success: Base address of allocated region of pages.
64 * Failure: NULL.
66 LPVOID WINAPI DECLSPEC_HOTPATCH VirtualAlloc( void *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.
77 * PARAMS
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.
85 * RETURNS
86 * Success: Base address of allocated region of pages.
87 * Failure: NULL.
89 LPVOID WINAPI VirtualAllocEx( HANDLE hProcess, LPVOID addr, SIZE_T size,
90 DWORD type, DWORD protect )
92 LPVOID ret = addr;
93 NTSTATUS status;
95 if ((status = NtAllocateVirtualMemory( hProcess, &ret, 0, &size, type, protect )))
97 SetLastError( RtlNtStatusToDosError(status) );
98 ret = NULL;
100 return ret;
104 /***********************************************************************
105 * VirtualFree (KERNEL32.@)
107 * Releases or decommits a region of pages in virtual address space.
109 * PARAMS
110 * addr [I] Address of region of committed pages.
111 * size [I] Size of region.
112 * type [I] Type of operation.
114 * RETURNS
115 * Success: TRUE.
116 * Failure: FALSE.
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.
129 * PARAMS
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.
135 * RETURNS
136 * Success: TRUE.
137 * Failure: FALSE.
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) );
143 return !status;
147 /***********************************************************************
148 * VirtualLock (KERNEL32.@)
150 * Locks the specified region of virtual address space.
152 * PARAMS
153 * addr [I] Address of first byte of range to lock.
154 * size [I] Number of bytes in range to lock.
156 * RETURNS
157 * Success: TRUE.
158 * Failure: FALSE.
160 * NOTES
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) );
168 return !status;
172 /***********************************************************************
173 * VirtualUnlock (KERNEL32.@)
175 * Unlocks a range of pages in the virtual address space.
177 * PARAMS
178 * addr [I] Address of first byte of range.
179 * size [I] Number of bytes in range.
181 * RETURNS
182 * Success: TRUE.
183 * Failure: FALSE.
185 * NOTES
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) );
193 return !status;
197 /***********************************************************************
198 * VirtualProtect (KERNEL32.@)
200 * Changes the access protection on a region of committed pages.
202 * PARAMS
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.
208 * RETURNS
209 * Success: TRUE.
210 * Failure: FALSE.
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.
224 * PARAMS
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.
231 * RETURNS
232 * Success: TRUE.
233 * Failure: FALSE.
235 BOOL WINAPI VirtualProtectEx( HANDLE process, LPVOID addr, SIZE_T size,
236 DWORD new_prot, LPDWORD old_prot )
238 NTSTATUS status;
239 DWORD prot;
241 /* Win9x allows passing NULL as old_prot while this fails on NT */
242 if (!old_prot && (GetVersion() & 0x80000000)) old_prot = &prot;
244 status = NtProtectVirtualMemory( process, &addr, &size, new_prot, old_prot );
245 if (status) SetLastError( RtlNtStatusToDosError(status) );
246 return !status;
250 /***********************************************************************
251 * VirtualQuery (KERNEL32.@)
253 * Provides info about a range of pages in virtual address space.
255 * PARAMS
256 * addr [I] Address of region.
257 * info [O] Address of info buffer.
258 * len [I] Size of buffer.
260 * RETURNS
261 * Number of bytes returned in information buffer or 0 if
262 * addr >= 0xc0000000 (kernel space).
264 SIZE_T WINAPI VirtualQuery( LPCVOID addr, PMEMORY_BASIC_INFORMATION info,
265 SIZE_T len )
267 return VirtualQueryEx( GetCurrentProcess(), addr, info, len );
271 /***********************************************************************
272 * VirtualQueryEx (KERNEL32.@)
274 * Provides info about a range of pages in virtual address space of a
275 * specified process.
277 * PARAMS
278 * process [I] Handle to process.
279 * addr [I] Address of region.
280 * info [O] Address of info buffer.
281 * len [I] Size of buffer.
283 * RETURNS
284 * Number of bytes returned in information buffer.
286 SIZE_T WINAPI VirtualQueryEx( HANDLE process, LPCVOID addr,
287 PMEMORY_BASIC_INFORMATION info, SIZE_T len )
289 SIZE_T ret;
290 NTSTATUS status;
292 if ((status = NtQueryVirtualMemory( process, addr, MemoryBasicInformation, info, len, &ret )))
294 SetLastError( RtlNtStatusToDosError(status) );
295 ret = 0;
297 return ret;
301 /***********************************************************************
302 * MapViewOfFile (KERNEL32.@)
304 * Maps a view of a file into the address space.
306 * PARAMS
307 * mapping [I] File-mapping object to map.
308 * access [I] Access mode.
309 * offset_high [I] High-order 32 bits of file offset.
310 * offset_low [I] Low-order 32 bits of file offset.
311 * count [I] Number of bytes to map.
313 * RETURNS
314 * Success: Starting address of mapped view.
315 * Failure: NULL.
317 LPVOID WINAPI DECLSPEC_HOTPATCH MapViewOfFile( HANDLE mapping, DWORD access,
318 DWORD offset_high, DWORD offset_low, SIZE_T count )
320 return MapViewOfFileEx( mapping, access, offset_high,
321 offset_low, count, NULL );
325 /***********************************************************************
326 * MapViewOfFileEx (KERNEL32.@)
328 * Maps a view of a file into the address space.
330 * PARAMS
331 * handle [I] File-mapping object to map.
332 * access [I] Access mode.
333 * offset_high [I] High-order 32 bits of file offset.
334 * offset_low [I] Low-order 32 bits of file offset.
335 * count [I] Number of bytes to map.
336 * addr [I] Suggested starting address for mapped view.
338 * RETURNS
339 * Success: Starting address of mapped view.
340 * Failure: NULL.
342 LPVOID WINAPI MapViewOfFileEx( HANDLE handle, DWORD access,
343 DWORD offset_high, DWORD offset_low, SIZE_T count, LPVOID addr )
345 NTSTATUS status;
346 LARGE_INTEGER offset;
347 ULONG protect;
348 BOOL exec;
350 offset.u.LowPart = offset_low;
351 offset.u.HighPart = offset_high;
353 exec = access & FILE_MAP_EXECUTE;
354 access &= ~FILE_MAP_EXECUTE;
356 if (access == FILE_MAP_COPY)
357 protect = exec ? PAGE_EXECUTE_WRITECOPY : PAGE_WRITECOPY;
358 else if (access & FILE_MAP_WRITE)
359 protect = exec ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
360 else if (access & FILE_MAP_READ)
361 protect = exec ? PAGE_EXECUTE_READ : PAGE_READONLY;
362 else protect = PAGE_NOACCESS;
364 if ((status = NtMapViewOfSection( handle, GetCurrentProcess(), &addr, 0, 0, &offset,
365 &count, ViewShare, 0, protect )) < 0)
367 SetLastError( RtlNtStatusToDosError(status) );
368 addr = NULL;
370 return addr;
374 /***********************************************************************
375 * UnmapViewOfFile (KERNEL32.@)
377 * Unmaps a mapped view of a file.
379 * PARAMS
380 * addr [I] Address where mapped view begins.
382 * RETURNS
383 * Success: TRUE.
384 * Failure: FALSE.
387 BOOL WINAPI UnmapViewOfFile( LPCVOID addr )
389 NTSTATUS status;
391 if (GetVersion() & 0x80000000)
393 MEMORY_BASIC_INFORMATION info;
394 if (!VirtualQuery( addr, &info, sizeof(info) ) || info.AllocationBase != addr)
396 SetLastError( ERROR_INVALID_ADDRESS );
397 return FALSE;
401 status = NtUnmapViewOfSection( GetCurrentProcess(), (void *)addr );
402 if (status) SetLastError( RtlNtStatusToDosError(status) );
403 return !status;
407 /***********************************************************************
408 * FlushViewOfFile (KERNEL32.@)
410 * Writes to the disk a byte range within a mapped view of a file.
412 * PARAMS
413 * base [I] Start address of byte range to flush.
414 * size [I] Number of bytes in range.
416 * RETURNS
417 * Success: TRUE.
418 * Failure: FALSE.
420 BOOL WINAPI FlushViewOfFile( LPCVOID base, SIZE_T size )
422 NTSTATUS status = NtFlushVirtualMemory( GetCurrentProcess(), &base, &size, 0 );
423 if (status)
425 if (status == STATUS_NOT_MAPPED_DATA) status = STATUS_SUCCESS;
426 else SetLastError( RtlNtStatusToDosError(status) );
428 return !status;
432 /***********************************************************************
433 * GetWriteWatch (KERNEL32.@)
435 UINT WINAPI GetWriteWatch( DWORD flags, LPVOID base, SIZE_T size, LPVOID *addresses,
436 ULONG_PTR *count, ULONG *granularity )
438 NTSTATUS status;
440 status = NtGetWriteWatch( GetCurrentProcess(), flags, base, size, addresses, count, granularity );
441 if (status) SetLastError( RtlNtStatusToDosError(status) );
442 return status ? ~0u : 0;
446 /***********************************************************************
447 * ResetWriteWatch (KERNEL32.@)
449 UINT WINAPI ResetWriteWatch( LPVOID base, SIZE_T size )
451 NTSTATUS status;
453 status = NtResetWriteWatch( GetCurrentProcess(), base, size );
454 if (status) SetLastError( RtlNtStatusToDosError(status) );
455 return status ? ~0u : 0;
459 /***********************************************************************
460 * IsBadReadPtr (KERNEL32.@)
462 * Check for read access on a memory block.
464 * ptr [I] Address of memory block.
465 * size [I] Size of block.
467 * RETURNS
468 * Success: TRUE.
469 * Failure: FALSE. Process has read access to entire block.
471 BOOL WINAPI IsBadReadPtr( LPCVOID ptr, UINT_PTR size )
473 if (!size) return FALSE; /* handle 0 size case w/o reference */
474 if (!ptr) return TRUE;
475 __TRY
477 volatile const char *p = ptr;
478 char dummy __attribute__((unused));
479 UINT_PTR count = size;
481 while (count > system_info.PageSize)
483 dummy = *p;
484 p += system_info.PageSize;
485 count -= system_info.PageSize;
487 dummy = p[0];
488 dummy = p[count - 1];
490 __EXCEPT_PAGE_FAULT
492 TRACE_(seh)("%p caused page fault during read\n", ptr);
493 return TRUE;
495 __ENDTRY
496 return FALSE;
500 /***********************************************************************
501 * IsBadWritePtr (KERNEL32.@)
503 * Check for write access on a memory block.
505 * PARAMS
506 * ptr [I] Address of memory block.
507 * size [I] Size of block in bytes.
509 * RETURNS
510 * Success: TRUE.
511 * Failure: FALSE. Process has write access to entire block.
513 BOOL WINAPI IsBadWritePtr( LPVOID ptr, UINT_PTR size )
515 if (!size) return FALSE; /* handle 0 size case w/o reference */
516 if (!ptr) return TRUE;
517 __TRY
519 volatile char *p = ptr;
520 UINT_PTR count = size;
522 while (count > system_info.PageSize)
524 *p |= 0;
525 p += system_info.PageSize;
526 count -= system_info.PageSize;
528 p[0] |= 0;
529 p[count - 1] |= 0;
531 __EXCEPT_PAGE_FAULT
533 TRACE_(seh)("%p caused page fault during write\n", ptr);
534 return TRUE;
536 __ENDTRY
537 return FALSE;
541 /***********************************************************************
542 * IsBadHugeReadPtr (KERNEL32.@)
544 * Check for read access on a memory block.
546 * PARAMS
547 * ptr [I] Address of memory block.
548 * size [I] Size of block.
550 * RETURNS
551 * Success: TRUE.
552 * Failure: FALSE. Process has read access to entire block.
554 BOOL WINAPI IsBadHugeReadPtr( LPCVOID ptr, UINT_PTR size )
556 return IsBadReadPtr( ptr, size );
560 /***********************************************************************
561 * IsBadHugeWritePtr (KERNEL32.@)
563 * Check for write access on a memory block.
565 * PARAMS
566 * ptr [I] Address of memory block.
567 * size [I] Size of block.
569 * RETURNS
570 * Success: TRUE.
571 * Failure: FALSE. Process has write access to entire block.
573 BOOL WINAPI IsBadHugeWritePtr( LPVOID ptr, UINT_PTR size )
575 return IsBadWritePtr( ptr, size );
579 /***********************************************************************
580 * IsBadCodePtr (KERNEL32.@)
582 * Check for read access on a memory address.
584 * PARAMS
585 * ptr [I] Address of function.
587 * RETURNS
588 * Success: TRUE.
589 * Failure: FALSE. Process has read access to specified memory.
591 BOOL WINAPI IsBadCodePtr( FARPROC ptr )
593 return IsBadReadPtr( ptr, 1 );
597 /***********************************************************************
598 * IsBadStringPtrA (KERNEL32.@)
600 * Check for read access on a range of memory pointed to by a string pointer.
602 * PARAMS
603 * str [I] Address of string.
604 * max [I] Maximum size of string.
606 * RETURNS
607 * Success: TRUE.
608 * Failure: FALSE. Read access to all bytes in string.
610 BOOL WINAPI IsBadStringPtrA( LPCSTR str, UINT_PTR max )
612 if (!str) return TRUE;
614 __TRY
616 volatile const char *p = str;
617 while (p != str + max) if (!*p++) break;
619 __EXCEPT_PAGE_FAULT
621 TRACE_(seh)("%p caused page fault during read\n", str);
622 return TRUE;
624 __ENDTRY
625 return FALSE;
629 /***********************************************************************
630 * IsBadStringPtrW (KERNEL32.@)
632 * See IsBadStringPtrA.
634 BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT_PTR max )
636 if (!str) return TRUE;
638 __TRY
640 volatile const WCHAR *p = str;
641 while (p != str + max) if (!*p++) break;
643 __EXCEPT_PAGE_FAULT
645 TRACE_(seh)("%p caused page fault during read\n", str);
646 return TRUE;
648 __ENDTRY
649 return FALSE;
652 /***********************************************************************
653 * K32GetMappedFileNameA (KERNEL32.@)
655 DWORD WINAPI K32GetMappedFileNameA(HANDLE process, LPVOID lpv, LPSTR file_name, DWORD size)
657 FIXME_(file)("(%p, %p, %p, %d): stub\n", process, lpv, file_name, size);
659 if (file_name && size)
660 file_name[0] = '\0';
662 return 0;
665 /***********************************************************************
666 * K32GetMappedFileNameW (KERNEL32.@)
668 DWORD WINAPI K32GetMappedFileNameW(HANDLE process, LPVOID lpv, LPWSTR file_name, DWORD size)
670 FIXME_(file)("(%p, %p, %p, %d): stub\n", process, lpv, file_name, size);
672 if (file_name && size)
673 file_name[0] = '\0';
675 return 0;
678 /***********************************************************************
679 * K32EnumPageFilesA (KERNEL32.@)
681 BOOL WINAPI K32EnumPageFilesA( PENUM_PAGE_FILE_CALLBACKA callback, LPVOID context )
683 FIXME_(file)("(%p, %p) stub\n", callback, context );
684 return FALSE;
687 /***********************************************************************
688 * K32EnumPageFilesW (KERNEL32.@)
690 BOOL WINAPI K32EnumPageFilesW( PENUM_PAGE_FILE_CALLBACKW callback, LPVOID context )
692 FIXME_(file)("(%p, %p) stub\n", callback, context );
693 return FALSE;
696 /***********************************************************************
697 * K32GetWsChanges (KERNEL32.@)
699 BOOL WINAPI K32GetWsChanges(HANDLE process, PPSAPI_WS_WATCH_INFORMATION watchinfo, DWORD size)
701 NTSTATUS status;
703 TRACE_(seh)("(%p, %p, %d)\n", process, watchinfo, size);
705 status = NtQueryInformationProcess( process, ProcessWorkingSetWatch, watchinfo, size, NULL );
707 if (status)
709 SetLastError( RtlNtStatusToDosError( status ) );
710 return FALSE;
712 return TRUE;
715 /***********************************************************************
716 * K32InitializeProcessForWsWatch (KERNEL32.@)
718 BOOL WINAPI K32InitializeProcessForWsWatch(HANDLE process)
720 FIXME_(seh)("(process=%p): stub\n", process);
722 return TRUE;