4 #define WIN32_NO_STATUS
5 #include <ndk/pstypes.h>
6 #include <ndk/mmfuncs.h>
8 static void Test_NtFreeVirtualMemory(void)
10 PVOID Buffer
= NULL
, Buffer2
;
11 SIZE_T Length
= PAGE_SIZE
;
14 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
20 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status
);
21 ok(Length
== PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
22 ok(((ULONG_PTR
)Buffer
% PAGE_SIZE
) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
24 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
28 ok(Status
== STATUS_SUCCESS
, "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
30 /* Now try to free more than we got */
32 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
36 ok(Status
== STATUS_UNABLE_TO_FREE_VM
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
38 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
42 ok(Status
== STATUS_UNABLE_TO_FREE_VM
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
44 /* Free out of bounds from the wrong origin */
46 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+1);
48 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
52 ok(Status
== STATUS_UNABLE_TO_FREE_VM
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
54 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+1);
56 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
60 ok(Status
== STATUS_UNABLE_TO_FREE_VM
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
62 /* Same but in bounds */
63 Length
= PAGE_SIZE
- 1;
64 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+1);
66 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
70 ok(Status
== STATUS_SUCCESS
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
71 ok(Buffer2
== Buffer
, "NtFreeVirtualMemory set wrong buffer.\n");
72 ok(Length
== PAGE_SIZE
, "NtFreeVirtualMemory did not round Length to PAGE_SIZE.\n");
74 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+1);
76 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
80 ok(Status
== STATUS_SUCCESS
, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status
);
81 ok(Buffer2
== Buffer
, "NtFreeVirtualMemory set wrong buffer.\n");
82 ok(Length
== PAGE_SIZE
, "NtFreeVirtualMemory did not round Length to PAGE_SIZE.\n");
84 /* Now allocate two pages and try to free them one after the other */
86 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
92 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status
);
93 ok(Length
== 2*PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
94 ok(((ULONG_PTR
)Buffer
% PAGE_SIZE
) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
98 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
102 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
103 ok(Length
== PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
104 ok(Buffer2
== Buffer
, "The buffer is not aligned to PAGE_SIZE.\n");
106 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+PAGE_SIZE
);
108 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
112 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
113 ok(Length
== PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
114 ok(Buffer2
== (PVOID
)((ULONG_PTR
)Buffer
+PAGE_SIZE
), "The buffer is not aligned to PAGE_SIZE.\n");
116 /* Same, but try to free the second page before the first one */
117 Length
= 2*PAGE_SIZE
;
118 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
124 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status
);
125 ok(Length
== 2*PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
126 ok(((ULONG_PTR
)Buffer
% PAGE_SIZE
) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
128 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+PAGE_SIZE
);
130 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
134 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
135 ok(Length
== PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
136 ok(Buffer2
== (PVOID
)((ULONG_PTR
)Buffer
+PAGE_SIZE
), "The buffer is not aligned to PAGE_SIZE.\n");
140 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
144 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
145 ok(Length
== PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
146 ok(Buffer2
== Buffer
, "The buffer is not aligned to PAGE_SIZE.\n");
148 /* Now allocate two pages and try to free them in the middle */
149 Length
= 2*PAGE_SIZE
;
150 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
156 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status
);
157 ok(Length
== 2*PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
158 ok(((ULONG_PTR
)Buffer
% PAGE_SIZE
) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
160 Buffer2
= (PVOID
)((ULONG_PTR
)Buffer
+1);
162 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
166 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed : 0x%08lx\n", Status
);
167 ok(Length
== 2*PAGE_SIZE
, "Length mismatch : 0x%08lx\n", (ULONG
)Length
);
168 ok(Buffer2
== Buffer
, "The buffer is not aligned to PAGE_SIZE.\n");
171 START_TEST(NtFreeVirtualMemory
)
173 Test_NtFreeVirtualMemory();