[APITESTS]
[reactos.git] / rostests / apitests / ntdll / NtProtectVirtualMemory.c
1 /*
2 * PROJECT: ReactOS API Tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for the NtProtectVirtualMemory API
5 */
6
7 #include <apitest.h>
8
9 #define WIN32_NO_STATUS
10 #include <ndk/rtlfuncs.h>
11 #include <ndk/mmfuncs.h>
12
13 START_TEST(NtProtectVirtualMemory)
14 {
15 ULONG* allocationStart = NULL;
16 NTSTATUS status;
17 SIZE_T allocationSize;
18 ULONG oldProtection;
19
20 /* Reserve a page */
21 allocationSize = PAGE_SIZE;
22 status = NtAllocateVirtualMemory(NtCurrentProcess(),
23 (void**)&allocationStart,
24 0,
25 &allocationSize,
26 MEM_RESERVE,
27 PAGE_NOACCESS);
28 ok(NT_SUCCESS(status), "Reserving memory failed\n");
29
30 /* Commit the page (RW) */
31 status = NtAllocateVirtualMemory(NtCurrentProcess(),
32 (void**)&allocationStart,
33 0,
34 &allocationSize,
35 MEM_COMMIT,
36 PAGE_READWRITE);
37 ok(NT_SUCCESS(status), "Commiting memory failed\n");
38
39 /* Try writing it */
40 StartSeh()
41 {
42 *allocationStart = 0xFF;
43 } EndSeh(STATUS_SUCCESS);
44
45 /* Try reading it */
46 StartSeh()
47 {
48 ok(*allocationStart == 0xFF, "Memory was not written\n");
49 } EndSeh(STATUS_SUCCESS);
50
51 /* Set it as read only */
52 status = NtProtectVirtualMemory(NtCurrentProcess(),
53 (void**)&allocationStart,
54 &allocationSize,
55 PAGE_READONLY,
56 &oldProtection);
57 ok(NT_SUCCESS(status), "NtProtectVirtualMemory failed.\n");
58 ok(oldProtection == PAGE_READWRITE, "Expected PAGE_READWRITE, got %08x.\n", oldProtection);
59
60 /* Try writing it */
61 StartSeh()
62 {
63 *allocationStart = 0xAA;
64 } EndSeh(STATUS_ACCESS_VIOLATION);
65
66 /* Try reading it */
67 StartSeh()
68 {
69 ok(*allocationStart == 0xFF, "read-only memory were changed.\n");
70 } EndSeh(STATUS_SUCCESS);
71
72 /* Set it as no access */
73 status = NtProtectVirtualMemory(NtCurrentProcess(),
74 (void**)&allocationStart,
75 &allocationSize,
76 PAGE_NOACCESS,
77 &oldProtection);
78 ok(NT_SUCCESS(status), "NtProtectVirtualMemory failed.\n");
79 ok(oldProtection == PAGE_READONLY, "Expected PAGE_READONLY, got %08x.\n", oldProtection);
80
81 /* Try writing it */
82 StartSeh()
83 {
84 *allocationStart = 0xAA;
85 } EndSeh(STATUS_ACCESS_VIOLATION);
86
87 /* Try reading it */
88 StartSeh()
89 {
90 ok(*allocationStart == 0, "Test should not go as far as this.\n");
91 } EndSeh(STATUS_ACCESS_VIOLATION);
92
93 /* Free memory */
94 status = NtFreeVirtualMemory(NtCurrentProcess(),
95 (void**)&allocationStart,
96 &allocationSize,
97 MEM_RELEASE);
98 ok(NT_SUCCESS(status), "Failed freeing memory.\n");
99 }