[NTDLL_WINETEST] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / rostests / winetests / ntdll / info.c
1 /* Unit test suite for *Information* Registry API functions
2 *
3 * Copyright 2005 Paul Vriens
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 *
19 */
20
21 #include "ntdll_test.h"
22 #include <winnls.h>
23 #include <stdio.h>
24
25 static NTSTATUS (WINAPI * pNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
26 static NTSTATUS (WINAPI * pNtPowerInformation)(POWER_INFORMATION_LEVEL, PVOID, ULONG, PVOID, ULONG);
27 static NTSTATUS (WINAPI * pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
28 static NTSTATUS (WINAPI * pNtQueryInformationThread)(HANDLE, THREADINFOCLASS, PVOID, ULONG, PULONG);
29 static NTSTATUS (WINAPI * pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
30 static NTSTATUS (WINAPI * pNtSetInformationThread)(HANDLE, THREADINFOCLASS, PVOID, ULONG);
31 static NTSTATUS (WINAPI * pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
32 static NTSTATUS (WINAPI * pNtQueryVirtualMemory)(HANDLE, LPCVOID, MEMORY_INFORMATION_CLASS , PVOID , SIZE_T , SIZE_T *);
33 static NTSTATUS (WINAPI * pNtCreateSection)(HANDLE*,ACCESS_MASK,const OBJECT_ATTRIBUTES*,const LARGE_INTEGER*,ULONG,ULONG,HANDLE);
34 static NTSTATUS (WINAPI * pNtMapViewOfSection)(HANDLE,HANDLE,PVOID*,ULONG,SIZE_T,const LARGE_INTEGER*,SIZE_T*,SECTION_INHERIT,ULONG,ULONG);
35 static NTSTATUS (WINAPI * pNtUnmapViewOfSection)(HANDLE,PVOID);
36 static NTSTATUS (WINAPI * pNtClose)(HANDLE);
37 static ULONG (WINAPI * pNtGetCurrentProcessorNumber)(void);
38 static BOOL (WINAPI * pIsWow64Process)(HANDLE, PBOOL);
39
40 static BOOL is_wow64;
41
42 /* one_before_last_pid is used to be able to compare values of a still running process
43 with the output of the test_query_process_times and test_query_process_handlecount tests.
44 */
45 static DWORD one_before_last_pid = 0;
46
47 #define NTDLL_GET_PROC(func) do { \
48 p ## func = (void*)GetProcAddress(hntdll, #func); \
49 if(!p ## func) { \
50 trace("GetProcAddress(%s) failed\n", #func); \
51 return FALSE; \
52 } \
53 } while(0)
54
55 static BOOL InitFunctionPtrs(void)
56 {
57 /* All needed functions are NT based, so using GetModuleHandle is a good check */
58 HMODULE hntdll = GetModuleHandleA("ntdll");
59 if (!hntdll)
60 {
61 win_skip("Not running on NT\n");
62 return FALSE;
63 }
64
65 NTDLL_GET_PROC(NtQuerySystemInformation);
66 NTDLL_GET_PROC(NtPowerInformation);
67 NTDLL_GET_PROC(NtQueryInformationProcess);
68 NTDLL_GET_PROC(NtQueryInformationThread);
69 NTDLL_GET_PROC(NtSetInformationProcess);
70 NTDLL_GET_PROC(NtSetInformationThread);
71 NTDLL_GET_PROC(NtReadVirtualMemory);
72 NTDLL_GET_PROC(NtQueryVirtualMemory);
73 NTDLL_GET_PROC(NtClose);
74 NTDLL_GET_PROC(NtCreateSection);
75 NTDLL_GET_PROC(NtMapViewOfSection);
76 NTDLL_GET_PROC(NtUnmapViewOfSection);
77
78 /* not present before XP */
79 pNtGetCurrentProcessorNumber = (void *) GetProcAddress(hntdll, "NtGetCurrentProcessorNumber");
80
81 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
82 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
83 return TRUE;
84 }
85
86 static void test_query_basic(void)
87 {
88 NTSTATUS status;
89 ULONG ReturnLength;
90 SYSTEM_BASIC_INFORMATION sbi;
91
92 /* This test also covers some basic parameter testing that should be the same for
93 * every information class
94 */
95
96 /* Use a nonexistent info class */
97 trace("Check nonexistent info class\n");
98 status = pNtQuerySystemInformation(-1, NULL, 0, NULL);
99 ok( status == STATUS_INVALID_INFO_CLASS || status == STATUS_NOT_IMPLEMENTED /* vista */,
100 "Expected STATUS_INVALID_INFO_CLASS or STATUS_NOT_IMPLEMENTED, got %08x\n", status);
101
102 /* Use an existing class but with a zero-length buffer */
103 trace("Check zero-length buffer\n");
104 status = pNtQuerySystemInformation(SystemBasicInformation, NULL, 0, NULL);
105 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
106
107 /* Use an existing class, correct length but no SystemInformation buffer */
108 trace("Check no SystemInformation buffer\n");
109 status = pNtQuerySystemInformation(SystemBasicInformation, NULL, sizeof(sbi), NULL);
110 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_PARAMETER /* vista */,
111 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER, got %08x\n", status);
112
113 /* Use an existing class, correct length, a pointer to a buffer but no ReturnLength pointer */
114 trace("Check no ReturnLength pointer\n");
115 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), NULL);
116 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
117
118 /* Check a too large buffer size */
119 trace("Check a too large buffer size\n");
120 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi) * 2, &ReturnLength);
121 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
122
123 /* Finally some correct calls */
124 trace("Check with correct parameters\n");
125 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
126 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
127 ok( sizeof(sbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
128
129 /* Check if we have some return values */
130 trace("Number of Processors : %d\n", sbi.NumberOfProcessors);
131 ok( sbi.NumberOfProcessors > 0, "Expected more than 0 processors, got %d\n", sbi.NumberOfProcessors);
132 }
133
134 static void test_query_cpu(void)
135 {
136 DWORD status;
137 ULONG ReturnLength;
138 SYSTEM_CPU_INFORMATION sci;
139
140 status = pNtQuerySystemInformation(SystemCpuInformation, &sci, sizeof(sci), &ReturnLength);
141 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
142 ok( sizeof(sci) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
143
144 /* Check if we have some return values */
145 trace("Processor FeatureSet : %08x\n", sci.FeatureSet);
146 ok( sci.FeatureSet != 0, "Expected some features for this processor, got %08x\n", sci.FeatureSet);
147 }
148
149 static void test_query_performance(void)
150 {
151 NTSTATUS status;
152 ULONG ReturnLength;
153 ULONGLONG buffer[sizeof(SYSTEM_PERFORMANCE_INFORMATION)/sizeof(ULONGLONG) + 5];
154 DWORD size = sizeof(SYSTEM_PERFORMANCE_INFORMATION);
155
156 status = pNtQuerySystemInformation(SystemPerformanceInformation, buffer, 0, &ReturnLength);
157 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
158
159 status = pNtQuerySystemInformation(SystemPerformanceInformation, buffer, size, &ReturnLength);
160 if (status == STATUS_INFO_LENGTH_MISMATCH && is_wow64)
161 {
162 /* size is larger on wow64 under w2k8/win7 */
163 size += 16;
164 status = pNtQuerySystemInformation(SystemPerformanceInformation, buffer, size, &ReturnLength);
165 }
166 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
167 ok( ReturnLength == size, "Inconsistent length %d\n", ReturnLength);
168
169 status = pNtQuerySystemInformation(SystemPerformanceInformation, buffer, size + 2, &ReturnLength);
170 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
171 ok( ReturnLength == size || ReturnLength == size + 2,
172 "Inconsistent length %d\n", ReturnLength);
173
174 /* Not return values yet, as struct members are unknown */
175 }
176
177 static void test_query_timeofday(void)
178 {
179 NTSTATUS status;
180 ULONG ReturnLength;
181
182 /* Copy of our winternl.h structure turned into a private one */
183 typedef struct _SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE {
184 LARGE_INTEGER liKeBootTime;
185 LARGE_INTEGER liKeSystemTime;
186 LARGE_INTEGER liExpTimeZoneBias;
187 ULONG uCurrentTimeZoneId;
188 DWORD dwUnknown1[5];
189 } SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE;
190
191 SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE sti;
192
193 /* The struct size for NT (32 bytes) and Win2K/XP (48 bytes) differ.
194 *
195 * Windows 2000 and XP return STATUS_INFO_LENGTH_MISMATCH if the given buffer size is greater
196 * then 48 and 0 otherwise
197 * Windows NT returns STATUS_INFO_LENGTH_MISMATCH when the given buffer size is not correct
198 * and 0 otherwise
199 *
200 * Windows 2000 and XP copy the given buffer size into the provided buffer, if the return code is STATUS_SUCCESS
201 * NT only fills the buffer if the return code is STATUS_SUCCESS
202 *
203 */
204
205 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
206
207 if (status == STATUS_INFO_LENGTH_MISMATCH)
208 {
209 trace("Windows version is NT, we have to cater for differences with W2K/WinXP\n");
210
211 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
212 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
213 ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
214
215 sti.uCurrentTimeZoneId = 0xdeadbeef;
216 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 28, &ReturnLength);
217 ok(status == STATUS_SUCCESS || broken(status == STATUS_INFO_LENGTH_MISMATCH /* NT4 */), "Expected STATUS_SUCCESS, got %08x\n", status);
218 ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
219
220 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
221 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
222 ok( 32 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
223 }
224 else
225 {
226 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
227 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
228 ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
229
230 sti.uCurrentTimeZoneId = 0xdeadbeef;
231 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 24, &ReturnLength);
232 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
233 ok( 24 == ReturnLength, "ReturnLength should be 24, it is (%d)\n", ReturnLength);
234 ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
235
236 sti.uCurrentTimeZoneId = 0xdeadbeef;
237 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
238 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
239 ok( 32 == ReturnLength, "ReturnLength should be 32, it is (%d)\n", ReturnLength);
240 ok( 0xdeadbeef != sti.uCurrentTimeZoneId, "Buffer should have been partially filled\n");
241
242 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 49, &ReturnLength);
243 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
244 ok( ReturnLength == 0 || ReturnLength == sizeof(sti) /* vista */,
245 "ReturnLength should be 0, it is (%d)\n", ReturnLength);
246
247 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
248 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
249 ok( sizeof(sti) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
250 }
251
252 /* Check if we have some return values */
253 trace("uCurrentTimeZoneId : (%d)\n", sti.uCurrentTimeZoneId);
254 }
255
256 static void test_query_process(void)
257 {
258 NTSTATUS status;
259 DWORD last_pid;
260 ULONG ReturnLength;
261 int i = 0, k = 0;
262 BOOL is_nt = FALSE;
263 SYSTEM_BASIC_INFORMATION sbi;
264
265 /* Copy of our winternl.h structure turned into a private one */
266 typedef struct _SYSTEM_PROCESS_INFORMATION_PRIVATE {
267 ULONG NextEntryOffset;
268 DWORD dwThreadCount;
269 DWORD dwUnknown1[6];
270 FILETIME ftCreationTime;
271 FILETIME ftUserTime;
272 FILETIME ftKernelTime;
273 UNICODE_STRING ProcessName;
274 DWORD dwBasePriority;
275 HANDLE UniqueProcessId;
276 HANDLE ParentProcessId;
277 ULONG HandleCount;
278 DWORD dwUnknown3;
279 DWORD dwUnknown4;
280 VM_COUNTERS vmCounters;
281 IO_COUNTERS ioCounters;
282 SYSTEM_THREAD_INFORMATION ti[1];
283 } SYSTEM_PROCESS_INFORMATION_PRIVATE;
284
285 ULONG SystemInformationLength = sizeof(SYSTEM_PROCESS_INFORMATION_PRIVATE);
286 SYSTEM_PROCESS_INFORMATION_PRIVATE *spi, *spi_buf = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
287
288 /* test ReturnLength */
289 ReturnLength = 0;
290 status = pNtQuerySystemInformation(SystemProcessInformation, NULL, 0, &ReturnLength);
291 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_LENGTH_MISMATCH got %08x\n", status);
292 ok( ReturnLength > 0 || broken(ReturnLength == 0) /* NT4, Win2K */,
293 "Expected a ReturnLength to show the needed length\n");
294
295 /* W2K3 and later returns the needed length, the rest returns 0, so we have to loop */
296 for (;;)
297 {
298 status = pNtQuerySystemInformation(SystemProcessInformation, spi_buf, SystemInformationLength, &ReturnLength);
299
300 if (status != STATUS_INFO_LENGTH_MISMATCH) break;
301
302 spi_buf = HeapReAlloc(GetProcessHeap(), 0, spi_buf , SystemInformationLength *= 2);
303 }
304 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
305 spi = spi_buf;
306
307 /* Get the first NextEntryOffset, from this we can deduce the OS version we're running
308 *
309 * W2K/WinXP/W2K3:
310 * NextEntryOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
311 * NT:
312 * NextEntryOffset for a process is 136 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
313 * Wine (with every windows version):
314 * NextEntryOffset for a process is 0 if just this test is running
315 * NextEntryOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION) +
316 * ProcessName.MaximumLength
317 * if more wine processes are running
318 *
319 * Note : On windows the first process is in fact the Idle 'process' with a thread for every processor
320 */
321
322 pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
323
324 is_nt = ( spi->NextEntryOffset - (sbi.NumberOfProcessors * sizeof(SYSTEM_THREAD_INFORMATION)) == 136);
325
326 if (is_nt) win_skip("Windows version is NT, we will skip thread tests\n");
327
328 /* Check if we have some return values
329 *
330 * On windows there will be several processes running (Including the always present Idle and System)
331 * On wine we only have one (if this test is the only wine process running)
332 */
333
334 /* Loop through the processes */
335
336 for (;;)
337 {
338 i++;
339
340 last_pid = (DWORD_PTR)spi->UniqueProcessId;
341
342 ok( spi->dwThreadCount > 0, "Expected some threads for this process, got 0\n");
343
344 /* Loop through the threads, skip NT4 for now */
345
346 if (!is_nt)
347 {
348 DWORD j;
349 for ( j = 0; j < spi->dwThreadCount; j++)
350 {
351 k++;
352 ok ( spi->ti[j].ClientId.UniqueProcess == spi->UniqueProcessId,
353 "The owning pid of the thread (%p) doesn't equal the pid (%p) of the process\n",
354 spi->ti[j].ClientId.UniqueProcess, spi->UniqueProcessId);
355 }
356 }
357
358 if (!spi->NextEntryOffset) break;
359
360 one_before_last_pid = last_pid;
361
362 spi = (SYSTEM_PROCESS_INFORMATION_PRIVATE*)((char*)spi + spi->NextEntryOffset);
363 }
364 trace("Total number of running processes : %d\n", i);
365 if (!is_nt) trace("Total number of running threads : %d\n", k);
366
367 if (one_before_last_pid == 0) one_before_last_pid = last_pid;
368
369 HeapFree( GetProcessHeap(), 0, spi_buf);
370 }
371
372 static void test_query_procperf(void)
373 {
374 NTSTATUS status;
375 ULONG ReturnLength;
376 ULONG NeededLength;
377 SYSTEM_BASIC_INFORMATION sbi;
378 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* sppi;
379
380 /* Find out the number of processors */
381 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
382 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
383 NeededLength = sbi.NumberOfProcessors * sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
384
385 sppi = HeapAlloc(GetProcessHeap(), 0, NeededLength);
386
387 status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, 0, &ReturnLength);
388 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
389
390 /* Try it for 1 processor */
391 sppi->KernelTime.QuadPart = 0xdeaddead;
392 sppi->UserTime.QuadPart = 0xdeaddead;
393 sppi->IdleTime.QuadPart = 0xdeaddead;
394 status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi,
395 sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), &ReturnLength);
396 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
397 ok( sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) == ReturnLength,
398 "Inconsistent length %d\n", ReturnLength);
399 ok (sppi->KernelTime.QuadPart != 0xdeaddead, "KernelTime unchanged\n");
400 ok (sppi->UserTime.QuadPart != 0xdeaddead, "UserTime unchanged\n");
401 ok (sppi->IdleTime.QuadPart != 0xdeaddead, "IdleTime unchanged\n");
402
403 /* Try it for all processors */
404 sppi->KernelTime.QuadPart = 0xdeaddead;
405 sppi->UserTime.QuadPart = 0xdeaddead;
406 sppi->IdleTime.QuadPart = 0xdeaddead;
407 status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, NeededLength, &ReturnLength);
408 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
409 ok( NeededLength == ReturnLength, "Inconsistent length (%d) <-> (%d)\n", NeededLength, ReturnLength);
410 ok (sppi->KernelTime.QuadPart != 0xdeaddead, "KernelTime unchanged\n");
411 ok (sppi->UserTime.QuadPart != 0xdeaddead, "UserTime unchanged\n");
412 ok (sppi->IdleTime.QuadPart != 0xdeaddead, "IdleTime unchanged\n");
413
414 /* A too large given buffer size */
415 sppi = HeapReAlloc(GetProcessHeap(), 0, sppi , NeededLength + 2);
416 sppi->KernelTime.QuadPart = 0xdeaddead;
417 sppi->UserTime.QuadPart = 0xdeaddead;
418 sppi->IdleTime.QuadPart = 0xdeaddead;
419 status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, NeededLength + 2, &ReturnLength);
420 ok( status == STATUS_SUCCESS || status == STATUS_INFO_LENGTH_MISMATCH /* vista */,
421 "Expected STATUS_SUCCESS or STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
422 ok( NeededLength == ReturnLength, "Inconsistent length (%d) <-> (%d)\n", NeededLength, ReturnLength);
423 if (status == STATUS_SUCCESS)
424 {
425 ok (sppi->KernelTime.QuadPart != 0xdeaddead, "KernelTime unchanged\n");
426 ok (sppi->UserTime.QuadPart != 0xdeaddead, "UserTime unchanged\n");
427 ok (sppi->IdleTime.QuadPart != 0xdeaddead, "IdleTime unchanged\n");
428 }
429 else /* vista and 2008 */
430 {
431 ok (sppi->KernelTime.QuadPart == 0xdeaddead, "KernelTime changed\n");
432 ok (sppi->UserTime.QuadPart == 0xdeaddead, "UserTime changed\n");
433 ok (sppi->IdleTime.QuadPart == 0xdeaddead, "IdleTime changed\n");
434 }
435
436 HeapFree( GetProcessHeap(), 0, sppi);
437 }
438
439 static void test_query_module(void)
440 {
441 NTSTATUS status;
442 ULONG ReturnLength;
443 ULONG ModuleCount, i;
444
445 ULONG SystemInformationLength = sizeof(SYSTEM_MODULE_INFORMATION);
446 SYSTEM_MODULE_INFORMATION* smi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
447 SYSTEM_MODULE* sm;
448
449 /* Request the needed length */
450 status = pNtQuerySystemInformation(SystemModuleInformation, smi, 0, &ReturnLength);
451 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
452 ok( ReturnLength > 0, "Expected a ReturnLength to show the needed length\n");
453
454 SystemInformationLength = ReturnLength;
455 smi = HeapReAlloc(GetProcessHeap(), 0, smi , SystemInformationLength);
456 status = pNtQuerySystemInformation(SystemModuleInformation, smi, SystemInformationLength, &ReturnLength);
457 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
458
459 ModuleCount = smi->ModulesCount;
460 sm = &smi->Modules[0];
461 /* our implementation is a stub for now */
462 ok( ModuleCount > 0, "Expected some modules to be loaded\n");
463
464 /* Loop through all the modules/drivers, Wine doesn't get here (yet) */
465 for (i = 0; i < ModuleCount ; i++)
466 {
467 ok( i == sm->Id, "Id (%d) should have matched %u\n", sm->Id, i);
468 sm++;
469 }
470
471 HeapFree( GetProcessHeap(), 0, smi);
472 }
473
474 static void test_query_handle(void)
475 {
476 NTSTATUS status;
477 ULONG ReturnLength;
478 ULONG SystemInformationLength = sizeof(SYSTEM_HANDLE_INFORMATION);
479 SYSTEM_HANDLE_INFORMATION* shi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
480 HANDLE event_handle;
481
482 event_handle = CreateEventA(NULL, FALSE, FALSE, NULL);
483 ok( event_handle != NULL, "CreateEventA failed %u\n", GetLastError() );
484
485 /* Request the needed length : a SystemInformationLength greater than one struct sets ReturnLength */
486 ReturnLength = 0xdeadbeef;
487 status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
488 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
489 ok( ReturnLength != 0xdeadbeef, "Expected valid ReturnLength\n" );
490
491 SystemInformationLength = ReturnLength;
492 shi = HeapReAlloc(GetProcessHeap(), 0, shi , SystemInformationLength);
493
494 ReturnLength = 0xdeadbeef;
495 status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
496 if (status != STATUS_INFO_LENGTH_MISMATCH) /* vista */
497 {
498 ULONG ExpectedLength = FIELD_OFFSET(SYSTEM_HANDLE_INFORMATION, Handle[shi->Count]);
499 unsigned int i;
500 BOOL found = FALSE;
501
502 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status );
503 ok( ReturnLength == ExpectedLength, "Expected length %u, got %u\n", ExpectedLength, ReturnLength );
504 ok( shi->Count > 1, "Expected more than 1 handles, got %u\n", shi->Count );
505 for (i = 0; i < shi->Count; i++)
506 {
507 if (shi->Handle[i].OwnerPid == GetCurrentProcessId() &&
508 (HANDLE)(ULONG_PTR)shi->Handle[i].HandleValue == event_handle)
509 {
510 found = TRUE;
511 break;
512 }
513 }
514 ok( found, "Expected to find event handle in handle list\n" );
515 }
516
517 status = pNtQuerySystemInformation(SystemHandleInformation, NULL, SystemInformationLength, &ReturnLength);
518 ok( status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %08x\n", status );
519
520 CloseHandle(event_handle);
521 HeapFree( GetProcessHeap(), 0, shi);
522 }
523
524 static void test_query_cache(void)
525 {
526 NTSTATUS status;
527 ULONG ReturnLength;
528 BYTE buffer[128];
529 SYSTEM_CACHE_INFORMATION *sci = (SYSTEM_CACHE_INFORMATION *) buffer;
530 ULONG expected;
531 INT i;
532
533 /* the large SYSTEM_CACHE_INFORMATION on WIN64 is not documented */
534 expected = sizeof(SYSTEM_CACHE_INFORMATION);
535 for (i = sizeof(buffer); i>= expected; i--)
536 {
537 ReturnLength = 0xdeadbeef;
538 status = pNtQuerySystemInformation(SystemCacheInformation, sci, i, &ReturnLength);
539 ok(!status && (ReturnLength == expected),
540 "%d: got 0x%x and %u (expected STATUS_SUCCESS and %u)\n", i, status, ReturnLength, expected);
541 }
542
543 /* buffer too small for the full result.
544 Up to win7, the function succeeds with a partial result. */
545 status = pNtQuerySystemInformation(SystemCacheInformation, sci, i, &ReturnLength);
546 if (!status)
547 {
548 expected = offsetof(SYSTEM_CACHE_INFORMATION, MinimumWorkingSet);
549 for (; i>= expected; i--)
550 {
551 ReturnLength = 0xdeadbeef;
552 status = pNtQuerySystemInformation(SystemCacheInformation, sci, i, &ReturnLength);
553 ok(!status && (ReturnLength == expected),
554 "%d: got 0x%x and %u (expected STATUS_SUCCESS and %u)\n", i, status, ReturnLength, expected);
555 }
556 }
557
558 /* buffer too small for the result, this call will always fail */
559 ReturnLength = 0xdeadbeef;
560 status = pNtQuerySystemInformation(SystemCacheInformation, sci, i, &ReturnLength);
561 ok( status == STATUS_INFO_LENGTH_MISMATCH &&
562 ((ReturnLength == expected) || broken(!ReturnLength) || broken(ReturnLength == 0xfffffff0)),
563 "%d: got 0x%x and %u (expected STATUS_INFO_LENGTH_MISMATCH and %u)\n", i, status, ReturnLength, expected);
564
565 if (0) {
566 /* this crashes on some vista / win7 machines */
567 ReturnLength = 0xdeadbeef;
568 status = pNtQuerySystemInformation(SystemCacheInformation, sci, 0, &ReturnLength);
569 ok( status == STATUS_INFO_LENGTH_MISMATCH &&
570 ((ReturnLength == expected) || broken(!ReturnLength) || broken(ReturnLength == 0xfffffff0)),
571 "0: got 0x%x and %u (expected STATUS_INFO_LENGTH_MISMATCH and %u)\n", status, ReturnLength, expected);
572 }
573 }
574
575 static void test_query_interrupt(void)
576 {
577 NTSTATUS status;
578 ULONG ReturnLength;
579 ULONG NeededLength;
580 SYSTEM_BASIC_INFORMATION sbi;
581 SYSTEM_INTERRUPT_INFORMATION* sii;
582
583 /* Find out the number of processors */
584 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
585 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
586 NeededLength = sbi.NumberOfProcessors * sizeof(SYSTEM_INTERRUPT_INFORMATION);
587
588 sii = HeapAlloc(GetProcessHeap(), 0, NeededLength);
589
590 status = pNtQuerySystemInformation(SystemInterruptInformation, sii, 0, &ReturnLength);
591 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
592
593 /* Try it for all processors */
594 status = pNtQuerySystemInformation(SystemInterruptInformation, sii, NeededLength, &ReturnLength);
595 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
596
597 /* Windows XP and W2K3 (and others?) always return 0 for the ReturnLength
598 * No test added for this as it's highly unlikely that an app depends on this
599 */
600
601 HeapFree( GetProcessHeap(), 0, sii);
602 }
603
604 static void test_query_kerndebug(void)
605 {
606 NTSTATUS status;
607 ULONG ReturnLength;
608 SYSTEM_KERNEL_DEBUGGER_INFORMATION skdi;
609
610 status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, 0, &ReturnLength);
611 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
612
613 status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, sizeof(skdi), &ReturnLength);
614 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
615 ok( sizeof(skdi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
616
617 status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, sizeof(skdi) + 2, &ReturnLength);
618 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
619 ok( sizeof(skdi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
620 }
621
622 static void test_query_regquota(void)
623 {
624 NTSTATUS status;
625 ULONG ReturnLength;
626 SYSTEM_REGISTRY_QUOTA_INFORMATION srqi;
627
628 status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, 0, &ReturnLength);
629 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
630
631 status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, sizeof(srqi), &ReturnLength);
632 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
633 ok( sizeof(srqi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
634
635 status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, sizeof(srqi) + 2, &ReturnLength);
636 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
637 ok( sizeof(srqi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
638 }
639
640 static void test_query_logicalproc(void)
641 {
642 NTSTATUS status;
643 ULONG len, i, proc_no;
644 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *slpi;
645 SYSTEM_INFO si;
646
647 GetSystemInfo(&si);
648
649 status = pNtQuerySystemInformation(SystemLogicalProcessorInformation, NULL, 0, &len);
650 if(status == STATUS_INVALID_INFO_CLASS)
651 {
652 win_skip("SystemLogicalProcessorInformation is not supported\n");
653 return;
654 }
655 if(status == STATUS_NOT_IMPLEMENTED)
656 {
657 todo_wine ok(0, "SystemLogicalProcessorInformation is not implemented\n");
658 return;
659 }
660 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
661 ok(len%sizeof(*slpi) == 0, "Incorrect length %d\n", len);
662
663 slpi = HeapAlloc(GetProcessHeap(), 0, len);
664 status = pNtQuerySystemInformation(SystemLogicalProcessorInformation, slpi, len, &len);
665 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
666
667 proc_no = 0;
668 for(i=0; i<len/sizeof(*slpi); i++) {
669 switch(slpi[i].Relationship) {
670 case RelationProcessorCore:
671 /* Get number of logical processors */
672 for(; slpi[i].ProcessorMask; slpi[i].ProcessorMask /= 2)
673 proc_no += slpi[i].ProcessorMask%2;
674 break;
675 default:
676 break;
677 }
678 }
679 ok(proc_no > 0, "No processors were found\n");
680 if(si.dwNumberOfProcessors <= 32)
681 ok(proc_no == si.dwNumberOfProcessors, "Incorrect number of logical processors: %d, expected %d\n",
682 proc_no, si.dwNumberOfProcessors);
683
684 HeapFree(GetProcessHeap(), 0, slpi);
685 }
686
687 static void test_query_processor_power_info(void)
688 {
689 NTSTATUS status;
690 PROCESSOR_POWER_INFORMATION* ppi;
691 ULONG size;
692 SYSTEM_INFO si;
693 int i;
694
695 GetSystemInfo(&si);
696 size = si.dwNumberOfProcessors * sizeof(PROCESSOR_POWER_INFORMATION);
697 ppi = HeapAlloc(GetProcessHeap(), 0, size);
698
699 /* If size < (sizeof(PROCESSOR_POWER_INFORMATION) * NumberOfProcessors), Win7 returns
700 * STATUS_BUFFER_TOO_SMALL. WinXP returns STATUS_SUCCESS for any value of size. It copies as
701 * many whole PROCESSOR_POWER_INFORMATION structures that there is room for. Even if there is
702 * not enough room for one structure, WinXP still returns STATUS_SUCCESS having done nothing.
703 *
704 * If ppi == NULL, Win7 returns STATUS_INVALID_PARAMETER while WinXP returns STATUS_SUCCESS
705 * and does nothing.
706 *
707 * The same behavior is seen with CallNtPowerInformation (in powrprof.dll).
708 */
709
710 if (si.dwNumberOfProcessors > 1)
711 {
712 for(i = 0; i < si.dwNumberOfProcessors; i++)
713 ppi[i].Number = 0xDEADBEEF;
714
715 /* Call with a buffer size that is large enough to hold at least one but not large
716 * enough to hold them all. This will be STATUS_SUCCESS on WinXP but not on Win7 */
717 status = pNtPowerInformation(ProcessorInformation, 0, 0, ppi, size - sizeof(PROCESSOR_POWER_INFORMATION));
718 if (status == STATUS_SUCCESS)
719 {
720 /* lax version found on older Windows like WinXP */
721 ok( (ppi[si.dwNumberOfProcessors - 2].Number != 0xDEADBEEF) &&
722 (ppi[si.dwNumberOfProcessors - 1].Number == 0xDEADBEEF),
723 "Expected all but the last record to be overwritten.\n");
724
725 status = pNtPowerInformation(ProcessorInformation, 0, 0, 0, size);
726 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
727
728 for(i = 0; i < si.dwNumberOfProcessors; i++)
729 ppi[i].Number = 0xDEADBEEF;
730 status = pNtPowerInformation(ProcessorInformation, 0, 0, ppi, sizeof(PROCESSOR_POWER_INFORMATION) - 1);
731 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
732 for(i = 0; i < si.dwNumberOfProcessors; i++)
733 if (ppi[i].Number != 0xDEADBEEF) break;
734 ok( i == si.dwNumberOfProcessors, "Expected untouched buffer\n");
735 }
736 else
737 {
738 /* picky version found on newer Windows like Win7 */
739 ok( ppi[1].Number == 0xDEADBEEF, "Expected untouched buffer.\n");
740 ok( status == STATUS_BUFFER_TOO_SMALL, "Expected STATUS_BUFFER_TOO_SMALL, got %08x\n", status);
741
742 status = pNtPowerInformation(ProcessorInformation, 0, 0, 0, size);
743 ok( status == STATUS_SUCCESS || status == STATUS_INVALID_PARAMETER, "Got %08x\n", status);
744
745 status = pNtPowerInformation(ProcessorInformation, 0, 0, ppi, 0);
746 ok( status == STATUS_BUFFER_TOO_SMALL || status == STATUS_INVALID_PARAMETER, "Got %08x\n", status);
747 }
748 }
749 else
750 {
751 skip("Test needs more than one processor.\n");
752 }
753
754 status = pNtPowerInformation(ProcessorInformation, 0, 0, ppi, size);
755 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
756
757 HeapFree(GetProcessHeap(), 0, ppi);
758 }
759
760 static void test_query_process_wow64(void)
761 {
762 NTSTATUS status;
763 ULONG ReturnLength;
764 ULONG_PTR pbi[2], dummy;
765
766 memset(&dummy, 0xcc, sizeof(dummy));
767
768 /* Do not give a handle and buffer */
769 status = pNtQueryInformationProcess(NULL, ProcessWow64Information, NULL, 0, NULL);
770 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
771
772 /* Use a correct info class and buffer size, but still no handle and buffer */
773 status = pNtQueryInformationProcess(NULL, ProcessWow64Information, NULL, sizeof(ULONG_PTR), NULL);
774 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
775 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE, got %08x\n", status);
776
777 /* Use a correct info class, buffer size and handle, but no buffer */
778 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information, NULL, sizeof(ULONG_PTR), NULL);
779 ok( status == STATUS_ACCESS_VIOLATION , "Expected STATUS_ACCESS_VIOLATION, got %08x\n", status);
780
781 /* Use a correct info class, buffer and buffer size, but no handle */
782 pbi[0] = pbi[1] = dummy;
783 status = pNtQueryInformationProcess(NULL, ProcessWow64Information, pbi, sizeof(ULONG_PTR), NULL);
784 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
785 ok( pbi[0] == dummy, "pbi[0] changed to %lx\n", pbi[0]);
786 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
787
788 /* Use a greater buffer size */
789 pbi[0] = pbi[1] = dummy;
790 status = pNtQueryInformationProcess(NULL, ProcessWow64Information, pbi, sizeof(ULONG_PTR) + 1, NULL);
791 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
792 ok( pbi[0] == dummy, "pbi[0] changed to %lx\n", pbi[0]);
793 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
794
795 /* Use no ReturnLength */
796 pbi[0] = pbi[1] = dummy;
797 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information, pbi, sizeof(ULONG_PTR), NULL);
798 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
799 trace( "Platform is_wow64 %d, ProcessInformation of ProcessWow64Information %lx\n", is_wow64, pbi[0]);
800 ok( is_wow64 == (pbi[0] != 0), "is_wow64 %x, pbi[0] %lx\n", is_wow64, pbi[0]);
801 ok( pbi[0] != dummy, "pbi[0] %lx\n", pbi[0]);
802 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
803 /* Test written size on 64 bit by checking high 32 bit buffer */
804 if (sizeof(ULONG_PTR) > sizeof(DWORD))
805 {
806 DWORD *ptr = (DWORD *)pbi;
807 ok( ptr[1] != (DWORD)dummy, "ptr[1] unchanged!\n");
808 }
809
810 /* Finally some correct calls */
811 pbi[0] = pbi[1] = dummy;
812 ReturnLength = 0xdeadbeef;
813 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information, pbi, sizeof(ULONG_PTR), &ReturnLength);
814 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
815 ok( is_wow64 == (pbi[0] != 0), "is_wow64 %x, pbi[0] %lx\n", is_wow64, pbi[0]);
816 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
817 ok( ReturnLength == sizeof(ULONG_PTR), "Inconsistent length %d\n", ReturnLength);
818
819 /* Everything is correct except a too small buffer size */
820 pbi[0] = pbi[1] = dummy;
821 ReturnLength = 0xdeadbeef;
822 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information, pbi, sizeof(ULONG_PTR) - 1, &ReturnLength);
823 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
824 ok( pbi[0] == dummy, "pbi[0] changed to %lx\n", pbi[0]);
825 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
826 todo_wine ok( ReturnLength == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", ReturnLength);
827
828 /* Everything is correct except a too large buffer size */
829 pbi[0] = pbi[1] = dummy;
830 ReturnLength = 0xdeadbeef;
831 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information, pbi, sizeof(ULONG_PTR) + 1, &ReturnLength);
832 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
833 ok( pbi[0] == dummy, "pbi[0] changed to %lx\n", pbi[0]);
834 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
835 todo_wine ok( ReturnLength == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", ReturnLength);
836 }
837
838 static void test_query_process_basic(void)
839 {
840 NTSTATUS status;
841 ULONG ReturnLength;
842
843 typedef struct _PROCESS_BASIC_INFORMATION_PRIVATE {
844 DWORD_PTR ExitStatus;
845 PPEB PebBaseAddress;
846 DWORD_PTR AffinityMask;
847 DWORD_PTR BasePriority;
848 ULONG_PTR UniqueProcessId;
849 ULONG_PTR InheritedFromUniqueProcessId;
850 } PROCESS_BASIC_INFORMATION_PRIVATE;
851
852 PROCESS_BASIC_INFORMATION_PRIVATE pbi;
853
854 /* This test also covers some basic parameter testing that should be the same for
855 * every information class
856 */
857
858 /* Use a nonexistent info class */
859 trace("Check nonexistent info class\n");
860 status = pNtQueryInformationProcess(NULL, -1, NULL, 0, NULL);
861 ok( status == STATUS_INVALID_INFO_CLASS || status == STATUS_NOT_IMPLEMENTED /* vista */,
862 "Expected STATUS_INVALID_INFO_CLASS or STATUS_NOT_IMPLEMENTED, got %08x\n", status);
863
864 /* Do not give a handle and buffer */
865 trace("Check NULL handle and buffer and zero-length buffersize\n");
866 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, 0, NULL);
867 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
868
869 /* Use a correct info class and buffer size, but still no handle and buffer */
870 trace("Check NULL handle and buffer\n");
871 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, sizeof(pbi), NULL);
872 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
873 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
874
875 /* Use a correct info class and buffer size, but still no handle */
876 trace("Check NULL handle\n");
877 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
878 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
879
880 /* Use a greater buffer size */
881 trace("Check NULL handle and too large buffersize\n");
882 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi) * 2, NULL);
883 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
884
885 /* Use no ReturnLength */
886 trace("Check NULL ReturnLength\n");
887 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
888 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
889
890 /* Finally some correct calls */
891 trace("Check with correct parameters\n");
892 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), &ReturnLength);
893 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
894 ok( sizeof(pbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
895
896 /* Everything is correct except a too large buffersize */
897 trace("Too large buffersize\n");
898 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi) * 2, &ReturnLength);
899 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
900 ok( sizeof(pbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
901
902 /* Check if we have some return values */
903 trace("ProcessID : %lx\n", pbi.UniqueProcessId);
904 ok( pbi.UniqueProcessId > 0, "Expected a ProcessID > 0, got 0\n");
905 }
906
907 static void test_query_process_vm(void)
908 {
909 NTSTATUS status;
910 ULONG ReturnLength;
911 VM_COUNTERS pvi;
912 ULONG old_size = FIELD_OFFSET(VM_COUNTERS,PrivatePageCount);
913
914 status = pNtQueryInformationProcess(NULL, ProcessVmCounters, NULL, sizeof(pvi), NULL);
915 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
916 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
917
918 status = pNtQueryInformationProcess(NULL, ProcessVmCounters, &pvi, old_size, NULL);
919 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
920
921 /* Windows XP and W2K3 will report success for a size of 44 AND 48 !
922 Windows W2K will only report success for 44.
923 For now we only care for 44, which is FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
924 */
925
926 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, 24, &ReturnLength);
927 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
928
929 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, old_size, &ReturnLength);
930 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
931 ok( old_size == ReturnLength, "Inconsistent length %d\n", ReturnLength);
932
933 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, 46, &ReturnLength);
934 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
935 ok( ReturnLength == old_size || ReturnLength == sizeof(pvi), "Inconsistent length %d\n", ReturnLength);
936
937 /* Check if we have some return values */
938 trace("WorkingSetSize : %ld\n", pvi.WorkingSetSize);
939 todo_wine
940 {
941 ok( pvi.WorkingSetSize > 0, "Expected a WorkingSetSize > 0\n");
942 }
943 }
944
945 static void test_query_process_io(void)
946 {
947 NTSTATUS status;
948 ULONG ReturnLength;
949 IO_COUNTERS pii;
950
951 /* NT4 doesn't support this information class, so check for it */
952 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii), &ReturnLength);
953 if (status == STATUS_NOT_SUPPORTED)
954 {
955 win_skip("ProcessIoCounters information class is not supported\n");
956 return;
957 }
958
959 status = pNtQueryInformationProcess(NULL, ProcessIoCounters, NULL, sizeof(pii), NULL);
960 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
961 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
962
963 status = pNtQueryInformationProcess(NULL, ProcessIoCounters, &pii, sizeof(pii), NULL);
964 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
965
966 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, 24, &ReturnLength);
967 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
968
969 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii), &ReturnLength);
970 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
971 ok( sizeof(pii) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
972
973 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii) * 2, &ReturnLength);
974 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
975 ok( sizeof(pii) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
976
977 /* Check if we have some return values */
978 trace("OtherOperationCount : 0x%x%08x\n", (DWORD)(pii.OtherOperationCount >> 32), (DWORD)pii.OtherOperationCount);
979 todo_wine
980 {
981 ok( pii.OtherOperationCount > 0, "Expected an OtherOperationCount > 0\n");
982 }
983 }
984
985 static void test_query_process_times(void)
986 {
987 NTSTATUS status;
988 ULONG ReturnLength;
989 HANDLE process;
990 SYSTEMTIME UTC, Local;
991 KERNEL_USER_TIMES spti;
992
993 status = pNtQueryInformationProcess(NULL, ProcessTimes, NULL, sizeof(spti), NULL);
994 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
995 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
996
997 status = pNtQueryInformationProcess(NULL, ProcessTimes, &spti, sizeof(spti), NULL);
998 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
999
1000 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessTimes, &spti, 24, &ReturnLength);
1001 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1002
1003 process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, one_before_last_pid);
1004 if (!process)
1005 {
1006 trace("Could not open process with ID : %d, error : %u. Going to use current one.\n", one_before_last_pid, GetLastError());
1007 process = GetCurrentProcess();
1008 trace("ProcessTimes for current process\n");
1009 }
1010 else
1011 trace("ProcessTimes for process with ID : %d\n", one_before_last_pid);
1012
1013 status = pNtQueryInformationProcess( process, ProcessTimes, &spti, sizeof(spti), &ReturnLength);
1014 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1015 ok( sizeof(spti) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
1016 CloseHandle(process);
1017
1018 FileTimeToSystemTime((const FILETIME *)&spti.CreateTime, &UTC);
1019 SystemTimeToTzSpecificLocalTime(NULL, &UTC, &Local);
1020 trace("CreateTime : %02d/%02d/%04d %02d:%02d:%02d\n", Local.wMonth, Local.wDay, Local.wYear,
1021 Local.wHour, Local.wMinute, Local.wSecond);
1022
1023 FileTimeToSystemTime((const FILETIME *)&spti.ExitTime, &UTC);
1024 SystemTimeToTzSpecificLocalTime(NULL, &UTC, &Local);
1025 trace("ExitTime : %02d/%02d/%04d %02d:%02d:%02d\n", Local.wMonth, Local.wDay, Local.wYear,
1026 Local.wHour, Local.wMinute, Local.wSecond);
1027
1028 FileTimeToSystemTime((const FILETIME *)&spti.KernelTime, &Local);
1029 trace("KernelTime : %02d:%02d:%02d.%03d\n", Local.wHour, Local.wMinute, Local.wSecond, Local.wMilliseconds);
1030
1031 FileTimeToSystemTime((const FILETIME *)&spti.UserTime, &Local);
1032 trace("UserTime : %02d:%02d:%02d.%03d\n", Local.wHour, Local.wMinute, Local.wSecond, Local.wMilliseconds);
1033
1034 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessTimes, &spti, sizeof(spti) * 2, &ReturnLength);
1035 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1036 ok( sizeof(spti) == ReturnLength ||
1037 ReturnLength == 0 /* vista */ ||
1038 broken(is_wow64), /* returns garbage on wow64 */
1039 "Inconsistent length %d\n", ReturnLength);
1040 }
1041
1042 static void test_query_process_debug_port(int argc, char **argv)
1043 {
1044 DWORD_PTR debug_port = 0xdeadbeef;
1045 char cmdline[MAX_PATH];
1046 PROCESS_INFORMATION pi;
1047 STARTUPINFOA si = { 0 };
1048 NTSTATUS status;
1049 BOOL ret;
1050
1051 sprintf(cmdline, "%s %s %s", argv[0], argv[1], "debuggee");
1052
1053 si.cb = sizeof(si);
1054 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
1055 ok(ret, "CreateProcess failed, last error %#x.\n", GetLastError());
1056 if (!ret) return;
1057
1058 status = pNtQueryInformationProcess(NULL, ProcessDebugPort,
1059 NULL, 0, NULL);
1060 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1061
1062 status = pNtQueryInformationProcess(NULL, ProcessDebugPort,
1063 NULL, sizeof(debug_port), NULL);
1064 ok(status == STATUS_INVALID_HANDLE || status == STATUS_ACCESS_VIOLATION,
1065 "Expected STATUS_INVALID_HANDLE, got %#x.\n", status);
1066
1067 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
1068 NULL, sizeof(debug_port), NULL);
1069 ok(status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %#x.\n", status);
1070
1071 status = pNtQueryInformationProcess(NULL, ProcessDebugPort,
1072 &debug_port, sizeof(debug_port), NULL);
1073 ok(status == STATUS_INVALID_HANDLE, "Expected STATUS_ACCESS_VIOLATION, got %#x.\n", status);
1074
1075 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
1076 &debug_port, sizeof(debug_port) - 1, NULL);
1077 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1078
1079 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
1080 &debug_port, sizeof(debug_port) + 1, NULL);
1081 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1082
1083 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
1084 &debug_port, sizeof(debug_port), NULL);
1085 ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
1086 ok(debug_port == 0, "Expected port 0, got %#lx.\n", debug_port);
1087
1088 status = pNtQueryInformationProcess(pi.hProcess, ProcessDebugPort,
1089 &debug_port, sizeof(debug_port), NULL);
1090 ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
1091 ok(debug_port == ~(DWORD_PTR)0, "Expected port %#lx, got %#lx.\n", ~(DWORD_PTR)0, debug_port);
1092
1093 for (;;)
1094 {
1095 DEBUG_EVENT ev;
1096
1097 ret = WaitForDebugEvent(&ev, INFINITE);
1098 ok(ret, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
1099 if (!ret) break;
1100
1101 if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) break;
1102
1103 ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
1104 ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
1105 if (!ret) break;
1106 }
1107
1108 ret = CloseHandle(pi.hThread);
1109 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
1110 ret = CloseHandle(pi.hProcess);
1111 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
1112 }
1113
1114 static void test_query_process_handlecount(void)
1115 {
1116 NTSTATUS status;
1117 ULONG ReturnLength;
1118 DWORD handlecount;
1119 BYTE buffer[2 * sizeof(DWORD)];
1120 HANDLE process;
1121
1122 status = pNtQueryInformationProcess(NULL, ProcessHandleCount, NULL, sizeof(handlecount), NULL);
1123 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
1124 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
1125
1126 status = pNtQueryInformationProcess(NULL, ProcessHandleCount, &handlecount, sizeof(handlecount), NULL);
1127 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
1128
1129 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessHandleCount, &handlecount, 2, &ReturnLength);
1130 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1131
1132 process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, one_before_last_pid);
1133 if (!process)
1134 {
1135 trace("Could not open process with ID : %d, error : %u. Going to use current one.\n", one_before_last_pid, GetLastError());
1136 process = GetCurrentProcess();
1137 trace("ProcessHandleCount for current process\n");
1138 }
1139 else
1140 trace("ProcessHandleCount for process with ID : %d\n", one_before_last_pid);
1141
1142 status = pNtQueryInformationProcess( process, ProcessHandleCount, &handlecount, sizeof(handlecount), &ReturnLength);
1143 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1144 ok( sizeof(handlecount) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
1145 CloseHandle(process);
1146
1147 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessHandleCount, buffer, sizeof(buffer), &ReturnLength);
1148 ok( status == STATUS_INFO_LENGTH_MISMATCH || status == STATUS_SUCCESS,
1149 "Expected STATUS_INFO_LENGTH_MISMATCH or STATUS_SUCCESS, got %08x\n", status);
1150 ok( sizeof(handlecount) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
1151
1152 /* Check if we have some return values */
1153 trace("HandleCount : %d\n", handlecount);
1154 todo_wine
1155 {
1156 ok( handlecount > 0, "Expected some handles, got 0\n");
1157 }
1158 }
1159
1160 static void test_query_process_image_file_name(void)
1161 {
1162 NTSTATUS status;
1163 ULONG ReturnLength;
1164 UNICODE_STRING image_file_name;
1165 void *buffer;
1166 char *file_nameA;
1167 INT len;
1168
1169 status = pNtQueryInformationProcess(NULL, ProcessImageFileName, &image_file_name, sizeof(image_file_name), NULL);
1170 if (status == STATUS_INVALID_INFO_CLASS)
1171 {
1172 win_skip("ProcessImageFileName is not supported\n");
1173 return;
1174 }
1175 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
1176
1177 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessImageFileName, &image_file_name, 2, &ReturnLength);
1178 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1179
1180 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessImageFileName, &image_file_name, sizeof(image_file_name), &ReturnLength);
1181 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1182
1183 buffer = HeapAlloc(GetProcessHeap(), 0, ReturnLength);
1184 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessImageFileName, buffer, ReturnLength, &ReturnLength);
1185 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1186 memcpy(&image_file_name, buffer, sizeof(image_file_name));
1187 len = WideCharToMultiByte(CP_ACP, 0, image_file_name.Buffer, image_file_name.Length/sizeof(WCHAR), NULL, 0, NULL, NULL);
1188 file_nameA = HeapAlloc(GetProcessHeap(), 0, len + 1);
1189 WideCharToMultiByte(CP_ACP, 0, image_file_name.Buffer, image_file_name.Length/sizeof(WCHAR), file_nameA, len, NULL, NULL);
1190 file_nameA[len] = '\0';
1191 HeapFree(GetProcessHeap(), 0, buffer);
1192 trace("process image file name: %s\n", file_nameA);
1193 todo_wine ok(strncmp(file_nameA, "\\Device\\", 8) == 0, "Process image name should be an NT path beginning with \\Device\\ (is %s)\n", file_nameA);
1194 HeapFree(GetProcessHeap(), 0, file_nameA);
1195 }
1196
1197 static void test_query_process_debug_object_handle(int argc, char **argv)
1198 {
1199 char cmdline[MAX_PATH];
1200 STARTUPINFOA si = {0};
1201 PROCESS_INFORMATION pi;
1202 BOOL ret;
1203 HANDLE debug_object;
1204 NTSTATUS status;
1205
1206 sprintf(cmdline, "%s %s %s", argv[0], argv[1], "debuggee");
1207
1208 si.cb = sizeof(si);
1209 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL,
1210 NULL, &si, &pi);
1211 ok(ret, "CreateProcess failed with last error %u\n", GetLastError());
1212 if (!ret) return;
1213
1214 status = pNtQueryInformationProcess(NULL, ProcessDebugObjectHandle, NULL,
1215 0, NULL);
1216 if (status == STATUS_INVALID_INFO_CLASS || status == STATUS_NOT_IMPLEMENTED)
1217 {
1218 win_skip("ProcessDebugObjectHandle is not supported\n");
1219 return;
1220 }
1221 ok(status == STATUS_INFO_LENGTH_MISMATCH,
1222 "Expected NtQueryInformationProcess to return STATUS_INFO_LENGTH_MISMATCH, got 0x%08x\n",
1223 status);
1224
1225 status = pNtQueryInformationProcess(NULL, ProcessDebugObjectHandle, NULL,
1226 sizeof(debug_object), NULL);
1227 ok(status == STATUS_INVALID_HANDLE ||
1228 status == STATUS_ACCESS_VIOLATION, /* XP */
1229 "Expected NtQueryInformationProcess to return STATUS_INVALID_HANDLE, got 0x%08x\n", status);
1230
1231 status = pNtQueryInformationProcess(GetCurrentProcess(),
1232 ProcessDebugObjectHandle, NULL, sizeof(debug_object), NULL);
1233 ok(status == STATUS_ACCESS_VIOLATION,
1234 "Expected NtQueryInformationProcess to return STATUS_ACCESS_VIOLATION, got 0x%08x\n", status);
1235
1236 status = pNtQueryInformationProcess(NULL, ProcessDebugObjectHandle,
1237 &debug_object, sizeof(debug_object), NULL);
1238 ok(status == STATUS_INVALID_HANDLE,
1239 "Expected NtQueryInformationProcess to return STATUS_ACCESS_VIOLATION, got 0x%08x\n", status);
1240
1241 status = pNtQueryInformationProcess(GetCurrentProcess(),
1242 ProcessDebugObjectHandle, &debug_object,
1243 sizeof(debug_object) - 1, NULL);
1244 ok(status == STATUS_INFO_LENGTH_MISMATCH,
1245 "Expected NtQueryInformationProcess to return STATUS_INFO_LENGTH_MISMATCH, got 0x%08x\n", status);
1246
1247 status = pNtQueryInformationProcess(GetCurrentProcess(),
1248 ProcessDebugObjectHandle, &debug_object,
1249 sizeof(debug_object) + 1, NULL);
1250 ok(status == STATUS_INFO_LENGTH_MISMATCH,
1251 "Expected NtQueryInformationProcess to return STATUS_INFO_LENGTH_MISMATCH, got 0x%08x\n", status);
1252
1253 debug_object = (HANDLE)0xdeadbeef;
1254 status = pNtQueryInformationProcess(GetCurrentProcess(),
1255 ProcessDebugObjectHandle, &debug_object,
1256 sizeof(debug_object), NULL);
1257 ok(status == STATUS_PORT_NOT_SET,
1258 "Expected NtQueryInformationProcess to return STATUS_PORT_NOT_SET, got 0x%08x\n", status);
1259 ok(debug_object == NULL ||
1260 broken(debug_object == (HANDLE)0xdeadbeef), /* Wow64 */
1261 "Expected debug object handle to be NULL, got %p\n", debug_object);
1262
1263 debug_object = (HANDLE)0xdeadbeef;
1264 status = pNtQueryInformationProcess(pi.hProcess, ProcessDebugObjectHandle,
1265 &debug_object, sizeof(debug_object), NULL);
1266 todo_wine
1267 ok(status == STATUS_SUCCESS,
1268 "Expected NtQueryInformationProcess to return STATUS_SUCCESS, got 0x%08x\n", status);
1269 todo_wine
1270 ok(debug_object != NULL,
1271 "Expected debug object handle to be non-NULL, got %p\n", debug_object);
1272
1273 for (;;)
1274 {
1275 DEBUG_EVENT ev;
1276
1277 ret = WaitForDebugEvent(&ev, INFINITE);
1278 ok(ret, "WaitForDebugEvent failed with last error %u\n", GetLastError());
1279 if (!ret) break;
1280
1281 if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) break;
1282
1283 ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
1284 ok(ret, "ContinueDebugEvent failed with last error %u\n", GetLastError());
1285 if (!ret) break;
1286 }
1287
1288 ret = CloseHandle(pi.hThread);
1289 ok(ret, "CloseHandle failed with last error %u\n", GetLastError());
1290 ret = CloseHandle(pi.hProcess);
1291 ok(ret, "CloseHandle failed with last error %u\n", GetLastError());
1292 }
1293
1294 static void test_query_process_debug_flags(int argc, char **argv)
1295 {
1296 DWORD debug_flags = 0xdeadbeef;
1297 char cmdline[MAX_PATH];
1298 PROCESS_INFORMATION pi;
1299 STARTUPINFOA si = { 0 };
1300 NTSTATUS status;
1301 BOOL ret;
1302
1303 sprintf(cmdline, "%s %s %s", argv[0], argv[1], "debuggee");
1304
1305 si.cb = sizeof(si);
1306 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi);
1307 ok(ret, "CreateProcess failed, last error %#x.\n", GetLastError());
1308 if (!ret) return;
1309
1310 status = pNtQueryInformationProcess(NULL, ProcessDebugFlags,
1311 NULL, 0, NULL);
1312 ok(status == STATUS_INFO_LENGTH_MISMATCH || broken(status == STATUS_INVALID_INFO_CLASS) /* NT4 */, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1313
1314 status = pNtQueryInformationProcess(NULL, ProcessDebugFlags,
1315 NULL, sizeof(debug_flags), NULL);
1316 ok(status == STATUS_INVALID_HANDLE || status == STATUS_ACCESS_VIOLATION || broken(status == STATUS_INVALID_INFO_CLASS) /* W7PROX64 (32-bit) */,
1317 "Expected STATUS_INVALID_HANDLE, got %#x.\n", status);
1318
1319 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugFlags,
1320 NULL, sizeof(debug_flags), NULL);
1321 ok(status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %#x.\n", status);
1322
1323 status = pNtQueryInformationProcess(NULL, ProcessDebugFlags,
1324 &debug_flags, sizeof(debug_flags), NULL);
1325 ok(status == STATUS_INVALID_HANDLE || broken(status == STATUS_INVALID_INFO_CLASS) /* NT4 */, "Expected STATUS_ACCESS_VIOLATION, got %#x.\n", status);
1326
1327 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugFlags,
1328 &debug_flags, sizeof(debug_flags) - 1, NULL);
1329 ok(status == STATUS_INFO_LENGTH_MISMATCH || broken(status == STATUS_INVALID_INFO_CLASS) /* NT4 */, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1330
1331 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugFlags,
1332 &debug_flags, sizeof(debug_flags) + 1, NULL);
1333 ok(status == STATUS_INFO_LENGTH_MISMATCH || broken(status == STATUS_INVALID_INFO_CLASS) /* NT4 */, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1334
1335 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugFlags,
1336 &debug_flags, sizeof(debug_flags), NULL);
1337 ok(!status || broken(status == STATUS_INVALID_INFO_CLASS) /* NT4 */, "NtQueryInformationProcess failed, status %#x.\n", status);
1338 ok(debug_flags == TRUE|| broken(status == STATUS_INVALID_INFO_CLASS) /* NT4 */, "Expected flag TRUE, got %x.\n", debug_flags);
1339
1340 status = pNtQueryInformationProcess(pi.hProcess, ProcessDebugFlags,
1341 &debug_flags, sizeof(debug_flags), NULL);
1342 ok(!status || broken(status == STATUS_INVALID_INFO_CLASS) /* NT4 */, "NtQueryInformationProcess failed, status %#x.\n", status);
1343 ok(debug_flags == FALSE || broken(status == STATUS_INVALID_INFO_CLASS) /* NT4 */, "Expected flag FALSE, got %x.\n", debug_flags);
1344
1345 for (;;)
1346 {
1347 DEBUG_EVENT ev;
1348
1349 ret = WaitForDebugEvent(&ev, INFINITE);
1350 ok(ret, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
1351 if (!ret) break;
1352
1353 if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) break;
1354
1355 ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
1356 ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
1357 if (!ret) break;
1358 }
1359
1360 ret = CloseHandle(pi.hThread);
1361 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
1362 ret = CloseHandle(pi.hProcess);
1363 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
1364 }
1365
1366 static void test_readvirtualmemory(void)
1367 {
1368 HANDLE process;
1369 NTSTATUS status;
1370 SIZE_T readcount;
1371 static const char teststring[] = "test string";
1372 char buffer[12];
1373
1374 process = OpenProcess(PROCESS_VM_READ, FALSE, GetCurrentProcessId());
1375 ok(process != 0, "Expected to be able to open own process for reading memory\n");
1376
1377 /* normal operation */
1378 status = pNtReadVirtualMemory(process, teststring, buffer, 12, &readcount);
1379 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1380 ok( readcount == 12, "Expected to read 12 bytes, got %ld\n",readcount);
1381 ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
1382
1383 /* no number of bytes */
1384 memset(buffer, 0, 12);
1385 status = pNtReadVirtualMemory(process, teststring, buffer, 12, NULL);
1386 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1387 ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
1388
1389 /* illegal remote address */
1390 todo_wine{
1391 status = pNtReadVirtualMemory(process, (void *) 0x1234, buffer, 12, &readcount);
1392 ok( status == STATUS_PARTIAL_COPY || broken(status == STATUS_ACCESS_VIOLATION), "Expected STATUS_PARTIAL_COPY, got %08x\n", status);
1393 if (status == STATUS_PARTIAL_COPY)
1394 ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
1395 }
1396
1397 /* 0 handle */
1398 status = pNtReadVirtualMemory(0, teststring, buffer, 12, &readcount);
1399 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
1400 ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
1401
1402 /* pseudo handle for current process*/
1403 memset(buffer, 0, 12);
1404 status = pNtReadVirtualMemory((HANDLE)-1, teststring, buffer, 12, &readcount);
1405 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1406 ok( readcount == 12, "Expected to read 12 bytes, got %ld\n",readcount);
1407 ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
1408
1409 /* illegal local address */
1410 status = pNtReadVirtualMemory(process, teststring, (void *)0x1234, 12, &readcount);
1411 ok( status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %08x\n", status);
1412 ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
1413
1414 CloseHandle(process);
1415 }
1416
1417 static void test_mapprotection(void)
1418 {
1419 HANDLE h;
1420 void* addr;
1421 MEMORY_BASIC_INFORMATION info;
1422 ULONG oldflags, flagsize, flags = MEM_EXECUTE_OPTION_ENABLE;
1423 LARGE_INTEGER size, offset;
1424 NTSTATUS status;
1425 SIZE_T retlen, count;
1426 void (*f)(void);
1427
1428 if (!pNtClose) {
1429 skip("No NtClose ... Win98\n");
1430 return;
1431 }
1432 /* Switch to being a noexec unaware process */
1433 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &oldflags, sizeof (oldflags), &flagsize);
1434 if (status == STATUS_INVALID_PARAMETER) {
1435 skip("Invalid Parameter on ProcessExecuteFlags query?\n");
1436 return;
1437 }
1438 ok( (status == STATUS_SUCCESS) || (status == STATUS_INVALID_INFO_CLASS), "Expected STATUS_SUCCESS, got %08x\n", status);
1439 status = pNtSetInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &flags, sizeof(flags) );
1440 ok( (status == STATUS_SUCCESS) || (status == STATUS_INVALID_INFO_CLASS), "Expected STATUS_SUCCESS, got %08x\n", status);
1441
1442 size.u.LowPart = 0x1000;
1443 size.u.HighPart = 0;
1444 status = pNtCreateSection ( &h,
1445 STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE,
1446 NULL,
1447 &size,
1448 PAGE_READWRITE,
1449 SEC_COMMIT | SEC_NOCACHE,
1450 0
1451 );
1452 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1453
1454 offset.u.LowPart = 0;
1455 offset.u.HighPart = 0;
1456 count = 0x1000;
1457 addr = NULL;
1458 status = pNtMapViewOfSection ( h, GetCurrentProcess(), &addr, 0, 0, &offset, &count, ViewShare, 0, PAGE_READWRITE);
1459 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1460
1461 #if defined(__x86_64__) || defined(__i386__)
1462 *(unsigned char*)addr = 0xc3; /* lret ... in both i386 and x86_64 */
1463 #elif defined(__arm__)
1464 *(unsigned long*)addr = 0xe12fff1e; /* bx lr */
1465 #elif defined(__aarch64__)
1466 *(unsigned long*)addr = 0xd65f03c0; /* ret */
1467 #else
1468 ok(0, "Add a return opcode for your architecture or expect a crash in this test\n");
1469 #endif
1470 trace("trying to execute code in the readwrite only mapped anon file...\n");
1471 f = addr;f();
1472 trace("...done.\n");
1473
1474 status = pNtQueryVirtualMemory( GetCurrentProcess(), addr, MemoryBasicInformation, &info, sizeof(info), &retlen );
1475 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1476 ok( retlen == sizeof(info), "Expected STATUS_SUCCESS, got %08x\n", status);
1477 ok((info.Protect & ~PAGE_NOCACHE) == PAGE_READWRITE, "addr.Protect is not PAGE_READWRITE, but 0x%x\n", info.Protect);
1478
1479 status = pNtUnmapViewOfSection (GetCurrentProcess(), addr);
1480 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1481 pNtClose (h);
1482
1483 /* Switch back */
1484 pNtSetInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &oldflags, sizeof(oldflags) );
1485 }
1486
1487 static void test_queryvirtualmemory(void)
1488 {
1489 NTSTATUS status;
1490 SIZE_T readcount;
1491 static const char teststring[] = "test string";
1492 static char datatestbuf[42] = "abc";
1493 static char rwtestbuf[42];
1494 MEMORY_BASIC_INFORMATION mbi;
1495 char stackbuf[42];
1496 HMODULE module;
1497
1498 module = GetModuleHandleA( "ntdll.dll" );
1499 trace("Check flags of the PE header of NTDLL.DLL at %p\n", module);
1500 status = pNtQueryVirtualMemory(NtCurrentProcess(), module, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1501 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1502 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1503 ok (mbi.AllocationBase == module, "mbi.AllocationBase is 0x%p, expected 0x%p\n", mbi.AllocationBase, module);
1504 ok (mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_EXECUTE_WRITECOPY);
1505 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%x\n", mbi.State, MEM_COMMIT);
1506 ok (mbi.Protect == PAGE_READONLY, "mbi.Protect is 0x%x, expected 0x%x\n", mbi.Protect, PAGE_READONLY);
1507 ok (mbi.Type == MEM_IMAGE, "mbi.Type is 0x%x, expected 0x%x\n", mbi.Type, MEM_IMAGE);
1508
1509 trace("Check flags of a function entry in NTDLL.DLL at %p\n", pNtQueryVirtualMemory);
1510 module = GetModuleHandleA( "ntdll.dll" );
1511 status = pNtQueryVirtualMemory(NtCurrentProcess(), pNtQueryVirtualMemory, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1512 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1513 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1514 ok (mbi.AllocationBase == module, "mbi.AllocationBase is 0x%p, expected 0x%p\n", mbi.AllocationBase, module);
1515 ok (mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_EXECUTE_WRITECOPY);
1516 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%x\n", mbi.State, MEM_COMMIT);
1517 ok (mbi.Protect == PAGE_EXECUTE_READ, "mbi.Protect is 0x%x, expected 0x%x\n", mbi.Protect, PAGE_EXECUTE_READ);
1518
1519 trace("Check flags of heap at %p\n", GetProcessHeap());
1520 status = pNtQueryVirtualMemory(NtCurrentProcess(), GetProcessHeap(), MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1521 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1522 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1523 ok (mbi.AllocationProtect == PAGE_READWRITE || mbi.AllocationProtect == PAGE_EXECUTE_READWRITE,
1524 "mbi.AllocationProtect is 0x%x\n", mbi.AllocationProtect);
1525 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%x\n", mbi.State, MEM_COMMIT);
1526 ok (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE,
1527 "mbi.Protect is 0x%x\n", mbi.Protect);
1528
1529 trace("Check flags of stack at %p\n", stackbuf);
1530 status = pNtQueryVirtualMemory(NtCurrentProcess(), stackbuf, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1531 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1532 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1533 ok (mbi.AllocationProtect == PAGE_READWRITE, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_READWRITE);
1534 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%x\n", mbi.State, MEM_COMMIT);
1535 ok (mbi.Protect == PAGE_READWRITE, "mbi.Protect is 0x%x, expected 0x%x\n", mbi.Protect, PAGE_READWRITE);
1536
1537 trace("Check flags of read-only data at %p\n", teststring);
1538 module = GetModuleHandleA( NULL );
1539 status = pNtQueryVirtualMemory(NtCurrentProcess(), teststring, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1540 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1541 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1542 ok (mbi.AllocationBase == module, "mbi.AllocationBase is 0x%p, expected 0x%p\n", mbi.AllocationBase, module);
1543 ok (mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_EXECUTE_WRITECOPY);
1544 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%X\n", mbi.State, MEM_COMMIT);
1545 if (mbi.Protect != PAGE_READONLY)
1546 todo_wine ok( mbi.Protect == PAGE_READONLY, "mbi.Protect is 0x%x, expected 0x%X\n", mbi.Protect, PAGE_READONLY);
1547
1548 trace("Check flags of read-write data at %p\n", datatestbuf);
1549 status = pNtQueryVirtualMemory(NtCurrentProcess(), datatestbuf, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1550 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1551 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1552 ok (mbi.AllocationBase == module, "mbi.AllocationBase is 0x%p, expected 0x%p\n", mbi.AllocationBase, module);
1553 ok (mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_EXECUTE_WRITECOPY);
1554 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%X\n", mbi.State, MEM_COMMIT);
1555 ok (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_WRITECOPY,
1556 "mbi.Protect is 0x%x\n", mbi.Protect);
1557
1558 trace("Check flags of read-write uninitialized data (.bss) at %p\n", rwtestbuf);
1559 status = pNtQueryVirtualMemory(NtCurrentProcess(), rwtestbuf, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1560 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1561 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1562 if (mbi.AllocationBase == module)
1563 {
1564 ok (mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_EXECUTE_WRITECOPY);
1565 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%X\n", mbi.State, MEM_COMMIT);
1566 ok (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_WRITECOPY,
1567 "mbi.Protect is 0x%x\n", mbi.Protect);
1568 }
1569 else skip( "bss is outside of module\n" ); /* this can happen on Mac OS */
1570 }
1571
1572 static void test_affinity(void)
1573 {
1574 NTSTATUS status;
1575 PROCESS_BASIC_INFORMATION pbi;
1576 DWORD_PTR proc_affinity, thread_affinity;
1577 THREAD_BASIC_INFORMATION tbi;
1578 SYSTEM_INFO si;
1579
1580 GetSystemInfo(&si);
1581 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL );
1582 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1583 proc_affinity = pbi.AffinityMask;
1584 ok( proc_affinity == (1 << si.dwNumberOfProcessors) - 1, "Unexpected process affinity\n" );
1585 proc_affinity = 1 << si.dwNumberOfProcessors;
1586 status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
1587 ok( status == STATUS_INVALID_PARAMETER,
1588 "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1589
1590 proc_affinity = 0;
1591 status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
1592 ok( status == STATUS_INVALID_PARAMETER,
1593 "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1594
1595 status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1596 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1597 ok( tbi.AffinityMask == (1 << si.dwNumberOfProcessors) - 1, "Unexpected thread affinity\n" );
1598 thread_affinity = 1 << si.dwNumberOfProcessors;
1599 status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1600 ok( status == STATUS_INVALID_PARAMETER,
1601 "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1602 thread_affinity = 0;
1603 status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1604 ok( status == STATUS_INVALID_PARAMETER,
1605 "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1606
1607 thread_affinity = 1;
1608 status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1609 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1610 status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1611 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1612 ok( tbi.AffinityMask == 1, "Unexpected thread affinity\n" );
1613
1614 /* NOTE: Pre-Vista does not recognize the "all processors" flag (all bits set) */
1615 thread_affinity = ~(DWORD_PTR)0;
1616 status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1617 ok( broken(status == STATUS_INVALID_PARAMETER) || status == STATUS_SUCCESS,
1618 "Expected STATUS_SUCCESS, got %08x\n", status);
1619
1620 if (si.dwNumberOfProcessors <= 1)
1621 {
1622 skip("only one processor, skipping affinity testing\n");
1623 return;
1624 }
1625
1626 /* Test thread affinity mask resulting from "all processors" flag */
1627 if (status == STATUS_SUCCESS)
1628 {
1629 status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1630 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1631 ok( broken(tbi.AffinityMask == 1) || tbi.AffinityMask == (1 << si.dwNumberOfProcessors) - 1,
1632 "Unexpected thread affinity\n" );
1633 }
1634 else
1635 skip("Cannot test thread affinity mask for 'all processors' flag\n");
1636
1637 proc_affinity = 2;
1638 status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
1639 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1640 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL );
1641 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1642 proc_affinity = pbi.AffinityMask;
1643 ok( proc_affinity == 2, "Unexpected process affinity\n" );
1644 /* Setting the process affinity changes the thread affinity to match */
1645 status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1646 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1647 ok( tbi.AffinityMask == 2, "Unexpected thread affinity\n" );
1648 /* The thread affinity is restricted to the process affinity */
1649 thread_affinity = 1;
1650 status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1651 ok( status == STATUS_INVALID_PARAMETER,
1652 "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1653
1654 proc_affinity = (1 << si.dwNumberOfProcessors) - 1;
1655 status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
1656 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1657 /* Resetting the process affinity also resets the thread affinity */
1658 status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1659 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1660 ok( tbi.AffinityMask == (1 << si.dwNumberOfProcessors) - 1,
1661 "Unexpected thread affinity\n" );
1662 }
1663
1664 static void test_NtGetCurrentProcessorNumber(void)
1665 {
1666 NTSTATUS status;
1667 SYSTEM_INFO si;
1668 PROCESS_BASIC_INFORMATION pbi;
1669 THREAD_BASIC_INFORMATION tbi;
1670 DWORD_PTR old_process_mask;
1671 DWORD_PTR old_thread_mask;
1672 DWORD_PTR new_mask;
1673 ULONG current_cpu;
1674 ULONG i;
1675
1676 if (!pNtGetCurrentProcessorNumber) {
1677 win_skip("NtGetCurrentProcessorNumber not available\n");
1678 return;
1679 }
1680
1681 GetSystemInfo(&si);
1682 current_cpu = pNtGetCurrentProcessorNumber();
1683 trace("dwNumberOfProcessors: %d, current processor: %d\n", si.dwNumberOfProcessors, current_cpu);
1684
1685 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
1686 old_process_mask = pbi.AffinityMask;
1687 ok(status == STATUS_SUCCESS, "got 0x%x (expected STATUS_SUCCESS)\n", status);
1688
1689 status = pNtQueryInformationThread(GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL);
1690 old_thread_mask = tbi.AffinityMask;
1691 ok(status == STATUS_SUCCESS, "got 0x%x (expected STATUS_SUCCESS)\n", status);
1692
1693 /* allow the test to run on all processors */
1694 new_mask = (1 << si.dwNumberOfProcessors) - 1;
1695 status = pNtSetInformationProcess(GetCurrentProcess(), ProcessAffinityMask, &new_mask, sizeof(new_mask));
1696 ok(status == STATUS_SUCCESS, "got 0x%x (expected STATUS_SUCCESS)\n", status);
1697
1698 for (i = 0; i < si.dwNumberOfProcessors; i++)
1699 {
1700 new_mask = 1 << i;
1701 status = pNtSetInformationThread(GetCurrentThread(), ThreadAffinityMask, &new_mask, sizeof(new_mask));
1702 ok(status == STATUS_SUCCESS, "%d: got 0x%x (expected STATUS_SUCCESS)\n", i, status);
1703
1704 status = pNtQueryInformationThread(GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL);
1705 ok(status == STATUS_SUCCESS, "%d: got 0x%x (expected STATUS_SUCCESS)\n", i, status);
1706
1707 current_cpu = pNtGetCurrentProcessorNumber();
1708 ok((current_cpu == i), "%d (new_mask 0x%lx): running on processor %d (AffinityMask: 0x%lx)\n",
1709 i, new_mask, current_cpu, tbi.AffinityMask);
1710 }
1711
1712 /* restore old values */
1713 status = pNtSetInformationProcess(GetCurrentProcess(), ProcessAffinityMask, &old_process_mask, sizeof(old_process_mask));
1714 ok(status == STATUS_SUCCESS, "got 0x%x (expected STATUS_SUCCESS)\n", status);
1715
1716 status = pNtSetInformationThread(GetCurrentThread(), ThreadAffinityMask, &old_thread_mask, sizeof(old_thread_mask));
1717 ok(status == STATUS_SUCCESS, "got 0x%x (expected STATUS_SUCCESS)\n", status);
1718 }
1719
1720 static DWORD WINAPI start_address_thread(void *arg)
1721 {
1722 PRTL_THREAD_START_ROUTINE entry;
1723 NTSTATUS status;
1724 DWORD ret;
1725
1726 entry = NULL;
1727 ret = 0xdeadbeef;
1728 status = pNtQueryInformationThread(GetCurrentThread(), ThreadQuerySetWin32StartAddress,
1729 &entry, sizeof(entry), &ret);
1730 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
1731 ok(ret == sizeof(entry), "NtQueryInformationThread returned %u bytes\n", ret);
1732 ok(entry == (void *)start_address_thread, "expected %p, got %p\n", start_address_thread, entry);
1733 return 0;
1734 }
1735
1736 static void test_thread_start_address(void)
1737 {
1738 PRTL_THREAD_START_ROUTINE entry, expected_entry;
1739 IMAGE_NT_HEADERS *nt;
1740 NTSTATUS status;
1741 HANDLE thread;
1742 void *module;
1743 DWORD ret;
1744
1745 module = GetModuleHandleA(0);
1746 ok(module != NULL, "expected non-NULL address for module\n");
1747 nt = RtlImageNtHeader(module);
1748 ok(nt != NULL, "expected non-NULL address for NT header\n");
1749
1750 entry = NULL;
1751 ret = 0xdeadbeef;
1752 status = pNtQueryInformationThread(GetCurrentThread(), ThreadQuerySetWin32StartAddress,
1753 &entry, sizeof(entry), &ret);
1754 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
1755 ok(ret == sizeof(entry), "NtQueryInformationThread returned %u bytes\n", ret);
1756 expected_entry = (void *)((char *)module + nt->OptionalHeader.AddressOfEntryPoint);
1757 ok(entry == expected_entry, "expected %p, got %p\n", expected_entry, entry);
1758
1759 entry = (void *)0xdeadbeef;
1760 status = pNtSetInformationThread(GetCurrentThread(), ThreadQuerySetWin32StartAddress,
1761 &entry, sizeof(entry));
1762 ok(status == STATUS_SUCCESS || status == STATUS_INVALID_PARAMETER, /* >= Vista */
1763 "expected STATUS_SUCCESS or STATUS_INVALID_PARAMETER, got %08x\n", status);
1764
1765 if (status == STATUS_SUCCESS)
1766 {
1767 entry = NULL;
1768 ret = 0xdeadbeef;
1769 status = pNtQueryInformationThread(GetCurrentThread(), ThreadQuerySetWin32StartAddress,
1770 &entry, sizeof(entry), &ret);
1771 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
1772 ok(ret == sizeof(entry), "NtQueryInformationThread returned %u bytes\n", ret);
1773 ok(entry == (void *)0xdeadbeef, "expected 0xdeadbeef, got %p\n", entry);
1774 }
1775
1776 thread = CreateThread(NULL, 0, start_address_thread, NULL, 0, NULL);
1777 ok(thread != INVALID_HANDLE_VALUE, "CreateThread failed with %d\n", GetLastError());
1778 ret = WaitForSingleObject(thread, 1000);
1779 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
1780 CloseHandle(thread);
1781 }
1782
1783 START_TEST(info)
1784 {
1785 char **argv;
1786 int argc;
1787
1788 if(!InitFunctionPtrs())
1789 return;
1790
1791 argc = winetest_get_mainargs(&argv);
1792 if (argc >= 3) return; /* Child */
1793
1794 /* NtQuerySystemInformation */
1795
1796 /* 0x0 SystemBasicInformation */
1797 trace("Starting test_query_basic()\n");
1798 test_query_basic();
1799
1800 /* 0x1 SystemCpuInformation */
1801 trace("Starting test_query_cpu()\n");
1802 test_query_cpu();
1803
1804 /* 0x2 SystemPerformanceInformation */
1805 trace("Starting test_query_performance()\n");
1806 test_query_performance();
1807
1808 /* 0x3 SystemTimeOfDayInformation */
1809 trace("Starting test_query_timeofday()\n");
1810 test_query_timeofday();
1811
1812 /* 0x5 SystemProcessInformation */
1813 trace("Starting test_query_process()\n");
1814 test_query_process();
1815
1816 /* 0x8 SystemProcessorPerformanceInformation */
1817 trace("Starting test_query_procperf()\n");
1818 test_query_procperf();
1819
1820 /* 0xb SystemModuleInformation */
1821 trace("Starting test_query_module()\n");
1822 test_query_module();
1823
1824 /* 0x10 SystemHandleInformation */
1825 trace("Starting test_query_handle()\n");
1826 test_query_handle();
1827
1828 /* 0x15 SystemCacheInformation */
1829 trace("Starting test_query_cache()\n");
1830 test_query_cache();
1831
1832 /* 0x17 SystemInterruptInformation */
1833 trace("Starting test_query_interrupt()\n");
1834 test_query_interrupt();
1835
1836 /* 0x23 SystemKernelDebuggerInformation */
1837 trace("Starting test_query_kerndebug()\n");
1838 test_query_kerndebug();
1839
1840 /* 0x25 SystemRegistryQuotaInformation */
1841 trace("Starting test_query_regquota()\n");
1842 test_query_regquota();
1843
1844 /* 0x49 SystemLogicalProcessorInformation */
1845 trace("Starting test_query_logicalproc()\n");
1846 test_query_logicalproc();
1847
1848 /* NtPowerInformation */
1849
1850 /* 0xb ProcessorInformation */
1851 trace("Starting test_query_processor_power_info()\n");
1852 test_query_processor_power_info();
1853
1854 /* NtQueryInformationProcess */
1855
1856 /* 0x0 ProcessBasicInformation */
1857 trace("Starting test_query_process_basic()\n");
1858 test_query_process_basic();
1859
1860 /* 0x2 ProcessIoCounters */
1861 trace("Starting test_query_process_io()\n");
1862 test_query_process_io();
1863
1864 /* 0x3 ProcessVmCounters */
1865 trace("Starting test_query_process_vm()\n");
1866 test_query_process_vm();
1867
1868 /* 0x4 ProcessTimes */
1869 trace("Starting test_query_process_times()\n");
1870 test_query_process_times();
1871
1872 /* 0x7 ProcessDebugPort */
1873 trace("Starting test_process_debug_port()\n");
1874 test_query_process_debug_port(argc, argv);
1875
1876 /* 0x14 ProcessHandleCount */
1877 trace("Starting test_query_process_handlecount()\n");
1878 test_query_process_handlecount();
1879
1880 /* 0x1A ProcessWow64Information */
1881 trace("Starting test_query_process_wow64()\n");
1882 test_query_process_wow64();
1883
1884 /* 0x1B ProcessImageFileName */
1885 trace("Starting test_query_process_image_file_name()\n");
1886 test_query_process_image_file_name();
1887
1888 /* 0x1E ProcessDebugObjectHandle */
1889 trace("Starting test_query_process_debug_object_handle()\n");
1890 test_query_process_debug_object_handle(argc, argv);
1891
1892 /* 0x1F ProcessDebugFlags */
1893 trace("Starting test_process_debug_flags()\n");
1894 test_query_process_debug_flags(argc, argv);
1895
1896 /* belongs to its own file */
1897 trace("Starting test_readvirtualmemory()\n");
1898 test_readvirtualmemory();
1899
1900 trace("Starting test_queryvirtualmemory()\n");
1901 test_queryvirtualmemory();
1902
1903 trace("Starting test_mapprotection()\n");
1904 test_mapprotection();
1905
1906 trace("Starting test_affinity()\n");
1907 test_affinity();
1908
1909 trace("Starting test_NtGetCurrentProcessorNumber()\n");
1910 test_NtGetCurrentProcessorNumber();
1911
1912 trace("Starting test_thread_start_address()\n");
1913 test_thread_start_address();
1914 }