urlmon: Added test of handlong its protocol.
[wine] / server / mach.c
1 /*
2  * Server-side debugger support using Mach primitives
3  *
4  * Copyright (C) 1999, 2006 Alexandre Julliard
5  * Copyright (C) 2006 Ken Thomases for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "winternl.h"
35
36 #include "file.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "request.h"
40 #include "wine/library.h"
41
42 #ifdef USE_MACH
43
44 #include <mach/mach.h>
45 #include <mach/mach_error.h>
46 #include <mach/thread_act.h>
47 #include <servers/bootstrap.h>
48
49 extern int __pthread_kill(mach_port_t, int);
50
51 static mach_port_t server_mach_port;
52
53 void sigchld_callback(void)
54 {
55     assert(0);  /* should never be called on MacOS */
56 }
57
58 static void mach_set_error(kern_return_t mach_error)
59 {
60     switch (mach_error)
61     {
62         case KERN_SUCCESS:              break;
63         case KERN_INVALID_ARGUMENT:     set_error(STATUS_INVALID_PARAMETER); break;
64         case KERN_NO_SPACE:             set_error(STATUS_NO_MEMORY); break;
65         case KERN_PROTECTION_FAILURE:   set_error(STATUS_ACCESS_DENIED); break;
66         case KERN_INVALID_ADDRESS:      set_error(STATUS_ACCESS_VIOLATION); break;
67         default:                        set_error(STATUS_UNSUCCESSFUL); break;
68     }
69 }
70
71 static mach_port_t get_process_port( struct process *process )
72 {
73     return process->trace_data;
74 }
75
76 /* initialize the process control mechanism */
77 void init_tracing_mechanism(void)
78 {
79     mach_port_t bp;
80
81     if (task_get_bootstrap_port(mach_task_self(), &bp) != KERN_SUCCESS)
82         fatal_error("Can't find bootstrap port\n");
83     if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server_mach_port) != KERN_SUCCESS)
84         fatal_error("Can't allocate port\n");
85     if  (mach_port_insert_right( mach_task_self(),
86                                  server_mach_port,
87                                  server_mach_port,
88                                  MACH_MSG_TYPE_MAKE_SEND ) != KERN_SUCCESS)
89             fatal_error("Error inserting rights\n");
90     if (bootstrap_register(bp, (char*)wine_get_server_dir(), server_mach_port) != KERN_SUCCESS)
91         fatal_error("Can't check in server_mach_port\n");
92     mach_port_deallocate(mach_task_self(), bp);
93 }
94
95 /* initialize the per-process tracing mechanism */
96 void init_process_tracing( struct process *process )
97 {
98     int pid, ret;
99     struct
100     {
101         mach_msg_header_t           header;
102         mach_msg_body_t             body;
103         mach_msg_port_descriptor_t  task_port;
104         mach_msg_trailer_t          trailer; /* only present on receive */
105     } msg;
106
107     for (;;)
108     {
109         ret = mach_msg( &msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof(msg),
110                         server_mach_port, 0, 0 );
111         if (ret)
112         {
113             if (ret != MACH_RCV_TIMED_OUT && debug_level)
114                 fprintf( stderr, "warning: mach port receive failed with %x\n", ret );
115             return;
116         }
117
118         /* if anything in the message is invalid, ignore it */
119         if (msg.header.msgh_size != offsetof(typeof(msg), trailer)) continue;
120         if (msg.body.msgh_descriptor_count != 1) continue;
121         if (msg.task_port.type != MACH_MSG_PORT_DESCRIPTOR) continue;
122         if (msg.task_port.disposition != MACH_MSG_TYPE_PORT_SEND) continue;
123         if (msg.task_port.name == MACH_PORT_NULL) continue;
124         if (msg.task_port.name == MACH_PORT_DEAD) continue;
125
126         if (!pid_for_task( msg.task_port.name, &pid ))
127         {
128             struct thread *thread = get_thread_from_pid( pid );
129
130             if (thread && !thread->process->trace_data)
131                 thread->process->trace_data = msg.task_port.name;
132             else
133                 mach_port_deallocate( mach_task_self(), msg.task_port.name );
134         }
135     }
136 }
137
138 /* terminate the per-process tracing mechanism */
139 void finish_process_tracing( struct process *process )
140 {
141     if (process->trace_data)
142     {
143         mach_port_deallocate( mach_task_self(), process->trace_data );
144         process->trace_data = 0;
145     }
146 }
147
148 /* retrieve the thread x86 registers */
149 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
150 {
151     struct x86_debug_state32 state;
152     mach_msg_type_number_t count = sizeof(state) / sizeof(int);
153     mach_msg_type_name_t type;
154     mach_port_t port, process_port = get_process_port( thread->process );
155
156     /* all other regs are handled on the client side */
157     assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
158
159     if (thread->unix_pid == -1 || !process_port ||
160         mach_port_extract_right( process_port, thread->unix_tid,
161                                  MACH_MSG_TYPE_COPY_SEND, &port, &type ))
162     {
163         set_error( STATUS_ACCESS_DENIED );
164         return;
165     }
166
167     if (!thread_get_state( port, x86_DEBUG_STATE32, (thread_state_t)&state, &count ))
168     {
169         context->Dr0 = state.dr0;
170         context->Dr1 = state.dr1;
171         context->Dr2 = state.dr2;
172         context->Dr3 = state.dr3;
173         context->Dr6 = state.dr6;
174         context->Dr7 = state.dr7;
175         context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
176     }
177     mach_port_deallocate( mach_task_self(), port );
178 }
179
180 /* set the thread x86 registers */
181 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
182 {
183     struct x86_debug_state32 state;
184     mach_msg_type_number_t count = sizeof(state) / sizeof(int);
185     mach_msg_type_name_t type;
186     mach_port_t port, process_port = get_process_port( thread->process );
187
188     /* all other regs are handled on the client side */
189     assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
190
191     if (thread->unix_pid == -1 || !process_port ||
192         mach_port_extract_right( process_port, thread->unix_tid,
193                                  MACH_MSG_TYPE_COPY_SEND, &port, &type ))
194     {
195         set_error( STATUS_ACCESS_DENIED );
196         return;
197     }
198
199     state.dr0 = context->Dr0;
200     state.dr1 = context->Dr1;
201     state.dr2 = context->Dr2;
202     state.dr3 = context->Dr3;
203     state.dr4 = 0;
204     state.dr5 = 0;
205     state.dr6 = context->Dr6;
206     state.dr7 = context->Dr7;
207     if (!thread_set_state( port, x86_DEBUG_STATE32, (thread_state_t)&state, count ))
208     {
209         if (thread->context)  /* update the cached values */
210         {
211             thread->context->Dr0 = context->Dr0;
212             thread->context->Dr1 = context->Dr1;
213             thread->context->Dr2 = context->Dr2;
214             thread->context->Dr3 = context->Dr3;
215             thread->context->Dr6 = context->Dr6;
216             thread->context->Dr7 = context->Dr7;
217         }
218     }
219     mach_port_deallocate( mach_task_self(), port );
220 }
221
222 int send_thread_signal( struct thread *thread, int sig )
223 {
224     int ret = -1;
225     mach_port_t process_port = get_process_port( thread->process );
226
227     if (thread->unix_pid != -1 && process_port)
228     {
229         mach_msg_type_name_t type;
230         mach_port_t port;
231
232         if (!mach_port_extract_right( process_port, thread->unix_tid,
233                                       MACH_MSG_TYPE_COPY_SEND, &port, &type ))
234         {
235             ret = __pthread_kill( port, sig );
236             mach_port_deallocate( mach_task_self(), port );
237         }
238         else errno = ESRCH;
239
240         if (ret == -1 && errno == ESRCH) /* thread got killed */
241         {
242             thread->unix_pid = -1;
243             thread->unix_tid = -1;
244         }
245     }
246     if (debug_level && ret != -1)
247         fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
248     return (ret != -1);
249 }
250
251 /* read data from a process memory space */
252 int read_process_memory( struct process *process, const void *ptr, data_size_t size, char *dest )
253 {
254     kern_return_t ret;
255     mach_msg_type_number_t bytes_read;
256     vm_offset_t offset, data;
257     vm_address_t aligned_address;
258     vm_size_t aligned_size;
259     unsigned int page_size = get_page_size();
260     mach_port_t process_port = get_process_port( process );
261
262     if (!process_port)
263     {
264         set_error( STATUS_ACCESS_DENIED );
265         return 0;
266     }
267
268     if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
269     {
270         mach_set_error( ret );
271         return 0;
272     }
273
274     offset = (unsigned long)ptr % page_size;
275     aligned_address = (vm_address_t)((char *)ptr - offset);
276     aligned_size = (size + offset + page_size - 1) / page_size * page_size;
277
278     ret = vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
279     if (ret != KERN_SUCCESS) mach_set_error( ret );
280     else
281     {
282         memcpy( dest, (char *)data + offset, size );
283         vm_deallocate( mach_task_self(), data, bytes_read );
284     }
285     task_resume( process_port );
286     return (ret == KERN_SUCCESS);
287 }
288
289 /* write data to a process memory space */
290 int write_process_memory( struct process *process, void *ptr, data_size_t size, const char *src )
291 {
292     kern_return_t ret;
293     vm_address_t aligned_address, region_address;
294     vm_size_t aligned_size, region_size;
295     mach_msg_type_number_t info_size, bytes_read;
296     vm_offset_t offset, task_mem = 0;
297     struct vm_region_basic_info info;
298     mach_port_t dummy;
299     unsigned int page_size = get_page_size();
300     mach_port_t process_port = get_process_port( process );
301
302     if (!process_port)
303     {
304         set_error( STATUS_ACCESS_DENIED );
305         return 0;
306     }
307
308     offset = (unsigned long)ptr % page_size;
309     aligned_address = (vm_address_t)((char *)ptr - offset);
310     aligned_size = (size + offset + page_size - 1) / page_size * page_size;
311
312     if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
313     {
314         mach_set_error( ret );
315         return 0;
316     }
317
318     ret = vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
319     if (ret != KERN_SUCCESS)
320     {
321         mach_set_error( ret );
322         goto failed;
323     }
324     region_address = aligned_address;
325     info_size = sizeof(info);
326     ret = vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO,
327                      (vm_region_info_t)&info, &info_size, &dummy );
328     if (ret != KERN_SUCCESS)
329     {
330         mach_set_error( ret );
331         goto failed;
332     }
333     if (region_address > aligned_address ||
334         region_address + region_size < aligned_address + aligned_size)
335     {
336         /* FIXME: should support multiple regions */
337         set_error( ERROR_ACCESS_DENIED );
338         goto failed;
339     }
340     ret = vm_protect( process_port, aligned_address, aligned_size, 0, VM_PROT_READ | VM_PROT_WRITE );
341     if (ret != KERN_SUCCESS)
342     {
343         mach_set_error( ret );
344         goto failed;
345     }
346
347     /* FIXME: there's an optimization that can be made: check first and last */
348     /* pages for writability; read first and last pages; write interior */
349     /* pages to task without ever reading&modifying them; if that succeeds, */
350     /* modify first and last pages and write them. */
351
352     memcpy( (char*)task_mem + offset, src, size );
353
354     ret = vm_write( process_port, aligned_address, task_mem, bytes_read );
355     if (ret != KERN_SUCCESS) mach_set_error( ret );
356     else
357     {
358         vm_deallocate( mach_task_self(), task_mem, bytes_read );
359         /* restore protection */
360         vm_protect( process_port, aligned_address, aligned_size, 0, info.protection );
361         task_resume( process_port );
362         return 1;
363     }
364
365 failed:
366     if (task_mem) vm_deallocate( mach_task_self(), task_mem, bytes_read );
367     task_resume( process_port );
368     return 0;
369 }
370
371 /* retrieve an LDT selector entry */
372 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
373                          unsigned int *limit, unsigned char *flags )
374 {
375     const unsigned int total_size = (2 * sizeof(int) + 1) * 8192;
376     struct process *process = thread->process;
377     unsigned int page_size = get_page_size();
378     vm_offset_t data;
379     kern_return_t ret;
380     mach_msg_type_number_t bytes_read;
381     mach_port_t process_port = get_process_port( thread->process );
382
383     if (!process->ldt_copy || !process_port)
384     {
385         set_error( STATUS_ACCESS_DENIED );
386         return;
387     }
388     if (entry >= 8192)
389     {
390         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
391         return;
392     }
393
394     if ((ret = task_suspend( process_port )) == KERN_SUCCESS)
395     {
396         void *ptr = process->ldt_copy;
397         vm_offset_t offset = (unsigned long)ptr % page_size;
398         vm_address_t aligned_address = (vm_address_t)((char *)ptr - offset);
399         vm_size_t aligned_size = (total_size + offset + page_size - 1) / page_size * page_size;
400
401         ret = vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
402         if (ret != KERN_SUCCESS) mach_set_error( ret );
403         else
404         {
405             const int *ldt = (const int *)((char *)data + offset);
406             memcpy( base, ldt + entry, sizeof(int) );
407             memcpy( limit, ldt + entry + 8192, sizeof(int) );
408             memcpy( flags, (char *)(ldt + 2 * 8192) + entry, 1 );
409             vm_deallocate( mach_task_self(), data, bytes_read );
410         }
411         task_resume( process_port );
412     }
413     else mach_set_error( ret );
414 }
415
416 #endif  /* USE_MACH */