2012-09-28 07:09:08 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS kernel-mode tests
|
|
|
|
* LICENSE: GPLv2+ - See COPYING in the top level directory
|
|
|
|
* PURPOSE: Kernel-Mode Test Suite Mutant/Mutex test
|
2013-10-12 16:05:54 +00:00
|
|
|
* PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
|
2012-09-28 07:09:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kmt_test.h>
|
|
|
|
|
2015-02-15 10:23:21 +00:00
|
|
|
static
|
|
|
|
_IRQL_requires_min_(PASSIVE_LEVEL)
|
|
|
|
_IRQL_requires_max_(DISPATCH_LEVEL)
|
|
|
|
BOOLEAN
|
|
|
|
(NTAPI
|
|
|
|
*pKeAreAllApcsDisabled)(VOID);
|
|
|
|
|
2012-09-28 07:09:08 +00:00
|
|
|
#define ULONGS_PER_POINTER (sizeof(PVOID) / sizeof(ULONG))
|
|
|
|
#define MUTANT_SIZE (2 + 6 * ULONGS_PER_POINTER)
|
|
|
|
|
|
|
|
C_ASSERT(sizeof(DISPATCHER_HEADER) == 8 + 2 * sizeof(PVOID));
|
|
|
|
C_ASSERT(sizeof(KMUTANT) == sizeof(DISPATCHER_HEADER) + 3 * sizeof(PVOID) + sizeof(PVOID));
|
|
|
|
C_ASSERT(sizeof(KMUTANT) == MUTANT_SIZE * sizeof(ULONG));
|
|
|
|
|
2015-09-19 23:59:02 +00:00
|
|
|
#define CheckMutex(Mutex, State, New, ExpectedApcDisable) do { \
|
2012-09-28 07:09:08 +00:00
|
|
|
PKTHREAD Thread = KeGetCurrentThread(); \
|
|
|
|
ok_eq_uint((Mutex)->Header.Type, MutantObject); \
|
|
|
|
ok_eq_uint((Mutex)->Header.Abandoned, 0x55); \
|
|
|
|
ok_eq_uint((Mutex)->Header.Size, MUTANT_SIZE); \
|
|
|
|
ok_eq_uint((Mutex)->Header.DpcActive, 0x55); \
|
|
|
|
ok_eq_pointer((Mutex)->Header.WaitListHead.Flink, \
|
|
|
|
&(Mutex)->Header.WaitListHead); \
|
|
|
|
ok_eq_pointer((Mutex)->Header.WaitListHead.Blink, \
|
|
|
|
&(Mutex)->Header.WaitListHead); \
|
2015-09-19 23:59:02 +00:00
|
|
|
if ((State) <= 0) \
|
2012-09-28 07:09:08 +00:00
|
|
|
{ \
|
2015-09-19 23:59:02 +00:00
|
|
|
ok_eq_long((Mutex)->Header.SignalState, State); \
|
2012-09-28 07:09:08 +00:00
|
|
|
ok_eq_pointer((Mutex)->MutantListEntry.Flink, &Thread->MutantListHead); \
|
|
|
|
ok_eq_pointer((Mutex)->MutantListEntry.Blink, &Thread->MutantListHead); \
|
|
|
|
ok_eq_pointer(Thread->MutantListHead.Flink, &(Mutex)->MutantListEntry); \
|
|
|
|
ok_eq_pointer(Thread->MutantListHead.Blink, &(Mutex)->MutantListEntry); \
|
|
|
|
ok_eq_pointer((Mutex)->OwnerThread, Thread); \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
2015-09-19 23:59:02 +00:00
|
|
|
ok_eq_long((Mutex)->Header.SignalState, State); \
|
2012-09-28 07:09:08 +00:00
|
|
|
if (New) \
|
|
|
|
{ \
|
|
|
|
ok_eq_pointer((Mutex)->MutantListEntry.Flink, \
|
|
|
|
(PVOID)0x5555555555555555ULL); \
|
|
|
|
ok_eq_pointer((Mutex)->MutantListEntry.Blink, \
|
|
|
|
(PVOID)0x5555555555555555ULL); \
|
|
|
|
} \
|
|
|
|
ok_eq_pointer(Thread->MutantListHead.Flink, &Thread->MutantListHead); \
|
|
|
|
ok_eq_pointer(Thread->MutantListHead.Blink, &Thread->MutantListHead); \
|
|
|
|
ok_eq_pointer((Mutex)->OwnerThread, NULL); \
|
|
|
|
} \
|
|
|
|
ok_eq_uint((Mutex)->Abandoned, 0); \
|
|
|
|
ok_eq_uint((Mutex)->ApcDisable, ExpectedApcDisable); \
|
|
|
|
} while (0)
|
|
|
|
|
2012-10-03 20:52:33 +00:00
|
|
|
#define CheckApcs(KernelApcsDisabled, SpecialApcsDisabled, AllApcsDisabled, Irql) do \
|
|
|
|
{ \
|
|
|
|
ok_eq_bool(KeAreApcsDisabled(), KernelApcsDisabled || SpecialApcsDisabled); \
|
|
|
|
ok_eq_int(Thread->KernelApcDisable, KernelApcsDisabled); \
|
2015-02-15 10:23:21 +00:00
|
|
|
if (pKeAreAllApcsDisabled) \
|
|
|
|
ok_eq_bool(pKeAreAllApcsDisabled(), AllApcsDisabled); \
|
2012-10-03 20:52:33 +00:00
|
|
|
ok_eq_int(Thread->SpecialApcDisable, SpecialApcsDisabled); \
|
|
|
|
ok_irql(Irql); \
|
|
|
|
} while (0)
|
|
|
|
|
2012-09-28 07:09:08 +00:00
|
|
|
static
|
|
|
|
VOID
|
|
|
|
TestMutant(VOID)
|
|
|
|
{
|
2012-10-03 20:52:33 +00:00
|
|
|
NTSTATUS Status;
|
2012-09-28 07:09:08 +00:00
|
|
|
KMUTANT Mutant;
|
|
|
|
LONG State;
|
2015-09-19 23:59:02 +00:00
|
|
|
LONG i;
|
2012-10-03 20:52:33 +00:00
|
|
|
PKTHREAD Thread = KeGetCurrentThread();
|
2012-09-28 07:09:08 +00:00
|
|
|
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
2012-09-28 07:09:08 +00:00
|
|
|
RtlFillMemory(&Mutant, sizeof(Mutant), 0x55);
|
|
|
|
KeInitializeMutant(&Mutant, FALSE);
|
2015-09-19 23:59:02 +00:00
|
|
|
CheckMutex(&Mutant, 1L, TRUE, 0);
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
2012-09-28 07:09:08 +00:00
|
|
|
|
|
|
|
RtlFillMemory(&Mutant, sizeof(Mutant), 0x55);
|
|
|
|
KeInitializeMutant(&Mutant, TRUE);
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
2015-09-19 23:59:02 +00:00
|
|
|
CheckMutex(&Mutant, 0L, TRUE, 0);
|
2012-10-03 20:52:33 +00:00
|
|
|
State = KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
|
2015-09-19 23:59:02 +00:00
|
|
|
ok_eq_long(State, 0L);
|
|
|
|
CheckMutex(&Mutant, 1L, FALSE, 0);
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
/* Acquire and release */
|
|
|
|
Status = KeWaitForSingleObject(&Mutant,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
2015-09-19 23:59:02 +00:00
|
|
|
CheckMutex(&Mutant, 0L, TRUE, 0);
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
2012-09-28 07:09:08 +00:00
|
|
|
State = KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
|
2015-09-19 23:59:02 +00:00
|
|
|
ok_eq_long(State, 0L);
|
|
|
|
CheckMutex(&Mutant, 1L, FALSE, 0);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
/* Acquire recursively */
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
KmtStartSeh()
|
|
|
|
Status = KeWaitForSingleObject(&Mutant,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
KmtEndSeh(STATUS_SUCCESS);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
|
|
|
CheckMutex(&Mutant, -i, FALSE, 0);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 7; i++)
|
|
|
|
{
|
|
|
|
KmtStartSeh()
|
|
|
|
State = KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
|
|
|
|
KmtEndSeh(STATUS_SUCCESS);
|
|
|
|
ok_eq_long(State, -7L + i);
|
|
|
|
CheckMutex(&Mutant, -6L + i, FALSE, 0);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
State = KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
|
|
|
|
ok_eq_long(State, 0L);
|
|
|
|
CheckMutex(&Mutant, 1L, FALSE, 0);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
/* Pretend to acquire it recursively -MINLONG times */
|
|
|
|
KmtStartSeh()
|
|
|
|
Status = KeWaitForSingleObject(&Mutant,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
KmtEndSeh(STATUS_SUCCESS);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
|
|
|
CheckMutex(&Mutant, 0L, FALSE, 0);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
Mutant.Header.SignalState = MINLONG + 1;
|
|
|
|
KmtStartSeh()
|
|
|
|
Status = KeWaitForSingleObject(&Mutant,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
KmtEndSeh(STATUS_SUCCESS);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
|
|
|
CheckMutex(&Mutant, (LONG)MINLONG, FALSE, 0);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
KmtStartSeh()
|
|
|
|
KeWaitForSingleObject(&Mutant,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
KmtEndSeh(STATUS_MUTANT_LIMIT_EXCEEDED);
|
|
|
|
CheckMutex(&Mutant, (LONG)MINLONG, FALSE, 0);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
State = KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
|
|
|
|
ok_eq_long(State, (LONG)MINLONG);
|
|
|
|
CheckMutex(&Mutant, (LONG)MINLONG + 1L, FALSE, 0);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
Mutant.Header.SignalState = -1;
|
|
|
|
State = KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
|
|
|
|
ok_eq_long(State, -1L);
|
|
|
|
CheckMutex(&Mutant, 0L, FALSE, 0);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
State = KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
|
|
|
|
ok_eq_long(State, 0L);
|
|
|
|
CheckMutex(&Mutant, 1L, FALSE, 0);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
/* Now release it once too often */
|
|
|
|
KmtStartSeh()
|
|
|
|
KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
|
|
|
|
KmtEndSeh(STATUS_MUTANT_NOT_OWNED);
|
|
|
|
CheckMutex(&Mutant, 1L, FALSE, 0);
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
2012-09-28 07:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
VOID
|
|
|
|
TestMutex(VOID)
|
|
|
|
{
|
2012-10-03 20:52:33 +00:00
|
|
|
NTSTATUS Status;
|
2012-09-28 07:09:08 +00:00
|
|
|
KMUTEX Mutex;
|
2012-10-03 20:52:33 +00:00
|
|
|
LONG State;
|
2015-09-19 23:59:02 +00:00
|
|
|
LONG i;
|
2012-10-03 20:52:33 +00:00
|
|
|
PKTHREAD Thread = KeGetCurrentThread();
|
2012-09-28 07:09:08 +00:00
|
|
|
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
2012-09-28 07:09:08 +00:00
|
|
|
RtlFillMemory(&Mutex, sizeof(Mutex), 0x55);
|
|
|
|
KeInitializeMutex(&Mutex, 0);
|
2015-09-19 23:59:02 +00:00
|
|
|
CheckMutex(&Mutex, 1L, TRUE, 1);
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
2012-09-28 07:09:08 +00:00
|
|
|
|
|
|
|
RtlFillMemory(&Mutex, sizeof(Mutex), 0x55);
|
|
|
|
KeInitializeMutex(&Mutex, 123);
|
2015-09-19 23:59:02 +00:00
|
|
|
CheckMutex(&Mutex, 1L, TRUE, 1);
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
2015-09-19 23:59:02 +00:00
|
|
|
/* Acquire and release */
|
2012-10-03 20:52:33 +00:00
|
|
|
Status = KeWaitForSingleObject(&Mutex,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
2015-09-19 23:59:02 +00:00
|
|
|
CheckMutex(&Mutex, 0L, FALSE, 1);
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(-1, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
State = KeReleaseMutex(&Mutex, FALSE);
|
2015-09-19 23:59:02 +00:00
|
|
|
ok_eq_long(State, 0L);
|
|
|
|
CheckMutex(&Mutex, 1L, FALSE, 1);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
/* Acquire recursively */
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
KmtStartSeh()
|
|
|
|
Status = KeWaitForSingleObject(&Mutex,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
KmtEndSeh(STATUS_SUCCESS);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
|
|
|
CheckMutex(&Mutex, -i, FALSE, 1);
|
|
|
|
CheckApcs(-1, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 7; i++)
|
|
|
|
{
|
|
|
|
KmtStartSeh()
|
|
|
|
State = KeReleaseMutex(&Mutex, FALSE);
|
|
|
|
KmtEndSeh(STATUS_SUCCESS);
|
|
|
|
ok_eq_long(State, -7L + i);
|
|
|
|
CheckMutex(&Mutex, -6L + i, FALSE, 1);
|
|
|
|
CheckApcs(-1, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
State = KeReleaseMutex(&Mutex, FALSE);
|
|
|
|
ok_eq_long(State, 0L);
|
|
|
|
CheckMutex(&Mutex, 1L, FALSE, 1);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
/* Pretend to acquire it recursively -MINLONG times */
|
|
|
|
KmtStartSeh()
|
|
|
|
Status = KeWaitForSingleObject(&Mutex,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
KmtEndSeh(STATUS_SUCCESS);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
|
|
|
CheckMutex(&Mutex, 0L, FALSE, 1);
|
|
|
|
CheckApcs(-1, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
Mutex.Header.SignalState = MINLONG + 1;
|
|
|
|
KmtStartSeh()
|
|
|
|
Status = KeWaitForSingleObject(&Mutex,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
KmtEndSeh(STATUS_SUCCESS);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
|
|
|
CheckMutex(&Mutex, (LONG)MINLONG, FALSE, 1);
|
|
|
|
CheckApcs(-1, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
KmtStartSeh()
|
|
|
|
KeWaitForSingleObject(&Mutex,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
KmtEndSeh(STATUS_MUTANT_LIMIT_EXCEEDED);
|
|
|
|
CheckMutex(&Mutex, (LONG)MINLONG, FALSE, 1);
|
|
|
|
CheckApcs(-1, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
State = KeReleaseMutex(&Mutex, FALSE);
|
|
|
|
ok_eq_long(State, (LONG)MINLONG);
|
|
|
|
CheckMutex(&Mutex, (LONG)MINLONG + 1L, FALSE, 1);
|
|
|
|
CheckApcs(-1, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
Mutex.Header.SignalState = -1;
|
|
|
|
State = KeReleaseMutex(&Mutex, FALSE);
|
|
|
|
ok_eq_long(State, -1L);
|
|
|
|
CheckMutex(&Mutex, 0L, FALSE, 1);
|
|
|
|
CheckApcs(-1, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
State = KeReleaseMutex(&Mutex, FALSE);
|
|
|
|
ok_eq_long(State, 0L);
|
|
|
|
CheckMutex(&Mutex, 1L, FALSE, 1);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
/* Now release it once too often */
|
|
|
|
KmtStartSeh()
|
|
|
|
KeReleaseMutex(&Mutex, FALSE);
|
|
|
|
KmtEndSeh(STATUS_MUTANT_NOT_OWNED);
|
|
|
|
CheckMutex(&Mutex, 1L, FALSE, 1);
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
2012-09-28 07:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(KeMutex)
|
|
|
|
{
|
2015-02-15 10:23:21 +00:00
|
|
|
pKeAreAllApcsDisabled = KmtGetSystemRoutineAddress(L"KeAreAllApcsDisabled");
|
|
|
|
if (skip(pKeAreAllApcsDisabled != NULL, "KeAreAllApcsDisabled unavailable\n"))
|
|
|
|
{
|
|
|
|
/* We can live without this function here */
|
|
|
|
}
|
|
|
|
|
2012-09-28 07:09:08 +00:00
|
|
|
TestMutant();
|
|
|
|
TestMutex();
|
|
|
|
}
|