Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: src/trusted/service_runtime/osx/mach_exception_handler.c

Issue 12207165: Mac x86_64: Mach exception support (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client/
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be 3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file. 4 * found in the LICENSE file.
5 */ 5 */
6 6
7 #include "native_client/src/trusted/service_runtime/osx/mach_exception_handler.h " 7 #include "native_client/src/trusted/service_runtime/osx/mach_exception_handler.h "
8 8
9 #include <mach/mach.h> 9 #include <mach/mach.h>
10 #include <mach/mach_vm.h> 10 #include <mach/mach_vm.h>
11 #include <mach/thread_status.h> 11 #include <mach/thread_status.h>
12 #include <pthread.h> 12 #include <pthread.h>
13 #include <stddef.h> 13 #include <stddef.h>
14 #include <stdio.h> 14 #include <stdio.h>
15 #include <stdlib.h> 15 #include <stdlib.h>
16 16
17 #include "native_client/src/include/nacl_macros.h" 17 #include "native_client/src/include/nacl_macros.h"
18 #include "native_client/src/include/portability.h" 18 #include "native_client/src/include/portability.h"
19 #include "native_client/src/shared/platform/nacl_check.h" 19 #include "native_client/src/shared/platform/nacl_check.h"
20 #include "native_client/src/shared/platform/nacl_log.h" 20 #include "native_client/src/shared/platform/nacl_log.h"
21 #include "native_client/src/trusted/service_runtime/arch/sel_ldr_arch.h" 21 #include "native_client/src/trusted/service_runtime/arch/sel_ldr_arch.h"
22 #include "native_client/src/trusted/service_runtime/nacl_app.h" 22 #include "native_client/src/trusted/service_runtime/nacl_app.h"
23 #include "native_client/src/trusted/service_runtime/nacl_app_thread.h" 23 #include "native_client/src/trusted/service_runtime/nacl_app_thread.h"
24 #include "native_client/src/trusted/service_runtime/nacl_config.h" 24 #include "native_client/src/trusted/service_runtime/nacl_config.h"
25 #include "native_client/src/trusted/service_runtime/nacl_exc.h" 25 #include "native_client/src/trusted/service_runtime/nacl_exc.h"
26 #include "native_client/src/trusted/service_runtime/nacl_exception.h" 26 #include "native_client/src/trusted/service_runtime/nacl_exception.h"
27 #include "native_client/src/trusted/service_runtime/nacl_globals.h" 27 #include "native_client/src/trusted/service_runtime/nacl_globals.h"
28 #include "native_client/src/trusted/service_runtime/nacl_switch_to_app.h" 28 #include "native_client/src/trusted/service_runtime/nacl_switch_to_app.h"
29 #include "native_client/src/trusted/service_runtime/nacl_tls.h"
30 #include "native_client/src/trusted/service_runtime/osx/crash_filter.h"
31 #include "native_client/src/trusted/service_runtime/osx/mach_thread_map.h"
29 #include "native_client/src/trusted/service_runtime/sel_ldr.h" 32 #include "native_client/src/trusted/service_runtime/sel_ldr.h"
30 #include "native_client/src/trusted/service_runtime/sel_rt.h" 33 #include "native_client/src/trusted/service_runtime/sel_rt.h"
31 34
32 /* Only handle x86_32 for now. */ 35 #if NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86
33 #if NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 && NACL_BUILD_SUBARCH == 32
34 36
35 37
36 /* 38 /*
37 * MIG generated message pump from /usr/include/mach/exc.defs 39 * MIG generated message pump from /usr/include/mach/exc.defs
38 * Tweaked to place in an isolated namespace. 40 * Tweaked to place in an isolated namespace.
39 */ 41 */
40 boolean_t nacl_exc_server( 42 boolean_t nacl_exc_server(
41 mach_msg_header_t *InHeadP, 43 mach_msg_header_t *InHeadP,
42 mach_msg_header_t *OutHeadP); 44 mach_msg_header_t *OutHeadP);
43 45
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 } 82 }
81 } 83 }
82 84
83 static void FireDebugStubEvent(int pipe_fd) { 85 static void FireDebugStubEvent(int pipe_fd) {
84 char buf = 0; 86 char buf = 0;
85 if (write(pipe_fd, &buf, sizeof(buf)) != sizeof(buf)) { 87 if (write(pipe_fd, &buf, sizeof(buf)) != sizeof(buf)) {
86 NaClLog(LOG_FATAL, "FireDebugStubEvent: Can't send debug stub event\n"); 88 NaClLog(LOG_FATAL, "FireDebugStubEvent: Can't send debug stub event\n");
87 } 89 }
88 } 90 }
89 91
92 #if NACL_BUILD_SUBARCH == 32
93
94 #define NATIVE_x86_THREAD_STATE x86_THREAD_STATE32
95 #define X86_REG_BP(regs) ((regs).uts.ts32.__ebp)
96 #define X86_REG_SP(regs) ((regs).uts.ts32.__esp)
97 #define X86_REG_IP(regs) ((regs).uts.ts32.__eip)
98 #define X86_REG_FLAGS(regs) ((regs).uts.ts32.__eflags)
99
100 #elif NACL_BUILD_SUBARCH == 64
101
102 #define NATIVE_x86_THREAD_STATE x86_THREAD_STATE64
103 #define X86_REG_BP(regs) ((regs).uts.ts64.__rbp)
104 #define X86_REG_SP(regs) ((regs).uts.ts64.__rsp)
105 #define X86_REG_IP(regs) ((regs).uts.ts64.__rip)
106 #define X86_REG_FLAGS(regs) ((regs).uts.ts64.__rflags)
107
108 #endif /* NACL_BUILD_SUBARCH */
109
90 static int HandleException(mach_port_t thread_port, 110 static int HandleException(mach_port_t thread_port,
91 exception_type_t exception, int *is_untrusted) { 111 exception_type_t exception, int *is_untrusted) {
92 mach_msg_type_number_t size; 112 mach_msg_type_number_t size;
93 x86_thread_state_t regs; 113 x86_thread_state_t regs;
94 kern_return_t result; 114 kern_return_t result;
95 uint16_t trusted_cs = NaClGetGlobalCs();
96 uint16_t trusted_ds = NaClGetGlobalDs();
97 uint32_t nacl_thread_index; 115 uint32_t nacl_thread_index;
98 struct NaClApp *nap; 116 struct NaClApp *nap;
99 struct NaClAppThread *natp; 117 struct NaClAppThread *natp;
100 struct NaClExceptionFrame frame; 118 struct NaClExceptionFrame frame;
101 uintptr_t frame_addr_user; 119 uint32_t frame_addr_user;
102 uintptr_t frame_addr_sys; 120 uintptr_t frame_addr_sys;
121 #if NACL_BUILD_SUBARCH == 32
122 uint16_t trusted_cs = NaClGetGlobalCs();
123 uint16_t trusted_ds = NaClGetGlobalDs();
124 #endif
103 125
104 /* Assume untrusted crash until we know otherwise. */ 126 /* Assume untrusted crash until we know otherwise. */
105 *is_untrusted = TRUE; 127 *is_untrusted = TRUE;
106 128
107 /* Capture the register state of the 'excepting' thread. */ 129 /* Capture the register state of the 'excepting' thread. */
108 size = sizeof(regs) / sizeof(natural_t); 130 size = x86_THREAD_STATE_COUNT;
109 result = thread_get_state(thread_port, x86_THREAD_STATE, 131 result = thread_get_state(thread_port, x86_THREAD_STATE,
110 (void *) &regs, &size); 132 (thread_state_t) &regs, &size);
111 if (result != KERN_SUCCESS) { 133 if (result != KERN_SUCCESS) {
112 return 0; 134 return 0;
113 } 135 }
136 CHECK(regs.tsh.flavor == NATIVE_x86_THREAD_STATE);
114 137
115 /* 138 #if NACL_BUILD_SUBARCH == 32
116 * If trusted_cs is 0 (which is not a usable segment selector), the
117 * sandbox has not been initialised yet, so there can be no untrusted
118 * code running.
119 */
120 if (trusted_cs == 0) {
121 *is_untrusted = FALSE;
122 return 0;
123 }
124
125 /*
126 * If the current code segment is the trusted one, we aren't in the
127 * sandbox.
128 * TODO(bradnelson): This makes the potentially false assumption that cs is
129 * the last thing to change when switching into untrusted code. We need
130 * tests to vet this.
131 */
132 if (regs.uts.ts32.__cs == trusted_cs) {
133 /*
134 * If we are single-stepping, allow NaClSwitchRemainingRegsViaECX()
135 * to continue in order to restore control to untrusted code.
136 */
137 if (exception == EXC_BREAKPOINT &&
138 (regs.uts.ts32.__eflags & NACL_X86_TRAP_FLAG) != 0 &&
139 regs.uts.ts32.__eip >= (uintptr_t) NaClSwitchRemainingRegsViaECX &&
140 regs.uts.ts32.__eip < (uintptr_t) NaClSwitchRemainingRegsAsmEnd) {
141 return 1;
142 }
143 *is_untrusted = FALSE;
144 return 0;
145 }
146
147 /* 139 /*
148 * We can get the thread index from the segment selector used for TLS 140 * We can get the thread index from the segment selector used for TLS
149 * from %gs >> 3. 141 * from %gs >> 3.
150 * TODO(bradnelson): Migrate that knowledge to a single shared location. 142 * TODO(bradnelson): Migrate that knowledge to a single shared location.
151 */ 143 */
152 nacl_thread_index = regs.uts.ts32.__gs >> 3; 144 nacl_thread_index = regs.uts.ts32.__gs >> 3;
145 #elif NACL_BUILD_SUBARCH == 64
146 nacl_thread_index = NaClGetThreadIndexForMachThread(thread_port);
147
148 if (nacl_thread_index == NACL_TLS_INDEX_INVALID) {
149 *is_untrusted = FALSE;
150 return 0;
151 }
152 #endif
153
153 natp = NaClAppThreadGetFromIndex(nacl_thread_index); 154 natp = NaClAppThreadGetFromIndex(nacl_thread_index);
155 if (natp == NULL) {
156 *is_untrusted = FALSE;
157 return 0;
158 }
154 nap = natp->nap; 159 nap = natp->nap;
155 160
161 /*
162 * TODO(bradnelson): For x86_32, this makes the potentially false assumption
163 * that cs is the last thing to change when switching into untrusted
164 * code. We need tests to vet this.
165 */
166 *is_untrusted = NaClMachThreadStateIsInUntrusted(&regs, nacl_thread_index);
167
168 /*
169 * If trusted code accidentally jumped to untrusted code, don't let the
170 * untrusted exception handler take over.
171 */
172 if (*is_untrusted &&
173 (natp->suspend_state & NACL_APP_THREAD_UNTRUSTED) == 0) {
174 *is_untrusted = 0;
175 return 0;
176 }
177
178 if (!*is_untrusted) {
179 #if NACL_BUILD_SUBARCH == 32
180 /*
181 * If we are single-stepping, allow NaClSwitchRemainingRegsViaECX()
182 * to continue in order to restore control to untrusted code.
183 */
184 if (exception == EXC_BREAKPOINT &&
185 (X86_REG_FLAGS(regs) & NACL_X86_TRAP_FLAG) != 0 &&
186 X86_REG_IP(regs) >= (uintptr_t) NaClSwitchRemainingRegsViaECX &&
187 X86_REG_IP(regs) < (uintptr_t) NaClSwitchRemainingRegsAsmEnd) {
188 return 1;
189 }
190 #endif
191 return 0;
192 }
193
156 if (nap->enable_faulted_thread_queue) { 194 if (nap->enable_faulted_thread_queue) {
195 #if NACL_BUILD_SUBARCH == 32
157 /* 196 /*
158 * If we are single-stepping, step through until we reach untrusted code. 197 * If we are single-stepping, step through until we reach untrusted code.
159 */ 198 */
160 if (exception == EXC_BREAKPOINT && 199 if (exception == EXC_BREAKPOINT &&
161 (regs.uts.ts32.__eflags & NACL_X86_TRAP_FLAG) != 0) { 200 (X86_REG_FLAGS(regs) & NACL_X86_TRAP_FLAG) != 0) {
162 if (regs.uts.ts32.__eip >= nap->all_regs_springboard.start_addr && 201 if (X86_REG_IP(regs) >= nap->all_regs_springboard.start_addr &&
163 regs.uts.ts32.__eip < nap->all_regs_springboard.end_addr) { 202 X86_REG_IP(regs) < nap->all_regs_springboard.end_addr) {
164 return 1; 203 return 1;
165 } 204 }
166 /* 205 /*
167 * Step through the instruction we have been asked to restore 206 * Step through the instruction we have been asked to restore
168 * control to. 207 * control to.
169 */ 208 */
170 if (regs.uts.ts32.__eip == natp->user.gs_segment.new_prog_ctr) { 209 if (X86_REG_IP(regs) == natp->user.gs_segment.new_prog_ctr) {
171 return 1; 210 return 1;
172 } 211 }
173 } 212 }
213 #endif
174 214
175 /* 215 /*
176 * Increment the kernel's thread suspension count so that the 216 * Increment the kernel's thread suspension count so that the
177 * thread remains suspended after we return. 217 * thread remains suspended after we return.
178 */ 218 */
179 result = thread_suspend(thread_port); 219 result = thread_suspend(thread_port);
180 if (result != KERN_SUCCESS) { 220 if (result != KERN_SUCCESS) {
181 NaClLog(LOG_FATAL, "HandleException: thread_suspend() call failed\n"); 221 NaClLog(LOG_FATAL, "HandleException: thread_suspend() call failed\n");
182 } 222 }
183 /* 223 /*
(...skipping 12 matching lines...) Expand all
196 * Ignore all but bad accesses for now. 236 * Ignore all but bad accesses for now.
197 * TODO(bradnelson): eventually consider these too: 237 * TODO(bradnelson): eventually consider these too:
198 * EXC_BAD_INSTRUCTION 238 * EXC_BAD_INSTRUCTION
199 * EXC_ARITHMETIC 239 * EXC_ARITHMETIC
200 * EXC_BREAKPOINT 240 * EXC_BREAKPOINT
201 */ 241 */
202 if (exception != EXC_BAD_ACCESS) { 242 if (exception != EXC_BAD_ACCESS) {
203 return 0; 243 return 0;
204 } 244 }
205 245
246 /* Don't handle if no exception handler is set. */
247 if (nap->exception_handler == 0) {
248 return 0;
249 }
250
206 /* Don't handle it if the exception flag is set. */ 251 /* Don't handle it if the exception flag is set. */
207 if (natp->exception_flag) { 252 if (natp->exception_flag) {
208 return 0; 253 return 0;
209 } 254 }
210 /* Set the flag. */ 255 /* Set the flag. */
211 natp->exception_flag = 1; 256 natp->exception_flag = 1;
212 257
213 /* Don't handle if no exception handler is set. */
214 if (nap->exception_handler == 0) {
215 return 0;
216 }
217
218 /* Get location of exception stack frame. */ 258 /* Get location of exception stack frame. */
219 if (natp->exception_stack) { 259 if (natp->exception_stack) {
220 frame_addr_user = natp->exception_stack; 260 frame_addr_user = natp->exception_stack;
221 } else { 261 } else {
222 /* If not set default to user stack. */ 262 /* If not set default to user stack. */
223 frame_addr_user = regs.uts.ts32.__esp; 263 frame_addr_user = X86_REG_SP(regs) - NACL_STACK_RED_ZONE;
224 } 264 }
225 265
226 /* Align stack frame properly. */ 266 /* Align stack frame properly. */
227 frame_addr_user -= 267 frame_addr_user -=
228 sizeof(struct NaClExceptionFrame) - NACL_STACK_PAD_BELOW_ALIGN; 268 sizeof(struct NaClExceptionFrame) - NACL_STACK_PAD_BELOW_ALIGN;
229 frame_addr_user &= ~NACL_STACK_ALIGN_MASK; 269 frame_addr_user &= ~NACL_STACK_ALIGN_MASK;
230 frame_addr_user -= NACL_STACK_PAD_BELOW_ALIGN; 270 frame_addr_user -= NACL_STACK_PAD_BELOW_ALIGN;
231 271
232 /* Convert from user to system space. */ 272 /* Convert from user to system space. */
233 frame_addr_sys = NaClUserToSysAddrRange( 273 frame_addr_sys = NaClUserToSysAddrRange(
234 nap, frame_addr_user, sizeof(struct NaClExceptionFrame)); 274 nap, frame_addr_user, sizeof(struct NaClExceptionFrame));
235 if (frame_addr_sys == kNaClBadAddress) { 275 if (frame_addr_sys == kNaClBadAddress) {
236 return 0; 276 return 0;
237 } 277 }
238 278
239 /* Set up the stack frame for the handler invocation. */ 279 /* Set up the stack frame for the handler invocation. */
240 frame.return_addr = 0; 280 frame.return_addr = 0;
281 frame.context.prog_ctr = X86_REG_IP(regs);
282 frame.context.stack_ptr = X86_REG_SP(regs);
283 frame.context.frame_ptr = X86_REG_BP(regs);
284 #if NACL_BUILD_SUBARCH == 32
241 frame.context_ptr = frame_addr_user + 285 frame.context_ptr = frame_addr_user +
242 offsetof(struct NaClExceptionFrame, context); 286 offsetof(struct NaClExceptionFrame, context);
243 frame.context.prog_ctr = regs.uts.ts32.__eip; 287 #endif
244 frame.context.stack_ptr = regs.uts.ts32.__esp;
245 frame.context.frame_ptr = regs.uts.ts32.__ebp;
246 288
247 /* 289 /*
248 * Write the stack frame into untrusted address space. We do not 290 * Write the stack frame into untrusted address space. We do not
249 * write to the memory directly because that will fault if the 291 * write to the memory directly because that will fault if the
250 * destination location is not writable. Faulting is OK for NaCl 292 * destination location is not writable. Faulting is OK for NaCl
251 * syscalls, but here we do not want to trigger an exception while 293 * syscalls, but here we do not want to trigger an exception while
252 * in the exception handler. The overhead of using a Mach system 294 * in the exception handler. The overhead of using a Mach system
253 * call to write to memory is acceptable here. 295 * call to write to memory is acceptable here.
254 */ 296 */
255 result = mach_vm_write(mach_task_self(), frame_addr_sys, 297 result = mach_vm_write(mach_task_self(), frame_addr_sys,
256 (uintptr_t) &frame, sizeof(frame)); 298 (uintptr_t) &frame, sizeof(frame));
257 if (result != KERN_SUCCESS) { 299 if (result != KERN_SUCCESS) {
258 return 0; 300 return 0;
259 } 301 }
260 302
261 /* Set up thread context to resume at handler. */ 303 /* Set up thread context to resume at handler. */
262 natp->user.new_prog_ctr = nap->exception_handler;
263 natp->user.stack_ptr = frame_addr_user;
264 /* TODO(bradnelson): put all registers in some default state. */ 304 /* TODO(bradnelson): put all registers in some default state. */
265 305 #if NACL_BUILD_SUBARCH == 32
266 /* 306 /*
267 * Put registers in right place to land at NaClSwitchNoSSEViaECX 307 * Put registers in right place to land at NaClSwitchNoSSEViaECX
268 * This is required because: 308 * This is required because:
269 * - For an unknown reason thread_set_state resets %cs to the default 309 * - For an unknown reason thread_set_state resets %cs to the default
270 * value, even when set to something else, in current XNU versions. 310 * value, even when set to something else, in current XNU versions.
271 * - An examination of the XNU sources indicates 311 * - An examination of the XNU sources indicates
272 * that setting the code which state the thread state resets 312 * that setting the code which state the thread state resets
273 * %cs, %ds, %es, %ss to their default values in some early versions. 313 * %cs, %ds, %es, %ss to their default values in some early versions.
274 * (For instance: xnu-792.6.22/osfmk/i386/pcb.c:616) 314 * (For instance: xnu-792.6.22/osfmk/i386/pcb.c:616)
275 * This precludes going directly to the untrusted handler. 315 * This precludes going directly to the untrusted handler.
276 * Instead we call a variant of NaClSwitchNoSSE which takes a pointer 316 * Instead we call a variant of NaClSwitchNoSSE which takes a pointer
277 * to the thread user context in %ecx. 317 * to the thread user context in %ecx.
278 */ 318 */
279 regs.uts.ts32.__eip = (uint32_t) &NaClSwitchNoSSEViaECX; 319 natp->user.new_prog_ctr = nap->exception_handler;
320 natp->user.stack_ptr = frame_addr_user;
321 X86_REG_IP(regs) = (uint32_t) &NaClSwitchNoSSEViaECX;
280 regs.uts.ts32.__cs = trusted_cs; 322 regs.uts.ts32.__cs = trusted_cs;
281 regs.uts.ts32.__ecx = (uint32_t) &natp->user; 323 regs.uts.ts32.__ecx = (uint32_t) &natp->user;
282 regs.uts.ts32.__ds = trusted_ds; 324 regs.uts.ts32.__ds = trusted_ds;
283 regs.uts.ts32.__es = trusted_ds; /* just for good measure */ 325 regs.uts.ts32.__es = trusted_ds; /* just for good measure */
284 regs.uts.ts32.__ss = trusted_ds; /* just for good measure */ 326 regs.uts.ts32.__ss = trusted_ds; /* just for good measure */
285 regs.uts.ts32.__eflags &= ~NACL_X86_DIRECTION_FLAG; 327 #elif NACL_BUILD_SUBARCH == 64
328 X86_REG_IP(regs) = NaClUserToSys(nap, nap->exception_handler);
329 X86_REG_SP(regs) = frame_addr_sys;
330 X86_REG_BP(regs) = nap->mem_start;
331
332 /* Argument 1 */
333 regs.uts.ts64.__rdi = frame_addr_user +
334 offsetof(struct NaClExceptionFrame, context);
335 #endif
336 X86_REG_FLAGS(regs) &= ~NACL_X86_DIRECTION_FLAG;
286 result = thread_set_state(thread_port, x86_THREAD_STATE, 337 result = thread_set_state(thread_port, x86_THREAD_STATE,
287 (void *) &regs, size); 338 (thread_state_t) &regs, size);
288 if (result != KERN_SUCCESS) { 339 if (result != KERN_SUCCESS) {
289 return 0; 340 return 0;
290 } 341 }
291 342
292 /* Return success, and resume the thread. */ 343 /* Return success, and resume the thread. */
293 return 1; 344 return 1;
294 } 345 }
295 346
296 347
297 static kern_return_t ForwardException( 348 static kern_return_t ForwardException(
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 failure: 614 failure:
564 if (data) { 615 if (data) {
565 if (MACH_PORT_NULL != data->exception_port) { 616 if (MACH_PORT_NULL != data->exception_port) {
566 mach_port_deallocate(current_task, data->exception_port); 617 mach_port_deallocate(current_task, data->exception_port);
567 } 618 }
568 free(data); 619 free(data);
569 } 620 }
570 return FALSE; 621 return FALSE;
571 } 622 }
572 623
573 #else /* NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 && NACL_BUILD_SUBARCH == 32 */ 624 #endif /* NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 */
574
575 int NaClInterceptMachExceptions(void) {
576 return FALSE;
577 }
578
579 #endif /* NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 && NACL_BUILD_SUBARCH == 32 */
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698