ReactOS 0.4.15-dev-7924-g5949c20
NtProtectVirtualMemory.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS API Tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for the NtProtectVirtualMemory API
5 * PROGRAMMERS: Jérôme Gardou <jerome.gardou@reactos.org>
6 * Thomas Faber <thomas.faber@reactos.org>
7 */
8
9#include "precomp.h"
10
11static
12void
14{
15 ULONG* allocationStart = NULL;
17 SIZE_T allocationSize;
18 ULONG oldProtection;
19
20 /* Reserve a page */
21 allocationSize = PAGE_SIZE;
23 (void**)&allocationStart,
24 0,
25 &allocationSize,
28 ok(NT_SUCCESS(status), "Reserving memory failed\n");
29
30 /* Commit the page (RW) */
32 (void**)&allocationStart,
33 0,
34 &allocationSize,
37 ok(NT_SUCCESS(status), "Commiting memory failed\n");
38
39 /* Try writing it */
40 StartSeh()
41 {
42 *allocationStart = 0xFF;
44
45 /* Try reading it */
46 StartSeh()
47 {
48 ok(*allocationStart == 0xFF, "Memory was not written\n");
50
51 /* Set it as read only */
53 (void**)&allocationStart,
54 &allocationSize,
56 &oldProtection);
57 ok(NT_SUCCESS(status), "NtProtectVirtualMemory failed.\n");
58 ok(oldProtection == PAGE_READWRITE, "Expected PAGE_READWRITE, got %08lx.\n", oldProtection);
59
60 /* Try writing it */
61 StartSeh()
62 {
63 *allocationStart = 0xAA;
65
66 /* Try reading it */
67 StartSeh()
68 {
69 ok(*allocationStart == 0xFF, "read-only memory were changed.\n");
71
72 /* Set it as no access */
74 (void**)&allocationStart,
75 &allocationSize,
77 &oldProtection);
78 ok(NT_SUCCESS(status), "NtProtectVirtualMemory failed.\n");
79 ok(oldProtection == PAGE_READONLY, "Expected PAGE_READONLY, got %08lx.\n", oldProtection);
80
81 /* Try writing it */
82 StartSeh()
83 {
84 *allocationStart = 0xAA;
86
87 /* Try reading it */
88 StartSeh()
89 {
90 ok(*allocationStart == 0, "Test should not go as far as this.\n");
92
93 /* Set it as readable again */
95 (void**)&allocationStart,
96 &allocationSize,
98 &oldProtection);
99 ok(NT_SUCCESS(status), "NtProtectVirtualMemory failed.\n");
100 ok(oldProtection == PAGE_NOACCESS, "Expected PAGE_READONLY, got %08lx.\n", oldProtection);
101
102 /* Try writing it */
103 StartSeh()
104 {
105 *allocationStart = 0xAA;
107
108 /* Try reading it */
109 StartSeh()
110 {
111 ok(*allocationStart == 0xFF, "Memory content was not preserved.\n");
113
114 /* Free memory */
116 (void**)&allocationStart,
117 &allocationSize,
119 ok(NT_SUCCESS(status), "Failed freeing memory.\n");
120}
121
122/* Regression test for CORE-13311 */
123static
124void
126{
127 PVOID Mem;
128 SIZE_T Size;
130 ULONG Iteration, PageNumber;
131 PUCHAR Page;
132 ULONG OldProtection;
133
134 for (Iteration = 0; Iteration < 50000; Iteration++)
135 {
136 Mem = NULL;
137 Size = 16 * PAGE_SIZE;
139 &Mem,
140 0,
141 &Size,
145 if (!NT_SUCCESS(Status))
146 {
147 break;
148 }
149
150 for (PageNumber = 0; PageNumber < 16; PageNumber++)
151 {
152 Page = Mem;
153 Page += PageNumber * PAGE_SIZE;
154 ok(*Page == 0,
155 "[%lu, %lu] Got non-zero memory. %x at %p\n",
156 Iteration, PageNumber, *Page, Page);
157 *Page = 123;
158 }
159
161 &Mem,
162 &Size,
164 &OldProtection);
166 ok_hex(OldProtection, PAGE_READWRITE);
167
168 Size = 0;
170 &Mem,
171 &Size,
174 }
175}
176
178{
181}
static void TestReadWrite(void)
static void TestFreeNoAccess(void)
#define StartSeh()
Definition: _sntprintf.h:16
#define EndSeh(ExpectedStatus)
Definition: _sntprintf.h:17
#define ok_hex(expression, result)
Definition: atltest.h:94
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
#define ok(value,...)
Definition: atltest.h:57
#define START_TEST(x)
Definition: atltest.h:75
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define PAGE_READONLY
Definition: compat.h:138
#define PAGE_SIZE
Definition: env_spec_w32.h:49
Status
Definition: gdiplustypes.h:25
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define MEM_RESERVE
Definition: nt_native.h:1314
#define MEM_RELEASE
Definition: nt_native.h:1316
#define MEM_COMMIT
Definition: nt_native.h:1313
#define PAGE_NOACCESS
Definition: nt_native.h:1302
_In_ PVOID _Out_opt_ BOOLEAN _Out_opt_ PPFN_NUMBER Page
Definition: mm.h:1306
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5230
NTSTATUS NTAPI NtProtectVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UnsafeBaseAddress, IN OUT SIZE_T *UnsafeNumberOfBytesToProtect, IN ULONG NewAccessProtection, OUT PULONG UnsafeOldAccessProtection)
Definition: virtual.c:3111
NTSTATUS NTAPI NtAllocateVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UBaseAddress, IN ULONG_PTR ZeroBits, IN OUT PSIZE_T URegionSize, IN ULONG AllocationType, IN ULONG Protect)
Definition: virtual.c:4492
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define STATUS_SUCCESS
Definition: shellext.h:65
Definition: ps.c:97
ULONG_PTR SIZE_T
Definition: typedefs.h:80
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533