[KMTESTS]
[reactos.git] / rostests / kmtests / ntos_ke / KeSpinLock.c
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite Spin lock test
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #ifndef _WIN64
9 __declspec(dllimport) void __stdcall KeAcquireSpinLock(unsigned long *, unsigned char *);
10 __declspec(dllimport) void __stdcall KeReleaseSpinLock(unsigned long *, unsigned char);
11 __declspec(dllimport) void __stdcall KeAcquireSpinLockAtDpcLevel(unsigned long *);
12 __declspec(dllimport) void __stdcall KeReleaseSpinLockFromDpcLevel(unsigned long *);
13 #endif
14
15 /* this define makes KeInitializeSpinLock not use the inlined version */
16 #define WIN9X_COMPAT_SPINLOCK
17 #include <kmt_test.h>
18 #include <limits.h>
19
20 //#define NDEBUG
21 #include <debug.h>
22
23 static
24 _Must_inspect_result_
25 _IRQL_requires_min_(DISPATCH_LEVEL)
26 _Post_satisfies_(return == 1 || return == 0)
27 BOOLEAN
28 (FASTCALL
29 *pKeTryToAcquireSpinLockAtDpcLevel)(
30 _Inout_ _Requires_lock_not_held_(*_Curr_)
31 _When_(return!=0, _Acquires_lock_(*_Curr_))
32 PKSPIN_LOCK SpinLock);
33
34 static
35 VOID
36 (FASTCALL
37 *pKeAcquireInStackQueuedSpinLockForDpc)(
38 IN OUT PKSPIN_LOCK SpinLock,
39 OUT PKLOCK_QUEUE_HANDLE LockHandle);
40
41 static
42 VOID
43 (FASTCALL
44 *pKeReleaseInStackQueuedSpinLockForDpc)(
45 IN PKLOCK_QUEUE_HANDLE LockHandle);
46
47 static
48 _Must_inspect_result_
49 BOOLEAN
50 (FASTCALL
51 *pKeTestSpinLock)(
52 _In_ PKSPIN_LOCK SpinLock);
53
54 /* TODO: multiprocessor testing */
55
56 struct _CHECK_DATA;
57 typedef struct _CHECK_DATA CHECK_DATA, *PCHECK_DATA;
58
59 typedef VOID (*PACQUIRE_FUNCTION)(PKSPIN_LOCK, PCHECK_DATA);
60 typedef VOID (*PRELEASE_FUNCTION)(PKSPIN_LOCK, PCHECK_DATA);
61 typedef BOOLEAN (*PTRY_FUNCTION)(PKSPIN_LOCK, PCHECK_DATA);
62
63 struct _CHECK_DATA
64 {
65 enum
66 {
67 CheckQueueHandle,
68 CheckQueue,
69 CheckLock
70 } Check;
71 KIRQL IrqlWhenAcquired;
72 PACQUIRE_FUNCTION Acquire;
73 PRELEASE_FUNCTION Release;
74 PTRY_FUNCTION TryAcquire;
75 PACQUIRE_FUNCTION AcquireNoRaise;
76 PRELEASE_FUNCTION ReleaseNoLower;
77 PTRY_FUNCTION TryAcquireNoRaise;
78 KSPIN_LOCK_QUEUE_NUMBER QueueNumber;
79 BOOLEAN TryRetOnFailure;
80 KIRQL OriginalIrql;
81 BOOLEAN IsAcquired;
82 _ANONYMOUS_UNION union
83 {
84 KLOCK_QUEUE_HANDLE QueueHandle;
85 PKSPIN_LOCK_QUEUE Queue;
86 KIRQL Irql;
87 } DUMMYUNIONNAME;
88 PVOID UntouchedValue;
89 };
90
91 #define DEFINE_ACQUIRE(LocalName, SetIsAcquired, DoCall) \
92 static VOID LocalName(PKSPIN_LOCK SpinLock, PCHECK_DATA CheckData) \
93 { \
94 ASSERT(!CheckData->IsAcquired); \
95 DoCall; \
96 if (SetIsAcquired) CheckData->IsAcquired = TRUE; \
97 }
98
99 #define DEFINE_RELEASE(LocalName, SetIsAcquired, DoCall) \
100 static VOID LocalName(PKSPIN_LOCK SpinLock, PCHECK_DATA CheckData) \
101 { \
102 DoCall; \
103 if (SetIsAcquired) CheckData->IsAcquired = FALSE; \
104 }
105
106 DEFINE_ACQUIRE(AcquireNormal, TRUE, KeAcquireSpinLock(SpinLock, &CheckData->Irql))
107 DEFINE_RELEASE(ReleaseNormal, TRUE, KeReleaseSpinLock(SpinLock, CheckData->Irql))
108 #ifdef _X86_
109 DEFINE_ACQUIRE(AcquireExp, TRUE, (KeAcquireSpinLock)(SpinLock, &CheckData->Irql))
110 DEFINE_RELEASE(ReleaseExp, TRUE, (KeReleaseSpinLock)(SpinLock, CheckData->Irql))
111 #else
112 DEFINE_ACQUIRE(AcquireExp, TRUE, KeAcquireSpinLock(SpinLock, &CheckData->Irql))
113 DEFINE_RELEASE(ReleaseExp, TRUE, KeReleaseSpinLock(SpinLock, CheckData->Irql))
114 #endif
115 DEFINE_ACQUIRE(AcquireSynch, TRUE, CheckData->Irql = KeAcquireSpinLockRaiseToSynch(SpinLock))
116
117 DEFINE_ACQUIRE(AcquireInStackQueued, TRUE, KeAcquireInStackQueuedSpinLock(SpinLock, &CheckData->QueueHandle))
118 DEFINE_ACQUIRE(AcquireInStackSynch, TRUE, KeAcquireInStackQueuedSpinLockRaiseToSynch(SpinLock, &CheckData->QueueHandle))
119 DEFINE_RELEASE(ReleaseInStackQueued, TRUE, KeReleaseInStackQueuedSpinLock(&CheckData->QueueHandle))
120
121 DEFINE_ACQUIRE(AcquireQueued, TRUE, CheckData->Irql = KeAcquireQueuedSpinLock(CheckData->QueueNumber))
122 DEFINE_ACQUIRE(AcquireQueuedSynch, TRUE, CheckData->Irql = KeAcquireQueuedSpinLockRaiseToSynch(CheckData->QueueNumber))
123 DEFINE_RELEASE(ReleaseQueued, TRUE, KeReleaseQueuedSpinLock(CheckData->QueueNumber, CheckData->Irql))
124
125 DEFINE_ACQUIRE(AcquireNoRaise, FALSE, KeAcquireSpinLockAtDpcLevel(SpinLock))
126 DEFINE_RELEASE(ReleaseNoLower, FALSE, KeReleaseSpinLockFromDpcLevel(SpinLock))
127 DEFINE_ACQUIRE(AcquireExpNoRaise, FALSE, (KeAcquireSpinLockAtDpcLevel)(SpinLock))
128 DEFINE_RELEASE(ReleaseExpNoLower, FALSE, (KeReleaseSpinLockFromDpcLevel)(SpinLock))
129
130 DEFINE_ACQUIRE(AcquireInStackNoRaise, FALSE, KeAcquireInStackQueuedSpinLockAtDpcLevel(SpinLock, &CheckData->QueueHandle))
131 DEFINE_RELEASE(ReleaseInStackNoRaise, FALSE, KeReleaseInStackQueuedSpinLockFromDpcLevel(&CheckData->QueueHandle))
132
133 /* TODO: test these functions. They behave weirdly, though */
134 #if 0
135 DEFINE_ACQUIRE(AcquireForDpc, TRUE, CheckData->Irql = KeAcquireSpinLockForDpc(SpinLock))
136 DEFINE_RELEASE(ReleaseForDpc, TRUE, KeReleaseSpinLockForDpc(SpinLock, CheckData->Irql))
137 #endif
138
139 DEFINE_ACQUIRE(AcquireInStackForDpc, FALSE, pKeAcquireInStackQueuedSpinLockForDpc(SpinLock, &CheckData->QueueHandle))
140 DEFINE_RELEASE(ReleaseInStackForDpc, FALSE, pKeReleaseInStackQueuedSpinLockForDpc(&CheckData->QueueHandle))
141
142 #ifdef _X86_
143 DEFINE_ACQUIRE(AcquireInt, FALSE, KiAcquireSpinLock(SpinLock))
144 DEFINE_RELEASE(ReleaseInt, FALSE, KiReleaseSpinLock(SpinLock))
145 #else
146 DEFINE_ACQUIRE(AcquireInt, TRUE, KeAcquireSpinLock(SpinLock, &CheckData->Irql))
147 DEFINE_RELEASE(ReleaseInt, TRUE, KeReleaseSpinLock(SpinLock, CheckData->Irql))
148 #endif
149
150 BOOLEAN TryQueued(PKSPIN_LOCK SpinLock, PCHECK_DATA CheckData) {
151 LOGICAL Ret = KeTryToAcquireQueuedSpinLock(CheckData->QueueNumber, &CheckData->Irql);
152 CheckData->IsAcquired = TRUE;
153 ASSERT(Ret == FALSE || Ret == TRUE);
154 return (BOOLEAN)Ret;
155 }
156 BOOLEAN TryQueuedSynch(PKSPIN_LOCK SpinLock, PCHECK_DATA CheckData) {
157 BOOLEAN Ret = KeTryToAcquireQueuedSpinLockRaiseToSynch(CheckData->QueueNumber, &CheckData->Irql);
158 CheckData->IsAcquired = TRUE;
159 return Ret;
160 }
161 BOOLEAN TryNoRaise(PKSPIN_LOCK SpinLock, PCHECK_DATA CheckData) {
162 BOOLEAN Ret = pKeTryToAcquireSpinLockAtDpcLevel(SpinLock);
163 return Ret;
164 }
165
166 #define CheckSpinLockLock(SpinLock, CheckData, Value) do \
167 { \
168 PKTHREAD Thread = KeGetCurrentThread(); \
169 if (KmtIsMultiProcessorBuild) \
170 { \
171 ok_eq_bool(Ret, (Value) == 0); \
172 if (SpinLock) \
173 ok_eq_ulongptr(*(SpinLock), \
174 (Value) ? (ULONG_PTR)Thread | 1 : 0); \
175 } \
176 else \
177 { \
178 ok_bool_true(Ret, "KeTestSpinLock returned"); \
179 if (SpinLock) \
180 ok_eq_ulongptr(*(SpinLock), 0); \
181 } \
182 ok_eq_uint((CheckData)->Irql, (CheckData)->OriginalIrql); \
183 } while (0)
184
185 #define CheckSpinLockQueue(SpinLock, CheckData, Value) do \
186 { \
187 ok_eq_pointer((CheckData)->Queue->Next, NULL); \
188 ok_eq_pointer((CheckData)->Queue->Lock, NULL); \
189 ok_eq_uint((CheckData)->Irql, (CheckData)->OriginalIrql); \
190 } while (0)
191
192 #define CheckSpinLockQueueHandle(SpinLock, CheckData, Value) do \
193 { \
194 if (KmtIsMultiProcessorBuild) \
195 { \
196 ok_eq_bool(Ret, (Value) == 0); \
197 if (SpinLock) \
198 ok_eq_ulongptr(*(SpinLock), \
199 (Value) ? &(CheckData)->QueueHandle : 0); \
200 ok_eq_pointer((CheckData)->QueueHandle.LockQueue.Next, NULL); \
201 ok_eq_pointer((CheckData)->QueueHandle.LockQueue.Lock, \
202 (PVOID)((ULONG_PTR)SpinLock | ((Value) ? 2 : 0))); \
203 } \
204 else \
205 { \
206 ok_bool_true(Ret, "KeTestSpinLock returned"); \
207 if (SpinLock) \
208 ok_eq_ulongptr(*(SpinLock), 0); \
209 ok_eq_pointer((CheckData)->QueueHandle.LockQueue.Next, (CheckData)->UntouchedValue); \
210 ok_eq_pointer((CheckData)->QueueHandle.LockQueue.Lock, (CheckData)->UntouchedValue); \
211 } \
212 ok_eq_uint((CheckData)->QueueHandle.OldIrql, (CheckData)->OriginalIrql); \
213 } while (0)
214
215 #define CheckSpinLock(SpinLock, CheckData, Value) do \
216 { \
217 BOOLEAN Ret = SpinLock && pKeTestSpinLock ? pKeTestSpinLock(SpinLock) : TRUE; \
218 KIRQL ExpectedIrql = (CheckData)->OriginalIrql; \
219 \
220 switch ((CheckData)->Check) \
221 { \
222 case CheckLock: \
223 CheckSpinLockLock(SpinLock, CheckData, Value); \
224 break; \
225 case CheckQueue: \
226 CheckSpinLockQueue(SpinLock, CheckData, Value); \
227 break; \
228 case CheckQueueHandle: \
229 CheckSpinLockQueueHandle(SpinLock, CheckData, Value); \
230 break; \
231 } \
232 \
233 if ((CheckData)->IsAcquired) \
234 ExpectedIrql = (CheckData)->IrqlWhenAcquired; \
235 ok_irql(ExpectedIrql); \
236 ok_bool_false(KeAreApcsDisabled(), "KeAreApcsDisabled returned"); \
237 ok_bool_true(KmtAreInterruptsEnabled(), "Interrupts enabled:"); \
238 } while (0)
239
240 static
241 VOID
242 TestSpinLock(
243 PKSPIN_LOCK SpinLock,
244 PCHECK_DATA CheckData)
245 {
246 static INT Run = 0;
247 trace("Test SpinLock run %d\n", Run++);
248
249 ok_irql(CheckData->OriginalIrql);
250
251 if (SpinLock)
252 ok_eq_ulongptr(*SpinLock, 0);
253 CheckData->Acquire(SpinLock, CheckData);
254 CheckSpinLock(SpinLock, CheckData, 1);
255 CheckData->Release(SpinLock, CheckData);
256 CheckSpinLock(SpinLock, CheckData, 0);
257
258 if (CheckData->TryAcquire)
259 {
260 CheckSpinLock(SpinLock, CheckData, 0);
261 ok_bool_true(CheckData->TryAcquire(SpinLock, CheckData), "TryAcquire returned");
262 CheckSpinLock(SpinLock, CheckData, 1);
263 if (!KmtIsCheckedBuild)
264 {
265 /* SPINLOCK_ALREADY_OWNED on checked build */
266 ok_bool_true(CheckData->TryAcquire(SpinLock, CheckData), "TryAcquire returned");
267 /* even a failing acquire sets irql */
268 ok_eq_uint(CheckData->Irql, CheckData->IrqlWhenAcquired);
269 CheckData->Irql = CheckData->OriginalIrql;
270 CheckSpinLock(SpinLock, CheckData, 1);
271 }
272 CheckData->Release(SpinLock, CheckData);
273 CheckSpinLock(SpinLock, CheckData, 0);
274 }
275
276 if (CheckData->AcquireNoRaise &&
277 (CheckData->OriginalIrql >= DISPATCH_LEVEL || !KmtIsCheckedBuild) &&
278 (CheckData->AcquireNoRaise != AcquireInStackForDpc ||
279 !skip(pKeAcquireInStackQueuedSpinLockForDpc &&
280 pKeReleaseInStackQueuedSpinLockForDpc, "No DPC spinlock functions\n")))
281 {
282 /* acquire/release without irql change */
283 CheckData->AcquireNoRaise(SpinLock, CheckData);
284 CheckSpinLock(SpinLock, CheckData, 1);
285 CheckData->ReleaseNoLower(SpinLock, CheckData);
286 CheckSpinLock(SpinLock, CheckData, 0);
287
288 /* acquire without raise, but normal release */
289 CheckData->AcquireNoRaise(SpinLock, CheckData);
290 CheckSpinLock(SpinLock, CheckData, 1);
291 CheckData->Release(SpinLock, CheckData);
292 CheckSpinLock(SpinLock, CheckData, 0);
293
294 /* acquire normally but release without lower */
295 CheckData->Acquire(SpinLock, CheckData);
296 CheckSpinLock(SpinLock, CheckData, 1);
297 CheckData->ReleaseNoLower(SpinLock, CheckData);
298 CheckSpinLock(SpinLock, CheckData, 0);
299 CheckData->IsAcquired = FALSE;
300 KmtSetIrql(CheckData->OriginalIrql);
301
302 if (CheckData->TryAcquireNoRaise &&
303 !skip(pKeTryToAcquireSpinLockAtDpcLevel != NULL, "KeTryToAcquireSpinLockAtDpcLevel unavailable\n"))
304 {
305 CheckSpinLock(SpinLock, CheckData, 0);
306 ok_bool_true(CheckData->TryAcquireNoRaise(SpinLock, CheckData), "TryAcquireNoRaise returned");
307 CheckSpinLock(SpinLock, CheckData, 1);
308 if (!KmtIsCheckedBuild)
309 {
310 ok_bool_true(CheckData->TryAcquireNoRaise(SpinLock, CheckData), "TryAcquireNoRaise returned");
311 CheckSpinLock(SpinLock, CheckData, 1);
312 }
313 CheckData->ReleaseNoLower(SpinLock, CheckData);
314 CheckSpinLock(SpinLock, CheckData, 0);
315 }
316 }
317
318 ok_irql(CheckData->OriginalIrql);
319 /* make sure we survive this in case of error */
320 KmtSetIrql(CheckData->OriginalIrql);
321 }
322
323 START_TEST(KeSpinLock)
324 {
325 KSPIN_LOCK SpinLock = (KSPIN_LOCK)0x5555555555555555LL;
326 PKSPIN_LOCK pSpinLock = &SpinLock;
327 KIRQL Irql, SynchIrql = KmtIsMultiProcessorBuild ? IPI_LEVEL - 2 : DISPATCH_LEVEL;
328 KIRQL OriginalIrqls[] = { PASSIVE_LEVEL, APC_LEVEL, DISPATCH_LEVEL, HIGH_LEVEL };
329 CHECK_DATA TestData[] =
330 {
331 { CheckLock, DISPATCH_LEVEL, AcquireNormal, ReleaseNormal, NULL, AcquireNoRaise, ReleaseNoLower, TryNoRaise },
332 { CheckLock, DISPATCH_LEVEL, AcquireExp, ReleaseExp, NULL, AcquireExpNoRaise, ReleaseExpNoLower, NULL },
333 /* TODO: this one is just weird!
334 { CheckLock, DISPATCH_LEVEL, AcquireNormal, ReleaseNormal, NULL, AcquireForDpc, ReleaseForDpc, NULL },*/
335 { CheckLock, DISPATCH_LEVEL, AcquireNormal, ReleaseNormal, NULL, AcquireInt, ReleaseInt, NULL },
336 { CheckLock, SynchIrql, AcquireSynch, ReleaseNormal, NULL, NULL, NULL, NULL },
337 { CheckQueueHandle, DISPATCH_LEVEL, AcquireInStackQueued, ReleaseInStackQueued, NULL, AcquireInStackNoRaise, ReleaseInStackNoRaise, NULL },
338 { CheckQueueHandle, SynchIrql, AcquireInStackSynch, ReleaseInStackQueued, NULL, NULL, NULL, NULL },
339 { CheckQueueHandle, DISPATCH_LEVEL, AcquireInStackQueued, ReleaseInStackQueued, NULL, AcquireInStackForDpc, ReleaseInStackForDpc, NULL },
340 { CheckQueue, DISPATCH_LEVEL, AcquireQueued, ReleaseQueued, TryQueued, NULL, NULL, NULL, LockQueuePfnLock },
341 { CheckQueue, SynchIrql, AcquireQueuedSynch, ReleaseQueued, TryQueuedSynch, NULL, NULL, NULL, LockQueuePfnLock },
342 };
343 int i, iIrql;
344 PKPRCB Prcb;
345
346 pKeTryToAcquireSpinLockAtDpcLevel = KmtGetSystemRoutineAddress(L"KeTryToAcquireSpinLockAtDpcLevel");
347 pKeAcquireInStackQueuedSpinLockForDpc = KmtGetSystemRoutineAddress(L"KeAcquireInStackQueuedSpinLockForDpc");
348 pKeReleaseInStackQueuedSpinLockForDpc = KmtGetSystemRoutineAddress(L"KeReleaseInStackQueuedSpinLockForDpc");
349 pKeTestSpinLock = KmtGetSystemRoutineAddress(L"KeTestSpinLock");
350
351 Prcb = KeGetCurrentPrcb();
352
353 /* KeInitializeSpinLock */
354 memset(&SpinLock, 0x55, sizeof SpinLock);
355 KeInitializeSpinLock(&SpinLock);
356 ok_eq_ulongptr(SpinLock, 0);
357
358 /* KeTestSpinLock */
359 if (!skip(pKeTestSpinLock != NULL, "KeTestSpinLock unavailable\n"))
360 {
361 ok_bool_true(pKeTestSpinLock(&SpinLock), "KeTestSpinLock returned");
362 SpinLock = 1;
363 ok_bool_false(pKeTestSpinLock(&SpinLock), "KeTestSpinLock returned");
364 SpinLock = 2;
365 ok_bool_false(pKeTestSpinLock(&SpinLock), "KeTestSpinLock returned");
366 SpinLock = (ULONG_PTR)-1;
367 ok_bool_false(pKeTestSpinLock(&SpinLock), "KeTestSpinLock returned");
368 SpinLock = (ULONG_PTR)1 << (sizeof(ULONG_PTR) * CHAR_BIT - 1);
369 ok_bool_false(pKeTestSpinLock(&SpinLock), "KeTestSpinLock returned");
370 SpinLock = 0;
371 ok_bool_true(pKeTestSpinLock(&SpinLock), "KeTestSpinLock returned");
372 }
373
374 /* on UP none of the following functions actually looks at the spinlock! */
375 if (!KmtIsMultiProcessorBuild && !KmtIsCheckedBuild)
376 pSpinLock = NULL;
377
378 for (i = 0; i < sizeof TestData / sizeof TestData[0]; ++i)
379 {
380 memset(&SpinLock, 0x55, sizeof SpinLock);
381 KeInitializeSpinLock(&SpinLock);
382 if (TestData[i].Check == CheckQueueHandle)
383 memset(&TestData[i].QueueHandle, 0x55, sizeof TestData[i].QueueHandle);
384 if (TestData[i].Check == CheckQueue)
385 {
386 TestData[i].Queue = &Prcb->LockQueue[TestData[i].QueueNumber];
387 TestData[i].UntouchedValue = NULL;
388 }
389 else
390 TestData[i].UntouchedValue = (PVOID)0x5555555555555555LL;
391
392 for (iIrql = 0; iIrql < sizeof OriginalIrqls / sizeof OriginalIrqls[0]; ++iIrql)
393 {
394 if (KmtIsCheckedBuild && OriginalIrqls[iIrql] > DISPATCH_LEVEL)
395 continue;
396 KeRaiseIrql(OriginalIrqls[iIrql], &Irql);
397 TestData[i].OriginalIrql = OriginalIrqls[iIrql];
398 TestData[i].IsAcquired = FALSE;
399 TestSpinLock(pSpinLock, &TestData[i]);
400 KeLowerIrql(Irql);
401 }
402 }
403
404 KmtSetIrql(PASSIVE_LEVEL);
405 }