1 /*
2  * PROJECT:     ReactOS kernel-mode tests
3  * LICENSE:     LGPL-2.1+ (https://spdx.org/licenses/LGPL-2.1+)
4  * PURPOSE:     Kernel-Mode Test Suite Guarded Memory example test
5  * COPYRIGHT:   Copyright 2011-2018 Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include <kmt_test.h>
9 
10 #ifdef __GNUC__
11 #pragma GCC diagnostic ignored "-Wnonnull"
12 #endif /* defined __GNUC__ */
13 
14 START_TEST(GuardedMemory)
15 {
16     NTSTATUS Status;
17     SIZE_T Size = 123;
18     PCHAR *Buffer;
19 
20     /* access some invalid memory to test SEH */
21     Status = STATUS_SUCCESS;
22     _SEH2_TRY
23     {
24         RtlFillMemory(NULL, 1, 0);
25     }
26     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
27     {
28         Status = _SEH2_GetExceptionCode();
29     } _SEH2_END;
30     ok_eq_hex(Status, STATUS_ACCESS_VIOLATION);
31 
32     /* get guarded mem */
33     Buffer = KmtAllocateGuarded(Size);
34 
35     if (skip(Buffer != NULL, "Failed to allocate guarded memory\n"))
36         return;
37 
38     /* access to guarded mem should be fine */
39     Status = STATUS_SUCCESS;
40     _SEH2_TRY
41     {
42         RtlFillMemory(Buffer, Size, 0);
43     }
44     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
45     {
46         Status = _SEH2_GetExceptionCode();
47     } _SEH2_END;
48     ok_eq_hex(Status, STATUS_SUCCESS);
49 
50     /* access one byte behind guarded mem must cause an access violation! */
51     Status = STATUS_SUCCESS;
52     _SEH2_TRY
53     {
54         RtlFillMemory(Buffer + Size, 1, 0);
55     }
56     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
57     {
58         Status = _SEH2_GetExceptionCode();
59     } _SEH2_END;
60     ok_eq_hex(Status, STATUS_ACCESS_VIOLATION);
61 
62     KmtFreeGuarded(Buffer);
63 }
64