msi: Constify some variables.
[wine] / dlls / kernel32 / virtual.c
1 /*
2  * Win32 virtual memory functions
3  *
4  * Copyright 1997 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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
32
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winnls.h"
40 #include "winternl.h"
41 #include "winerror.h"
42 #include "wine/exception.h"
43 #include "wine/debug.h"
44
45 #include "kernel_private.h"
46
47 WINE_DECLARE_DEBUG_CHANNEL(seh);
48
49 static unsigned int page_size;
50
51
52 /***********************************************************************
53  *             VirtualAlloc   (KERNEL32.@)
54  *
55  * Reserves or commits a region of pages in virtual address space.
56  *
57  * PARAMS
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.
62  *
63  * RETURNS
64  *      Success: Base address of allocated region of pages.
65  *      Failure: NULL.
66  */
67 LPVOID WINAPI VirtualAlloc( LPVOID addr, SIZE_T size, DWORD type, DWORD protect )
68 {
69     return VirtualAllocEx( GetCurrentProcess(), addr, size, type, protect );
70 }
71
72
73 /***********************************************************************
74  *             VirtualAllocEx   (KERNEL32.@)
75  *
76  * Seems to be just as VirtualAlloc, but with process handle.
77  *
78  * PARAMS
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.
84  *
85  *
86  * RETURNS
87  *      Success: Base address of allocated region of pages.
88  *      Failure: NULL.
89  */
90 LPVOID WINAPI VirtualAllocEx( HANDLE hProcess, LPVOID addr, SIZE_T size,
91     DWORD type, DWORD protect )
92 {
93     LPVOID ret = addr;
94     NTSTATUS status;
95
96     if ((status = NtAllocateVirtualMemory( hProcess, &ret, 0, &size, type, protect )))
97     {
98         SetLastError( RtlNtStatusToDosError(status) );
99         ret = NULL;
100     }
101     return ret;
102 }
103
104
105 /***********************************************************************
106  *             VirtualFree   (KERNEL32.@)
107  *
108  * Releases or decommits a region of pages in virtual address space.
109  *
110  * PARAMS
111  *  addr [I] Address of region of committed pages.
112  *  size [I] Size of region.
113  *  type [I] Type of operation.
114  *
115  * RETURNS
116  *      Success: TRUE.
117  *      Failure: FALSE.
118  */
119 BOOL WINAPI VirtualFree( LPVOID addr, SIZE_T size, DWORD type )
120 {
121     return VirtualFreeEx( GetCurrentProcess(), addr, size, type );
122 }
123
124
125 /***********************************************************************
126  *             VirtualFreeEx   (KERNEL32.@)
127  *
128  * Releases or decommits a region of pages in virtual address space.
129  *
130  * PARAMS
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.
135  *
136  * RETURNS
137  *      Success: TRUE.
138  *      Failure: FALSE.
139  */
140 BOOL WINAPI VirtualFreeEx( HANDLE process, LPVOID addr, SIZE_T size, DWORD type )
141 {
142     NTSTATUS status = NtFreeVirtualMemory( process, &addr, &size, type );
143     if (status) SetLastError( RtlNtStatusToDosError(status) );
144     return !status;
145 }
146
147
148 /***********************************************************************
149  *             VirtualLock   (KERNEL32.@)
150  *
151  * Locks the specified region of virtual address space.
152  *
153  * PARAMS
154  *  addr [I] Address of first byte of range to lock.
155  *  size [I] Number of bytes in range to lock.
156  *
157  * RETURNS
158  *      Success: TRUE.
159  *      Failure: FALSE.
160  *
161  * NOTES
162  *      Always returns TRUE.
163  *
164  */
165 BOOL WINAPI VirtualLock( LPVOID addr, SIZE_T size )
166 {
167     NTSTATUS status = NtLockVirtualMemory( GetCurrentProcess(), &addr, &size, 1 );
168     if (status) SetLastError( RtlNtStatusToDosError(status) );
169     return !status;
170 }
171
172
173 /***********************************************************************
174  *             VirtualUnlock   (KERNEL32.@)
175  *
176  * Unlocks a range of pages in the virtual address space.
177  *
178  * PARAMS
179  *  addr [I] Address of first byte of range.
180  *  size [I] Number of bytes in range.
181  *
182  * RETURNS
183  *      Success: TRUE.
184  *      Failure: FALSE.
185  *
186  * NOTES
187  *      Always returns TRUE.
188  *
189  */
190 BOOL WINAPI VirtualUnlock( LPVOID addr, SIZE_T size )
191 {
192     NTSTATUS status = NtUnlockVirtualMemory( GetCurrentProcess(), &addr, &size, 1 );
193     if (status) SetLastError( RtlNtStatusToDosError(status) );
194     return !status;
195 }
196
197
198 /***********************************************************************
199  *             VirtualProtect   (KERNEL32.@)
200  *
201  * Changes the access protection on a region of committed pages.
202  *
203  * PARAMS
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.
208  *
209  * RETURNS
210  *      Success: TRUE.
211  *      Failure: FALSE.
212  */
213 BOOL WINAPI VirtualProtect( LPVOID addr, SIZE_T size, DWORD new_prot, LPDWORD old_prot)
214 {
215     return VirtualProtectEx( GetCurrentProcess(), addr, size, new_prot, old_prot );
216 }
217
218
219 /***********************************************************************
220  *             VirtualProtectEx   (KERNEL32.@)
221  *
222  * Changes the access protection on a region of committed pages in the
223  * virtual address space of a specified process.
224  *
225  * PARAMS
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.
231  *
232  * RETURNS
233  *      Success: TRUE.
234  *      Failure: FALSE.
235  */
236 BOOL WINAPI VirtualProtectEx( HANDLE process, LPVOID addr, SIZE_T size,
237     DWORD new_prot, LPDWORD old_prot )
238 {
239     NTSTATUS status = NtProtectVirtualMemory( process, &addr, &size, new_prot, old_prot );
240     if (status) SetLastError( RtlNtStatusToDosError(status) );
241     return !status;
242 }
243
244
245 /***********************************************************************
246  *             VirtualQuery   (KERNEL32.@)
247  *
248  * Provides info about a range of pages in virtual address space.
249  *
250  * PARAMS
251  *  addr [I] Address of region.
252  *  info [O] Address of info buffer.
253  *  len  [I] Size of buffer.
254  *
255  * RETURNS
256  *      Number of bytes returned in information buffer or 0 if
257  *      addr >= 0xc0000000 (kernel space).
258  */
259 SIZE_T WINAPI VirtualQuery( LPCVOID addr, PMEMORY_BASIC_INFORMATION info,
260     SIZE_T len )
261 {
262     return VirtualQueryEx( GetCurrentProcess(), addr, info, len );
263 }
264
265
266 /***********************************************************************
267  *             VirtualQueryEx   (KERNEL32.@)
268  *
269  * Provides info about a range of pages in virtual address space of a
270  * specified process.
271  *
272  * PARAMS
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.
277  *
278  * RETURNS
279  *      Number of bytes returned in information buffer.
280  */
281 SIZE_T WINAPI VirtualQueryEx( HANDLE process, LPCVOID addr,
282     PMEMORY_BASIC_INFORMATION info, SIZE_T len )
283 {
284     SIZE_T ret;
285     NTSTATUS status;
286
287     if ((status = NtQueryVirtualMemory( process, addr, MemoryBasicInformation, info, len, &ret )))
288     {
289         SetLastError( RtlNtStatusToDosError(status) );
290         ret = 0;
291     }
292     return ret;
293 }
294
295
296 /***********************************************************************
297  *             CreateFileMappingA   (KERNEL32.@)
298  *
299  * Creates a named or unnamed file-mapping object for the specified file.
300  *
301  * PARAMS
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.
308  *
309  * RETURNS
310  *      Success: Handle.
311  *      Failure: NULL. Mapping object does not exist.
312  */
313 HANDLE WINAPI CreateFileMappingA( HANDLE hFile, SECURITY_ATTRIBUTES *sa,
314     DWORD protect, DWORD size_high, DWORD size_low, LPCSTR name )
315 {
316     WCHAR buffer[MAX_PATH];
317
318     if (!name) return CreateFileMappingW( hFile, sa, protect, size_high, size_low, NULL );
319
320     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
321     {
322         SetLastError( ERROR_FILENAME_EXCED_RANGE );
323         return 0;
324     }
325     return CreateFileMappingW( hFile, sa, protect, size_high, size_low, buffer );
326 }
327
328
329 /***********************************************************************
330  *             CreateFileMappingW   (KERNEL32.@)
331  *
332  * See CreateFileMappingA.
333  */
334 HANDLE WINAPI CreateFileMappingW( HANDLE hFile, LPSECURITY_ATTRIBUTES sa,
335                                   DWORD protect, DWORD size_high,
336                                   DWORD size_low, LPCWSTR name )
337 {
338     static const int sec_flags = SEC_FILE | SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_NOCACHE;
339
340     HANDLE ret;
341     OBJECT_ATTRIBUTES attr;
342     UNICODE_STRING nameW;
343     NTSTATUS status;
344     DWORD access, sec_type;
345     LARGE_INTEGER size;
346
347     attr.Length                   = sizeof(attr);
348     attr.RootDirectory            = 0;
349     attr.ObjectName               = NULL;
350     attr.Attributes               = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
351                                     ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
352     attr.SecurityDescriptor       = sa ? sa->lpSecurityDescriptor : NULL;
353     attr.SecurityQualityOfService = NULL;
354
355     if (name)
356     {
357         RtlInitUnicodeString( &nameW, name );
358         attr.ObjectName = &nameW;
359         attr.RootDirectory = get_BaseNamedObjects_handle();
360     }
361
362     sec_type = protect & sec_flags;
363     protect &= ~sec_flags;
364     if (!sec_type) sec_type = SEC_COMMIT;
365
366     switch(protect)
367     {
368     case 0:
369         protect = PAGE_READONLY;  /* Win9x compatibility */
370         /* fall through */
371     case PAGE_READONLY:
372     case PAGE_WRITECOPY:
373         access = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ;
374         break;
375     case PAGE_READWRITE:
376         access = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE;
377         break;
378     default:
379         SetLastError( ERROR_INVALID_PARAMETER );
380         return 0;
381     }
382
383     if (hFile == INVALID_HANDLE_VALUE)
384     {
385         hFile = 0;
386         if (!size_low && !size_high)
387         {
388             SetLastError( ERROR_INVALID_PARAMETER );
389             return 0;
390         }
391     }
392
393     size.u.LowPart  = size_low;
394     size.u.HighPart = size_high;
395
396     status = NtCreateSection( &ret, access, &attr, &size, protect, sec_type, hFile );
397     if (status == STATUS_OBJECT_NAME_EXISTS)
398         SetLastError( ERROR_ALREADY_EXISTS );
399     else
400         SetLastError( RtlNtStatusToDosError(status) );
401     return ret;
402 }
403
404
405 /***********************************************************************
406  *             OpenFileMappingA   (KERNEL32.@)
407  *
408  * Opens a named file-mapping object.
409  *
410  * PARAMS
411  *  access  [I] Access mode.
412  *  inherit [I] Inherit flag.
413  *  name    [I] Name of file-mapping object.
414  *
415  * RETURNS
416  *      Success: Handle.
417  *      Failure: NULL.
418  */
419 HANDLE WINAPI OpenFileMappingA( DWORD access, BOOL inherit, LPCSTR name )
420 {
421     WCHAR buffer[MAX_PATH];
422
423     if (!name) return OpenFileMappingW( access, inherit, NULL );
424
425     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
426     {
427         SetLastError( ERROR_FILENAME_EXCED_RANGE );
428         return 0;
429     }
430     return OpenFileMappingW( access, inherit, buffer );
431 }
432
433
434 /***********************************************************************
435  *             OpenFileMappingW   (KERNEL32.@)
436  *
437  * See OpenFileMappingA.
438  */
439 HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
440 {
441     OBJECT_ATTRIBUTES attr;
442     UNICODE_STRING nameW;
443     HANDLE ret;
444     NTSTATUS status;
445
446     if (!name)
447     {
448         SetLastError( ERROR_INVALID_PARAMETER );
449         return 0;
450     }
451     attr.Length = sizeof(attr);
452     attr.RootDirectory = get_BaseNamedObjects_handle();
453     attr.ObjectName = &nameW;
454     attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
455     attr.SecurityDescriptor = NULL;
456     attr.SecurityQualityOfService = NULL;
457     RtlInitUnicodeString( &nameW, name );
458
459     if (access == FILE_MAP_COPY) access = FILE_MAP_READ;
460
461     if ((status = NtOpenSection( &ret, access, &attr )))
462     {
463         SetLastError( RtlNtStatusToDosError(status) );
464         ret = 0;
465     }
466     return ret;
467 }
468
469
470 /***********************************************************************
471  *             MapViewOfFile   (KERNEL32.@)
472  *
473  * Maps a view of a file into the address space.
474  *
475  * PARAMS
476  *  mapping     [I] File-mapping object to map.
477  *  access      [I] Access mode.
478  *  offset_high [I] High-order 32 bits of file offset.
479  *  offset_low  [I] Low-order 32 bits of file offset.
480  *  count       [I] Number of bytes to map.
481  *
482  * RETURNS
483  *      Success: Starting address of mapped view.
484  *      Failure: NULL.
485  */
486 LPVOID WINAPI MapViewOfFile( HANDLE mapping, DWORD access,
487     DWORD offset_high, DWORD offset_low, SIZE_T count )
488 {
489     return MapViewOfFileEx( mapping, access, offset_high,
490                             offset_low, count, NULL );
491 }
492
493
494 /***********************************************************************
495  *             MapViewOfFileEx   (KERNEL32.@)
496  *
497  * Maps a view of a file into the address space.
498  *
499  * PARAMS
500  *  handle      [I] File-mapping object to map.
501  *  access      [I] Access mode.
502  *  offset_high [I] High-order 32 bits of file offset.
503  *  offset_low  [I] Low-order 32 bits of file offset.
504  *  count       [I] Number of bytes to map.
505  *  addr        [I] Suggested starting address for mapped view.
506  *
507  * RETURNS
508  *      Success: Starting address of mapped view.
509  *      Failure: NULL.
510  */
511 LPVOID WINAPI MapViewOfFileEx( HANDLE handle, DWORD access,
512     DWORD offset_high, DWORD offset_low, SIZE_T count, LPVOID addr )
513 {
514     NTSTATUS status;
515     LARGE_INTEGER offset;
516     ULONG protect;
517
518     offset.u.LowPart  = offset_low;
519     offset.u.HighPart = offset_high;
520
521     if (access & FILE_MAP_WRITE) protect = PAGE_READWRITE;
522     else if (access & FILE_MAP_READ) protect = PAGE_READONLY;
523     else if (access & FILE_MAP_COPY) protect = PAGE_WRITECOPY;
524     else protect = PAGE_NOACCESS;
525
526     if ((status = NtMapViewOfSection( handle, GetCurrentProcess(), &addr, 0, 0, &offset,
527                                       &count, ViewShare, 0, protect )))
528     {
529         SetLastError( RtlNtStatusToDosError(status) );
530         addr = NULL;
531     }
532     return addr;
533 }
534
535
536 /***********************************************************************
537  *             UnmapViewOfFile   (KERNEL32.@)
538  *
539  * Unmaps a mapped view of a file.
540  *
541  * PARAMS
542  *  addr [I] Address where mapped view begins.
543  *
544  * RETURNS
545  *      Success: TRUE.
546  *      Failure: FALSE.
547  *
548  */
549 BOOL WINAPI UnmapViewOfFile( LPCVOID addr )
550 {
551     NTSTATUS status = NtUnmapViewOfSection( GetCurrentProcess(), (void *)addr );
552     if (status) SetLastError( RtlNtStatusToDosError(status) );
553     return !status;
554 }
555
556
557 /***********************************************************************
558  *             FlushViewOfFile   (KERNEL32.@)
559  *
560  * Writes to the disk a byte range within a mapped view of a file.
561  *
562  * PARAMS
563  *  base [I] Start address of byte range to flush.
564  *  size [I] Number of bytes in range.
565  *
566  * RETURNS
567  *      Success: TRUE.
568  *      Failure: FALSE.
569  */
570 BOOL WINAPI FlushViewOfFile( LPCVOID base, SIZE_T size )
571 {
572     NTSTATUS status = NtFlushVirtualMemory( GetCurrentProcess(), &base, &size, 0 );
573     if (status)
574     {
575         if (status == STATUS_NOT_MAPPED_DATA) status = STATUS_SUCCESS;
576         else SetLastError( RtlNtStatusToDosError(status) );
577     }
578     return !status;
579 }
580
581
582 /***********************************************************************
583  *             IsBadReadPtr   (KERNEL32.@)
584  *
585  * Check for read access on a memory block.
586  *
587  * ptr  [I] Address of memory block.
588  * size [I] Size of block.
589  *
590  * RETURNS
591  *  Success: TRUE.
592  *      Failure: FALSE. Process has read access to entire block.
593  */
594 BOOL WINAPI IsBadReadPtr( LPCVOID ptr, UINT size )
595 {
596     if (!size) return FALSE;  /* handle 0 size case w/o reference */
597     if (!ptr) return TRUE;
598     
599     if (!page_size) page_size = getpagesize();
600     __TRY
601     {
602         volatile const char *p = ptr;
603         char dummy;
604         UINT count = size;
605
606         while (count > page_size)
607         {
608             dummy = *p;
609             p += page_size;
610             count -= page_size;
611         }
612         dummy = p[0];
613         dummy = p[count - 1];
614     }
615     __EXCEPT_PAGE_FAULT
616     {
617         TRACE_(seh)("%p caused page fault during read\n", ptr);
618         return TRUE;
619     }
620     __ENDTRY
621     return FALSE;
622 }
623
624
625 /***********************************************************************
626  *             IsBadWritePtr   (KERNEL32.@)
627  *
628  * Check for write access on a memory block.
629  *
630  * PARAMS
631  *  ptr  [I] Address of memory block.
632  *  size [I] Size of block in bytes.
633  *
634  * RETURNS
635  *  Success: TRUE.
636  *      Failure: FALSE. Process has write access to entire block.
637  */
638 BOOL WINAPI IsBadWritePtr( LPVOID ptr, UINT size )
639 {
640     if (!size) return FALSE;  /* handle 0 size case w/o reference */
641     if (!ptr) return TRUE;
642     
643     if (!page_size) page_size = getpagesize();
644     __TRY
645     {
646         volatile char *p = ptr;
647         UINT count = size;
648
649         while (count > page_size)
650         {
651             *p |= 0;
652             p += page_size;
653             count -= page_size;
654         }
655         p[0] |= 0;
656         p[count - 1] |= 0;
657     }
658     __EXCEPT_PAGE_FAULT
659     {
660         TRACE_(seh)("%p caused page fault during write\n", ptr);
661         return TRUE;
662     }
663     __ENDTRY
664     return FALSE;
665 }
666
667
668 /***********************************************************************
669  *             IsBadHugeReadPtr   (KERNEL32.@)
670  *
671  * Check for read access on a memory block.
672  *
673  * PARAMS
674  *  ptr  [I] Address of memory block.
675  *  size [I] Size of block.
676  *
677  * RETURNS
678  *  Success: TRUE.
679  *      Failure: FALSE. Process has read access to entire block.
680  */
681 BOOL WINAPI IsBadHugeReadPtr( LPCVOID ptr, UINT size )
682 {
683     return IsBadReadPtr( ptr, size );
684 }
685
686
687 /***********************************************************************
688  *             IsBadHugeWritePtr   (KERNEL32.@)
689  *
690  * Check for write access on a memory block.
691  *
692  * PARAMS
693  *  ptr  [I] Address of memory block.
694  *  size [I] Size of block.
695  *
696  * RETURNS
697  *  Success: TRUE.
698  *      Failure: FALSE. Process has write access to entire block.
699  */
700 BOOL WINAPI IsBadHugeWritePtr( LPVOID ptr, UINT size )
701 {
702     return IsBadWritePtr( ptr, size );
703 }
704
705
706 /***********************************************************************
707  *             IsBadCodePtr   (KERNEL32.@)
708  *
709  * Check for read access on a memory address.
710  *
711  * PARAMS
712  *  ptr [I] Address of function.
713  *
714  * RETURNS
715  *      Success: TRUE.
716  *      Failure: FALSE. Process has read access to specified memory.
717  */
718 BOOL WINAPI IsBadCodePtr( FARPROC ptr )
719 {
720     return IsBadReadPtr( ptr, 1 );
721 }
722
723
724 /***********************************************************************
725  *             IsBadStringPtrA   (KERNEL32.@)
726  *
727  * Check for read access on a range of memory pointed to by a string pointer.
728  *
729  * PARAMS
730  *  str [I] Address of string.
731  *  max [I] Maximum size of string.
732  *
733  * RETURNS
734  *      Success: TRUE.
735  *      Failure: FALSE. Read access to all bytes in string.
736  */
737 BOOL WINAPI IsBadStringPtrA( LPCSTR str, UINT max )
738 {
739     if (!str) return TRUE;
740     
741     __TRY
742     {
743         volatile const char *p = str;
744         while (p != str + max) if (!*p++) break;
745     }
746     __EXCEPT_PAGE_FAULT
747     {
748         TRACE_(seh)("%p caused page fault during read\n", str);
749         return TRUE;
750     }
751     __ENDTRY
752     return FALSE;
753 }
754
755
756 /***********************************************************************
757  *             IsBadStringPtrW   (KERNEL32.@)
758  *
759  * See IsBadStringPtrA.
760  */
761 BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT max )
762 {
763     if (!str) return TRUE;
764     
765     __TRY
766     {
767         volatile const WCHAR *p = str;
768         while (p != str + max) if (!*p++) break;
769     }
770     __EXCEPT_PAGE_FAULT
771     {
772         TRACE_(seh)("%p caused page fault during read\n", str);
773         return TRUE;
774     }
775     __ENDTRY
776     return FALSE;
777 }