2 * PROJECT: ReactOS API Tests
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Test for NtFreeVirtualMemory
5 * COPYRIGHT: Copyright 2011 Jérôme Gardou <jerome.gardou@reactos.org>
10 static void Test_NtFreeVirtualMemory(void)
12 PVOID Buffer
= NULL
, Buffer2
;
13 SIZE_T Length
= PAGE_SIZE
;
16 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
22 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status
);
23 ok(Length
== PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
24 ok(((ULONG_PTR
)Buffer
% PAGE_SIZE
) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
26 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
30 ok(Status
== STATUS_SUCCESS
, "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
32 /* Now try to free more than we got */
34 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
38 ok(Status
== STATUS_UNABLE_TO_FREE_VM
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
40 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
44 ok(Status
== STATUS_UNABLE_TO_FREE_VM
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
46 /* Free out of bounds from the wrong origin */
48 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+1);
50 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
54 ok(Status
== STATUS_UNABLE_TO_FREE_VM
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
56 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+1);
58 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
62 ok(Status
== STATUS_UNABLE_TO_FREE_VM
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
64 /* Same but in bounds */
65 Length
= PAGE_SIZE
- 1;
66 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+1);
68 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
72 ok(Status
== STATUS_SUCCESS
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
73 ok(Buffer2
== Buffer
, "NtFreeVirtualMemory set wrong buffer.\n");
74 ok(Length
== PAGE_SIZE
, "NtFreeVirtualMemory did not round Length to PAGE_SIZE.\n");
76 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+1);
78 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
82 ok(Status
== STATUS_SUCCESS
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
83 ok(Buffer2
== Buffer
, "NtFreeVirtualMemory set wrong buffer.\n");
84 ok(Length
== PAGE_SIZE
, "NtFreeVirtualMemory did not round Length to PAGE_SIZE.\n");
86 /* Now allocate two pages and try to free them one after the other */
88 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
94 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status
);
95 ok(Length
== 2*PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
96 ok(((ULONG_PTR
)Buffer
% PAGE_SIZE
) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
100 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
104 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
105 ok(Length
== PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
106 ok(Buffer2
== Buffer
, "The buffer is not aligned to PAGE_SIZE.\n");
108 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+PAGE_SIZE
);
110 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
114 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
115 ok(Length
== PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
116 ok(Buffer2
== (PVOID
)((ULONG_PTR
)Buffer
+PAGE_SIZE
), "The buffer is not aligned to PAGE_SIZE.\n");
118 /* Same, but try to free the second page before the first one */
119 Length
= 2*PAGE_SIZE
;
120 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
126 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status
);
127 ok(Length
== 2*PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
128 ok(((ULONG_PTR
)Buffer
% PAGE_SIZE
) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
130 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+PAGE_SIZE
);
132 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
136 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
137 ok(Length
== PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
138 ok(Buffer2
== (PVOID
)((ULONG_PTR
)Buffer
+PAGE_SIZE
), "The buffer is not aligned to PAGE_SIZE.\n");
142 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
146 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
147 ok(Length
== PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
148 ok(Buffer2
== Buffer
, "The buffer is not aligned to PAGE_SIZE.\n");
150 /* Now allocate two pages and try to free them in the middle */
151 Length
= 2*PAGE_SIZE
;
152 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
158 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status
);
159 ok(Length
== 2*PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
160 ok(((ULONG_PTR
)Buffer
% PAGE_SIZE
) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
162 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+1);
164 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
168 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
169 ok(Length
== 2*PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
170 ok(Buffer2
== Buffer
, "The buffer is not aligned to PAGE_SIZE.\n");
173 START_TEST(NtFreeVirtualMemory
)
175 Test_NtFreeVirtualMemory();