OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 #include <errno.h> |
| 5 #include <signal.h> |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "components/nacl/loader/nonsfi/irt_interfaces.h" |
| 10 #include "native_client/src/include/nacl_macros.h" |
| 11 #include "native_client/src/trusted/service_runtime/nacl_exception.h" |
| 12 #include "native_client/src/trusted/service_runtime/nacl_signal.h" |
| 13 |
| 14 namespace nacl { |
| 15 namespace nonsfi { |
| 16 namespace { |
| 17 |
| 18 /* NaCl side of things resides in: |
| 19 * native_client/src/trusted/service_runtime/linux/nacl_signal.c |
| 20 */ |
| 21 |
| 22 // TODO(uekawa): The list of signals to be handled might need updating. |
| 23 static const int kSignals[] = { |
| 24 SIGSTKFLT, |
| 25 // SIGSYS, /* sigsys is reserved for seccomp-bpf */ |
| 26 // NACL_THREAD_SUSPEND_SIGNAL, // what is this signal? |
| 27 SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGBUS, SIGFPE, SIGSEGV, |
| 28 /* Handle SIGABRT in case someone sends it asynchronously using kill(). */ |
| 29 SIGABRT |
| 30 }; |
| 31 |
| 32 NaClExceptionHandler signal_handler_function_pointer = NULL; |
| 33 |
| 34 // Signal handler, responsible for calling the registered handlers. |
| 35 static void SignalCatch(int sig, siginfo_t *info, void *uc) { |
| 36 if (signal_handler_function_pointer) { |
| 37 NaClSignalContext signal_context; |
| 38 NaClSignalContextFromHandler(&signal_context, uc); |
| 39 // Is this safe to allocate this on stack ? |
| 40 NaClExceptionFrame exception_frame; |
| 41 NaClSignalSetUpExceptionFrame(&exception_frame, |
| 42 &signal_context, |
| 43 0 /* context_user_addr, what is this? */); |
| 44 |
| 45 signal_handler_function_pointer(&exception_frame.context); |
| 46 } |
| 47 // TODO(uekawa): only exit on crash signals ? |
| 48 _exit(-1); |
| 49 } |
| 50 |
| 51 // Based off NaCl version |
| 52 // native_client/src/trusted/service_runtime/sys_exception.c |
| 53 static int IrtExceptionHandler(NaClExceptionHandler handler, |
| 54 NaClExceptionHandler *old_handler) { |
| 55 // TODO(uekawa): Do I need to mutex lock? |
| 56 if (old_handler) { |
| 57 *old_handler = signal_handler_function_pointer; |
| 58 } |
| 59 signal_handler_function_pointer = handler; |
| 60 return 0; |
| 61 } |
| 62 |
| 63 static int IrtExceptionStack(void *stack, size_t size) { |
| 64 // TODO(uekawa): implement for unit tests? IrtThreadCreate allocates |
| 65 // sigaltstack and there's very little point in re-allocating a new |
| 66 // altstack. Note, this is required by |
| 67 // tests/exception_test/exception_crash_test.c |
| 68 return -EINVAL; |
| 69 } |
| 70 |
| 71 static int IrtExceptionClearFlag(void) { |
| 72 // TODO(uekawa): I think we shouldn't implement this for now. |
| 73 return -EINVAL; |
| 74 } |
| 75 |
| 76 } // namespace |
| 77 |
| 78 const struct nacl_irt_exception_handling kIrtExceptionHandling = { |
| 79 IrtExceptionHandler, |
| 80 IrtExceptionStack, |
| 81 IrtExceptionClearFlag, |
| 82 }; |
| 83 |
| 84 void InitializeSignalHandler(void) { |
| 85 struct sigaction sa; |
| 86 unsigned int a; |
| 87 |
| 88 memset(&sa, 0, sizeof(sa)); |
| 89 sigemptyset(&sa.sa_mask); |
| 90 sa.sa_sigaction = SignalCatch; |
| 91 sa.sa_flags = SA_ONSTACK | SA_SIGINFO; |
| 92 |
| 93 /* |
| 94 * Mask all signals we catch to prevent re-entry. |
| 95 * |
| 96 * In particular, NACL_THREAD_SUSPEND_SIGNAL must be masked while we |
| 97 * are handling a fault from untrusted code, otherwise the |
| 98 * suspension signal will interrupt the trusted fault handler. That |
| 99 * would cause NaClAppThreadGetSuspendedRegisters() to report |
| 100 * trusted-code register state rather than untrusted-code register |
| 101 * state from the point where the fault occurred. |
| 102 */ |
| 103 for (a = 0; a < NACL_ARRAY_SIZE(kSignals); a++) { |
| 104 sigaddset(&sa.sa_mask, kSignals[a]); |
| 105 } |
| 106 |
| 107 /* Install all handlers */ |
| 108 for (a = 0; a < NACL_ARRAY_SIZE(kSignals); a++) { |
| 109 if (sigaction(kSignals[a], &sa, NULL) != 0) { |
| 110 // TODO: log something when things fail. This is bad. |
| 111 perror("sigaction"); |
| 112 // LOG(FATAL) << "Failed to install signal handler"; |
| 113 } |
| 114 } |
| 115 } |
| 116 |
| 117 } // namespace nonsfi |
| 118 } // namespace nacl |
OLD | NEW |