ntdll: Restore stack guard and prevent stack from shrinking.
[wine] / dlls / ntdll / process.c
1 /*
2  * NT basis DLL
3  *
4  * This file contains the Nt* API functions of NTDLL.DLL.
5  * In the original ntdll.dll they all seem to just call int 0x2e (down to the NTOSKRNL)
6  *
7  * Copyright 1996-1998 Marcus Meissner
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "wine/debug.h"
33 #include "windef.h"
34 #include "winternl.h"
35 #include "ntdll_misc.h"
36 #include "wine/server.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
39
40 static ULONG execute_flags = MEM_EXECUTE_OPTION_DISABLE;
41
42 /*
43  *      Process object
44  */
45
46 /******************************************************************************
47  *  NtTerminateProcess                  [NTDLL.@]
48  *
49  *  Native applications must kill themselves when done
50  */
51 NTSTATUS WINAPI NtTerminateProcess( HANDLE handle, LONG exit_code )
52 {
53     NTSTATUS ret;
54     BOOL self;
55     SERVER_START_REQ( terminate_process )
56     {
57         req->handle    = wine_server_obj_handle( handle );
58         req->exit_code = exit_code;
59         ret = wine_server_call( req );
60         self = !ret && reply->self;
61     }
62     SERVER_END_REQ;
63     if (self) exit( exit_code );
64     return ret;
65 }
66
67 /******************************************************************************
68  *  RtlGetCurrentPeb  [NTDLL.@]
69  *
70  */
71 PEB * WINAPI RtlGetCurrentPeb(void)
72 {
73     return NtCurrentTeb()->Peb;
74 }
75
76 /***********************************************************************
77  *           __wine_make_process_system   (NTDLL.@)
78  *
79  * Mark the current process as a system process.
80  * Returns the event that is signaled when all non-system processes have exited.
81  */
82 HANDLE CDECL __wine_make_process_system(void)
83 {
84     HANDLE ret = 0;
85     SERVER_START_REQ( make_process_system )
86     {
87         if (!wine_server_call( req )) ret = wine_server_ptr_handle( reply->event );
88     }
89     SERVER_END_REQ;
90     return ret;
91 }
92
93 static UINT process_error_mode;
94
95 #define UNIMPLEMENTED_INFO_CLASS(c) \
96     case c: \
97         FIXME("(process=%p) Unimplemented information class: " #c "\n", ProcessHandle); \
98         ret = STATUS_INVALID_INFO_CLASS; \
99         break
100
101 /******************************************************************************
102 *  NtQueryInformationProcess            [NTDLL.@]
103 *  ZwQueryInformationProcess            [NTDLL.@]
104 *
105 */
106 NTSTATUS WINAPI NtQueryInformationProcess(
107         IN HANDLE ProcessHandle,
108         IN PROCESSINFOCLASS ProcessInformationClass,
109         OUT PVOID ProcessInformation,
110         IN ULONG ProcessInformationLength,
111         OUT PULONG ReturnLength)
112 {
113     NTSTATUS ret = STATUS_SUCCESS;
114     ULONG len = 0;
115
116     TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
117           ProcessHandle,ProcessInformationClass,
118           ProcessInformation,ProcessInformationLength,
119           ReturnLength);
120
121     switch (ProcessInformationClass) 
122     {
123     UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits);
124     UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority);
125     UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority);
126     UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort);
127     UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken);
128     UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation);
129     UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize);
130     UNIMPLEMENTED_INFO_CLASS(ProcessIoPortHandlers);
131     UNIMPLEMENTED_INFO_CLASS(ProcessPooledUsageAndLimits);
132     UNIMPLEMENTED_INFO_CLASS(ProcessWorkingSetWatch);
133     UNIMPLEMENTED_INFO_CLASS(ProcessUserModeIOPL);
134     UNIMPLEMENTED_INFO_CLASS(ProcessEnableAlignmentFaultFixup);
135     UNIMPLEMENTED_INFO_CLASS(ProcessPriorityClass);
136     UNIMPLEMENTED_INFO_CLASS(ProcessWx86Information);
137     UNIMPLEMENTED_INFO_CLASS(ProcessAffinityMask);
138     UNIMPLEMENTED_INFO_CLASS(ProcessPriorityBoost);
139     UNIMPLEMENTED_INFO_CLASS(ProcessDeviceMap);
140     UNIMPLEMENTED_INFO_CLASS(ProcessSessionInformation);
141     UNIMPLEMENTED_INFO_CLASS(ProcessForegroundInformation);
142     UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled);
143     UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination);
144     UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing);
145
146     case ProcessBasicInformation:
147         {
148             PROCESS_BASIC_INFORMATION pbi;
149             const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
150
151             if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
152             {
153                 if (!ProcessInformation)
154                     ret = STATUS_ACCESS_VIOLATION;
155                 else if (!ProcessHandle)
156                     ret = STATUS_INVALID_HANDLE;
157                 else
158                 {
159                     SERVER_START_REQ(get_process_info)
160                     {
161                         req->handle = wine_server_obj_handle( ProcessHandle );
162                         if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
163                         {
164                             pbi.ExitStatus = reply->exit_code;
165                             pbi.PebBaseAddress = wine_server_get_ptr( reply->peb );
166                             pbi.AffinityMask = reply->affinity & affinity_mask;
167                             pbi.BasePriority = reply->priority;
168                             pbi.UniqueProcessId = reply->pid;
169                             pbi.InheritedFromUniqueProcessId = reply->ppid;
170                         }
171                     }
172                     SERVER_END_REQ;
173
174                     memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
175
176                     len = sizeof(PROCESS_BASIC_INFORMATION);
177                 }
178
179                 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
180                     ret = STATUS_INFO_LENGTH_MISMATCH;
181             }
182             else
183             {
184                 len = sizeof(PROCESS_BASIC_INFORMATION);
185                 ret = STATUS_INFO_LENGTH_MISMATCH;
186             }
187         }
188         break;
189     case ProcessIoCounters:
190         {
191             IO_COUNTERS pii;
192
193             if (ProcessInformationLength >= sizeof(IO_COUNTERS))
194             {
195                 if (!ProcessInformation)
196                     ret = STATUS_ACCESS_VIOLATION;
197                 else if (!ProcessHandle)
198                     ret = STATUS_INVALID_HANDLE;
199                 else
200                 {
201                     /* FIXME : real data */
202                     memset(&pii, 0 , sizeof(IO_COUNTERS));
203
204                     memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
205
206                     len = sizeof(IO_COUNTERS);
207                 }
208
209                 if (ProcessInformationLength > sizeof(IO_COUNTERS))
210                     ret = STATUS_INFO_LENGTH_MISMATCH;
211             }
212             else
213             {
214                 len = sizeof(IO_COUNTERS);
215                 ret = STATUS_INFO_LENGTH_MISMATCH;
216             }
217         }
218         break;
219     case ProcessVmCounters:
220         {
221             VM_COUNTERS pvmi;
222
223             /* older Windows versions don't have the PrivatePageCount field */
224             if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
225             {
226                 if (!ProcessInformation)
227                     ret = STATUS_ACCESS_VIOLATION;
228                 else if (!ProcessHandle)
229                     ret = STATUS_INVALID_HANDLE;
230                 else
231                 {
232                     /* FIXME : real data */
233                     memset(&pvmi, 0 , sizeof(VM_COUNTERS));
234
235                     len = ProcessInformationLength;
236                     if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
237
238                     memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
239                 }
240
241                 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
242                     ProcessInformationLength != sizeof(VM_COUNTERS))
243                     ret = STATUS_INFO_LENGTH_MISMATCH;
244             }
245             else
246             {
247                 len = sizeof(pvmi);
248                 ret = STATUS_INFO_LENGTH_MISMATCH;
249             }
250         }
251         break;
252     case ProcessTimes:
253         {
254             KERNEL_USER_TIMES pti;
255
256             if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
257             {
258                 if (!ProcessInformation)
259                     ret = STATUS_ACCESS_VIOLATION;
260                 else if (!ProcessHandle)
261                     ret = STATUS_INVALID_HANDLE;
262                 else
263                 {
264                     /* FIXME : User- and KernelTime have to be implemented */
265                     memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
266
267                     SERVER_START_REQ(get_process_info)
268                     {
269                       req->handle = wine_server_obj_handle( ProcessHandle );
270                       if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
271                       {
272                           pti.CreateTime.QuadPart = reply->start_time;
273                           pti.ExitTime.QuadPart = reply->end_time;
274                       }
275                     }
276                     SERVER_END_REQ;
277
278                     memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
279                     len = sizeof(KERNEL_USER_TIMES);
280                 }
281
282                 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
283                     ret = STATUS_INFO_LENGTH_MISMATCH;
284             }
285             else
286             {
287                 len = sizeof(KERNEL_USER_TIMES);
288                 ret = STATUS_INFO_LENGTH_MISMATCH;
289             }
290         }
291         break;
292     case ProcessDebugPort:
293         len = sizeof(DWORD_PTR);
294         if (ProcessInformationLength == len)
295         {
296             if (!ProcessInformation)
297                 ret = STATUS_ACCESS_VIOLATION;
298             else if (!ProcessHandle)
299                 ret = STATUS_INVALID_HANDLE;
300             else
301             {
302                 SERVER_START_REQ(get_process_info)
303                 {
304                     req->handle = wine_server_obj_handle( ProcessHandle );
305                     if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
306                     {
307                         *(DWORD_PTR *)ProcessInformation = reply->debugger_present ? ~(DWORD_PTR)0 : 0;
308                     }
309                 }
310                 SERVER_END_REQ;
311             }
312         }
313         else
314             ret = STATUS_INFO_LENGTH_MISMATCH;
315         break;
316     case ProcessDebugFlags:
317         len = sizeof(DWORD);
318         if (ProcessInformationLength == len)
319         {
320             if (!ProcessInformation)
321                 ret = STATUS_ACCESS_VIOLATION;
322             else if (!ProcessHandle)
323                 ret = STATUS_INVALID_HANDLE;
324             else
325             {
326                 SERVER_START_REQ(get_process_info)
327                 {
328                     req->handle = wine_server_obj_handle( ProcessHandle );
329                     if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
330                     {
331                         *(DWORD *)ProcessInformation = !reply->debugger_present;
332                     }
333                 }
334                 SERVER_END_REQ;
335             }
336         }
337         else
338             ret = STATUS_INFO_LENGTH_MISMATCH;
339         break;
340     case ProcessDefaultHardErrorMode:
341         len = sizeof(process_error_mode);
342         if (ProcessInformationLength == len)
343             memcpy(ProcessInformation, &process_error_mode, len);
344         else
345             ret = STATUS_INFO_LENGTH_MISMATCH;
346         break;
347     case ProcessDebugObjectHandle:
348         /* "These are not the debuggers you are looking for." *
349          * set it to 0 aka "no debugger" to satisfy copy protections */
350         len = sizeof(HANDLE);
351         if (ProcessInformationLength == len)
352         {
353             if (!ProcessInformation)
354                 ret = STATUS_ACCESS_VIOLATION;
355             else if (!ProcessHandle)
356                 ret = STATUS_INVALID_HANDLE;
357             else
358             {
359                 memset(ProcessInformation, 0, ProcessInformationLength);
360                 ret = STATUS_PORT_NOT_SET;
361             }
362         }
363         else
364             ret = STATUS_INFO_LENGTH_MISMATCH;
365         break;
366     case ProcessHandleCount:
367         if (ProcessInformationLength >= 4)
368         {
369             if (!ProcessInformation)
370                 ret = STATUS_ACCESS_VIOLATION;
371             else if (!ProcessHandle)
372                 ret = STATUS_INVALID_HANDLE;
373             else
374             {
375                 memset(ProcessInformation, 0, 4);
376                 len = 4;
377             }
378
379             if (ProcessInformationLength > 4)
380                 ret = STATUS_INFO_LENGTH_MISMATCH;
381         }
382         else
383         {
384             len = 4;
385             ret = STATUS_INFO_LENGTH_MISMATCH;
386         }
387         break;
388     case ProcessWow64Information:
389         len = sizeof(DWORD);
390         if (ProcessInformationLength == len)
391         {
392             DWORD val = 0;
393
394             if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
395             else if (server_cpus & (1 << CPU_x86_64))
396             {
397                 SERVER_START_REQ( get_process_info )
398                 {
399                     req->handle = wine_server_obj_handle( ProcessHandle );
400                     if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
401                 }
402                 SERVER_END_REQ;
403             }
404             *(DWORD *)ProcessInformation = val;
405         }
406         else ret = STATUS_INFO_LENGTH_MISMATCH;
407         break;
408     case ProcessImageFileName:
409         /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
410          * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
411         SERVER_START_REQ(get_dll_info)
412         {
413             UNICODE_STRING *image_file_name_str = ProcessInformation;
414
415             req->handle = wine_server_obj_handle( ProcessHandle );
416             req->base_address = 0; /* main module */
417             wine_server_set_reply( req, image_file_name_str ? image_file_name_str + 1 : NULL,
418                                    ProcessInformationLength > sizeof(UNICODE_STRING) ? ProcessInformationLength - sizeof(UNICODE_STRING) : 0 );
419             ret = wine_server_call( req );
420             if (ret == STATUS_BUFFER_TOO_SMALL) ret = STATUS_INFO_LENGTH_MISMATCH;
421
422             len = sizeof(UNICODE_STRING) + reply->filename_len;
423             if (ret == STATUS_SUCCESS)
424             {
425                 image_file_name_str->MaximumLength = image_file_name_str->Length = reply->filename_len;
426                 image_file_name_str->Buffer = (PWSTR)(image_file_name_str + 1);
427             }
428         }
429         SERVER_END_REQ;
430         break;
431     case ProcessExecuteFlags:
432         len = sizeof(ULONG);
433         if (ProcessInformationLength == len)
434             *(ULONG *)ProcessInformation = execute_flags;
435         else
436             ret = STATUS_INFO_LENGTH_MISMATCH;
437         break;
438     default:
439         FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
440               ProcessHandle,ProcessInformationClass,
441               ProcessInformation,ProcessInformationLength,
442               ReturnLength);
443         ret = STATUS_INVALID_INFO_CLASS;
444         break;
445     }
446
447     if (ReturnLength) *ReturnLength = len;
448     
449     return ret;
450 }
451
452 /******************************************************************************
453  * NtSetInformationProcess [NTDLL.@]
454  * ZwSetInformationProcess [NTDLL.@]
455  */
456 NTSTATUS WINAPI NtSetInformationProcess(
457         IN HANDLE ProcessHandle,
458         IN PROCESSINFOCLASS ProcessInformationClass,
459         IN PVOID ProcessInformation,
460         IN ULONG ProcessInformationLength)
461 {
462     NTSTATUS ret = STATUS_SUCCESS;
463
464     switch (ProcessInformationClass)
465     {
466     case ProcessDefaultHardErrorMode:
467         if (ProcessInformationLength != sizeof(UINT)) return STATUS_INVALID_PARAMETER;
468         process_error_mode = *(UINT *)ProcessInformation;
469         break;
470     case ProcessAffinityMask:
471         if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
472         if (*(PDWORD_PTR)ProcessInformation & ~(((DWORD_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1))
473             return STATUS_INVALID_PARAMETER;
474         if (!*(PDWORD_PTR)ProcessInformation)
475             return STATUS_INVALID_PARAMETER;
476         SERVER_START_REQ( set_process_info )
477         {
478             req->handle   = wine_server_obj_handle( ProcessHandle );
479             req->affinity = *(PDWORD_PTR)ProcessInformation;
480             req->mask     = SET_PROCESS_INFO_AFFINITY;
481             ret = wine_server_call( req );
482         }
483         SERVER_END_REQ;
484         break;
485     case ProcessPriorityClass:
486         if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
487             return STATUS_INVALID_PARAMETER;
488         else
489         {
490             PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
491
492             SERVER_START_REQ( set_process_info )
493             {
494                 req->handle   = wine_server_obj_handle( ProcessHandle );
495                 /* FIXME Foreground isn't used */
496                 req->priority = ppc->PriorityClass;
497                 req->mask     = SET_PROCESS_INFO_PRIORITY;
498                 ret = wine_server_call( req );
499             }
500             SERVER_END_REQ;
501         }
502         break;
503
504     case ProcessExecuteFlags:
505         if (ProcessInformationLength != sizeof(ULONG))
506             return STATUS_INVALID_PARAMETER;
507         else if (execute_flags & MEM_EXECUTE_OPTION_PERMANENT)
508             return STATUS_ACCESS_DENIED;
509         else
510         {
511             BOOL enable;
512             switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
513             {
514             case MEM_EXECUTE_OPTION_ENABLE:
515                 enable = TRUE;
516                 break;
517             case MEM_EXECUTE_OPTION_DISABLE:
518                 enable = FALSE;
519                 break;
520             default:
521                 return STATUS_INVALID_PARAMETER;
522             }
523             execute_flags = *(ULONG *)ProcessInformation;
524             VIRTUAL_SetForceExec( enable );
525         }
526         break;
527
528     default:
529         FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
530               ProcessHandle,ProcessInformationClass,ProcessInformation,
531               ProcessInformationLength);
532         ret = STATUS_NOT_IMPLEMENTED;
533         break;
534     }
535     return ret;
536 }
537
538 /******************************************************************************
539  * NtFlushInstructionCache [NTDLL.@]
540  * ZwFlushInstructionCache [NTDLL.@]
541  */
542 NTSTATUS WINAPI NtFlushInstructionCache(
543         IN HANDLE ProcessHandle,
544         IN LPCVOID BaseAddress,
545         IN SIZE_T Size)
546 {
547 #ifdef __i386__
548     TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
549 #else
550     FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
551 #endif
552     return STATUS_SUCCESS;
553 }
554
555 /******************************************************************
556  *              NtOpenProcess [NTDLL.@]
557  *              ZwOpenProcess [NTDLL.@]
558  */
559 NTSTATUS  WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
560                                const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
561 {
562     NTSTATUS    status;
563
564     SERVER_START_REQ( open_process )
565     {
566         req->pid        = HandleToULong(cid->UniqueProcess);
567         req->access     = access;
568         req->attributes = attr ? attr->Attributes : 0;
569         status = wine_server_call( req );
570         if (!status) *handle = wine_server_ptr_handle( reply->handle );
571     }
572     SERVER_END_REQ;
573     return status;
574 }