ntdll: Implement ProcessPriorityClass in NtQueryInformationProcess.
[wine.git] / dlls / ntdll / tests / info.c
blob54321e549fa4aeaa0b094e0dc374ab2cb6f88df3
1 /* Unit test suite for *Information* Registry API functions
3 * Copyright 2005 Paul Vriens
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.
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.
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
21 #include "ntdll_test.h"
22 #include <winnls.h>
23 #include <stdio.h>
25 static NTSTATUS (WINAPI * pNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
26 static NTSTATUS (WINAPI * pNtQuerySystemInformationEx)(SYSTEM_INFORMATION_CLASS, void*, ULONG, void*, ULONG, ULONG*);
27 static NTSTATUS (WINAPI * pNtPowerInformation)(POWER_INFORMATION_LEVEL, PVOID, ULONG, PVOID, ULONG);
28 static NTSTATUS (WINAPI * pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
29 static NTSTATUS (WINAPI * pNtQueryInformationThread)(HANDLE, THREADINFOCLASS, PVOID, ULONG, PULONG);
30 static NTSTATUS (WINAPI * pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
31 static NTSTATUS (WINAPI * pNtSetInformationThread)(HANDLE, THREADINFOCLASS, PVOID, ULONG);
32 static NTSTATUS (WINAPI * pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
33 static NTSTATUS (WINAPI * pNtQueryVirtualMemory)(HANDLE, LPCVOID, MEMORY_INFORMATION_CLASS , PVOID , SIZE_T , SIZE_T *);
34 static NTSTATUS (WINAPI * pNtCreateSection)(HANDLE*,ACCESS_MASK,const OBJECT_ATTRIBUTES*,const LARGE_INTEGER*,ULONG,ULONG,HANDLE);
35 static NTSTATUS (WINAPI * pNtMapViewOfSection)(HANDLE,HANDLE,PVOID*,ULONG,SIZE_T,const LARGE_INTEGER*,SIZE_T*,SECTION_INHERIT,ULONG,ULONG);
36 static NTSTATUS (WINAPI * pNtUnmapViewOfSection)(HANDLE,PVOID);
37 static NTSTATUS (WINAPI * pNtClose)(HANDLE);
38 static ULONG (WINAPI * pNtGetCurrentProcessorNumber)(void);
39 static BOOL (WINAPI * pIsWow64Process)(HANDLE, PBOOL);
40 static BOOL (WINAPI * pGetLogicalProcessorInformationEx)(LOGICAL_PROCESSOR_RELATIONSHIP,SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*,DWORD*);
42 static BOOL is_wow64;
44 /* one_before_last_pid is used to be able to compare values of a still running process
45 with the output of the test_query_process_times and test_query_process_handlecount tests.
47 static DWORD one_before_last_pid = 0;
49 #define NTDLL_GET_PROC(func) do { \
50 p ## func = (void*)GetProcAddress(hntdll, #func); \
51 if(!p ## func) { \
52 trace("GetProcAddress(%s) failed\n", #func); \
53 return FALSE; \
54 } \
55 } while(0)
57 static BOOL InitFunctionPtrs(void)
59 /* All needed functions are NT based, so using GetModuleHandle is a good check */
60 HMODULE hntdll = GetModuleHandleA("ntdll");
61 HMODULE hkernel32 = GetModuleHandleA("kernel32");
63 if (!hntdll)
65 win_skip("Not running on NT\n");
66 return FALSE;
69 NTDLL_GET_PROC(NtQuerySystemInformation);
70 NTDLL_GET_PROC(NtPowerInformation);
71 NTDLL_GET_PROC(NtQueryInformationProcess);
72 NTDLL_GET_PROC(NtQueryInformationThread);
73 NTDLL_GET_PROC(NtSetInformationProcess);
74 NTDLL_GET_PROC(NtSetInformationThread);
75 NTDLL_GET_PROC(NtReadVirtualMemory);
76 NTDLL_GET_PROC(NtQueryVirtualMemory);
77 NTDLL_GET_PROC(NtClose);
78 NTDLL_GET_PROC(NtCreateSection);
79 NTDLL_GET_PROC(NtMapViewOfSection);
80 NTDLL_GET_PROC(NtUnmapViewOfSection);
82 /* not present before XP */
83 pNtGetCurrentProcessorNumber = (void *) GetProcAddress(hntdll, "NtGetCurrentProcessorNumber");
85 pIsWow64Process = (void *)GetProcAddress(hkernel32, "IsWow64Process");
86 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
88 /* starting with Win7 */
89 pNtQuerySystemInformationEx = (void *) GetProcAddress(hntdll, "NtQuerySystemInformationEx");
90 if (!pNtQuerySystemInformationEx)
91 win_skip("NtQuerySystemInformationEx() is not supported, some tests will be skipped.\n");
93 pGetLogicalProcessorInformationEx = (void *) GetProcAddress(hkernel32, "GetLogicalProcessorInformationEx");
95 return TRUE;
98 static void test_query_basic(void)
100 NTSTATUS status;
101 ULONG ReturnLength;
102 SYSTEM_BASIC_INFORMATION sbi;
104 /* This test also covers some basic parameter testing that should be the same for
105 * every information class
108 /* Use a nonexistent info class */
109 trace("Check nonexistent info class\n");
110 status = pNtQuerySystemInformation(-1, NULL, 0, NULL);
111 ok( status == STATUS_INVALID_INFO_CLASS || status == STATUS_NOT_IMPLEMENTED /* vista */,
112 "Expected STATUS_INVALID_INFO_CLASS or STATUS_NOT_IMPLEMENTED, got %08x\n", status);
114 /* Use an existing class but with a zero-length buffer */
115 trace("Check zero-length buffer\n");
116 status = pNtQuerySystemInformation(SystemBasicInformation, NULL, 0, NULL);
117 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
119 /* Use an existing class, correct length but no SystemInformation buffer */
120 trace("Check no SystemInformation buffer\n");
121 status = pNtQuerySystemInformation(SystemBasicInformation, NULL, sizeof(sbi), NULL);
122 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_PARAMETER /* vista */,
123 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER, got %08x\n", status);
125 /* Use an existing class, correct length, a pointer to a buffer but no ReturnLength pointer */
126 trace("Check no ReturnLength pointer\n");
127 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), NULL);
128 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
130 /* Check a too large buffer size */
131 trace("Check a too large buffer size\n");
132 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi) * 2, &ReturnLength);
133 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
135 /* Finally some correct calls */
136 trace("Check with correct parameters\n");
137 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
138 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
139 ok( sizeof(sbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
141 /* Check if we have some return values */
142 trace("Number of Processors : %d\n", sbi.NumberOfProcessors);
143 ok( sbi.NumberOfProcessors > 0, "Expected more than 0 processors, got %d\n", sbi.NumberOfProcessors);
146 static void test_query_cpu(void)
148 DWORD status;
149 ULONG ReturnLength;
150 SYSTEM_CPU_INFORMATION sci;
152 status = pNtQuerySystemInformation(SystemCpuInformation, &sci, sizeof(sci), &ReturnLength);
153 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
154 ok( sizeof(sci) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
156 /* Check if we have some return values */
157 trace("Processor FeatureSet : %08x\n", sci.FeatureSet);
158 ok( sci.FeatureSet != 0, "Expected some features for this processor, got %08x\n", sci.FeatureSet);
161 static void test_query_performance(void)
163 NTSTATUS status;
164 ULONG ReturnLength;
165 ULONGLONG buffer[sizeof(SYSTEM_PERFORMANCE_INFORMATION)/sizeof(ULONGLONG) + 5];
166 DWORD size = sizeof(SYSTEM_PERFORMANCE_INFORMATION);
168 status = pNtQuerySystemInformation(SystemPerformanceInformation, buffer, 0, &ReturnLength);
169 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
171 status = pNtQuerySystemInformation(SystemPerformanceInformation, buffer, size, &ReturnLength);
172 if (status == STATUS_INFO_LENGTH_MISMATCH && is_wow64)
174 /* size is larger on wow64 under w2k8/win7 */
175 size += 16;
176 status = pNtQuerySystemInformation(SystemPerformanceInformation, buffer, size, &ReturnLength);
178 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
179 ok( ReturnLength == size, "Inconsistent length %d\n", ReturnLength);
181 status = pNtQuerySystemInformation(SystemPerformanceInformation, buffer, size + 2, &ReturnLength);
182 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
183 ok( ReturnLength == size || ReturnLength == size + 2,
184 "Inconsistent length %d\n", ReturnLength);
186 /* Not return values yet, as struct members are unknown */
189 static void test_query_timeofday(void)
191 NTSTATUS status;
192 ULONG ReturnLength;
194 /* Copy of our winternl.h structure turned into a private one */
195 typedef struct _SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE {
196 LARGE_INTEGER liKeBootTime;
197 LARGE_INTEGER liKeSystemTime;
198 LARGE_INTEGER liExpTimeZoneBias;
199 ULONG uCurrentTimeZoneId;
200 DWORD dwUnknown1[5];
201 } SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE;
203 SYSTEM_TIMEOFDAY_INFORMATION_PRIVATE sti;
205 /* The struct size for NT (32 bytes) and Win2K/XP (48 bytes) differ.
207 * Windows 2000 and XP return STATUS_INFO_LENGTH_MISMATCH if the given buffer size is greater
208 * then 48 and 0 otherwise
209 * Windows NT returns STATUS_INFO_LENGTH_MISMATCH when the given buffer size is not correct
210 * and 0 otherwise
212 * Windows 2000 and XP copy the given buffer size into the provided buffer, if the return code is STATUS_SUCCESS
213 * NT only fills the buffer if the return code is STATUS_SUCCESS
217 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
219 if (status == STATUS_INFO_LENGTH_MISMATCH)
221 trace("Windows version is NT, we have to cater for differences with W2K/WinXP\n");
223 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
224 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
225 ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
227 sti.uCurrentTimeZoneId = 0xdeadbeef;
228 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 28, &ReturnLength);
229 ok(status == STATUS_SUCCESS || broken(status == STATUS_INFO_LENGTH_MISMATCH /* NT4 */), "Expected STATUS_SUCCESS, got %08x\n", status);
230 ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
232 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
233 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
234 ok( 32 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
236 else
238 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 0, &ReturnLength);
239 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
240 ok( 0 == ReturnLength, "ReturnLength should be 0, it is (%d)\n", ReturnLength);
242 sti.uCurrentTimeZoneId = 0xdeadbeef;
243 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 24, &ReturnLength);
244 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
245 ok( 24 == ReturnLength, "ReturnLength should be 24, it is (%d)\n", ReturnLength);
246 ok( 0xdeadbeef == sti.uCurrentTimeZoneId, "This part of the buffer should not have been filled\n");
248 sti.uCurrentTimeZoneId = 0xdeadbeef;
249 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 32, &ReturnLength);
250 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
251 ok( 32 == ReturnLength, "ReturnLength should be 32, it is (%d)\n", ReturnLength);
252 ok( 0xdeadbeef != sti.uCurrentTimeZoneId, "Buffer should have been partially filled\n");
254 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, 49, &ReturnLength);
255 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
256 ok( ReturnLength == 0 || ReturnLength == sizeof(sti) /* vista */,
257 "ReturnLength should be 0, it is (%d)\n", ReturnLength);
259 status = pNtQuerySystemInformation(SystemTimeOfDayInformation, &sti, sizeof(sti), &ReturnLength);
260 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
261 ok( sizeof(sti) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
264 /* Check if we have some return values */
265 trace("uCurrentTimeZoneId : (%d)\n", sti.uCurrentTimeZoneId);
268 static void test_query_process(void)
270 NTSTATUS status;
271 DWORD last_pid;
272 ULONG ReturnLength;
273 int i = 0, k = 0;
274 BOOL is_nt = FALSE;
275 SYSTEM_BASIC_INFORMATION sbi;
277 /* Copy of our winternl.h structure turned into a private one */
278 typedef struct _SYSTEM_PROCESS_INFORMATION_PRIVATE {
279 ULONG NextEntryOffset;
280 DWORD dwThreadCount;
281 DWORD dwUnknown1[6];
282 FILETIME ftCreationTime;
283 FILETIME ftUserTime;
284 FILETIME ftKernelTime;
285 UNICODE_STRING ProcessName;
286 DWORD dwBasePriority;
287 HANDLE UniqueProcessId;
288 HANDLE ParentProcessId;
289 ULONG HandleCount;
290 DWORD dwUnknown3;
291 DWORD dwUnknown4;
292 VM_COUNTERS vmCounters;
293 IO_COUNTERS ioCounters;
294 SYSTEM_THREAD_INFORMATION ti[1];
295 } SYSTEM_PROCESS_INFORMATION_PRIVATE;
297 ULONG SystemInformationLength = sizeof(SYSTEM_PROCESS_INFORMATION_PRIVATE);
298 SYSTEM_PROCESS_INFORMATION_PRIVATE *spi, *spi_buf = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
300 /* test ReturnLength */
301 ReturnLength = 0;
302 status = pNtQuerySystemInformation(SystemProcessInformation, NULL, 0, &ReturnLength);
303 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH got %08x\n", status);
304 ok( ReturnLength > 0 || broken(ReturnLength == 0) /* NT4, Win2K */,
305 "Expected a ReturnLength to show the needed length\n");
307 /* W2K3 and later returns the needed length, the rest returns 0, so we have to loop */
308 for (;;)
310 status = pNtQuerySystemInformation(SystemProcessInformation, spi_buf, SystemInformationLength, &ReturnLength);
312 if (status != STATUS_INFO_LENGTH_MISMATCH) break;
314 spi_buf = HeapReAlloc(GetProcessHeap(), 0, spi_buf , SystemInformationLength *= 2);
316 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
317 spi = spi_buf;
319 /* Get the first NextEntryOffset, from this we can deduce the OS version we're running
321 * W2K/WinXP/W2K3:
322 * NextEntryOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
323 * NT:
324 * NextEntryOffset for a process is 136 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION)
325 * Wine (with every windows version):
326 * NextEntryOffset for a process is 0 if just this test is running
327 * NextEntryOffset for a process is 184 + (no. of threads) * sizeof(SYSTEM_THREAD_INFORMATION) +
328 * ProcessName.MaximumLength
329 * if more wine processes are running
331 * Note : On windows the first process is in fact the Idle 'process' with a thread for every processor
334 pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
336 is_nt = ( spi->NextEntryOffset - (sbi.NumberOfProcessors * sizeof(SYSTEM_THREAD_INFORMATION)) == 136);
338 if (is_nt) win_skip("Windows version is NT, we will skip thread tests\n");
340 /* Check if we have some return values
342 * On windows there will be several processes running (Including the always present Idle and System)
343 * On wine we only have one (if this test is the only wine process running)
346 /* Loop through the processes */
348 for (;;)
350 i++;
352 last_pid = (DWORD_PTR)spi->UniqueProcessId;
354 ok( spi->dwThreadCount > 0, "Expected some threads for this process, got 0\n");
356 /* Loop through the threads, skip NT4 for now */
358 if (!is_nt)
360 DWORD j;
361 for ( j = 0; j < spi->dwThreadCount; j++)
363 k++;
364 ok ( spi->ti[j].ClientId.UniqueProcess == spi->UniqueProcessId,
365 "The owning pid of the thread (%p) doesn't equal the pid (%p) of the process\n",
366 spi->ti[j].ClientId.UniqueProcess, spi->UniqueProcessId);
370 if (!spi->NextEntryOffset) break;
372 one_before_last_pid = last_pid;
374 spi = (SYSTEM_PROCESS_INFORMATION_PRIVATE*)((char*)spi + spi->NextEntryOffset);
376 trace("Total number of running processes : %d\n", i);
377 if (!is_nt) trace("Total number of running threads : %d\n", k);
379 if (one_before_last_pid == 0) one_before_last_pid = last_pid;
381 HeapFree( GetProcessHeap(), 0, spi_buf);
384 static void test_query_procperf(void)
386 NTSTATUS status;
387 ULONG ReturnLength;
388 ULONG NeededLength;
389 SYSTEM_BASIC_INFORMATION sbi;
390 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* sppi;
392 /* Find out the number of processors */
393 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
394 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
395 NeededLength = sbi.NumberOfProcessors * sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
397 sppi = HeapAlloc(GetProcessHeap(), 0, NeededLength);
399 status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, 0, &ReturnLength);
400 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
402 /* Try it for 1 processor */
403 sppi->KernelTime.QuadPart = 0xdeaddead;
404 sppi->UserTime.QuadPart = 0xdeaddead;
405 sppi->IdleTime.QuadPart = 0xdeaddead;
406 status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi,
407 sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), &ReturnLength);
408 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
409 ok( sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) == ReturnLength,
410 "Inconsistent length %d\n", ReturnLength);
411 ok (sppi->KernelTime.QuadPart != 0xdeaddead, "KernelTime unchanged\n");
412 ok (sppi->UserTime.QuadPart != 0xdeaddead, "UserTime unchanged\n");
413 ok (sppi->IdleTime.QuadPart != 0xdeaddead, "IdleTime unchanged\n");
415 /* Try it for all processors */
416 sppi->KernelTime.QuadPart = 0xdeaddead;
417 sppi->UserTime.QuadPart = 0xdeaddead;
418 sppi->IdleTime.QuadPart = 0xdeaddead;
419 status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, NeededLength, &ReturnLength);
420 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
421 ok( NeededLength == ReturnLength, "Inconsistent length (%d) <-> (%d)\n", NeededLength, ReturnLength);
422 ok (sppi->KernelTime.QuadPart != 0xdeaddead, "KernelTime unchanged\n");
423 ok (sppi->UserTime.QuadPart != 0xdeaddead, "UserTime unchanged\n");
424 ok (sppi->IdleTime.QuadPart != 0xdeaddead, "IdleTime unchanged\n");
426 /* A too large given buffer size */
427 sppi = HeapReAlloc(GetProcessHeap(), 0, sppi , NeededLength + 2);
428 sppi->KernelTime.QuadPart = 0xdeaddead;
429 sppi->UserTime.QuadPart = 0xdeaddead;
430 sppi->IdleTime.QuadPart = 0xdeaddead;
431 status = pNtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi, NeededLength + 2, &ReturnLength);
432 ok( status == STATUS_SUCCESS || status == STATUS_INFO_LENGTH_MISMATCH /* vista */,
433 "Expected STATUS_SUCCESS or STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
434 ok( NeededLength == ReturnLength, "Inconsistent length (%d) <-> (%d)\n", NeededLength, ReturnLength);
435 if (status == STATUS_SUCCESS)
437 ok (sppi->KernelTime.QuadPart != 0xdeaddead, "KernelTime unchanged\n");
438 ok (sppi->UserTime.QuadPart != 0xdeaddead, "UserTime unchanged\n");
439 ok (sppi->IdleTime.QuadPart != 0xdeaddead, "IdleTime unchanged\n");
441 else /* vista and 2008 */
443 ok (sppi->KernelTime.QuadPart == 0xdeaddead, "KernelTime changed\n");
444 ok (sppi->UserTime.QuadPart == 0xdeaddead, "UserTime changed\n");
445 ok (sppi->IdleTime.QuadPart == 0xdeaddead, "IdleTime changed\n");
448 HeapFree( GetProcessHeap(), 0, sppi);
451 static void test_query_module(void)
453 NTSTATUS status;
454 ULONG ReturnLength;
455 ULONG ModuleCount, i;
457 ULONG SystemInformationLength = sizeof(SYSTEM_MODULE_INFORMATION);
458 SYSTEM_MODULE_INFORMATION* smi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
459 SYSTEM_MODULE* sm;
461 /* Request the needed length */
462 status = pNtQuerySystemInformation(SystemModuleInformation, smi, 0, &ReturnLength);
463 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
464 ok( ReturnLength > 0, "Expected a ReturnLength to show the needed length\n");
466 SystemInformationLength = ReturnLength;
467 smi = HeapReAlloc(GetProcessHeap(), 0, smi , SystemInformationLength);
468 status = pNtQuerySystemInformation(SystemModuleInformation, smi, SystemInformationLength, &ReturnLength);
469 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
471 ModuleCount = smi->ModulesCount;
472 sm = &smi->Modules[0];
473 /* our implementation is a stub for now */
474 ok( ModuleCount > 0, "Expected some modules to be loaded\n");
476 /* Loop through all the modules/drivers, Wine doesn't get here (yet) */
477 for (i = 0; i < ModuleCount ; i++)
479 ok( i == sm->Id, "Id (%d) should have matched %u\n", sm->Id, i);
480 sm++;
483 HeapFree( GetProcessHeap(), 0, smi);
486 static void test_query_handle(void)
488 NTSTATUS status;
489 ULONG ExpectedLength, ReturnLength;
490 ULONG SystemInformationLength = sizeof(SYSTEM_HANDLE_INFORMATION);
491 SYSTEM_HANDLE_INFORMATION* shi = HeapAlloc(GetProcessHeap(), 0, SystemInformationLength);
492 HANDLE EventHandle;
493 BOOL found;
494 INT i;
496 EventHandle = CreateEventA(NULL, FALSE, FALSE, NULL);
497 ok( EventHandle != NULL, "CreateEventA failed %u\n", GetLastError() );
499 /* Request the needed length : a SystemInformationLength greater than one struct sets ReturnLength */
500 ReturnLength = 0xdeadbeef;
501 status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
502 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
503 ok( ReturnLength != 0xdeadbeef, "Expected valid ReturnLength\n" );
505 SystemInformationLength = ReturnLength;
506 shi = HeapReAlloc(GetProcessHeap(), 0, shi , SystemInformationLength);
507 memset(shi, 0x55, SystemInformationLength);
509 ReturnLength = 0xdeadbeef;
510 status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
511 while (status == STATUS_INFO_LENGTH_MISMATCH) /* Vista / 2008 */
513 SystemInformationLength *= 2;
514 shi = HeapReAlloc(GetProcessHeap(), 0, shi, SystemInformationLength);
515 memset(shi, 0x55, SystemInformationLength);
516 status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
518 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status );
519 ExpectedLength = FIELD_OFFSET(SYSTEM_HANDLE_INFORMATION, Handle[shi->Count]);
520 ok( ReturnLength == ExpectedLength || broken(ReturnLength == ExpectedLength - sizeof(DWORD)), /* Vista / 2008 */
521 "Expected length %u, got %u\n", ExpectedLength, ReturnLength );
522 ok( shi->Count > 1, "Expected more than 1 handle, got %u\n", shi->Count );
523 ok( shi->Handle[1].HandleValue != 0x5555 || broken( shi->Handle[1].HandleValue == 0x5555 ), /* Vista / 2008 */
524 "Uninitialized second handle\n" );
525 if (shi->Handle[1].HandleValue == 0x5555)
527 win_skip("Skipping broken SYSTEM_HANDLE_INFORMATION\n");
528 CloseHandle(EventHandle);
529 goto done;
532 for (i = 0, found = FALSE; i < shi->Count && !found; i++)
533 found = (shi->Handle[i].OwnerPid == GetCurrentProcessId()) &&
534 ((HANDLE)(ULONG_PTR)shi->Handle[i].HandleValue == EventHandle);
535 ok( found, "Expected to find event handle %p (pid %x) in handle list\n", EventHandle, GetCurrentProcessId() );
537 if (!found)
538 for (i = 0; i < shi->Count; i++)
539 trace( "%d: handle %x pid %x\n", i, shi->Handle[i].HandleValue, shi->Handle[i].OwnerPid );
541 CloseHandle(EventHandle);
543 ReturnLength = 0xdeadbeef;
544 status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
545 while (status == STATUS_INFO_LENGTH_MISMATCH) /* Vista / 2008 */
547 SystemInformationLength *= 2;
548 shi = HeapReAlloc(GetProcessHeap(), 0, shi, SystemInformationLength);
549 status = pNtQuerySystemInformation(SystemHandleInformation, shi, SystemInformationLength, &ReturnLength);
551 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status );
552 for (i = 0, found = FALSE; i < shi->Count && !found; i++)
553 found = (shi->Handle[i].OwnerPid == GetCurrentProcessId()) &&
554 ((HANDLE)(ULONG_PTR)shi->Handle[i].HandleValue == EventHandle);
555 ok( !found, "Unexpectedly found event handle in handle list\n" );
557 status = pNtQuerySystemInformation(SystemHandleInformation, NULL, SystemInformationLength, &ReturnLength);
558 ok( status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %08x\n", status );
560 done:
561 HeapFree( GetProcessHeap(), 0, shi);
564 static void test_query_cache(void)
566 NTSTATUS status;
567 ULONG ReturnLength;
568 BYTE buffer[128];
569 SYSTEM_CACHE_INFORMATION *sci = (SYSTEM_CACHE_INFORMATION *) buffer;
570 ULONG expected;
571 INT i;
573 /* the large SYSTEM_CACHE_INFORMATION on WIN64 is not documented */
574 expected = sizeof(SYSTEM_CACHE_INFORMATION);
575 for (i = sizeof(buffer); i>= expected; i--)
577 ReturnLength = 0xdeadbeef;
578 status = pNtQuerySystemInformation(SystemCacheInformation, sci, i, &ReturnLength);
579 ok(!status && (ReturnLength == expected),
580 "%d: got 0x%x and %u (expected STATUS_SUCCESS and %u)\n", i, status, ReturnLength, expected);
583 /* buffer too small for the full result.
584 Up to win7, the function succeeds with a partial result. */
585 status = pNtQuerySystemInformation(SystemCacheInformation, sci, i, &ReturnLength);
586 if (!status)
588 expected = offsetof(SYSTEM_CACHE_INFORMATION, MinimumWorkingSet);
589 for (; i>= expected; i--)
591 ReturnLength = 0xdeadbeef;
592 status = pNtQuerySystemInformation(SystemCacheInformation, sci, i, &ReturnLength);
593 ok(!status && (ReturnLength == expected),
594 "%d: got 0x%x and %u (expected STATUS_SUCCESS and %u)\n", i, status, ReturnLength, expected);
598 /* buffer too small for the result, this call will always fail */
599 ReturnLength = 0xdeadbeef;
600 status = pNtQuerySystemInformation(SystemCacheInformation, sci, i, &ReturnLength);
601 ok( status == STATUS_INFO_LENGTH_MISMATCH &&
602 ((ReturnLength == expected) || broken(!ReturnLength) || broken(ReturnLength == 0xfffffff0)),
603 "%d: got 0x%x and %u (expected STATUS_INFO_LENGTH_MISMATCH and %u)\n", i, status, ReturnLength, expected);
605 if (0) {
606 /* this crashes on some vista / win7 machines */
607 ReturnLength = 0xdeadbeef;
608 status = pNtQuerySystemInformation(SystemCacheInformation, sci, 0, &ReturnLength);
609 ok( status == STATUS_INFO_LENGTH_MISMATCH &&
610 ((ReturnLength == expected) || broken(!ReturnLength) || broken(ReturnLength == 0xfffffff0)),
611 "0: got 0x%x and %u (expected STATUS_INFO_LENGTH_MISMATCH and %u)\n", status, ReturnLength, expected);
615 static void test_query_interrupt(void)
617 NTSTATUS status;
618 ULONG ReturnLength;
619 ULONG NeededLength;
620 SYSTEM_BASIC_INFORMATION sbi;
621 SYSTEM_INTERRUPT_INFORMATION* sii;
623 /* Find out the number of processors */
624 status = pNtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), &ReturnLength);
625 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
626 NeededLength = sbi.NumberOfProcessors * sizeof(SYSTEM_INTERRUPT_INFORMATION);
628 sii = HeapAlloc(GetProcessHeap(), 0, NeededLength);
630 status = pNtQuerySystemInformation(SystemInterruptInformation, sii, 0, &ReturnLength);
631 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
633 /* Try it for all processors */
634 status = pNtQuerySystemInformation(SystemInterruptInformation, sii, NeededLength, &ReturnLength);
635 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
637 /* Windows XP and W2K3 (and others?) always return 0 for the ReturnLength
638 * No test added for this as it's highly unlikely that an app depends on this
641 HeapFree( GetProcessHeap(), 0, sii);
644 static void test_query_kerndebug(void)
646 NTSTATUS status;
647 ULONG ReturnLength;
648 SYSTEM_KERNEL_DEBUGGER_INFORMATION skdi;
650 status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, 0, &ReturnLength);
651 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
653 status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, sizeof(skdi), &ReturnLength);
654 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
655 ok( sizeof(skdi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
657 status = pNtQuerySystemInformation(SystemKernelDebuggerInformation, &skdi, sizeof(skdi) + 2, &ReturnLength);
658 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
659 ok( sizeof(skdi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
662 static void test_query_regquota(void)
664 NTSTATUS status;
665 ULONG ReturnLength;
666 SYSTEM_REGISTRY_QUOTA_INFORMATION srqi;
668 status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, 0, &ReturnLength);
669 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
671 status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, sizeof(srqi), &ReturnLength);
672 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
673 ok( sizeof(srqi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
675 status = pNtQuerySystemInformation(SystemRegistryQuotaInformation, &srqi, sizeof(srqi) + 2, &ReturnLength);
676 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
677 ok( sizeof(srqi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
680 static void test_query_logicalproc(void)
682 NTSTATUS status;
683 ULONG len, i, proc_no;
684 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *slpi;
685 SYSTEM_INFO si;
687 GetSystemInfo(&si);
689 status = pNtQuerySystemInformation(SystemLogicalProcessorInformation, NULL, 0, &len);
690 if(status == STATUS_INVALID_INFO_CLASS)
692 win_skip("SystemLogicalProcessorInformation is not supported\n");
693 return;
695 if(status == STATUS_NOT_IMPLEMENTED)
697 todo_wine ok(0, "SystemLogicalProcessorInformation is not implemented\n");
698 return;
700 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
701 ok(len%sizeof(*slpi) == 0, "Incorrect length %d\n", len);
703 slpi = HeapAlloc(GetProcessHeap(), 0, len);
704 status = pNtQuerySystemInformation(SystemLogicalProcessorInformation, slpi, len, &len);
705 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
707 proc_no = 0;
708 for(i=0; i<len/sizeof(*slpi); i++) {
709 switch(slpi[i].Relationship) {
710 case RelationProcessorCore:
711 /* Get number of logical processors */
712 for(; slpi[i].ProcessorMask; slpi[i].ProcessorMask /= 2)
713 proc_no += slpi[i].ProcessorMask%2;
714 break;
715 default:
716 break;
719 ok(proc_no > 0, "No processors were found\n");
720 if(si.dwNumberOfProcessors <= 32)
721 ok(proc_no == si.dwNumberOfProcessors, "Incorrect number of logical processors: %d, expected %d\n",
722 proc_no, si.dwNumberOfProcessors);
724 HeapFree(GetProcessHeap(), 0, slpi);
727 static void test_query_logicalprocex(void)
729 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *infoex, *infoex2;
730 DWORD relationship, len2, len;
731 NTSTATUS status;
732 BOOL ret;
734 if (!pNtQuerySystemInformationEx)
735 return;
737 len = 0;
738 relationship = RelationProcessorCore;
739 status = pNtQuerySystemInformationEx(SystemLogicalProcessorInformationEx, &relationship, sizeof(relationship), NULL, 0, &len);
740 ok(status == STATUS_INFO_LENGTH_MISMATCH, "got 0x%08x\n", status);
741 ok(len > 0, "got %u\n", len);
743 len = 0;
744 relationship = RelationAll;
745 status = pNtQuerySystemInformationEx(SystemLogicalProcessorInformationEx, &relationship, sizeof(relationship), NULL, 0, &len);
746 ok(status == STATUS_INFO_LENGTH_MISMATCH, "got 0x%08x\n", status);
747 ok(len > 0, "got %u\n", len);
749 len2 = 0;
750 ret = pGetLogicalProcessorInformationEx(RelationAll, NULL, &len2);
751 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %d, error %d\n", ret, GetLastError());
752 ok(len == len2, "got %u, expected %u\n", len2, len);
754 if (len && len == len2) {
755 int j, i;
757 infoex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
758 infoex2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
760 status = pNtQuerySystemInformationEx(SystemLogicalProcessorInformationEx, &relationship, sizeof(relationship), infoex, len, &len);
761 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
763 ret = pGetLogicalProcessorInformationEx(RelationAll, infoex2, &len2);
764 ok(ret, "got %d, error %d\n", ret, GetLastError());
765 ok(!memcmp(infoex, infoex2, len), "returned info data mismatch\n");
767 for(i = 0; status == STATUS_SUCCESS && i < len; ){
768 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *ex = (void*)(((char *)infoex) + i);
770 ok(ex->Relationship >= RelationProcessorCore && ex->Relationship <= RelationGroup,
771 "Got invalid relationship value: 0x%x\n", ex->Relationship);
772 if (!ex->Size)
774 ok(0, "got infoex[%u].Size=0\n", i);
775 break;
778 trace("infoex[%u].Size: %u\n", i, ex->Size);
779 switch(ex->Relationship){
780 case RelationProcessorCore:
781 case RelationProcessorPackage:
782 trace("infoex[%u].Relationship: 0x%x (Core == 0x0 or Package == 0x3)\n", i, ex->Relationship);
783 trace("infoex[%u].Processor.Flags: 0x%x\n", i, ex->Processor.Flags);
784 trace("infoex[%u].Processor.EfficiencyClass: 0x%x\n", i, ex->Processor.EfficiencyClass);
785 trace("infoex[%u].Processor.GroupCount: 0x%x\n", i, ex->Processor.GroupCount);
786 for(j = 0; j < ex->Processor.GroupCount; ++j){
787 trace("infoex[%u].Processor.GroupMask[%u].Mask: 0x%lx\n", i, j, ex->Processor.GroupMask[j].Mask);
788 trace("infoex[%u].Processor.GroupMask[%u].Group: 0x%x\n", i, j, ex->Processor.GroupMask[j].Group);
790 break;
791 case RelationNumaNode:
792 trace("infoex[%u].Relationship: 0x%x (NumaNode)\n", i, ex->Relationship);
793 trace("infoex[%u].NumaNode.NodeNumber: 0x%x\n", i, ex->NumaNode.NodeNumber);
794 trace("infoex[%u].NumaNode.GroupMask.Mask: 0x%lx\n", i, ex->NumaNode.GroupMask.Mask);
795 trace("infoex[%u].NumaNode.GroupMask.Group: 0x%x\n", i, ex->NumaNode.GroupMask.Group);
796 break;
797 case RelationCache:
798 trace("infoex[%u].Relationship: 0x%x (Cache)\n", i, ex->Relationship);
799 trace("infoex[%u].Cache.Level: 0x%x\n", i, ex->Cache.Level);
800 trace("infoex[%u].Cache.Associativity: 0x%x\n", i, ex->Cache.Associativity);
801 trace("infoex[%u].Cache.LineSize: 0x%x\n", i, ex->Cache.LineSize);
802 trace("infoex[%u].Cache.CacheSize: 0x%x\n", i, ex->Cache.CacheSize);
803 trace("infoex[%u].Cache.Type: 0x%x\n", i, ex->Cache.Type);
804 trace("infoex[%u].Cache.GroupMask.Mask: 0x%lx\n", i, ex->Cache.GroupMask.Mask);
805 trace("infoex[%u].Cache.GroupMask.Group: 0x%x\n", i, ex->Cache.GroupMask.Group);
806 break;
807 case RelationGroup:
808 trace("infoex[%u].Relationship: 0x%x (Group)\n", i, ex->Relationship);
809 trace("infoex[%u].Group.MaximumGroupCount: 0x%x\n", i, ex->Group.MaximumGroupCount);
810 trace("infoex[%u].Group.ActiveGroupCount: 0x%x\n", i, ex->Group.ActiveGroupCount);
811 for(j = 0; j < ex->Group.ActiveGroupCount; ++j){
812 trace("infoex[%u].Group.GroupInfo[%u].MaximumProcessorCount: 0x%x\n", i, j, ex->Group.GroupInfo[j].MaximumProcessorCount);
813 trace("infoex[%u].Group.GroupInfo[%u].ActiveProcessorCount: 0x%x\n", i, j, ex->Group.GroupInfo[j].ActiveProcessorCount);
814 trace("infoex[%u].Group.GroupInfo[%u].ActiveProcessorMask: 0x%lx\n", i, j, ex->Group.GroupInfo[j].ActiveProcessorMask);
816 break;
817 default:
818 break;
821 i += ex->Size;
824 HeapFree(GetProcessHeap(), 0, infoex);
825 HeapFree(GetProcessHeap(), 0, infoex2);
829 static void test_query_processor_power_info(void)
831 NTSTATUS status;
832 PROCESSOR_POWER_INFORMATION* ppi;
833 ULONG size;
834 SYSTEM_INFO si;
835 int i;
837 GetSystemInfo(&si);
838 size = si.dwNumberOfProcessors * sizeof(PROCESSOR_POWER_INFORMATION);
839 ppi = HeapAlloc(GetProcessHeap(), 0, size);
841 /* If size < (sizeof(PROCESSOR_POWER_INFORMATION) * NumberOfProcessors), Win7 returns
842 * STATUS_BUFFER_TOO_SMALL. WinXP returns STATUS_SUCCESS for any value of size. It copies as
843 * many whole PROCESSOR_POWER_INFORMATION structures that there is room for. Even if there is
844 * not enough room for one structure, WinXP still returns STATUS_SUCCESS having done nothing.
846 * If ppi == NULL, Win7 returns STATUS_INVALID_PARAMETER while WinXP returns STATUS_SUCCESS
847 * and does nothing.
849 * The same behavior is seen with CallNtPowerInformation (in powrprof.dll).
852 if (si.dwNumberOfProcessors > 1)
854 for(i = 0; i < si.dwNumberOfProcessors; i++)
855 ppi[i].Number = 0xDEADBEEF;
857 /* Call with a buffer size that is large enough to hold at least one but not large
858 * enough to hold them all. This will be STATUS_SUCCESS on WinXP but not on Win7 */
859 status = pNtPowerInformation(ProcessorInformation, 0, 0, ppi, size - sizeof(PROCESSOR_POWER_INFORMATION));
860 if (status == STATUS_SUCCESS)
862 /* lax version found on older Windows like WinXP */
863 ok( (ppi[si.dwNumberOfProcessors - 2].Number != 0xDEADBEEF) &&
864 (ppi[si.dwNumberOfProcessors - 1].Number == 0xDEADBEEF),
865 "Expected all but the last record to be overwritten.\n");
867 status = pNtPowerInformation(ProcessorInformation, 0, 0, 0, size);
868 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
870 for(i = 0; i < si.dwNumberOfProcessors; i++)
871 ppi[i].Number = 0xDEADBEEF;
872 status = pNtPowerInformation(ProcessorInformation, 0, 0, ppi, sizeof(PROCESSOR_POWER_INFORMATION) - 1);
873 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
874 for(i = 0; i < si.dwNumberOfProcessors; i++)
875 if (ppi[i].Number != 0xDEADBEEF) break;
876 ok( i == si.dwNumberOfProcessors, "Expected untouched buffer\n");
878 else
880 /* picky version found on newer Windows like Win7 */
881 ok( ppi[1].Number == 0xDEADBEEF, "Expected untouched buffer.\n");
882 ok( status == STATUS_BUFFER_TOO_SMALL, "Expected STATUS_BUFFER_TOO_SMALL, got %08x\n", status);
884 status = pNtPowerInformation(ProcessorInformation, 0, 0, 0, size);
885 ok( status == STATUS_SUCCESS || status == STATUS_INVALID_PARAMETER, "Got %08x\n", status);
887 status = pNtPowerInformation(ProcessorInformation, 0, 0, ppi, 0);
888 ok( status == STATUS_BUFFER_TOO_SMALL || status == STATUS_INVALID_PARAMETER, "Got %08x\n", status);
891 else
893 skip("Test needs more than one processor.\n");
896 status = pNtPowerInformation(ProcessorInformation, 0, 0, ppi, size);
897 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
899 HeapFree(GetProcessHeap(), 0, ppi);
902 static void test_query_process_wow64(void)
904 NTSTATUS status;
905 ULONG ReturnLength;
906 ULONG_PTR pbi[2], dummy;
908 memset(&dummy, 0xcc, sizeof(dummy));
910 /* Do not give a handle and buffer */
911 status = pNtQueryInformationProcess(NULL, ProcessWow64Information, NULL, 0, NULL);
912 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
914 /* Use a correct info class and buffer size, but still no handle and buffer */
915 status = pNtQueryInformationProcess(NULL, ProcessWow64Information, NULL, sizeof(ULONG_PTR), NULL);
916 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
917 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE, got %08x\n", status);
919 /* Use a correct info class, buffer size and handle, but no buffer */
920 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information, NULL, sizeof(ULONG_PTR), NULL);
921 ok( status == STATUS_ACCESS_VIOLATION , "Expected STATUS_ACCESS_VIOLATION, got %08x\n", status);
923 /* Use a correct info class, buffer and buffer size, but no handle */
924 pbi[0] = pbi[1] = dummy;
925 status = pNtQueryInformationProcess(NULL, ProcessWow64Information, pbi, sizeof(ULONG_PTR), NULL);
926 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
927 ok( pbi[0] == dummy, "pbi[0] changed to %lx\n", pbi[0]);
928 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
930 /* Use a greater buffer size */
931 pbi[0] = pbi[1] = dummy;
932 status = pNtQueryInformationProcess(NULL, ProcessWow64Information, pbi, sizeof(ULONG_PTR) + 1, NULL);
933 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
934 ok( pbi[0] == dummy, "pbi[0] changed to %lx\n", pbi[0]);
935 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
937 /* Use no ReturnLength */
938 pbi[0] = pbi[1] = dummy;
939 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information, pbi, sizeof(ULONG_PTR), NULL);
940 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
941 trace( "Platform is_wow64 %d, ProcessInformation of ProcessWow64Information %lx\n", is_wow64, pbi[0]);
942 ok( is_wow64 == (pbi[0] != 0), "is_wow64 %x, pbi[0] %lx\n", is_wow64, pbi[0]);
943 ok( pbi[0] != dummy, "pbi[0] %lx\n", pbi[0]);
944 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
945 /* Test written size on 64 bit by checking high 32 bit buffer */
946 if (sizeof(ULONG_PTR) > sizeof(DWORD))
948 DWORD *ptr = (DWORD *)pbi;
949 ok( ptr[1] != (DWORD)dummy, "ptr[1] unchanged!\n");
952 /* Finally some correct calls */
953 pbi[0] = pbi[1] = dummy;
954 ReturnLength = 0xdeadbeef;
955 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information, pbi, sizeof(ULONG_PTR), &ReturnLength);
956 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
957 ok( is_wow64 == (pbi[0] != 0), "is_wow64 %x, pbi[0] %lx\n", is_wow64, pbi[0]);
958 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
959 ok( ReturnLength == sizeof(ULONG_PTR), "Inconsistent length %d\n", ReturnLength);
961 /* Everything is correct except a too small buffer size */
962 pbi[0] = pbi[1] = dummy;
963 ReturnLength = 0xdeadbeef;
964 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information, pbi, sizeof(ULONG_PTR) - 1, &ReturnLength);
965 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
966 ok( pbi[0] == dummy, "pbi[0] changed to %lx\n", pbi[0]);
967 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
968 todo_wine ok( ReturnLength == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", ReturnLength);
970 /* Everything is correct except a too large buffer size */
971 pbi[0] = pbi[1] = dummy;
972 ReturnLength = 0xdeadbeef;
973 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information, pbi, sizeof(ULONG_PTR) + 1, &ReturnLength);
974 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
975 ok( pbi[0] == dummy, "pbi[0] changed to %lx\n", pbi[0]);
976 ok( pbi[1] == dummy, "pbi[1] changed to %lx\n", pbi[1]);
977 todo_wine ok( ReturnLength == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", ReturnLength);
980 static void test_query_process_basic(void)
982 NTSTATUS status;
983 ULONG ReturnLength;
985 typedef struct _PROCESS_BASIC_INFORMATION_PRIVATE {
986 DWORD_PTR ExitStatus;
987 PPEB PebBaseAddress;
988 DWORD_PTR AffinityMask;
989 DWORD_PTR BasePriority;
990 ULONG_PTR UniqueProcessId;
991 ULONG_PTR InheritedFromUniqueProcessId;
992 } PROCESS_BASIC_INFORMATION_PRIVATE;
994 PROCESS_BASIC_INFORMATION_PRIVATE pbi;
996 /* This test also covers some basic parameter testing that should be the same for
997 * every information class
1000 /* Use a nonexistent info class */
1001 trace("Check nonexistent info class\n");
1002 status = pNtQueryInformationProcess(NULL, -1, NULL, 0, NULL);
1003 ok( status == STATUS_INVALID_INFO_CLASS || status == STATUS_NOT_IMPLEMENTED /* vista */,
1004 "Expected STATUS_INVALID_INFO_CLASS or STATUS_NOT_IMPLEMENTED, got %08x\n", status);
1006 /* Do not give a handle and buffer */
1007 trace("Check NULL handle and buffer and zero-length buffersize\n");
1008 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, 0, NULL);
1009 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1011 /* Use a correct info class and buffer size, but still no handle and buffer */
1012 trace("Check NULL handle and buffer\n");
1013 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, NULL, sizeof(pbi), NULL);
1014 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
1015 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
1017 /* Use a correct info class and buffer size, but still no handle */
1018 trace("Check NULL handle\n");
1019 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
1020 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
1022 /* Use a greater buffer size */
1023 trace("Check NULL handle and too large buffersize\n");
1024 status = pNtQueryInformationProcess(NULL, ProcessBasicInformation, &pbi, sizeof(pbi) * 2, NULL);
1025 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1027 /* Use no ReturnLength */
1028 trace("Check NULL ReturnLength\n");
1029 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
1030 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1032 /* Finally some correct calls */
1033 trace("Check with correct parameters\n");
1034 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), &ReturnLength);
1035 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1036 ok( sizeof(pbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
1038 /* Everything is correct except a too large buffersize */
1039 trace("Too large buffersize\n");
1040 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi) * 2, &ReturnLength);
1041 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1042 ok( sizeof(pbi) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
1044 /* Check if we have some return values */
1045 trace("ProcessID : %lx\n", pbi.UniqueProcessId);
1046 ok( pbi.UniqueProcessId > 0, "Expected a ProcessID > 0, got 0\n");
1049 static void dump_vm_counters(const char *header, const VM_COUNTERS *pvi)
1051 trace("%s:\n", header);
1052 trace("PeakVirtualSize : %lu\n", pvi->PeakVirtualSize);
1053 trace("VirtualSize : %lu\n", pvi->VirtualSize);
1054 trace("PageFaultCount : %u\n", pvi->PageFaultCount);
1055 trace("PeakWorkingSetSize : %lu\n", pvi->PeakWorkingSetSize);
1056 trace("WorkingSetSize : %lu\n", pvi->WorkingSetSize);
1057 trace("QuotaPeakPagedPoolUsage : %lu\n", pvi->QuotaPeakPagedPoolUsage);
1058 trace("QuotaPagedPoolUsage : %lu\n", pvi->QuotaPagedPoolUsage);
1059 trace("QuotaPeakNonPagePoolUsage : %lu\n", pvi->QuotaPeakNonPagedPoolUsage);
1060 trace("QuotaNonPagePoolUsage : %lu\n", pvi->QuotaNonPagedPoolUsage);
1061 trace("PagefileUsage : %lu\n", pvi->PagefileUsage);
1062 trace("PeakPagefileUsage : %lu\n", pvi->PeakPagefileUsage);
1065 static void test_query_process_vm(void)
1067 NTSTATUS status;
1068 ULONG ReturnLength;
1069 VM_COUNTERS pvi;
1070 ULONG old_size = FIELD_OFFSET(VM_COUNTERS,PrivatePageCount);
1071 HANDLE process;
1072 SIZE_T prev_size;
1073 const SIZE_T alloc_size = 16 * 1024 * 1024;
1074 void *ptr;
1076 status = pNtQueryInformationProcess(NULL, ProcessVmCounters, NULL, sizeof(pvi), NULL);
1077 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
1078 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
1080 status = pNtQueryInformationProcess(NULL, ProcessVmCounters, &pvi, old_size, NULL);
1081 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
1083 /* Windows XP and W2K3 will report success for a size of 44 AND 48 !
1084 Windows W2K will only report success for 44.
1085 For now we only care for 44, which is FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
1088 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, 24, &ReturnLength);
1089 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1091 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, old_size, &ReturnLength);
1092 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1093 ok( old_size == ReturnLength, "Inconsistent length %d\n", ReturnLength);
1095 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessVmCounters, &pvi, 46, &ReturnLength);
1096 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1097 ok( ReturnLength == old_size || ReturnLength == sizeof(pvi), "Inconsistent length %d\n", ReturnLength);
1099 /* Check if we have some return values */
1100 dump_vm_counters("VM counters for GetCurrentProcess", &pvi);
1101 ok( pvi.WorkingSetSize > 0, "Expected a WorkingSetSize > 0\n");
1102 ok( pvi.PagefileUsage > 0, "Expected a PagefileUsage > 0\n");
1104 process = OpenProcess(PROCESS_VM_READ, FALSE, GetCurrentProcessId());
1105 status = pNtQueryInformationProcess(process, ProcessVmCounters, &pvi, sizeof(pvi), NULL);
1106 ok( status == STATUS_ACCESS_DENIED, "Expected STATUS_ACCESS_DENIED, got %08x\n", status);
1107 CloseHandle(process);
1109 process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentProcessId());
1110 status = pNtQueryInformationProcess(process, ProcessVmCounters, &pvi, sizeof(pvi), NULL);
1111 ok( status == STATUS_SUCCESS || broken(!process) /* XP */, "Expected STATUS_SUCCESS, got %08x\n", status);
1112 CloseHandle(process);
1114 memset(&pvi, 0, sizeof(pvi));
1115 process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
1116 status = pNtQueryInformationProcess(process, ProcessVmCounters, &pvi, sizeof(pvi), NULL);
1117 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1119 /* Check if we have some return values */
1120 dump_vm_counters("VM counters for GetCurrentProcessId", &pvi);
1121 ok( pvi.WorkingSetSize > 0, "Expected a WorkingSetSize > 0\n");
1122 ok( pvi.PagefileUsage > 0, "Expected a PagefileUsage > 0\n");
1124 CloseHandle(process);
1126 /* Check if we have real counters */
1127 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessVmCounters, &pvi, sizeof(pvi), NULL);
1128 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1129 prev_size = pvi.VirtualSize;
1130 if (winetest_debug > 1)
1131 dump_vm_counters("VM counters before VirtualAlloc", &pvi);
1132 ptr = VirtualAlloc(NULL, alloc_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
1133 ok( ptr != NULL, "VirtualAlloc failed, err %u\n", GetLastError());
1134 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessVmCounters, &pvi, sizeof(pvi), NULL);
1135 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1136 if (winetest_debug > 1)
1137 dump_vm_counters("VM counters after VirtualAlloc", &pvi);
1138 todo_wine ok( pvi.VirtualSize >= prev_size + alloc_size,
1139 "Expected to be greater than %lu, got %lu\n", prev_size + alloc_size, pvi.VirtualSize);
1140 VirtualFree( ptr, 0, MEM_RELEASE);
1142 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessVmCounters, &pvi, sizeof(pvi), NULL);
1143 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1144 prev_size = pvi.VirtualSize;
1145 if (winetest_debug > 1)
1146 dump_vm_counters("VM counters before VirtualAlloc", &pvi);
1147 ptr = VirtualAlloc(NULL, alloc_size, MEM_RESERVE, PAGE_READWRITE);
1148 ok( ptr != NULL, "VirtualAlloc failed, err %u\n", GetLastError());
1149 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessVmCounters, &pvi, sizeof(pvi), NULL);
1150 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1151 if (winetest_debug > 1)
1152 dump_vm_counters("VM counters after VirtualAlloc(MEM_RESERVE)", &pvi);
1153 todo_wine ok( pvi.VirtualSize >= prev_size + alloc_size,
1154 "Expected to be greater than %lu, got %lu\n", prev_size + alloc_size, pvi.VirtualSize);
1155 prev_size = pvi.VirtualSize;
1157 ptr = VirtualAlloc(ptr, alloc_size, MEM_COMMIT, PAGE_READWRITE);
1158 ok( ptr != NULL, "VirtualAlloc failed, err %u\n", GetLastError());
1159 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessVmCounters, &pvi, sizeof(pvi), NULL);
1160 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1161 if (winetest_debug > 1)
1162 dump_vm_counters("VM counters after VirtualAlloc(MEM_COMMIT)", &pvi);
1163 ok( pvi.VirtualSize == prev_size,
1164 "Expected to equal to %lu, got %lu\n", prev_size, pvi.VirtualSize);
1165 VirtualFree( ptr, 0, MEM_RELEASE);
1168 static void test_query_process_io(void)
1170 NTSTATUS status;
1171 ULONG ReturnLength;
1172 IO_COUNTERS pii;
1174 /* NT4 doesn't support this information class, so check for it */
1175 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii), &ReturnLength);
1176 if (status == STATUS_NOT_SUPPORTED)
1178 win_skip("ProcessIoCounters information class is not supported\n");
1179 return;
1182 status = pNtQueryInformationProcess(NULL, ProcessIoCounters, NULL, sizeof(pii), NULL);
1183 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
1184 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
1186 status = pNtQueryInformationProcess(NULL, ProcessIoCounters, &pii, sizeof(pii), NULL);
1187 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
1189 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, 24, &ReturnLength);
1190 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1192 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii), &ReturnLength);
1193 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1194 ok( sizeof(pii) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
1196 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessIoCounters, &pii, sizeof(pii) * 2, &ReturnLength);
1197 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1198 ok( sizeof(pii) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
1200 /* Check if we have some return values */
1201 trace("OtherOperationCount : 0x%s\n", wine_dbgstr_longlong(pii.OtherOperationCount));
1202 todo_wine
1204 ok( pii.OtherOperationCount > 0, "Expected an OtherOperationCount > 0\n");
1208 static void test_query_process_times(void)
1210 NTSTATUS status;
1211 ULONG ReturnLength;
1212 HANDLE process;
1213 SYSTEMTIME UTC, Local;
1214 KERNEL_USER_TIMES spti;
1216 status = pNtQueryInformationProcess(NULL, ProcessTimes, NULL, sizeof(spti), NULL);
1217 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
1218 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
1220 status = pNtQueryInformationProcess(NULL, ProcessTimes, &spti, sizeof(spti), NULL);
1221 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
1223 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessTimes, &spti, 24, &ReturnLength);
1224 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1226 process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, one_before_last_pid);
1227 if (!process)
1229 trace("Could not open process with ID : %d, error : %u. Going to use current one.\n", one_before_last_pid, GetLastError());
1230 process = GetCurrentProcess();
1231 trace("ProcessTimes for current process\n");
1233 else
1234 trace("ProcessTimes for process with ID : %d\n", one_before_last_pid);
1236 status = pNtQueryInformationProcess( process, ProcessTimes, &spti, sizeof(spti), &ReturnLength);
1237 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1238 ok( sizeof(spti) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
1239 CloseHandle(process);
1241 FileTimeToSystemTime((const FILETIME *)&spti.CreateTime, &UTC);
1242 SystemTimeToTzSpecificLocalTime(NULL, &UTC, &Local);
1243 trace("CreateTime : %02d/%02d/%04d %02d:%02d:%02d\n", Local.wMonth, Local.wDay, Local.wYear,
1244 Local.wHour, Local.wMinute, Local.wSecond);
1246 FileTimeToSystemTime((const FILETIME *)&spti.ExitTime, &UTC);
1247 SystemTimeToTzSpecificLocalTime(NULL, &UTC, &Local);
1248 trace("ExitTime : %02d/%02d/%04d %02d:%02d:%02d\n", Local.wMonth, Local.wDay, Local.wYear,
1249 Local.wHour, Local.wMinute, Local.wSecond);
1251 FileTimeToSystemTime((const FILETIME *)&spti.KernelTime, &Local);
1252 trace("KernelTime : %02d:%02d:%02d.%03d\n", Local.wHour, Local.wMinute, Local.wSecond, Local.wMilliseconds);
1254 FileTimeToSystemTime((const FILETIME *)&spti.UserTime, &Local);
1255 trace("UserTime : %02d:%02d:%02d.%03d\n", Local.wHour, Local.wMinute, Local.wSecond, Local.wMilliseconds);
1257 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessTimes, &spti, sizeof(spti) * 2, &ReturnLength);
1258 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1259 ok( sizeof(spti) == ReturnLength ||
1260 ReturnLength == 0 /* vista */ ||
1261 broken(is_wow64), /* returns garbage on wow64 */
1262 "Inconsistent length %d\n", ReturnLength);
1265 static void test_query_process_debug_port(int argc, char **argv)
1267 DWORD_PTR debug_port = 0xdeadbeef;
1268 char cmdline[MAX_PATH];
1269 PROCESS_INFORMATION pi;
1270 STARTUPINFOA si = { 0 };
1271 NTSTATUS status;
1272 BOOL ret;
1274 sprintf(cmdline, "%s %s %s", argv[0], argv[1], "debuggee");
1276 si.cb = sizeof(si);
1277 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
1278 ok(ret, "CreateProcess failed, last error %#x.\n", GetLastError());
1279 if (!ret) return;
1281 status = pNtQueryInformationProcess(NULL, ProcessDebugPort,
1282 NULL, 0, NULL);
1283 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1285 status = pNtQueryInformationProcess(NULL, ProcessDebugPort,
1286 NULL, sizeof(debug_port), NULL);
1287 ok(status == STATUS_INVALID_HANDLE || status == STATUS_ACCESS_VIOLATION,
1288 "Expected STATUS_INVALID_HANDLE, got %#x.\n", status);
1290 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
1291 NULL, sizeof(debug_port), NULL);
1292 ok(status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %#x.\n", status);
1294 status = pNtQueryInformationProcess(NULL, ProcessDebugPort,
1295 &debug_port, sizeof(debug_port), NULL);
1296 ok(status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %#x.\n", status);
1298 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
1299 &debug_port, sizeof(debug_port) - 1, NULL);
1300 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1302 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
1303 &debug_port, sizeof(debug_port) + 1, NULL);
1304 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1306 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort,
1307 &debug_port, sizeof(debug_port), NULL);
1308 ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
1309 ok(debug_port == 0, "Expected port 0, got %#lx.\n", debug_port);
1311 status = pNtQueryInformationProcess(pi.hProcess, ProcessDebugPort,
1312 &debug_port, sizeof(debug_port), NULL);
1313 ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
1314 ok(debug_port == ~(DWORD_PTR)0, "Expected port %#lx, got %#lx.\n", ~(DWORD_PTR)0, debug_port);
1316 for (;;)
1318 DEBUG_EVENT ev;
1320 ret = WaitForDebugEvent(&ev, INFINITE);
1321 ok(ret, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
1322 if (!ret) break;
1324 if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) break;
1326 ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
1327 ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
1328 if (!ret) break;
1331 ret = CloseHandle(pi.hThread);
1332 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
1333 ret = CloseHandle(pi.hProcess);
1334 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
1337 static void test_query_process_priority(void)
1339 PROCESS_PRIORITY_CLASS priority[2];
1340 ULONG ReturnLength;
1341 DWORD orig_priority;
1342 NTSTATUS status;
1343 BOOL ret;
1345 status = pNtQueryInformationProcess(NULL, ProcessPriorityClass, NULL, sizeof(priority[0]), NULL);
1346 ok(status == STATUS_ACCESS_VIOLATION || broken(status == STATUS_INVALID_HANDLE) /* w2k3 */,
1347 "Expected STATUS_ACCESS_VIOLATION, got %08x\n", status);
1349 status = pNtQueryInformationProcess(NULL, ProcessPriorityClass, &priority, sizeof(priority[0]), NULL);
1350 ok(status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
1352 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessPriorityClass, &priority, 1, &ReturnLength);
1353 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1355 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessPriorityClass, &priority, sizeof(priority), &ReturnLength);
1356 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1358 orig_priority = GetPriorityClass(GetCurrentProcess());
1359 ret = SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
1360 ok(ret, "Failed to set priority class: %u\n", GetLastError());
1362 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessPriorityClass, &priority, sizeof(priority[0]), &ReturnLength);
1363 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1364 ok(priority[0].PriorityClass == PROCESS_PRIOCLASS_BELOW_NORMAL,
1365 "Expected PROCESS_PRIOCLASS_BELOW_NORMAL, got %u\n", priority[0].PriorityClass);
1367 ret = SetPriorityClass(GetCurrentProcess(), orig_priority);
1368 ok(ret, "Failed to reset priority class: %u\n", GetLastError());
1371 static void test_query_process_handlecount(void)
1373 NTSTATUS status;
1374 ULONG ReturnLength;
1375 DWORD handlecount;
1376 BYTE buffer[2 * sizeof(DWORD)];
1377 HANDLE process;
1379 status = pNtQueryInformationProcess(NULL, ProcessHandleCount, NULL, sizeof(handlecount), NULL);
1380 ok( status == STATUS_ACCESS_VIOLATION || status == STATUS_INVALID_HANDLE,
1381 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_HANDLE(W2K3), got %08x\n", status);
1383 status = pNtQueryInformationProcess(NULL, ProcessHandleCount, &handlecount, sizeof(handlecount), NULL);
1384 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
1386 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessHandleCount, &handlecount, 2, &ReturnLength);
1387 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1389 process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, one_before_last_pid);
1390 if (!process)
1392 trace("Could not open process with ID : %d, error : %u. Going to use current one.\n", one_before_last_pid, GetLastError());
1393 process = GetCurrentProcess();
1394 trace("ProcessHandleCount for current process\n");
1396 else
1397 trace("ProcessHandleCount for process with ID : %d\n", one_before_last_pid);
1399 status = pNtQueryInformationProcess( process, ProcessHandleCount, &handlecount, sizeof(handlecount), &ReturnLength);
1400 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1401 ok( sizeof(handlecount) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
1402 CloseHandle(process);
1404 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessHandleCount, buffer, sizeof(buffer), &ReturnLength);
1405 ok( status == STATUS_INFO_LENGTH_MISMATCH || status == STATUS_SUCCESS,
1406 "Expected STATUS_INFO_LENGTH_MISMATCH or STATUS_SUCCESS, got %08x\n", status);
1407 ok( sizeof(handlecount) == ReturnLength, "Inconsistent length %d\n", ReturnLength);
1409 /* Check if we have some return values */
1410 trace("HandleCount : %d\n", handlecount);
1411 todo_wine
1413 ok( handlecount > 0, "Expected some handles, got 0\n");
1417 static void test_query_process_image_file_name(void)
1419 NTSTATUS status;
1420 ULONG ReturnLength;
1421 UNICODE_STRING image_file_name;
1422 void *buffer;
1423 char *file_nameA;
1424 INT len;
1426 status = pNtQueryInformationProcess(NULL, ProcessImageFileName, &image_file_name, sizeof(image_file_name), NULL);
1427 if (status == STATUS_INVALID_INFO_CLASS)
1429 win_skip("ProcessImageFileName is not supported\n");
1430 return;
1432 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
1434 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessImageFileName, &image_file_name, 2, &ReturnLength);
1435 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1437 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessImageFileName, &image_file_name, sizeof(image_file_name), &ReturnLength);
1438 ok( status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %08x\n", status);
1440 buffer = HeapAlloc(GetProcessHeap(), 0, ReturnLength);
1441 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessImageFileName, buffer, ReturnLength, &ReturnLength);
1442 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1443 memcpy(&image_file_name, buffer, sizeof(image_file_name));
1444 len = WideCharToMultiByte(CP_ACP, 0, image_file_name.Buffer, image_file_name.Length/sizeof(WCHAR), NULL, 0, NULL, NULL);
1445 file_nameA = HeapAlloc(GetProcessHeap(), 0, len + 1);
1446 WideCharToMultiByte(CP_ACP, 0, image_file_name.Buffer, image_file_name.Length/sizeof(WCHAR), file_nameA, len, NULL, NULL);
1447 file_nameA[len] = '\0';
1448 HeapFree(GetProcessHeap(), 0, buffer);
1449 trace("process image file name: %s\n", file_nameA);
1450 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);
1451 HeapFree(GetProcessHeap(), 0, file_nameA);
1454 static void test_query_process_debug_object_handle(int argc, char **argv)
1456 char cmdline[MAX_PATH];
1457 STARTUPINFOA si = {0};
1458 PROCESS_INFORMATION pi;
1459 BOOL ret;
1460 HANDLE debug_object;
1461 NTSTATUS status;
1463 sprintf(cmdline, "%s %s %s", argv[0], argv[1], "debuggee");
1465 si.cb = sizeof(si);
1466 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL,
1467 NULL, &si, &pi);
1468 ok(ret, "CreateProcess failed with last error %u\n", GetLastError());
1469 if (!ret) return;
1471 status = pNtQueryInformationProcess(NULL, ProcessDebugObjectHandle, NULL,
1472 0, NULL);
1473 if (status == STATUS_INVALID_INFO_CLASS || status == STATUS_NOT_IMPLEMENTED)
1475 win_skip("ProcessDebugObjectHandle is not supported\n");
1476 return;
1478 ok(status == STATUS_INFO_LENGTH_MISMATCH,
1479 "Expected NtQueryInformationProcess to return STATUS_INFO_LENGTH_MISMATCH, got 0x%08x\n",
1480 status);
1482 status = pNtQueryInformationProcess(NULL, ProcessDebugObjectHandle, NULL,
1483 sizeof(debug_object), NULL);
1484 ok(status == STATUS_INVALID_HANDLE ||
1485 status == STATUS_ACCESS_VIOLATION, /* XP */
1486 "Expected NtQueryInformationProcess to return STATUS_INVALID_HANDLE, got 0x%08x\n", status);
1488 status = pNtQueryInformationProcess(GetCurrentProcess(),
1489 ProcessDebugObjectHandle, NULL, sizeof(debug_object), NULL);
1490 ok(status == STATUS_ACCESS_VIOLATION,
1491 "Expected NtQueryInformationProcess to return STATUS_ACCESS_VIOLATION, got 0x%08x\n", status);
1493 status = pNtQueryInformationProcess(NULL, ProcessDebugObjectHandle,
1494 &debug_object, sizeof(debug_object), NULL);
1495 ok(status == STATUS_INVALID_HANDLE,
1496 "Expected NtQueryInformationProcess to return STATUS_ACCESS_VIOLATION, got 0x%08x\n", status);
1498 status = pNtQueryInformationProcess(GetCurrentProcess(),
1499 ProcessDebugObjectHandle, &debug_object,
1500 sizeof(debug_object) - 1, NULL);
1501 ok(status == STATUS_INFO_LENGTH_MISMATCH,
1502 "Expected NtQueryInformationProcess to return STATUS_INFO_LENGTH_MISMATCH, got 0x%08x\n", status);
1504 status = pNtQueryInformationProcess(GetCurrentProcess(),
1505 ProcessDebugObjectHandle, &debug_object,
1506 sizeof(debug_object) + 1, NULL);
1507 ok(status == STATUS_INFO_LENGTH_MISMATCH,
1508 "Expected NtQueryInformationProcess to return STATUS_INFO_LENGTH_MISMATCH, got 0x%08x\n", status);
1510 debug_object = (HANDLE)0xdeadbeef;
1511 status = pNtQueryInformationProcess(GetCurrentProcess(),
1512 ProcessDebugObjectHandle, &debug_object,
1513 sizeof(debug_object), NULL);
1514 ok(status == STATUS_PORT_NOT_SET,
1515 "Expected NtQueryInformationProcess to return STATUS_PORT_NOT_SET, got 0x%08x\n", status);
1516 ok(debug_object == NULL ||
1517 broken(debug_object == (HANDLE)0xdeadbeef), /* Wow64 */
1518 "Expected debug object handle to be NULL, got %p\n", debug_object);
1520 debug_object = (HANDLE)0xdeadbeef;
1521 status = pNtQueryInformationProcess(pi.hProcess, ProcessDebugObjectHandle,
1522 &debug_object, sizeof(debug_object), NULL);
1523 todo_wine
1524 ok(status == STATUS_SUCCESS,
1525 "Expected NtQueryInformationProcess to return STATUS_SUCCESS, got 0x%08x\n", status);
1526 todo_wine
1527 ok(debug_object != NULL,
1528 "Expected debug object handle to be non-NULL, got %p\n", debug_object);
1530 for (;;)
1532 DEBUG_EVENT ev;
1534 ret = WaitForDebugEvent(&ev, INFINITE);
1535 ok(ret, "WaitForDebugEvent failed with last error %u\n", GetLastError());
1536 if (!ret) break;
1538 if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) break;
1540 ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
1541 ok(ret, "ContinueDebugEvent failed with last error %u\n", GetLastError());
1542 if (!ret) break;
1545 ret = CloseHandle(pi.hThread);
1546 ok(ret, "CloseHandle failed with last error %u\n", GetLastError());
1547 ret = CloseHandle(pi.hProcess);
1548 ok(ret, "CloseHandle failed with last error %u\n", GetLastError());
1551 static void test_query_process_debug_flags(int argc, char **argv)
1553 static const DWORD test_flags[] = { DEBUG_PROCESS,
1554 DEBUG_ONLY_THIS_PROCESS,
1555 DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS,
1556 CREATE_SUSPENDED };
1557 DWORD debug_flags = 0xdeadbeef;
1558 char cmdline[MAX_PATH];
1559 PROCESS_INFORMATION pi;
1560 STARTUPINFOA si = { 0 };
1561 NTSTATUS status;
1562 DEBUG_EVENT ev;
1563 DWORD result;
1564 BOOL ret;
1565 int i, j;
1567 /* test invalid arguments */
1568 status = pNtQueryInformationProcess(NULL, ProcessDebugFlags, NULL, 0, NULL);
1569 ok(status == STATUS_INFO_LENGTH_MISMATCH || broken(status == STATUS_INVALID_INFO_CLASS) /* WOW64 */,
1570 "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1572 status = pNtQueryInformationProcess(NULL, ProcessDebugFlags, NULL, sizeof(debug_flags), NULL);
1573 ok(status == STATUS_INVALID_HANDLE || status == STATUS_ACCESS_VIOLATION || broken(status == STATUS_INVALID_INFO_CLASS) /* WOW64 */,
1574 "Expected STATUS_INVALID_HANDLE, got %#x.\n", status);
1576 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugFlags,
1577 NULL, sizeof(debug_flags), NULL);
1578 ok(status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %#x.\n", status);
1580 status = pNtQueryInformationProcess(NULL, ProcessDebugFlags,
1581 &debug_flags, sizeof(debug_flags), NULL);
1582 ok(status == STATUS_INVALID_HANDLE || broken(status == STATUS_INVALID_INFO_CLASS) /* WOW64 */,
1583 "Expected STATUS_INVALID_HANDLE, got %#x.\n", status);
1585 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugFlags,
1586 &debug_flags, sizeof(debug_flags) - 1, NULL);
1587 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1589 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugFlags,
1590 &debug_flags, sizeof(debug_flags) + 1, NULL);
1591 ok(status == STATUS_INFO_LENGTH_MISMATCH, "Expected STATUS_INFO_LENGTH_MISMATCH, got %#x.\n", status);
1593 /* test ProcessDebugFlags of current process */
1594 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugFlags,
1595 &debug_flags, sizeof(debug_flags), NULL);
1596 ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
1597 ok(debug_flags == TRUE, "Expected flag TRUE, got %x.\n", debug_flags);
1599 for (i = 0; i < sizeof(test_flags)/sizeof(test_flags[0]); i++)
1601 DWORD expected_flags = !(test_flags[i] & DEBUG_ONLY_THIS_PROCESS);
1602 sprintf(cmdline, "%s %s %s", argv[0], argv[1], "debuggee");
1604 si.cb = sizeof(si);
1605 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, test_flags[i], NULL, NULL, &si, &pi);
1606 ok(ret, "CreateProcess failed, last error %#x.\n", GetLastError());
1608 if (!(test_flags[i] & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS)))
1610 /* test ProcessDebugFlags before attaching with debugger */
1611 status = pNtQueryInformationProcess(pi.hProcess, ProcessDebugFlags,
1612 &debug_flags, sizeof(debug_flags), NULL);
1613 ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
1614 ok(debug_flags == TRUE, "Expected flag TRUE, got %x.\n", debug_flags);
1616 ret = DebugActiveProcess(pi.dwProcessId);
1617 ok(ret, "DebugActiveProcess failed, last error %#x.\n", GetLastError());
1618 expected_flags = FALSE;
1621 /* test ProcessDebugFlags after attaching with debugger */
1622 status = pNtQueryInformationProcess(pi.hProcess, ProcessDebugFlags,
1623 &debug_flags, sizeof(debug_flags), NULL);
1624 ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
1625 ok(debug_flags == expected_flags, "Expected flag %x, got %x.\n", expected_flags, debug_flags);
1627 if (!(test_flags[i] & CREATE_SUSPENDED))
1629 /* Continue a couple of times to make sure the process is fully initialized,
1630 * otherwise Windows XP deadlocks in the following DebugActiveProcess(). */
1631 for (;;)
1633 ret = WaitForDebugEvent(&ev, 1000);
1634 ok(ret, "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
1635 if (!ret) break;
1637 if (ev.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT) break;
1639 ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
1640 ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
1641 if (!ret) break;
1644 result = SuspendThread(pi.hThread);
1645 ok(result == 0, "Expected 0, got %u.\n", result);
1648 ret = DebugActiveProcessStop(pi.dwProcessId);
1649 ok(ret, "DebugActiveProcessStop failed, last error %#x.\n", GetLastError());
1651 /* test ProcessDebugFlags after detaching debugger */
1652 status = pNtQueryInformationProcess(pi.hProcess, ProcessDebugFlags,
1653 &debug_flags, sizeof(debug_flags), NULL);
1654 ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
1655 ok(debug_flags == expected_flags, "Expected flag %x, got %x.\n", expected_flags, debug_flags);
1657 ret = DebugActiveProcess(pi.dwProcessId);
1658 ok(ret, "DebugActiveProcess failed, last error %#x.\n", GetLastError());
1660 /* test ProcessDebugFlags after re-attaching debugger */
1661 status = pNtQueryInformationProcess(pi.hProcess, ProcessDebugFlags,
1662 &debug_flags, sizeof(debug_flags), NULL);
1663 ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
1664 ok(debug_flags == FALSE, "Expected flag FALSE, got %x.\n", debug_flags);
1666 result = ResumeThread(pi.hThread);
1667 todo_wine ok(result == 2, "Expected 2, got %u.\n", result);
1669 /* Wait until the process is terminated. On Windows XP the process randomly
1670 * gets stuck in a non-continuable exception, so stop after 100 iterations.
1671 * On Windows 2003, the debugged process disappears (or stops?) without
1672 * any EXIT_PROCESS_DEBUG_EVENT after a couple of events. */
1673 for (j = 0; j < 100; j++)
1675 ret = WaitForDebugEvent(&ev, 1000);
1676 ok(ret || broken(GetLastError() == ERROR_SEM_TIMEOUT),
1677 "WaitForDebugEvent failed, last error %#x.\n", GetLastError());
1678 if (!ret) break;
1680 if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) break;
1682 ret = ContinueDebugEvent(ev.dwProcessId, ev.dwThreadId, DBG_CONTINUE);
1683 ok(ret, "ContinueDebugEvent failed, last error %#x.\n", GetLastError());
1684 if (!ret) break;
1686 ok(j < 100 || broken(j >= 100) /* Win XP */, "Expected less than 100 debug events.\n");
1688 /* test ProcessDebugFlags after process has terminated */
1689 status = pNtQueryInformationProcess(pi.hProcess, ProcessDebugFlags,
1690 &debug_flags, sizeof(debug_flags), NULL);
1691 ok(!status, "NtQueryInformationProcess failed, status %#x.\n", status);
1692 ok(debug_flags == FALSE, "Expected flag FALSE, got %x.\n", debug_flags);
1694 ret = CloseHandle(pi.hThread);
1695 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
1696 ret = CloseHandle(pi.hProcess);
1697 ok(ret, "CloseHandle failed, last error %#x.\n", GetLastError());
1701 static void test_readvirtualmemory(void)
1703 HANDLE process;
1704 NTSTATUS status;
1705 SIZE_T readcount;
1706 static const char teststring[] = "test string";
1707 char buffer[12];
1709 process = OpenProcess(PROCESS_VM_READ, FALSE, GetCurrentProcessId());
1710 ok(process != 0, "Expected to be able to open own process for reading memory\n");
1712 /* normal operation */
1713 status = pNtReadVirtualMemory(process, teststring, buffer, 12, &readcount);
1714 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1715 ok( readcount == 12, "Expected to read 12 bytes, got %ld\n",readcount);
1716 ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
1718 /* no number of bytes */
1719 memset(buffer, 0, 12);
1720 status = pNtReadVirtualMemory(process, teststring, buffer, 12, NULL);
1721 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1722 ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
1724 /* illegal remote address */
1725 todo_wine{
1726 status = pNtReadVirtualMemory(process, (void *) 0x1234, buffer, 12, &readcount);
1727 ok( status == STATUS_PARTIAL_COPY || broken(status == STATUS_ACCESS_VIOLATION), "Expected STATUS_PARTIAL_COPY, got %08x\n", status);
1728 if (status == STATUS_PARTIAL_COPY)
1729 ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
1732 /* 0 handle */
1733 status = pNtReadVirtualMemory(0, teststring, buffer, 12, &readcount);
1734 ok( status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %08x\n", status);
1735 ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
1737 /* pseudo handle for current process*/
1738 memset(buffer, 0, 12);
1739 status = pNtReadVirtualMemory((HANDLE)-1, teststring, buffer, 12, &readcount);
1740 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1741 ok( readcount == 12, "Expected to read 12 bytes, got %ld\n",readcount);
1742 ok( strcmp(teststring, buffer) == 0, "Expected read memory to be the same as original memory\n");
1744 /* illegal local address */
1745 status = pNtReadVirtualMemory(process, teststring, (void *)0x1234, 12, &readcount);
1746 ok( status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got %08x\n", status);
1747 ok( readcount == 0, "Expected to read 0 bytes, got %ld\n",readcount);
1749 CloseHandle(process);
1752 static void test_mapprotection(void)
1754 HANDLE h;
1755 void* addr;
1756 MEMORY_BASIC_INFORMATION info;
1757 ULONG oldflags, flagsize, flags = MEM_EXECUTE_OPTION_ENABLE;
1758 LARGE_INTEGER size, offset;
1759 NTSTATUS status;
1760 SIZE_T retlen, count;
1761 void (*f)(void);
1763 if (!pNtClose) {
1764 skip("No NtClose ... Win98\n");
1765 return;
1767 /* Switch to being a noexec unaware process */
1768 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &oldflags, sizeof (oldflags), &flagsize);
1769 if (status == STATUS_INVALID_PARAMETER) {
1770 skip("Invalid Parameter on ProcessExecuteFlags query?\n");
1771 return;
1773 ok( (status == STATUS_SUCCESS) || (status == STATUS_INVALID_INFO_CLASS), "Expected STATUS_SUCCESS, got %08x\n", status);
1774 status = pNtSetInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &flags, sizeof(flags) );
1775 ok( (status == STATUS_SUCCESS) || (status == STATUS_INVALID_INFO_CLASS), "Expected STATUS_SUCCESS, got %08x\n", status);
1777 size.u.LowPart = 0x2000;
1778 size.u.HighPart = 0;
1779 status = pNtCreateSection ( &h,
1780 STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE,
1781 NULL,
1782 &size,
1783 PAGE_READWRITE,
1784 SEC_COMMIT | SEC_NOCACHE,
1787 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1789 offset.u.LowPart = 0;
1790 offset.u.HighPart = 0;
1791 count = 0x2000;
1792 addr = NULL;
1793 status = pNtMapViewOfSection ( h, GetCurrentProcess(), &addr, 0, 0, &offset, &count, ViewShare, 0, PAGE_READWRITE);
1794 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1796 #if defined(__x86_64__) || defined(__i386__)
1797 *(unsigned char*)addr = 0xc3; /* lret ... in both i386 and x86_64 */
1798 #elif defined(__arm__)
1799 *(unsigned long*)addr = 0xe12fff1e; /* bx lr */
1800 #elif defined(__aarch64__)
1801 *(unsigned long*)addr = 0xd65f03c0; /* ret */
1802 #else
1803 ok(0, "Add a return opcode for your architecture or expect a crash in this test\n");
1804 #endif
1805 trace("trying to execute code in the readwrite only mapped anon file...\n");
1806 f = addr;f();
1807 trace("...done.\n");
1809 status = pNtQueryVirtualMemory( GetCurrentProcess(), addr, MemoryBasicInformation, &info, sizeof(info), &retlen );
1810 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1811 ok( retlen == sizeof(info), "Expected STATUS_SUCCESS, got %08x\n", status);
1812 ok((info.Protect & ~PAGE_NOCACHE) == PAGE_READWRITE, "addr.Protect is not PAGE_READWRITE, but 0x%x\n", info.Protect);
1814 status = pNtUnmapViewOfSection( GetCurrentProcess(), (char *)addr + 0x1050 );
1815 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1816 pNtClose (h);
1818 /* Switch back */
1819 pNtSetInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &oldflags, sizeof(oldflags) );
1822 static void test_queryvirtualmemory(void)
1824 NTSTATUS status;
1825 SIZE_T readcount;
1826 static const char teststring[] = "test string";
1827 static char datatestbuf[42] = "abc";
1828 static char rwtestbuf[42];
1829 MEMORY_BASIC_INFORMATION mbi;
1830 char stackbuf[42];
1831 HMODULE module;
1833 module = GetModuleHandleA( "ntdll.dll" );
1834 trace("Check flags of the PE header of NTDLL.DLL at %p\n", module);
1835 status = pNtQueryVirtualMemory(NtCurrentProcess(), module, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1836 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1837 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1838 ok (mbi.AllocationBase == module, "mbi.AllocationBase is 0x%p, expected 0x%p\n", mbi.AllocationBase, module);
1839 ok (mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_EXECUTE_WRITECOPY);
1840 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%x\n", mbi.State, MEM_COMMIT);
1841 ok (mbi.Protect == PAGE_READONLY, "mbi.Protect is 0x%x, expected 0x%x\n", mbi.Protect, PAGE_READONLY);
1842 ok (mbi.Type == MEM_IMAGE, "mbi.Type is 0x%x, expected 0x%x\n", mbi.Type, MEM_IMAGE);
1844 trace("Check flags of a function entry in NTDLL.DLL at %p\n", pNtQueryVirtualMemory);
1845 module = GetModuleHandleA( "ntdll.dll" );
1846 status = pNtQueryVirtualMemory(NtCurrentProcess(), pNtQueryVirtualMemory, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1847 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1848 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1849 ok (mbi.AllocationBase == module, "mbi.AllocationBase is 0x%p, expected 0x%p\n", mbi.AllocationBase, module);
1850 ok (mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_EXECUTE_WRITECOPY);
1851 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%x\n", mbi.State, MEM_COMMIT);
1852 ok (mbi.Protect == PAGE_EXECUTE_READ, "mbi.Protect is 0x%x, expected 0x%x\n", mbi.Protect, PAGE_EXECUTE_READ);
1854 trace("Check flags of heap at %p\n", GetProcessHeap());
1855 status = pNtQueryVirtualMemory(NtCurrentProcess(), GetProcessHeap(), MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1856 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1857 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1858 ok (mbi.AllocationProtect == PAGE_READWRITE || mbi.AllocationProtect == PAGE_EXECUTE_READWRITE,
1859 "mbi.AllocationProtect is 0x%x\n", mbi.AllocationProtect);
1860 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%x\n", mbi.State, MEM_COMMIT);
1861 ok (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE,
1862 "mbi.Protect is 0x%x\n", mbi.Protect);
1864 trace("Check flags of stack at %p\n", stackbuf);
1865 status = pNtQueryVirtualMemory(NtCurrentProcess(), stackbuf, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1866 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1867 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1868 ok (mbi.AllocationProtect == PAGE_READWRITE, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_READWRITE);
1869 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%x\n", mbi.State, MEM_COMMIT);
1870 ok (mbi.Protect == PAGE_READWRITE, "mbi.Protect is 0x%x, expected 0x%x\n", mbi.Protect, PAGE_READWRITE);
1872 trace("Check flags of read-only data at %p\n", teststring);
1873 module = GetModuleHandleA( NULL );
1874 status = pNtQueryVirtualMemory(NtCurrentProcess(), teststring, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1875 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1876 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1877 ok (mbi.AllocationBase == module, "mbi.AllocationBase is 0x%p, expected 0x%p\n", mbi.AllocationBase, module);
1878 ok (mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_EXECUTE_WRITECOPY);
1879 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%X\n", mbi.State, MEM_COMMIT);
1880 if (mbi.Protect != PAGE_READONLY)
1881 todo_wine ok( mbi.Protect == PAGE_READONLY, "mbi.Protect is 0x%x, expected 0x%X\n", mbi.Protect, PAGE_READONLY);
1883 trace("Check flags of read-write data at %p\n", datatestbuf);
1884 status = pNtQueryVirtualMemory(NtCurrentProcess(), datatestbuf, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1885 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1886 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1887 ok (mbi.AllocationBase == module, "mbi.AllocationBase is 0x%p, expected 0x%p\n", mbi.AllocationBase, module);
1888 ok (mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_EXECUTE_WRITECOPY);
1889 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%X\n", mbi.State, MEM_COMMIT);
1890 ok (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_WRITECOPY,
1891 "mbi.Protect is 0x%x\n", mbi.Protect);
1893 trace("Check flags of read-write uninitialized data (.bss) at %p\n", rwtestbuf);
1894 status = pNtQueryVirtualMemory(NtCurrentProcess(), rwtestbuf, MemoryBasicInformation, &mbi, sizeof(MEMORY_BASIC_INFORMATION), &readcount);
1895 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1896 ok( readcount == sizeof(MEMORY_BASIC_INFORMATION), "Expected to read %d bytes, got %ld\n",(int)sizeof(MEMORY_BASIC_INFORMATION),readcount);
1897 if (mbi.AllocationBase == module)
1899 ok (mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "mbi.AllocationProtect is 0x%x, expected 0x%x\n", mbi.AllocationProtect, PAGE_EXECUTE_WRITECOPY);
1900 ok (mbi.State == MEM_COMMIT, "mbi.State is 0x%x, expected 0x%X\n", mbi.State, MEM_COMMIT);
1901 ok (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_WRITECOPY,
1902 "mbi.Protect is 0x%x\n", mbi.Protect);
1904 else skip( "bss is outside of module\n" ); /* this can happen on Mac OS */
1906 /* check error code when addr is higher than working set limit */
1907 status = pNtQueryVirtualMemory(NtCurrentProcess(), (void *)~0, MemoryBasicInformation, &mbi, sizeof(mbi), &readcount);
1908 ok(status == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1911 static void test_affinity(void)
1913 NTSTATUS status;
1914 PROCESS_BASIC_INFORMATION pbi;
1915 DWORD_PTR proc_affinity, thread_affinity;
1916 THREAD_BASIC_INFORMATION tbi;
1917 SYSTEM_INFO si;
1919 GetSystemInfo(&si);
1920 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL );
1921 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1922 proc_affinity = pbi.AffinityMask;
1923 ok( proc_affinity == (1 << si.dwNumberOfProcessors) - 1, "Unexpected process affinity\n" );
1924 proc_affinity = 1 << si.dwNumberOfProcessors;
1925 status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
1926 ok( status == STATUS_INVALID_PARAMETER,
1927 "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1929 proc_affinity = 0;
1930 status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
1931 ok( status == STATUS_INVALID_PARAMETER,
1932 "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1934 status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1935 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1936 ok( tbi.AffinityMask == (1 << si.dwNumberOfProcessors) - 1, "Unexpected thread affinity\n" );
1937 thread_affinity = 1 << si.dwNumberOfProcessors;
1938 status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1939 ok( status == STATUS_INVALID_PARAMETER,
1940 "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1941 thread_affinity = 0;
1942 status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1943 ok( status == STATUS_INVALID_PARAMETER,
1944 "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1946 thread_affinity = 1;
1947 status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1948 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1949 status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1950 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1951 ok( tbi.AffinityMask == 1, "Unexpected thread affinity\n" );
1953 /* NOTE: Pre-Vista does not recognize the "all processors" flag (all bits set) */
1954 thread_affinity = ~(DWORD_PTR)0;
1955 status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1956 ok( broken(status == STATUS_INVALID_PARAMETER) || status == STATUS_SUCCESS,
1957 "Expected STATUS_SUCCESS, got %08x\n", status);
1959 if (si.dwNumberOfProcessors <= 1)
1961 skip("only one processor, skipping affinity testing\n");
1962 return;
1965 /* Test thread affinity mask resulting from "all processors" flag */
1966 if (status == STATUS_SUCCESS)
1968 status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1969 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1970 ok( broken(tbi.AffinityMask == 1) || tbi.AffinityMask == (1 << si.dwNumberOfProcessors) - 1,
1971 "Unexpected thread affinity\n" );
1973 else
1974 skip("Cannot test thread affinity mask for 'all processors' flag\n");
1976 proc_affinity = 2;
1977 status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
1978 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1979 status = pNtQueryInformationProcess( GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL );
1980 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1981 proc_affinity = pbi.AffinityMask;
1982 ok( proc_affinity == 2, "Unexpected process affinity\n" );
1983 /* Setting the process affinity changes the thread affinity to match */
1984 status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1985 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1986 ok( tbi.AffinityMask == 2, "Unexpected thread affinity\n" );
1987 /* The thread affinity is restricted to the process affinity */
1988 thread_affinity = 1;
1989 status = pNtSetInformationThread( GetCurrentThread(), ThreadAffinityMask, &thread_affinity, sizeof(thread_affinity) );
1990 ok( status == STATUS_INVALID_PARAMETER,
1991 "Expected STATUS_INVALID_PARAMETER, got %08x\n", status);
1993 proc_affinity = (1 << si.dwNumberOfProcessors) - 1;
1994 status = pNtSetInformationProcess( GetCurrentProcess(), ProcessAffinityMask, &proc_affinity, sizeof(proc_affinity) );
1995 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1996 /* Resetting the process affinity also resets the thread affinity */
1997 status = pNtQueryInformationThread( GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL );
1998 ok( status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
1999 ok( tbi.AffinityMask == (1 << si.dwNumberOfProcessors) - 1,
2000 "Unexpected thread affinity\n" );
2003 static void test_NtGetCurrentProcessorNumber(void)
2005 NTSTATUS status;
2006 SYSTEM_INFO si;
2007 PROCESS_BASIC_INFORMATION pbi;
2008 THREAD_BASIC_INFORMATION tbi;
2009 DWORD_PTR old_process_mask;
2010 DWORD_PTR old_thread_mask;
2011 DWORD_PTR new_mask;
2012 ULONG current_cpu;
2013 ULONG i;
2015 if (!pNtGetCurrentProcessorNumber) {
2016 win_skip("NtGetCurrentProcessorNumber not available\n");
2017 return;
2020 GetSystemInfo(&si);
2021 current_cpu = pNtGetCurrentProcessorNumber();
2022 trace("dwNumberOfProcessors: %d, current processor: %d\n", si.dwNumberOfProcessors, current_cpu);
2024 status = pNtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
2025 old_process_mask = pbi.AffinityMask;
2026 ok(status == STATUS_SUCCESS, "got 0x%x (expected STATUS_SUCCESS)\n", status);
2028 status = pNtQueryInformationThread(GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL);
2029 old_thread_mask = tbi.AffinityMask;
2030 ok(status == STATUS_SUCCESS, "got 0x%x (expected STATUS_SUCCESS)\n", status);
2032 /* allow the test to run on all processors */
2033 new_mask = (1 << si.dwNumberOfProcessors) - 1;
2034 status = pNtSetInformationProcess(GetCurrentProcess(), ProcessAffinityMask, &new_mask, sizeof(new_mask));
2035 ok(status == STATUS_SUCCESS, "got 0x%x (expected STATUS_SUCCESS)\n", status);
2037 for (i = 0; i < si.dwNumberOfProcessors; i++)
2039 new_mask = 1 << i;
2040 status = pNtSetInformationThread(GetCurrentThread(), ThreadAffinityMask, &new_mask, sizeof(new_mask));
2041 ok(status == STATUS_SUCCESS, "%d: got 0x%x (expected STATUS_SUCCESS)\n", i, status);
2043 status = pNtQueryInformationThread(GetCurrentThread(), ThreadBasicInformation, &tbi, sizeof(tbi), NULL);
2044 ok(status == STATUS_SUCCESS, "%d: got 0x%x (expected STATUS_SUCCESS)\n", i, status);
2046 current_cpu = pNtGetCurrentProcessorNumber();
2047 ok((current_cpu == i), "%d (new_mask 0x%lx): running on processor %d (AffinityMask: 0x%lx)\n",
2048 i, new_mask, current_cpu, tbi.AffinityMask);
2051 /* restore old values */
2052 status = pNtSetInformationProcess(GetCurrentProcess(), ProcessAffinityMask, &old_process_mask, sizeof(old_process_mask));
2053 ok(status == STATUS_SUCCESS, "got 0x%x (expected STATUS_SUCCESS)\n", status);
2055 status = pNtSetInformationThread(GetCurrentThread(), ThreadAffinityMask, &old_thread_mask, sizeof(old_thread_mask));
2056 ok(status == STATUS_SUCCESS, "got 0x%x (expected STATUS_SUCCESS)\n", status);
2059 static DWORD WINAPI start_address_thread(void *arg)
2061 PRTL_THREAD_START_ROUTINE entry;
2062 NTSTATUS status;
2063 DWORD ret;
2065 entry = NULL;
2066 ret = 0xdeadbeef;
2067 status = pNtQueryInformationThread(GetCurrentThread(), ThreadQuerySetWin32StartAddress,
2068 &entry, sizeof(entry), &ret);
2069 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
2070 ok(ret == sizeof(entry), "NtQueryInformationThread returned %u bytes\n", ret);
2071 ok(entry == (void *)start_address_thread, "expected %p, got %p\n", start_address_thread, entry);
2072 return 0;
2075 static void test_thread_start_address(void)
2077 PRTL_THREAD_START_ROUTINE entry, expected_entry;
2078 IMAGE_NT_HEADERS *nt;
2079 NTSTATUS status;
2080 HANDLE thread;
2081 void *module;
2082 DWORD ret;
2084 module = GetModuleHandleA(0);
2085 ok(module != NULL, "expected non-NULL address for module\n");
2086 nt = RtlImageNtHeader(module);
2087 ok(nt != NULL, "expected non-NULL address for NT header\n");
2089 entry = NULL;
2090 ret = 0xdeadbeef;
2091 status = pNtQueryInformationThread(GetCurrentThread(), ThreadQuerySetWin32StartAddress,
2092 &entry, sizeof(entry), &ret);
2093 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
2094 ok(ret == sizeof(entry), "NtQueryInformationThread returned %u bytes\n", ret);
2095 expected_entry = (void *)((char *)module + nt->OptionalHeader.AddressOfEntryPoint);
2096 ok(entry == expected_entry, "expected %p, got %p\n", expected_entry, entry);
2098 entry = (void *)0xdeadbeef;
2099 status = pNtSetInformationThread(GetCurrentThread(), ThreadQuerySetWin32StartAddress,
2100 &entry, sizeof(entry));
2101 ok(status == STATUS_SUCCESS || status == STATUS_INVALID_PARAMETER, /* >= Vista */
2102 "expected STATUS_SUCCESS or STATUS_INVALID_PARAMETER, got %08x\n", status);
2104 if (status == STATUS_SUCCESS)
2106 entry = NULL;
2107 ret = 0xdeadbeef;
2108 status = pNtQueryInformationThread(GetCurrentThread(), ThreadQuerySetWin32StartAddress,
2109 &entry, sizeof(entry), &ret);
2110 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08x\n", status);
2111 ok(ret == sizeof(entry), "NtQueryInformationThread returned %u bytes\n", ret);
2112 ok(entry == (void *)0xdeadbeef, "expected 0xdeadbeef, got %p\n", entry);
2115 thread = CreateThread(NULL, 0, start_address_thread, NULL, 0, NULL);
2116 ok(thread != INVALID_HANDLE_VALUE, "CreateThread failed with %d\n", GetLastError());
2117 ret = WaitForSingleObject(thread, 1000);
2118 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2119 CloseHandle(thread);
2122 static void test_query_data_alignment(void)
2124 ULONG ReturnLength;
2125 NTSTATUS status;
2126 DWORD value;
2128 value = 0xdeadbeef;
2129 status = pNtQuerySystemInformation(SystemRecommendedSharedDataAlignment, &value, sizeof(value), &ReturnLength);
2130 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %08x\n", status);
2131 ok(sizeof(value) == ReturnLength, "Inconsistent length %u\n", ReturnLength);
2132 ok(value == 64, "Expected 64, got %u\n", value);
2135 START_TEST(info)
2137 char **argv;
2138 int argc;
2140 if(!InitFunctionPtrs())
2141 return;
2143 argc = winetest_get_mainargs(&argv);
2144 if (argc >= 3) return; /* Child */
2146 /* NtQuerySystemInformation */
2148 /* 0x0 SystemBasicInformation */
2149 trace("Starting test_query_basic()\n");
2150 test_query_basic();
2152 /* 0x1 SystemCpuInformation */
2153 trace("Starting test_query_cpu()\n");
2154 test_query_cpu();
2156 /* 0x2 SystemPerformanceInformation */
2157 trace("Starting test_query_performance()\n");
2158 test_query_performance();
2160 /* 0x3 SystemTimeOfDayInformation */
2161 trace("Starting test_query_timeofday()\n");
2162 test_query_timeofday();
2164 /* 0x5 SystemProcessInformation */
2165 trace("Starting test_query_process()\n");
2166 test_query_process();
2168 /* 0x8 SystemProcessorPerformanceInformation */
2169 trace("Starting test_query_procperf()\n");
2170 test_query_procperf();
2172 /* 0xb SystemModuleInformation */
2173 trace("Starting test_query_module()\n");
2174 test_query_module();
2176 /* 0x10 SystemHandleInformation */
2177 trace("Starting test_query_handle()\n");
2178 test_query_handle();
2180 /* 0x15 SystemCacheInformation */
2181 trace("Starting test_query_cache()\n");
2182 test_query_cache();
2184 /* 0x17 SystemInterruptInformation */
2185 trace("Starting test_query_interrupt()\n");
2186 test_query_interrupt();
2188 /* 0x23 SystemKernelDebuggerInformation */
2189 trace("Starting test_query_kerndebug()\n");
2190 test_query_kerndebug();
2192 /* 0x25 SystemRegistryQuotaInformation */
2193 trace("Starting test_query_regquota()\n");
2194 test_query_regquota();
2196 /* 0x49 SystemLogicalProcessorInformation */
2197 trace("Starting test_query_logicalproc()\n");
2198 test_query_logicalproc();
2199 test_query_logicalprocex();
2201 /* NtPowerInformation */
2203 /* 0xb ProcessorInformation */
2204 trace("Starting test_query_processor_power_info()\n");
2205 test_query_processor_power_info();
2207 /* NtQueryInformationProcess */
2209 /* 0x0 ProcessBasicInformation */
2210 trace("Starting test_query_process_basic()\n");
2211 test_query_process_basic();
2213 /* 0x2 ProcessIoCounters */
2214 trace("Starting test_query_process_io()\n");
2215 test_query_process_io();
2217 /* 0x3 ProcessVmCounters */
2218 trace("Starting test_query_process_vm()\n");
2219 test_query_process_vm();
2221 /* 0x4 ProcessTimes */
2222 trace("Starting test_query_process_times()\n");
2223 test_query_process_times();
2225 /* 0x7 ProcessDebugPort */
2226 trace("Starting test_process_debug_port()\n");
2227 test_query_process_debug_port(argc, argv);
2229 /* 0x12 ProcessPriorityClass */
2230 trace("Starting test_query_process_priority()\n");
2231 test_query_process_priority();
2233 /* 0x14 ProcessHandleCount */
2234 trace("Starting test_query_process_handlecount()\n");
2235 test_query_process_handlecount();
2237 /* 0x1A ProcessWow64Information */
2238 trace("Starting test_query_process_wow64()\n");
2239 test_query_process_wow64();
2241 /* 0x1B ProcessImageFileName */
2242 trace("Starting test_query_process_image_file_name()\n");
2243 test_query_process_image_file_name();
2245 /* 0x1E ProcessDebugObjectHandle */
2246 trace("Starting test_query_process_debug_object_handle()\n");
2247 test_query_process_debug_object_handle(argc, argv);
2249 /* 0x1F ProcessDebugFlags */
2250 trace("Starting test_process_debug_flags()\n");
2251 test_query_process_debug_flags(argc, argv);
2253 /* belongs to its own file */
2254 trace("Starting test_readvirtualmemory()\n");
2255 test_readvirtualmemory();
2257 trace("Starting test_queryvirtualmemory()\n");
2258 test_queryvirtualmemory();
2260 trace("Starting test_mapprotection()\n");
2261 test_mapprotection();
2263 trace("Starting test_affinity()\n");
2264 test_affinity();
2266 trace("Starting test_NtGetCurrentProcessorNumber()\n");
2267 test_NtGetCurrentProcessorNumber();
2269 trace("Starting test_thread_start_address()\n");
2270 test_thread_start_address();
2272 trace("Starting test_query_data_alignment()\n");
2273 test_query_data_alignment();