ReactOS  0.4.15-dev-3173-g40ee59d
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 
11 static
12 void
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,
35  MEM_COMMIT,
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,
118  MEM_RELEASE);
119  ok(NT_SUCCESS(status), "Failed freeing memory.\n");
120 }
121 
122 /* Regression test for CORE-13311 */
123 static
124 void
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,
142  MEM_COMMIT,
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,
172  MEM_RELEASE);
174  }
175 }
176 
178 {
179  TestReadWrite();
181 }
unsigned char * PUCHAR
Definition: retypes.h:3
NTSTATUS NTAPI NtProtectVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UnsafeBaseAddress, IN OUT SIZE_T *UnsafeNumberOfBytesToProtect, IN ULONG NewAccessProtection, OUT PULONG UnsafeOldAccessProtection)
Definition: virtual.c:3100
LONG NTSTATUS
Definition: precomp.h:26
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
#define MEM_COMMIT
Definition: nt_native.h:1313
#define StartSeh()
Definition: _sntprintf.h:16
#define ok_hex(expression, result)
Definition: atltest.h:94
#define MEM_RESERVE
Definition: nt_native.h:1314
#define PAGE_NOACCESS
Definition: nt_native.h:1302
PFN_NUMBER Page
Definition: section.c:4923
#define NtCurrentProcess()
Definition: nt_native.h:1657
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define PAGE_SIZE
Definition: env_spec_w32.h:49
ULONG_PTR SIZE_T
Definition: typedefs.h:80
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:4481
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define ok(value,...)
Definition: atltest.h:57
START_TEST(NtProtectVirtualMemory)
#define NULL
Definition: types.h:112
#define PAGE_READONLY
Definition: compat.h:138
#define MEM_RELEASE
Definition: nt_native.h:1316
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
unsigned int ULONG
Definition: retypes.h:1
#define STATUS_SUCCESS
Definition: shellext.h:65
static void TestReadWrite(void)
static SERVICE_STATUS status
Definition: service.c:31
static void TestFreeNoAccess(void)
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define EndSeh(ExpectedStatus)
Definition: _sntprintf.h:17
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5204
Definition: ps.c:97