1 /* Unit test suite for Rtl* API functions
3 * Copyright 2003 Thomas Mertes
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * We use function pointers here as there is no import library for NTDLL on
26 #include "ntdll_test.h"
28 #ifndef __WINE_WINTERNL_H
30 typedef struct _RTL_HANDLE
32 struct _RTL_HANDLE
* Next
;
35 typedef struct _RTL_HANDLE_TABLE
48 /* Function ptrs for ntdll calls */
49 static HMODULE hntdll
= 0;
50 static SIZE_T (WINAPI
*pRtlCompareMemory
)(LPCVOID
,LPCVOID
,SIZE_T
);
51 static SIZE_T (WINAPI
*pRtlCompareMemoryUlong
)(PULONG
, SIZE_T
, ULONG
);
52 static NTSTATUS (WINAPI
*pRtlDeleteTimer
)(HANDLE
, HANDLE
, HANDLE
);
53 static VOID (WINAPI
*pRtlMoveMemory
)(LPVOID
,LPCVOID
,SIZE_T
);
54 static VOID (WINAPI
*pRtlFillMemory
)(LPVOID
,SIZE_T
,BYTE
);
55 static VOID (WINAPI
*pRtlFillMemoryUlong
)(LPVOID
,SIZE_T
,ULONG
);
56 static VOID (WINAPI
*pRtlZeroMemory
)(LPVOID
,SIZE_T
);
57 static ULONGLONG (WINAPIV
*pRtlUlonglongByteSwap
)(ULONGLONG source
);
58 static ULONG (WINAPI
*pRtlUniform
)(PULONG
);
59 static ULONG (WINAPI
*pRtlRandom
)(PULONG
);
60 static BOOLEAN (WINAPI
*pRtlAreAllAccessesGranted
)(ACCESS_MASK
, ACCESS_MASK
);
61 static BOOLEAN (WINAPI
*pRtlAreAnyAccessesGranted
)(ACCESS_MASK
, ACCESS_MASK
);
62 static DWORD (WINAPI
*pRtlComputeCrc32
)(DWORD
,const BYTE
*,INT
);
63 static void (WINAPI
* pRtlInitializeHandleTable
)(ULONG
, ULONG
, RTL_HANDLE_TABLE
*);
64 static BOOLEAN (WINAPI
* pRtlIsValidIndexHandle
)(const RTL_HANDLE_TABLE
*, ULONG
, RTL_HANDLE
**);
65 static NTSTATUS (WINAPI
* pRtlDestroyHandleTable
)(RTL_HANDLE_TABLE
*);
66 static RTL_HANDLE
* (WINAPI
* pRtlAllocateHandle
)(RTL_HANDLE_TABLE
*, ULONG
*);
67 static BOOLEAN (WINAPI
* pRtlFreeHandle
)(RTL_HANDLE_TABLE
*, RTL_HANDLE
*);
68 static NTSTATUS (WINAPI
*pRtlAllocateAndInitializeSid
)(PSID_IDENTIFIER_AUTHORITY
,BYTE
,DWORD
,DWORD
,DWORD
,DWORD
,DWORD
,DWORD
,DWORD
,DWORD
,PSID
*);
69 static NTSTATUS (WINAPI
*pRtlFreeSid
)(PSID
);
70 static struct _TEB
* (WINAPI
*pNtCurrentTeb
)(void);
71 static DWORD (WINAPI
*pRtlGetThreadErrorMode
)(void);
72 static NTSTATUS (WINAPI
*pRtlSetThreadErrorMode
)(DWORD
, LPDWORD
);
73 static HMODULE hkernel32
= 0;
74 static BOOL (WINAPI
*pIsWow64Process
)(HANDLE
, PBOOL
);
76 static const char* src_src
= "This is a test!"; /* 16 bytes long, incl NUL */
77 static ULONG src_aligned_block
[4];
78 static ULONG dest_aligned_block
[32];
79 static const char *src
= (const char*)src_aligned_block
;
80 static char* dest
= (char*)dest_aligned_block
;
82 static void InitFunctionPtrs(void)
84 hntdll
= LoadLibraryA("ntdll.dll");
85 ok(hntdll
!= 0, "LoadLibrary failed\n");
87 pRtlCompareMemory
= (void *)GetProcAddress(hntdll
, "RtlCompareMemory");
88 pRtlCompareMemoryUlong
= (void *)GetProcAddress(hntdll
, "RtlCompareMemoryUlong");
89 pRtlDeleteTimer
= (void *)GetProcAddress(hntdll
, "RtlDeleteTimer");
90 pRtlMoveMemory
= (void *)GetProcAddress(hntdll
, "RtlMoveMemory");
91 pRtlFillMemory
= (void *)GetProcAddress(hntdll
, "RtlFillMemory");
92 pRtlFillMemoryUlong
= (void *)GetProcAddress(hntdll
, "RtlFillMemoryUlong");
93 pRtlZeroMemory
= (void *)GetProcAddress(hntdll
, "RtlZeroMemory");
94 pRtlUlonglongByteSwap
= (void *)GetProcAddress(hntdll
, "RtlUlonglongByteSwap");
95 pRtlUniform
= (void *)GetProcAddress(hntdll
, "RtlUniform");
96 pRtlRandom
= (void *)GetProcAddress(hntdll
, "RtlRandom");
97 pRtlAreAllAccessesGranted
= (void *)GetProcAddress(hntdll
, "RtlAreAllAccessesGranted");
98 pRtlAreAnyAccessesGranted
= (void *)GetProcAddress(hntdll
, "RtlAreAnyAccessesGranted");
99 pRtlComputeCrc32
= (void *)GetProcAddress(hntdll
, "RtlComputeCrc32");
100 pRtlInitializeHandleTable
= (void *)GetProcAddress(hntdll
, "RtlInitializeHandleTable");
101 pRtlIsValidIndexHandle
= (void *)GetProcAddress(hntdll
, "RtlIsValidIndexHandle");
102 pRtlDestroyHandleTable
= (void *)GetProcAddress(hntdll
, "RtlDestroyHandleTable");
103 pRtlAllocateHandle
= (void *)GetProcAddress(hntdll
, "RtlAllocateHandle");
104 pRtlFreeHandle
= (void *)GetProcAddress(hntdll
, "RtlFreeHandle");
105 pRtlAllocateAndInitializeSid
= (void *)GetProcAddress(hntdll
, "RtlAllocateAndInitializeSid");
106 pRtlFreeSid
= (void *)GetProcAddress(hntdll
, "RtlFreeSid");
107 pNtCurrentTeb
= (void *)GetProcAddress(hntdll
, "NtCurrentTeb");
108 pRtlGetThreadErrorMode
= (void *)GetProcAddress(hntdll
, "RtlGetThreadErrorMode");
109 pRtlSetThreadErrorMode
= (void *)GetProcAddress(hntdll
, "RtlSetThreadErrorMode");
111 hkernel32
= LoadLibraryA("kernel32.dll");
112 ok(hkernel32
!= 0, "LoadLibrary failed\n");
114 pIsWow64Process
= (void *)GetProcAddress(hkernel32
, "IsWow64Process");
116 strcpy((char*)src_aligned_block
, src_src
);
117 ok(strlen(src
) == 15, "Source must be 16 bytes long!\n");
120 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
121 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
123 static void test_RtlCompareMemory(void)
127 if (!pRtlCompareMemory
)
129 win_skip("RtlCompareMemory is not available\n");
136 COMP(src
,src
,LEN
,LEN
);
138 COMP(src
,dest
,LEN
,0);
141 static void test_RtlCompareMemoryUlong(void)
146 if (!pRtlCompareMemoryUlong
)
148 win_skip("RtlCompareMemoryUlong is not available\n");
156 result
= pRtlCompareMemoryUlong(a
, 0, 0x0123);
157 ok(result
== 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %u, expected 0\n", a
, result
);
158 result
= pRtlCompareMemoryUlong(a
, 3, 0x0123);
159 ok(result
== 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a
, result
);
160 result
= pRtlCompareMemoryUlong(a
, 4, 0x0123);
161 ok(result
== 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a
, result
);
162 result
= pRtlCompareMemoryUlong(a
, 5, 0x0123);
163 ok(result
== 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a
, result
);
164 result
= pRtlCompareMemoryUlong(a
, 7, 0x0123);
165 ok(result
== 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a
, result
);
166 result
= pRtlCompareMemoryUlong(a
, 8, 0x0123);
167 ok(result
== 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 4\n", a
, result
);
168 result
= pRtlCompareMemoryUlong(a
, 9, 0x0123);
169 ok(result
== 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 4\n", a
, result
);
170 result
= pRtlCompareMemoryUlong(a
, 4, 0x0127);
171 ok(result
== 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %u, expected 0\n", a
, result
);
172 result
= pRtlCompareMemoryUlong(a
, 4, 0x7123);
173 ok(result
== 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %u, expected 0\n", a
, result
);
174 result
= pRtlCompareMemoryUlong(a
, 16, 0x4567);
175 ok(result
== 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %u, expected 0\n", a
, result
);
178 result
= pRtlCompareMemoryUlong(a
, 3, 0x0123);
179 ok(result
== 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a
, result
);
180 result
= pRtlCompareMemoryUlong(a
, 4, 0x0123);
181 ok(result
== 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a
, result
);
182 result
= pRtlCompareMemoryUlong(a
, 5, 0x0123);
183 ok(result
== 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a
, result
);
184 result
= pRtlCompareMemoryUlong(a
, 7, 0x0123);
185 ok(result
== 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a
, result
);
186 result
= pRtlCompareMemoryUlong(a
, 8, 0x0123);
187 ok(result
== 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 8\n", a
, result
);
188 result
= pRtlCompareMemoryUlong(a
, 9, 0x0123);
189 ok(result
== 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 8\n", a
, result
);
192 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
193 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
195 static void test_RtlMoveMemory(void)
199 win_skip("RtlMoveMemory is not available\n");
203 /* Length should be in bytes and not rounded. Use strcmp to ensure we
204 * didn't write past the end (it checks for the final NUL left by memset)
210 COPY(4); CMP("This");
211 COPY(5); CMP("This ");
212 COPY(6); CMP("This i");
213 COPY(7); CMP("This is");
214 COPY(8); CMP("This is ");
215 COPY(9); CMP("This is a");
218 strcpy(dest
, src
); pRtlMoveMemory(dest
, dest
+ 1, strlen(src
) - 1);
219 CMP("his is a test!!");
220 strcpy(dest
, src
); pRtlMoveMemory(dest
+ 1, dest
, strlen(src
));
221 CMP("TThis is a test!");
224 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
226 static void test_RtlFillMemory(void)
230 win_skip("RtlFillMemory is not available\n");
234 /* Length should be in bytes and not rounded. Use strcmp to ensure we
235 * didn't write past the end (the remainder of the string should match)
237 FILL(0); CMP("This is a test!");
238 FILL(1); CMP("xhis is a test!");
239 FILL(2); CMP("xxis is a test!");
240 FILL(3); CMP("xxxs is a test!");
241 FILL(4); CMP("xxxx is a test!");
242 FILL(5); CMP("xxxxxis a test!");
243 FILL(6); CMP("xxxxxxs a test!");
244 FILL(7); CMP("xxxxxxx a test!");
245 FILL(8); CMP("xxxxxxxxa test!");
246 FILL(9); CMP("xxxxxxxxx test!");
249 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
251 static void test_RtlFillMemoryUlong(void)
253 ULONG val
= ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
254 if (!pRtlFillMemoryUlong
)
256 win_skip("RtlFillMemoryUlong is not available\n");
260 /* Length should be in bytes and not rounded. Use strcmp to ensure we
261 * didn't write past the end (the remainder of the string should match)
263 LFILL(0); CMP("This is a test!");
264 LFILL(1); CMP("This is a test!");
265 LFILL(2); CMP("This is a test!");
266 LFILL(3); CMP("This is a test!");
267 LFILL(4); CMP("xxxx is a test!");
268 LFILL(5); CMP("xxxx is a test!");
269 LFILL(6); CMP("xxxx is a test!");
270 LFILL(7); CMP("xxxx is a test!");
271 LFILL(8); CMP("xxxxxxxxa test!");
272 LFILL(9); CMP("xxxxxxxxa test!");
275 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
276 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
278 static void test_RtlZeroMemory(void)
282 win_skip("RtlZeroMemory is not available\n");
286 /* Length should be in bytes and not rounded. */
287 ZERO(0); MCMP("This is a test!");
288 ZERO(1); MCMP("\0his is a test!");
289 ZERO(2); MCMP("\0\0is is a test!");
290 ZERO(3); MCMP("\0\0\0s is a test!");
291 ZERO(4); MCMP("\0\0\0\0 is a test!");
292 ZERO(5); MCMP("\0\0\0\0\0is a test!");
293 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
294 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
295 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
296 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
299 static void test_RtlUlonglongByteSwap(void)
303 if ( !pRtlUlonglongByteSwap
)
305 win_skip("RtlUlonglongByteSwap is not available\n");
309 if ( pRtlUlonglongByteSwap( 0 ) != 0 )
311 win_skip("Broken RtlUlonglongByteSwap in win2k\n");
315 result
= pRtlUlonglongByteSwap( ((ULONGLONG
)0x76543210 << 32) | 0x87654321 );
316 ok( (((ULONGLONG
)0x21436587 << 32) | 0x10325476) == result
,
317 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%x%08x, expected 0x2143658710325476\n",
318 (DWORD
)(result
>> 32), (DWORD
)result
);
322 static void test_RtlUniform(void)
332 win_skip("RtlUniform is not available\n");
337 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
338 * algorithm. This algorithm is:
340 * seed = (seed * const_1 + const_2) % const_3;
342 * According to the documentation the random number is distributed over
343 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
345 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
347 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
348 * algorithm can be expressed without division as:
350 * seed = (seed * const_1 + const_2) & MAXLONG;
352 * To find out const_2 we just call RtlUniform with seed set to 0:
355 expected
= 0x7fffffc3;
356 result
= pRtlUniform(&seed
);
357 ok(result
== expected
,
358 "RtlUniform(&seed (seed == 0)) returns %x, expected %x\n",
361 * The algorithm is now:
363 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
365 * To find out const_1 we can use:
367 * const_1 = RtlUniform(1) - 0x7fffffc3;
369 * If that does not work a search loop can try all possible values of
370 * const_1 and compare to the result to RtlUniform(1).
371 * This way we find out that const_1 is 0xffffffed.
373 * For seed = 1 the const_2 is 0x7fffffc4:
376 expected
= seed
* 0xffffffed + 0x7fffffc3 + 1;
377 result
= pRtlUniform(&seed
);
378 ok(result
== expected
,
379 "RtlUniform(&seed (seed == 1)) returns %x, expected %x\n",
382 * For seed = 2 the const_2 is 0x7fffffc3:
385 expected
= seed
* 0xffffffed + 0x7fffffc3;
386 result
= pRtlUniform(&seed
);
389 * Windows Vista uses different algorithms, so skip the rest of the tests
390 * until that is figured out. Trace output for the failures is about 10.5 MB!
393 if (result
== 0x7fffff9f) {
394 skip("Most likely running on Windows Vista which uses a different algorithm\n");
398 ok(result
== expected
,
399 "RtlUniform(&seed (seed == 2)) returns %x, expected %x\n",
403 * More tests show that if seed is odd the result must be incremented by 1:
406 expected
= seed
* 0xffffffed + 0x7fffffc3 + (seed
& 1);
407 result
= pRtlUniform(&seed
);
408 ok(result
== expected
,
409 "RtlUniform(&seed (seed == 3)) returns %x, expected %x\n",
413 expected
= seed
* 0xffffffed + 0x7fffffc3;
414 result
= pRtlUniform(&seed
);
415 ok(result
== expected
,
416 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %x, expected %x\n",
420 expected
= seed
* 0xffffffed + 0x7fffffc3 + 1;
421 result
= pRtlUniform(&seed
);
422 ok(result
== expected
,
423 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %x, expected %x\n",
426 * When seed is 0x6bca1ac there is an exception:
429 expected
= seed
* 0xffffffed + 0x7fffffc3 + 2;
430 result
= pRtlUniform(&seed
);
431 ok(result
== expected
,
432 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %x, expected %x\n",
435 * Note that up to here const_3 is not used
436 * (the highest bit of the result is not set).
438 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
441 expected
= (seed
* 0xffffffed + 0x7fffffc3) & MAXLONG
;
442 result
= pRtlUniform(&seed
);
443 ok(result
== expected
,
444 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %x, expected %x\n",
448 expected
= (seed
* 0xffffffed + 0x7fffffc3 + 1) & MAXLONG
;
449 result
= pRtlUniform(&seed
);
450 ok(result
== expected
,
451 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %x, expected %x\n",
454 * There are several ranges where for odd or even seed the result must be
455 * incremented by 1. You can see this ranges in the following test.
457 * For a full test use one of the following loop heads:
459 * for (num = 0; num <= 0xffffffff; num++) {
464 * for (num = 0; num <= 0xffffffff; num++) {
468 for (num
= 0; num
<= 100000; num
++) {
470 expected
= seed
* 0xffffffed + 0x7fffffc3;
471 if (seed
< 0x6bca1ac) {
472 expected
= expected
+ (seed
& 1);
473 } else if (seed
== 0x6bca1ac) {
474 expected
= (expected
+ 2) & MAXLONG
;
475 } else if (seed
< 0xd79435c) {
476 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
477 } else if (seed
< 0x1435e50b) {
478 expected
= expected
+ (seed
& 1);
479 } else if (seed
< 0x1af286ba) {
480 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
481 } else if (seed
< 0x21af2869) {
482 expected
= expected
+ (seed
& 1);
483 } else if (seed
< 0x286bca18) {
484 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
485 } else if (seed
< 0x2f286bc7) {
486 expected
= expected
+ (seed
& 1);
487 } else if (seed
< 0x35e50d77) {
488 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
489 } else if (seed
< 0x3ca1af26) {
490 expected
= expected
+ (seed
& 1);
491 } else if (seed
< 0x435e50d5) {
492 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
493 } else if (seed
< 0x4a1af284) {
494 expected
= expected
+ (seed
& 1);
495 } else if (seed
< 0x50d79433) {
496 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
497 } else if (seed
< 0x579435e2) {
498 expected
= expected
+ (seed
& 1);
499 } else if (seed
< 0x5e50d792) {
500 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
501 } else if (seed
< 0x650d7941) {
502 expected
= expected
+ (seed
& 1);
503 } else if (seed
< 0x6bca1af0) {
504 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
505 } else if (seed
< 0x7286bc9f) {
506 expected
= expected
+ (seed
& 1);
507 } else if (seed
< 0x79435e4e) {
508 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
509 } else if (seed
< 0x7ffffffd) {
510 expected
= expected
+ (seed
& 1);
511 } else if (seed
< 0x86bca1ac) {
512 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
513 } else if (seed
== 0x86bca1ac) {
514 expected
= (expected
+ 1) & MAXLONG
;
515 } else if (seed
< 0x8d79435c) {
516 expected
= expected
+ (seed
& 1);
517 } else if (seed
< 0x9435e50b) {
518 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
519 } else if (seed
< 0x9af286ba) {
520 expected
= expected
+ (seed
& 1);
521 } else if (seed
< 0xa1af2869) {
522 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
523 } else if (seed
< 0xa86bca18) {
524 expected
= expected
+ (seed
& 1);
525 } else if (seed
< 0xaf286bc7) {
526 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
527 } else if (seed
== 0xaf286bc7) {
528 expected
= (expected
+ 2) & MAXLONG
;
529 } else if (seed
< 0xb5e50d77) {
530 expected
= expected
+ (seed
& 1);
531 } else if (seed
< 0xbca1af26) {
532 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
533 } else if (seed
< 0xc35e50d5) {
534 expected
= expected
+ (seed
& 1);
535 } else if (seed
< 0xca1af284) {
536 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
537 } else if (seed
< 0xd0d79433) {
538 expected
= expected
+ (seed
& 1);
539 } else if (seed
< 0xd79435e2) {
540 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
541 } else if (seed
< 0xde50d792) {
542 expected
= expected
+ (seed
& 1);
543 } else if (seed
< 0xe50d7941) {
544 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
545 } else if (seed
< 0xebca1af0) {
546 expected
= expected
+ (seed
& 1);
547 } else if (seed
< 0xf286bc9f) {
548 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
549 } else if (seed
< 0xf9435e4e) {
550 expected
= expected
+ (seed
& 1);
551 } else if (seed
< 0xfffffffd) {
552 expected
= (expected
+ (~seed
& 1)) & MAXLONG
;
554 expected
= expected
+ (seed
& 1);
557 result
= pRtlUniform(&seed
);
558 ok(result
== expected
,
559 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
560 (DWORD
)(num
>> 32), (DWORD
)num
, seed_bak
, result
, expected
);
562 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
563 (DWORD
)(num
>> 32), (DWORD
)num
, seed_bak
, result
, expected
);
566 * Further investigation shows: In the different regions the highest bit
567 * is set or cleared when even or odd seeds need an increment by 1.
568 * This leads to a simplified algorithm:
570 * seed = seed * 0xffffffed + 0x7fffffc3;
571 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
572 * seed = (seed + 2) & MAXLONG;
573 * } else if (seed == 0x7fffffff) {
575 * } else if ((seed & 0x80000000) == 0) {
576 * seed = seed + (~seed & 1);
578 * seed = (seed + (seed & 1)) & MAXLONG;
581 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
583 * Now comes the funny part:
584 * It took me one weekend, to find the complicated algorithm and one day more,
585 * to find the simplified algorithm. Several weeks later I found out: The value
586 * MAXLONG (=0x7fffffff) is never returned, neither with the native function
587 * nor with the simplified algorithm. In reality the native function and our
588 * function return a random number distributed over [0..MAXLONG-1]. Note
589 * that this is different from what native documentation states [0..MAXLONG].
590 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
592 * seed = (seed * const_1 + const_2) % MAXLONG;
594 * Further investigations show that the real algorithm is:
596 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
598 * This is checked with the test below:
601 for (num
= 0; num
<= 100000; num
++) {
602 expected
= (seed
* 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
604 result
= pRtlUniform(&seed
);
605 ok(result
== expected
,
606 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
607 (DWORD
)(num
>> 32), (DWORD
)num
, seed_bak
, result
, expected
);
609 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
610 (DWORD
)(num
>> 32), (DWORD
)num
, seed_bak
, result
, expected
);
613 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
614 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
615 * that there is more than one cycle of generated randon numbers ...
620 static ULONG
my_RtlRandom(PULONG seed
)
622 static ULONG saved_value
[128] =
623 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
624 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
625 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
626 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
627 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
628 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
629 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
630 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
631 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
632 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
633 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
634 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
635 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
636 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
637 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
638 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
643 rand
= (*seed
* 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
644 *seed
= (rand
* 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
646 result
= saved_value
[pos
];
647 saved_value
[pos
] = rand
;
652 static void test_RtlRandom(void)
659 ULONG result_expected
;
663 win_skip("RtlRandom is not available\n");
668 * Unlike RtlUniform, RtlRandom is not documented. We guess that for
669 * RtlRandom D.H. Lehmer's 1948 algorithm is used like stated in
670 * the documentation of the RtlUniform function. This algorithm is:
672 * seed = (seed * const_1 + const_2) % const_3;
674 * According to the RtlUniform documentation the random number is
675 * distributed over [0..MAXLONG], but in reality it is distributed
676 * over [0..MAXLONG-1]. Therefore const_3 might be MAXLONG + 1 or
679 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
683 * seed = (seed * const_1 + const_2) % MAXLONG;
685 * To find out const_2 we just call RtlRandom with seed set to 0:
688 result_expected
= 0x320a1743;
689 seed_expected
=0x44b;
690 result
= pRtlRandom(&seed
);
693 * Windows Vista uses different algorithms, so skip the rest of the tests
694 * until that is figured out. Trace output for the failures is about 10.5 MB!
698 skip("Most likely running on Windows Vista which uses a different algorithm\n");
702 ok(result
== result_expected
,
703 "pRtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
704 result
, result_expected
);
705 ok(seed
== seed_expected
,
706 "pRtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
707 seed
, seed_expected
);
709 * Seed is not equal to result as with RtlUniform. To see more we
710 * call RtlRandom again with seed set to 0:
713 result_expected
= 0x7fffffc3;
714 seed_expected
=0x44b;
715 result
= pRtlRandom(&seed
);
716 ok(result
== result_expected
,
717 "RtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
718 result
, result_expected
);
719 ok(seed
== seed_expected
,
720 "RtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
721 seed
, seed_expected
);
723 * Seed is set to the same value as before but the result is different.
724 * To see more we call RtlRandom again with seed set to 0:
727 result_expected
= 0x7fffffc3;
728 seed_expected
=0x44b;
729 result
= pRtlRandom(&seed
);
730 ok(result
== result_expected
,
731 "RtlRandom(&seed (seed == 0)) returns %x, expected %x\n",
732 result
, result_expected
);
733 ok(seed
== seed_expected
,
734 "RtlRandom(&seed (seed == 0)) sets seed to %x, expected %x\n",
735 seed
, seed_expected
);
737 * Seed is again set to the same value as before. This time we also
738 * have the same result as before. Interestingly the value of the
739 * result is 0x7fffffc3 which is the same value used in RtlUniform
740 * as const_2. If we do
743 * result = RtlUniform(&seed);
745 * we get the same result (0x7fffffc3) as with
750 * result = RtlRandom(&seed);
752 * And there is another interesting thing. If we do
758 * seed is set to the value 0x44b which ist the same value that
763 * assigns to seed. Putting these two findings together leads to
764 * the conclusion that RtlRandom saves the value in some variable,
765 * like in the following algorithm:
767 * result = saved_value;
768 * saved_value = RtlUniform(&seed);
772 * Now we do further tests with seed set to 1:
775 result_expected
= 0x7a50bbc6;
776 seed_expected
=0x5a1;
777 result
= pRtlRandom(&seed
);
778 ok(result
== result_expected
,
779 "RtlRandom(&seed (seed == 1)) returns %x, expected %x\n",
780 result
, result_expected
);
781 ok(seed
== seed_expected
,
782 "RtlRandom(&seed (seed == 1)) sets seed to %x, expected %x\n",
783 seed
, seed_expected
);
785 * If there is just one saved_value the result now would be
786 * 0x7fffffc3. From this test we can see that there is more than
787 * one saved_value, like with this algorithm:
789 * result = saved_value[pos];
790 * saved_value[pos] = RtlUniform(&seed);
794 * But how is the value of pos determined? The calls to RtlUniform
795 * create a sequence of random numbers. Every second random number
796 * is put into the saved_value array and is used in some later call
797 * of RtlRandom as result. The only reasonable source to determine
798 * pos are the random numbers generated by RtlUniform which are not
799 * put into the saved_value array. This are the values of seed
800 * between the two calls of RtlUniform as in this algorithm:
802 * rand = RtlUniform(&seed);
804 * pos = position(seed);
805 * result = saved_value[pos];
806 * saved_value[pos] = rand;
809 * What remains to be determined is: The size of the saved_value array,
810 * the initial values of the saved_value array and the function
811 * position(seed). These tests are not shown here.
812 * The result of these tests is: The size of the saved_value array
813 * is 128, the initial values can be seen in the my_RtlRandom
814 * function and the position(seed) function is (seed & 0x7f).
816 * For a full test of RtlRandom use one of the following loop heads:
818 * for (num = 0; num <= 0xffffffff; num++) {
823 * for (num = 0; num <= 0xffffffff; num++) {
827 for (num
= 0; num
<= 100000; num
++) {
829 seed_expected
= seed
;
830 result_expected
= my_RtlRandom(&seed_expected
);
831 /* The following corrections are necessary because the */
832 /* previous tests changed the saved_value array */
834 result_expected
= 0x7fffffc3;
835 } else if (num
== 81) {
836 result_expected
= 0x7fffffb1;
838 result
= pRtlRandom(&seed
);
839 ok(result
== result_expected
,
840 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
841 (DWORD
)(num
>> 32), (DWORD
)num
, seed_bak
, result
, result_expected
);
842 ok(seed
== seed_expected
,
843 "test: 0x%x%08x RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
844 (DWORD
)(num
>> 32), (DWORD
)num
, seed_bak
, result
, seed_expected
);
850 ACCESS_MASK GrantedAccess
;
851 ACCESS_MASK DesiredAccess
;
855 static const all_accesses_t all_accesses
[] = {
856 {0xFEDCBA76, 0xFEDCBA76, 1},
857 {0x00000000, 0xFEDCBA76, 0},
858 {0xFEDCBA76, 0x00000000, 1},
859 {0x00000000, 0x00000000, 1},
860 {0xFEDCBA76, 0xFEDCBA70, 1},
861 {0xFEDCBA70, 0xFEDCBA76, 0},
862 {0xFEDCBA76, 0xFEDC8A76, 1},
863 {0xFEDC8A76, 0xFEDCBA76, 0},
864 {0xFEDCBA76, 0xC8C4B242, 1},
865 {0xC8C4B242, 0xFEDCBA76, 0},
867 #define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))
870 static void test_RtlAreAllAccessesGranted(void)
872 unsigned int test_num
;
875 if (!pRtlAreAllAccessesGranted
)
877 win_skip("RtlAreAllAccessesGranted is not available\n");
881 for (test_num
= 0; test_num
< NB_ALL_ACCESSES
; test_num
++) {
882 result
= pRtlAreAllAccessesGranted(all_accesses
[test_num
].GrantedAccess
,
883 all_accesses
[test_num
].DesiredAccess
);
884 ok(all_accesses
[test_num
].result
== result
,
885 "(test %d): RtlAreAllAccessesGranted(%08x, %08x) returns %d, expected %d\n",
886 test_num
, all_accesses
[test_num
].GrantedAccess
,
887 all_accesses
[test_num
].DesiredAccess
,
888 result
, all_accesses
[test_num
].result
);
894 ACCESS_MASK GrantedAccess
;
895 ACCESS_MASK DesiredAccess
;
899 static const any_accesses_t any_accesses
[] = {
900 {0xFEDCBA76, 0xFEDCBA76, 1},
901 {0x00000000, 0xFEDCBA76, 0},
902 {0xFEDCBA76, 0x00000000, 0},
903 {0x00000000, 0x00000000, 0},
904 {0xFEDCBA76, 0x01234589, 0},
905 {0x00040000, 0xFEDCBA76, 1},
906 {0x00040000, 0xFED8BA76, 0},
907 {0xFEDCBA76, 0x00040000, 1},
908 {0xFED8BA76, 0x00040000, 0},
910 #define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))
913 static void test_RtlAreAnyAccessesGranted(void)
915 unsigned int test_num
;
918 if (!pRtlAreAnyAccessesGranted
)
920 win_skip("RtlAreAnyAccessesGranted is not available\n");
924 for (test_num
= 0; test_num
< NB_ANY_ACCESSES
; test_num
++) {
925 result
= pRtlAreAnyAccessesGranted(any_accesses
[test_num
].GrantedAccess
,
926 any_accesses
[test_num
].DesiredAccess
);
927 ok(any_accesses
[test_num
].result
== result
,
928 "(test %d): RtlAreAnyAccessesGranted(%08x, %08x) returns %d, expected %d\n",
929 test_num
, any_accesses
[test_num
].GrantedAccess
,
930 any_accesses
[test_num
].DesiredAccess
,
931 result
, any_accesses
[test_num
].result
);
935 static void test_RtlComputeCrc32(void)
939 if (!pRtlComputeCrc32
)
941 win_skip("RtlComputeCrc32 is not available\n");
945 crc
= pRtlComputeCrc32(crc
, (const BYTE
*)src
, LEN
);
946 ok(crc
== 0x40861dc2,"Expected 0x40861dc2, got %8x\n", crc
);
950 typedef struct MY_HANDLE
952 RTL_HANDLE RtlHandle
;
956 static inline void RtlpMakeHandleAllocated(RTL_HANDLE
* Handle
)
958 ULONG_PTR
*AllocatedBit
= (ULONG_PTR
*)(&Handle
->Next
);
959 *AllocatedBit
= *AllocatedBit
| 1;
962 static void test_HandleTables(void)
967 MY_HANDLE
* MyHandle
;
968 RTL_HANDLE_TABLE HandleTable
;
970 if (!pRtlInitializeHandleTable
)
972 win_skip("RtlInitializeHandleTable is not available\n");
976 pRtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE
), &HandleTable
);
977 MyHandle
= (MY_HANDLE
*)pRtlAllocateHandle(&HandleTable
, &Index
);
978 ok(MyHandle
!= NULL
, "RtlAllocateHandle failed\n");
979 RtlpMakeHandleAllocated(&MyHandle
->RtlHandle
);
981 result
= pRtlIsValidIndexHandle(&HandleTable
, Index
, (RTL_HANDLE
**)&MyHandle
);
982 ok(result
, "Handle %p wasn't valid\n", MyHandle
);
983 result
= pRtlFreeHandle(&HandleTable
, &MyHandle
->RtlHandle
);
984 ok(result
, "Couldn't free handle %p\n", MyHandle
);
985 status
= pRtlDestroyHandleTable(&HandleTable
);
986 ok(status
== STATUS_SUCCESS
, "RtlDestroyHandleTable failed with error 0x%08x\n", status
);
989 static void test_RtlAllocateAndInitializeSid(void)
992 SID_IDENTIFIER_AUTHORITY sia
= {{ 1, 2, 3, 4, 5, 6 }};
995 if (!pRtlAllocateAndInitializeSid
)
997 win_skip("RtlAllocateAndInitializeSid is not available\n");
1001 ret
= pRtlAllocateAndInitializeSid(&sia
, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid
);
1002 ok(!ret
, "RtlAllocateAndInitializeSid error %08x\n", ret
);
1003 ret
= pRtlFreeSid(psid
);
1004 ok(!ret
, "RtlFreeSid error %08x\n", ret
);
1006 /* these tests crash on XP */
1009 ret
= pRtlAllocateAndInitializeSid(NULL
, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid
);
1010 ret
= pRtlAllocateAndInitializeSid(&sia
, 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL
);
1013 ret
= pRtlAllocateAndInitializeSid(&sia
, 9, 1, 2, 3, 4, 5, 6, 7, 8, &psid
);
1014 ok(ret
== STATUS_INVALID_SID
, "wrong error %08x\n", ret
);
1017 static void test_RtlDeleteTimer(void)
1021 if (!pRtlDeleteTimer
)
1023 win_skip("RtlDeleteTimer is not available\n");
1027 ret
= pRtlDeleteTimer(NULL
, NULL
, NULL
);
1028 ok(ret
== STATUS_INVALID_PARAMETER_1
||
1029 ret
== STATUS_INVALID_PARAMETER
, /* W2K */
1030 "expected STATUS_INVALID_PARAMETER_1 or STATUS_INVALID_PARAMETER, got %x\n", ret
);
1033 static void test_RtlThreadErrorMode(void)
1040 if (!pRtlGetThreadErrorMode
|| !pRtlSetThreadErrorMode
)
1042 win_skip("RtlGetThreadErrorMode and/or RtlSetThreadErrorMode not available\n");
1046 if (!pIsWow64Process
|| !pIsWow64Process(GetCurrentProcess(), &is_wow64
))
1049 oldmode
= pRtlGetThreadErrorMode();
1051 status
= pRtlSetThreadErrorMode(0x70, &mode
);
1052 ok(status
== STATUS_SUCCESS
||
1053 status
== STATUS_WAIT_1
, /* Vista */
1054 "RtlSetThreadErrorMode failed with error 0x%08x\n", status
);
1056 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
1058 ok(pRtlGetThreadErrorMode() == 0x70,
1059 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode
, 0x70);
1060 if (!is_wow64
&& pNtCurrentTeb
)
1061 ok(pNtCurrentTeb()->HardErrorDisabled
== 0x70,
1062 "The TEB contains 0x%x, expected 0x%x\n",
1063 pNtCurrentTeb()->HardErrorDisabled
, 0x70);
1065 status
= pRtlSetThreadErrorMode(0, &mode
);
1066 ok(status
== STATUS_SUCCESS
||
1067 status
== STATUS_WAIT_1
, /* Vista */
1068 "RtlSetThreadErrorMode failed with error 0x%08x\n", status
);
1070 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
1072 ok(pRtlGetThreadErrorMode() == 0,
1073 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode
, 0);
1074 if (!is_wow64
&& pNtCurrentTeb
)
1075 ok(pNtCurrentTeb()->HardErrorDisabled
== 0,
1076 "The TEB contains 0x%x, expected 0x%x\n",
1077 pNtCurrentTeb()->HardErrorDisabled
, 0);
1079 for (mode
= 1; mode
; mode
<<= 1)
1081 status
= pRtlSetThreadErrorMode(mode
, NULL
);
1083 ok(status
== STATUS_SUCCESS
||
1084 status
== STATUS_WAIT_1
, /* Vista */
1085 "RtlSetThreadErrorMode(%x,NULL) failed with error 0x%08x\n",
1088 ok(status
== STATUS_INVALID_PARAMETER_1
,
1089 "RtlSetThreadErrorMode(%x,NULL) returns 0x%08x, "
1090 "expected STATUS_INVALID_PARAMETER_1\n",
1094 pRtlSetThreadErrorMode(oldmode
, NULL
);
1101 test_RtlCompareMemory();
1102 test_RtlCompareMemoryUlong();
1103 test_RtlMoveMemory();
1104 test_RtlFillMemory();
1105 test_RtlFillMemoryUlong();
1106 test_RtlZeroMemory();
1107 test_RtlUlonglongByteSwap();
1110 test_RtlAreAllAccessesGranted();
1111 test_RtlAreAnyAccessesGranted();
1112 test_RtlComputeCrc32();
1113 test_HandleTables();
1114 test_RtlAllocateAndInitializeSid();
1115 test_RtlDeleteTimer();
1116 test_RtlThreadErrorMode();