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));
|
|
|
|
|
|
|
|
#define CheckMutex(Mutex, Held, New, ExpectedApcDisable) do { \
|
|
|
|
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); \
|
|
|
|
if (Held) \
|
|
|
|
{ \
|
|
|
|
ok_eq_long((Mutex)->Header.SignalState, 0); \
|
|
|
|
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 \
|
|
|
|
{ \
|
|
|
|
ok_eq_long((Mutex)->Header.SignalState, 1); \
|
|
|
|
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;
|
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);
|
|
|
|
CheckMutex(&Mutant, FALSE, 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);
|
|
|
|
CheckMutex(&Mutant, TRUE, TRUE, 0);
|
|
|
|
State = KeReleaseMutant(&Mutant, 1, FALSE, FALSE);
|
|
|
|
ok_eq_long(State, 0);
|
|
|
|
CheckMutex(&Mutant, FALSE, FALSE, 0);
|
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
/* Acquire and release */
|
|
|
|
Status = KeWaitForSingleObject(&Mutant,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
2012-09-28 07:09:08 +00:00
|
|
|
CheckMutex(&Mutant, TRUE, 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);
|
|
|
|
ok_eq_long(State, 0);
|
|
|
|
CheckMutex(&Mutant, FALSE, 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;
|
|
|
|
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);
|
|
|
|
CheckMutex(&Mutex, FALSE, 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);
|
|
|
|
CheckMutex(&Mutex, FALSE, TRUE, 1);
|
2012-10-03 20:52:33 +00:00
|
|
|
CheckApcs(0, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
Status = KeWaitForSingleObject(&Mutex,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
|
|
|
CheckMutex(&Mutex, TRUE, FALSE, 1);
|
|
|
|
CheckApcs(-1, 0, FALSE, PASSIVE_LEVEL);
|
|
|
|
|
|
|
|
State = KeReleaseMutex(&Mutex, FALSE);
|
|
|
|
ok_eq_long(State, 0);
|
|
|
|
CheckMutex(&Mutex, FALSE, FALSE, 1);
|
|
|
|
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();
|
|
|
|
}
|