ntdll: Validate blocks in the heap pending free request list.
[wine.git] / server / mach.c
blob6051396a1bf3a97f0d2f5e15d195dccd65c2c27b
1 /*
2 * Server-side debugger support using Mach primitives
4 * Copyright (C) 1999, 2006 Alexandre Julliard
5 * Copyright (C) 2006 Ken Thomases for CodeWeavers
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.
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.
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
22 #include "config.h"
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 #ifdef HAVE_SYS_SYSCALL_H
32 #include <sys/syscall.h>
33 #endif
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "winternl.h"
39 #include "file.h"
40 #include "process.h"
41 #include "thread.h"
42 #include "request.h"
44 #ifdef USE_MACH
46 #include <mach/mach.h>
47 #include <mach/mach_error.h>
48 #include <mach/thread_act.h>
49 #include <mach/mach_vm.h>
50 #include <servers/bootstrap.h>
52 static mach_port_t server_mach_port;
54 void sigchld_callback(void)
56 assert(0); /* should never be called on MacOS */
59 static void mach_set_error(kern_return_t mach_error)
61 switch (mach_error)
63 case KERN_SUCCESS: break;
64 case KERN_INVALID_ARGUMENT: set_error(STATUS_INVALID_PARAMETER); break;
65 case KERN_NO_SPACE: set_error(STATUS_NO_MEMORY); break;
66 case KERN_PROTECTION_FAILURE: set_error(STATUS_ACCESS_DENIED); break;
67 case KERN_INVALID_ADDRESS: set_error(STATUS_ACCESS_VIOLATION); break;
68 default: set_error(STATUS_UNSUCCESSFUL); break;
72 static mach_port_t get_process_port( struct process *process )
74 return process->trace_data;
77 /* initialize the process control mechanism */
78 void init_tracing_mechanism(void)
80 mach_port_t bp;
82 if (task_get_bootstrap_port(mach_task_self(), &bp) != KERN_SUCCESS)
83 fatal_error("Can't find bootstrap port\n");
84 if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server_mach_port) != KERN_SUCCESS)
85 fatal_error("Can't allocate port\n");
86 if (mach_port_insert_right( mach_task_self(),
87 server_mach_port,
88 server_mach_port,
89 MACH_MSG_TYPE_MAKE_SEND ) != KERN_SUCCESS)
90 fatal_error("Error inserting rights\n");
91 if (bootstrap_register(bp, server_dir, server_mach_port) != KERN_SUCCESS)
92 fatal_error("Can't check in server_mach_port\n");
93 mach_port_deallocate(mach_task_self(), bp);
96 /* initialize the per-process tracing mechanism */
97 void init_process_tracing( struct process *process )
99 int pid, ret;
100 struct
102 mach_msg_header_t header;
103 mach_msg_body_t body;
104 mach_msg_port_descriptor_t task_port;
105 mach_msg_trailer_t trailer; /* only present on receive */
106 } msg;
108 for (;;)
110 ret = mach_msg( &msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof(msg),
111 server_mach_port, 0, 0 );
112 if (ret)
114 if (ret != MACH_RCV_TIMED_OUT && debug_level)
115 fprintf( stderr, "warning: mach port receive failed with %x\n", ret );
116 return;
119 /* if anything in the message is invalid, ignore it */
120 if (msg.header.msgh_size != offsetof(typeof(msg), trailer)) continue;
121 if (msg.body.msgh_descriptor_count != 1) continue;
122 if (msg.task_port.type != MACH_MSG_PORT_DESCRIPTOR) continue;
123 if (msg.task_port.disposition != MACH_MSG_TYPE_PORT_SEND) continue;
124 if (msg.task_port.name == MACH_PORT_NULL) continue;
125 if (msg.task_port.name == MACH_PORT_DEAD) continue;
127 if (!pid_for_task( msg.task_port.name, &pid ))
129 struct thread *thread = get_thread_from_pid( pid );
131 if (thread && !thread->process->trace_data)
132 thread->process->trace_data = msg.task_port.name;
133 else
134 mach_port_deallocate( mach_task_self(), msg.task_port.name );
139 /* terminate the per-process tracing mechanism */
140 void finish_process_tracing( struct process *process )
142 if (process->trace_data)
144 mach_port_deallocate( mach_task_self(), process->trace_data );
145 process->trace_data = 0;
149 /* initialize registers in new thread if necessary */
150 void init_thread_context( struct thread *thread )
154 /* retrieve the thread x86 registers */
155 void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
157 #if defined(__i386__) || defined(__x86_64__)
158 x86_debug_state_t state;
159 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
160 mach_msg_type_name_t type;
161 mach_port_t port, process_port = get_process_port( thread->process );
162 kern_return_t ret;
163 unsigned long dr[8];
165 /* all other regs are handled on the client side */
166 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
168 if (thread->unix_pid == -1 || !process_port ||
169 mach_port_extract_right( process_port, thread->unix_tid,
170 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
172 set_error( STATUS_ACCESS_DENIED );
173 return;
176 ret = thread_get_state( port, x86_DEBUG_STATE, (thread_state_t)&state, &count );
177 if (!ret)
179 assert( state.dsh.flavor == x86_DEBUG_STATE32 ||
180 state.dsh.flavor == x86_DEBUG_STATE64 );
182 if (state.dsh.flavor == x86_DEBUG_STATE64)
184 dr[0] = state.uds.ds64.__dr0;
185 dr[1] = state.uds.ds64.__dr1;
186 dr[2] = state.uds.ds64.__dr2;
187 dr[3] = state.uds.ds64.__dr3;
188 dr[6] = state.uds.ds64.__dr6;
189 dr[7] = state.uds.ds64.__dr7;
191 else
193 dr[0] = state.uds.ds32.__dr0;
194 dr[1] = state.uds.ds32.__dr1;
195 dr[2] = state.uds.ds32.__dr2;
196 dr[3] = state.uds.ds32.__dr3;
197 dr[6] = state.uds.ds32.__dr6;
198 dr[7] = state.uds.ds32.__dr7;
201 switch (context->machine)
203 case IMAGE_FILE_MACHINE_I386:
204 context->debug.i386_regs.dr0 = dr[0];
205 context->debug.i386_regs.dr1 = dr[1];
206 context->debug.i386_regs.dr2 = dr[2];
207 context->debug.i386_regs.dr3 = dr[3];
208 context->debug.i386_regs.dr6 = dr[6];
209 context->debug.i386_regs.dr7 = dr[7];
210 break;
211 case IMAGE_FILE_MACHINE_AMD64:
212 context->debug.x86_64_regs.dr0 = dr[0];
213 context->debug.x86_64_regs.dr1 = dr[1];
214 context->debug.x86_64_regs.dr2 = dr[2];
215 context->debug.x86_64_regs.dr3 = dr[3];
216 context->debug.x86_64_regs.dr6 = dr[6];
217 context->debug.x86_64_regs.dr7 = dr[7];
218 break;
219 default:
220 set_error( STATUS_INVALID_PARAMETER );
221 goto done;
223 context->flags |= SERVER_CTX_DEBUG_REGISTERS;
225 else
226 mach_set_error( ret );
227 done:
228 mach_port_deallocate( mach_task_self(), port );
229 #endif
232 /* set the thread x86 registers */
233 void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
235 #if defined(__i386__) || defined(__x86_64__)
236 x86_debug_state_t state;
237 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
238 mach_msg_type_name_t type;
239 mach_port_t port, process_port = get_process_port( thread->process );
240 unsigned long dr[8];
241 kern_return_t ret;
243 /* all other regs are handled on the client side */
244 assert( flags == SERVER_CTX_DEBUG_REGISTERS );
246 if (thread->unix_pid == -1 || !process_port ||
247 mach_port_extract_right( process_port, thread->unix_tid,
248 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
250 set_error( STATUS_ACCESS_DENIED );
251 return;
254 /* get the debug state to determine which flavor to use */
255 ret = thread_get_state(port, x86_DEBUG_STATE, (thread_state_t)&state, &count);
256 if (ret)
258 mach_set_error( ret );
259 goto done;
261 assert( state.dsh.flavor == x86_DEBUG_STATE32 ||
262 state.dsh.flavor == x86_DEBUG_STATE64 );
264 switch (context->machine)
266 case IMAGE_FILE_MACHINE_I386:
267 dr[0] = context->debug.i386_regs.dr0;
268 dr[1] = context->debug.i386_regs.dr1;
269 dr[2] = context->debug.i386_regs.dr2;
270 dr[3] = context->debug.i386_regs.dr3;
271 dr[6] = context->debug.i386_regs.dr6;
272 dr[7] = context->debug.i386_regs.dr7;
273 break;
274 case IMAGE_FILE_MACHINE_AMD64:
275 dr[0] = context->debug.x86_64_regs.dr0;
276 dr[1] = context->debug.x86_64_regs.dr1;
277 dr[2] = context->debug.x86_64_regs.dr2;
278 dr[3] = context->debug.x86_64_regs.dr3;
279 dr[6] = context->debug.x86_64_regs.dr6;
280 dr[7] = context->debug.x86_64_regs.dr7;
281 break;
282 default:
283 set_error( STATUS_INVALID_PARAMETER );
284 goto done;
287 /* Mac OS doesn't allow setting the global breakpoint flags */
288 dr[7] = (dr[7] & ~0xaa) | ((dr[7] & 0xaa) >> 1);
290 if (state.dsh.flavor == x86_DEBUG_STATE64)
292 state.dsh.count = sizeof(state.uds.ds64) / sizeof(int);
293 state.uds.ds64.__dr0 = dr[0];
294 state.uds.ds64.__dr1 = dr[1];
295 state.uds.ds64.__dr2 = dr[2];
296 state.uds.ds64.__dr3 = dr[3];
297 state.uds.ds64.__dr4 = 0;
298 state.uds.ds64.__dr5 = 0;
299 state.uds.ds64.__dr6 = dr[6];
300 state.uds.ds64.__dr7 = dr[7];
302 else
304 state.dsh.count = sizeof(state.uds.ds32) / sizeof(int);
305 state.uds.ds32.__dr0 = dr[0];
306 state.uds.ds32.__dr1 = dr[1];
307 state.uds.ds32.__dr2 = dr[2];
308 state.uds.ds32.__dr3 = dr[3];
309 state.uds.ds32.__dr4 = 0;
310 state.uds.ds32.__dr5 = 0;
311 state.uds.ds32.__dr6 = dr[6];
312 state.uds.ds32.__dr7 = dr[7];
314 ret = thread_set_state( port, x86_DEBUG_STATE, (thread_state_t)&state, count );
315 if (ret)
316 mach_set_error( ret );
317 done:
318 mach_port_deallocate( mach_task_self(), port );
319 #endif
322 int send_thread_signal( struct thread *thread, int sig )
324 int ret = -1;
325 mach_port_t process_port = get_process_port( thread->process );
327 if (thread->unix_pid != -1 && process_port)
329 mach_msg_type_name_t type;
330 mach_port_t port;
332 if (!mach_port_extract_right( process_port, thread->unix_tid,
333 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
335 ret = syscall( SYS___pthread_kill, port, sig );
336 mach_port_deallocate( mach_task_self(), port );
338 else errno = ESRCH;
340 if (ret == -1 && errno == ESRCH) /* thread got killed */
342 thread->unix_pid = -1;
343 thread->unix_tid = -1;
346 if (debug_level && ret != -1)
347 fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
348 return (ret != -1);
351 /* read data from a process memory space */
352 int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, char *dest )
354 kern_return_t ret;
355 mach_msg_type_number_t bytes_read;
356 mach_vm_offset_t offset;
357 vm_offset_t data;
358 mach_vm_address_t aligned_address;
359 mach_vm_size_t aligned_size;
360 unsigned int page_size = get_page_size();
361 mach_port_t process_port = get_process_port( process );
363 if (!process_port)
365 set_error( STATUS_ACCESS_DENIED );
366 return 0;
368 if ((mach_vm_address_t)ptr != ptr)
370 set_error( STATUS_ACCESS_DENIED );
371 return 0;
374 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
376 mach_set_error( ret );
377 return 0;
380 offset = ptr % page_size;
381 aligned_address = (mach_vm_address_t)(ptr - offset);
382 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
384 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
385 if (ret != KERN_SUCCESS) mach_set_error( ret );
386 else
388 memcpy( dest, (char *)data + offset, size );
389 mach_vm_deallocate( mach_task_self(), data, bytes_read );
391 task_resume( process_port );
392 return (ret == KERN_SUCCESS);
395 /* write data to a process memory space */
396 int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
398 kern_return_t ret;
399 mach_vm_address_t aligned_address, region_address;
400 mach_vm_size_t aligned_size, region_size;
401 mach_msg_type_number_t info_size, bytes_read;
402 mach_vm_offset_t offset;
403 vm_offset_t task_mem = 0;
404 struct vm_region_basic_info_64 info;
405 mach_port_t dummy;
406 unsigned int page_size = get_page_size();
407 mach_port_t process_port = get_process_port( process );
409 if (!process_port)
411 set_error( STATUS_ACCESS_DENIED );
412 return 0;
414 if ((mach_vm_address_t)ptr != ptr)
416 set_error( STATUS_ACCESS_DENIED );
417 return 0;
420 offset = ptr % page_size;
421 aligned_address = (mach_vm_address_t)(ptr - offset);
422 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
424 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
426 mach_set_error( ret );
427 return 0;
430 ret = mach_vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
431 if (ret != KERN_SUCCESS)
433 mach_set_error( ret );
434 goto failed;
436 region_address = aligned_address;
437 info_size = sizeof(info);
438 ret = mach_vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO_64,
439 (vm_region_info_t)&info, &info_size, &dummy );
440 if (ret != KERN_SUCCESS)
442 mach_set_error( ret );
443 goto failed;
445 if (region_address > aligned_address ||
446 region_address + region_size < aligned_address + aligned_size)
448 /* FIXME: should support multiple regions */
449 set_error( ERROR_ACCESS_DENIED );
450 goto failed;
452 ret = mach_vm_protect( process_port, aligned_address, aligned_size, 0, VM_PROT_READ | VM_PROT_WRITE );
453 if (ret != KERN_SUCCESS)
455 mach_set_error( ret );
456 goto failed;
459 /* FIXME: there's an optimization that can be made: check first and last */
460 /* pages for writability; read first and last pages; write interior */
461 /* pages to task without ever reading&modifying them; if that succeeds, */
462 /* modify first and last pages and write them. */
464 memcpy( (char*)task_mem + offset, src, size );
466 ret = mach_vm_write( process_port, aligned_address, task_mem, bytes_read );
467 if (ret != KERN_SUCCESS) mach_set_error( ret );
468 else
470 mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
471 /* restore protection */
472 mach_vm_protect( process_port, aligned_address, aligned_size, 0, info.protection );
473 task_resume( process_port );
474 return 1;
477 failed:
478 if (task_mem) mach_vm_deallocate( mach_task_self(), task_mem, bytes_read );
479 task_resume( process_port );
480 return 0;
483 /* retrieve an LDT selector entry */
484 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
485 unsigned int *limit, unsigned char *flags )
487 const unsigned int total_size = (2 * sizeof(int) + 1) * 8192;
488 struct process *process = thread->process;
489 unsigned int page_size = get_page_size();
490 vm_offset_t data;
491 kern_return_t ret;
492 mach_msg_type_number_t bytes_read;
493 mach_port_t process_port = get_process_port( thread->process );
495 if (!process->ldt_copy || !process_port)
497 set_error( STATUS_ACCESS_DENIED );
498 return;
500 if (entry >= 8192)
502 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
503 return;
506 if ((ret = task_suspend( process_port )) == KERN_SUCCESS)
508 mach_vm_offset_t offset = process->ldt_copy % page_size;
509 mach_vm_address_t aligned_address = (mach_vm_address_t)(process->ldt_copy - offset);
510 mach_vm_size_t aligned_size = (total_size + offset + page_size - 1) / page_size * page_size;
512 ret = mach_vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
513 if (ret != KERN_SUCCESS) mach_set_error( ret );
514 else
516 const int *ldt = (const int *)((char *)data + offset);
517 memcpy( base, ldt + entry, sizeof(int) );
518 memcpy( limit, ldt + entry + 8192, sizeof(int) );
519 memcpy( flags, (char *)(ldt + 2 * 8192) + entry, 1 );
520 mach_vm_deallocate( mach_task_self(), data, bytes_read );
522 task_resume( process_port );
524 else mach_set_error( ret );
527 #endif /* USE_MACH */