1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * PROJECT:         ReactOS kernel-mode tests
 * LICENSE:         GPLv2+ - See COPYING in the top level directory
 * PURPOSE:         Kernel-Mode Test Suite Interrupt test
 * PROGRAMMER:      Thomas Faber <thomas.faber@reactos.org>
 */

#include <kmt_test.h>

#define NDEBUG
#include <debug.h>

#define CheckSpinLock(Lock, Locked) do                  \
{                                                       \
    if (KmtIsMultiProcessorBuild)                       \
        ok_eq_ulongptr(*(Lock), (Locked) != 0);         \
    else                                                \
        ok_eq_ulongptr(*(Lock), 0);                     \
} while (0)

typedef struct
{
    BOOLEAN ReturnValue;
    KIRQL ExpectedIrql;
    PKINTERRUPT Interrupt;
} TEST_CONTEXT, *PTEST_CONTEXT;

static KSYNCHRONIZE_ROUTINE SynchronizeRoutine;

static
BOOLEAN
NTAPI
SynchronizeRoutine(
    IN PVOID Context)
{
    PTEST_CONTEXT TestContext = Context;

    ok_irql(TestContext->ExpectedIrql);

    CheckSpinLock(TestContext->Interrupt->ActualLock, TRUE);

    return TestContext->ReturnValue;
}

static
VOID
TestSynchronizeExecution(VOID)
{
    KINTERRUPT Interrupt;
    TEST_CONTEXT TestContext;
    KIRQL SynchIrql;
    KIRQL OriginalIrql;
    KIRQL Irql;
    KSPIN_LOCK ActualLock;
    BOOLEAN Ret;

    RtlFillMemory(&Interrupt, sizeof Interrupt, 0x55);
    Interrupt.ActualLock = &ActualLock;
    KeInitializeSpinLock(Interrupt.ActualLock);
    CheckSpinLock(Interrupt.ActualLock, FALSE);

    TestContext.Interrupt = &Interrupt;
    TestContext.ReturnValue = TRUE;<--- TestContext.ReturnValue is assigned

    for (TestContext.ReturnValue = 0; TestContext.ReturnValue <= 2; ++TestContext.ReturnValue)<--- TestContext.ReturnValue is overwritten
    {
        for (OriginalIrql = PASSIVE_LEVEL; OriginalIrql <= HIGH_LEVEL; ++OriginalIrql)
        {
            /* TODO: don't hardcode this :| */
            if (OriginalIrql == 3 || (OriginalIrql >= 11 && OriginalIrql <= 26) || OriginalIrql == 30)
                continue;
            KeRaiseIrql(OriginalIrql, &Irql);
            for (SynchIrql = max(DISPATCH_LEVEL, OriginalIrql); SynchIrql <= HIGH_LEVEL; ++SynchIrql)
            {
                if (SynchIrql == 3 || (SynchIrql >= 11 && SynchIrql <= 26) || SynchIrql == 30)
                    continue;
                Interrupt.SynchronizeIrql = SynchIrql;
                ok_irql(OriginalIrql);
                CheckSpinLock(Interrupt.ActualLock, FALSE);
                TestContext.ExpectedIrql = SynchIrql;
                Ret = KeSynchronizeExecution(&Interrupt, SynchronizeRoutine, &TestContext);
                ok_eq_int(Ret, TestContext.ReturnValue);
                ok_irql(OriginalIrql);
                CheckSpinLock(Interrupt.ActualLock, FALSE);
                /* TODO: Check that all other fields of the interrupt are untouched */
            }
            KeLowerIrql(Irql);
        }
    }
}

START_TEST(IoInterrupt)
{
    TestSynchronizeExecution();
}