2 * PROJECT: ReactOS Win32 Base API
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/kernel32/mem/isbad.c
5 * PURPOSE: Handles probing of memory addresses
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Thomas Weidenmueller (w3seek@reactos.org)
10 /* INCLUDES ******************************************************************/
17 extern SYSTEM_BASIC_INFORMATION BaseCachedSysInfo
;
19 /* FUNCTIONS *****************************************************************/
26 IsBadReadPtr(IN LPCVOID lp
,
30 BOOLEAN Result
= FALSE
;
31 volatile CHAR
*Current
;
35 if (!ucb
) return FALSE
;
38 /* Get the page size */
39 PageSize
= BaseCachedSysInfo
.PageSize
;
41 /* Calculate the last page */
42 Last
= (PCHAR
)((ULONG_PTR
)lp
+ ucb
- 1);
44 /* Another quick failure case */
45 if ((ULONG_PTR
)Last
< (ULONG_PTR
)lp
) return TRUE
;
50 /* Probe the entire range */
51 Current
= (volatile CHAR
*)lp
;
52 Last
= (PCHAR
)(PAGE_ROUND_DOWN(Last
));
56 Current
= (volatile CHAR
*)(PAGE_ROUND_DOWN(Current
) + PAGE_SIZE
);
57 } while (Current
<= Last
);
59 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
61 /* We hit an exception, so return true */
66 /* Return exception status */
75 IsBadHugeReadPtr(LPCVOID lp
,
78 /* Implementation is the same on 32-bit */
79 return IsBadReadPtr(lp
, ucb
);
87 IsBadCodePtr(FARPROC lpfn
)
89 /* Executing has the same privileges as reading */
90 return IsBadReadPtr((LPVOID
)lpfn
, 1);
98 IsBadWritePtr(LPVOID lp
,
102 BOOLEAN Result
= FALSE
;
103 volatile CHAR
*Current
;
107 if (!ucb
) return FALSE
;
108 if (!lp
) return TRUE
;
110 /* Get the page size */
111 PageSize
= BaseCachedSysInfo
.PageSize
;
113 /* Calculate the last page */
114 Last
= (PCHAR
)((ULONG_PTR
)lp
+ ucb
- 1);
116 /* Another quick failure case */
117 if ((ULONG_PTR
)Last
< (ULONG_PTR
)lp
) return TRUE
;
122 /* Probe the entire range */
123 Current
= (volatile CHAR
*)lp
;
124 Last
= (PCHAR
)(PAGE_ROUND_DOWN(Last
));
128 Current
= (volatile CHAR
*)(PAGE_ROUND_DOWN(Current
) + PAGE_SIZE
);
129 } while (Current
<= Last
);
131 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
133 /* We hit an exception, so return true */
138 /* Return exception status */
147 IsBadHugeWritePtr(LPVOID lp
,
150 /* Implementation is the same on 32-bit */
151 return IsBadWritePtr(lp
, ucb
);
159 IsBadStringPtrW(IN LPCWSTR lpsz
,
162 BOOLEAN Result
= FALSE
;
163 volatile WCHAR
*Current
;
168 if (!ucchMax
) return FALSE
;
169 if (!lpsz
) return TRUE
;
171 /* Calculate the last page */
172 Last
= (PWCHAR
)((ULONG_PTR
)lpsz
+ (ucchMax
* 2) - 2);
177 /* Probe the entire range */
178 Current
= (volatile WCHAR
*)lpsz
;
179 Last
= (PWCHAR
)(PAGE_ROUND_DOWN(Last
));
184 } while (Char
&& (Current
!= Last
+ 1));
186 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
188 /* We hit an exception, so return true */
193 /* Return exception status */
202 IsBadStringPtrA(IN LPCSTR lpsz
,
205 BOOLEAN Result
= FALSE
;
206 volatile CHAR
*Current
;
211 if (!ucchMax
) return FALSE
;
212 if (!lpsz
) return TRUE
;
214 /* Calculate the last page */
215 Last
= (PCHAR
)((ULONG_PTR
)lpsz
+ ucchMax
- 1);
220 /* Probe the entire range */
221 Current
= (volatile CHAR
*)lpsz
;
222 Last
= (PCHAR
)(PAGE_ROUND_DOWN(Last
));
227 } while (Char
&& (Current
!= Last
+ 1));
229 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
231 /* We hit an exception, so return true */
236 /* Return exception status */