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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * PROJECT:         ReactOS kernel-mode tests
 * LICENSE:         GPLv2+ - See COPYING in the top level directory
 * PURPOSE:         Kernel-Mode Test Suite Exception test
 * PROGRAMMER:      Thomas Faber <thomas.faber@reactos.org>
 */

#define KMT_EMULATE_KERNEL
#include <kmt_test.h>

static
VOID
PossiblyRaise(
    _In_ BOOLEAN Raise)
{
    if (Raise)
    {
        ExRaiseStatus(STATUS_ASSERTION_FAILURE);
    }
}

static
VOID
InnerFunction(
    _Inout_ PULONG State,
    _In_ BOOLEAN Raise)
{
    _SEH2_VOLATILE INT Var = 123;
    static _SEH2_VOLATILE INT *AddressOfVar;

    AddressOfVar = &Var;
    ok_eq_ulong(*State, 1);
    _SEH2_TRY
    {
        *State = 2;
        PossiblyRaise(Raise);
        ok_eq_ulong(*State, 2);
        *State = 3;
    }
    _SEH2_FINALLY
    {
        ok_eq_int(Var, 123);
        ok_eq_pointer(&Var, AddressOfVar);
        if (Raise)
            ok_eq_ulong(*State, 2);
        else
            ok_eq_ulong(*State, 3);
        *State = 4;
    }
    _SEH2_END;

    ok_eq_int(Var, 123);
    ok_eq_pointer(&Var, AddressOfVar);
    ok_eq_ulong(*State, 4);
    *State = 5;
}

static
VOID
OuterFunction(
    _Inout_ PULONG State,
    _In_ BOOLEAN Raise)
{
    _SEH2_VOLATILE INT Var = 456;
    static _SEH2_VOLATILE INT *AddressOfVar;

    AddressOfVar = &Var;
    ok_eq_ulong(*State, 0);
    _SEH2_TRY
    {
        *State = 1;
        InnerFunction(State, Raise);
        ok_eq_ulong(*State, 5);
        *State = 6;
    }
    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
    {
        ok_eq_int(Var, 456);
        ok_eq_pointer(&Var, AddressOfVar);
        ok_eq_ulong(*State, 4);
        *State = 7;
    }
    _SEH2_END;

    ok_eq_int(Var, 456);
    ok_eq_pointer(&Var, AddressOfVar);
    if (Raise)
        ok_eq_ulong(*State, 7);
    else
        ok_eq_ulong(*State, 6);
    *State = 8;
}

static
VOID
TestNestedExceptionHandler(VOID)
{
    ULONG State;

    State = 0;
    OuterFunction(&State, FALSE);
    ok_eq_ulong(State, 8);

    State = 0;
    OuterFunction(&State, TRUE);
    ok_eq_ulong(State, 8);
}

START_TEST(RtlException)
{
    PCHAR Buffer[128];

    /* Access a valid pointer - must not trigger SEH */
    KmtStartSeh()
        RtlFillMemory(Buffer, sizeof(Buffer), 0x12);
    KmtEndSeh(STATUS_SUCCESS);

    /* Read from a NULL pointer - must cause an access violation */
    KmtStartSeh()
        (void)*(volatile CHAR *)NULL;<--- Null pointer dereference
    KmtEndSeh(STATUS_ACCESS_VIOLATION);

    /* Write to a NULL pointer - must cause an access violation */
    KmtStartSeh()
        *(volatile CHAR *)NULL = 5;
    KmtEndSeh(STATUS_ACCESS_VIOLATION);

    /* TODO: Find where MmBadPointer is defined - gives an unresolved external */
#if 0 //def KMT_KERNEL_MODE
    /* Read from MmBadPointer - must cause an access violation */
    KmtStartSeh()
        (void)*(volatile CHAR *)MmBadPointer;
    KmtEndSeh(STATUS_ACCESS_VIOLATION);

    /* Write to MmBadPointer - must cause an access violation */
    KmtStartSeh()
        *(volatile CHAR *)MmBadPointer = 5;
    KmtEndSeh(STATUS_ACCESS_VIOLATION);
#endif

    KmtStartSeh()
        ExRaiseStatus(STATUS_ACCESS_VIOLATION);
    KmtEndSeh(STATUS_ACCESS_VIOLATION);

    KmtStartSeh()
        ExRaiseStatus(STATUS_TIMEOUT);
    KmtEndSeh(STATUS_TIMEOUT);

    KmtStartSeh()
        ExRaiseStatus(STATUS_STACK_OVERFLOW);
    KmtEndSeh(STATUS_STACK_OVERFLOW);

    KmtStartSeh()
        ExRaiseStatus(STATUS_GUARD_PAGE_VIOLATION);
    KmtEndSeh(STATUS_GUARD_PAGE_VIOLATION);

    TestNestedExceptionHandler();

    /* We cannot test this in kernel mode easily - the stack is just "somewhere"
     * in system space, and there's no guard page below it */
#ifdef KMT_USER_MODE
    /* Overflow the stack - must cause a special exception */
    KmtStartSeh()
        volatile CHAR *Pointer;

        while (1)
        {
            Pointer = _alloca(1024);
            *Pointer = 5;
        }
    KmtEndSeh(STATUS_STACK_OVERFLOW);
#endif
}