Chromium Code Reviews| 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 /* This is NonSFI version of exception handling codebase, NaCl side of | |
|
hidehiko
2014/04/22 00:22:37
nit: // comment style.
Ditto for below.
Junichi Uekawa
2014/04/22 04:57:01
Done.
| |
| 19 * things resides in: | |
| 20 * native_client/src/trusted/service_runtime/linux/nacl_signal.c | |
| 21 * native_client/src/trusted/service_runtime/sys_exception.c | |
| 22 */ | |
| 23 | |
| 24 // TODO(uekawa): The list of signals to be handled might need updating. | |
| 25 // NonSFI NaCl does not use NACL_THREAD_SUSPEND_SIGNAL (==SIGUSR1) (??check??) | |
| 26 // and SIGSYS is reserved for seccomp-bpf. | |
| 27 static const int kSignals[] = { | |
| 28 SIGSTKFLT, | |
| 29 SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGBUS, SIGFPE, SIGSEGV, | |
| 30 /* Handle SIGABRT in case someone sends it asynchronously using kill(). */ | |
| 31 SIGABRT | |
| 32 }; | |
| 33 | |
| 34 NaClExceptionHandler signal_handler_function_pointer = NULL; | |
| 35 | |
| 36 // Signal handler, responsible for calling the registered handlers. | |
| 37 static void SignalCatch(int sig, siginfo_t *info, void *uc) { | |
| 38 if (signal_handler_function_pointer) { | |
| 39 // TODO(uekawa): Whether to add dependency or copy the implementation? | |
| 40 NaClSignalContext signal_context; | |
| 41 NaClSignalContextFromHandler(&signal_context, uc); | |
| 42 // Is this safe to allocate this on stack ? | |
| 43 NaClExceptionFrame exception_frame; | |
| 44 NaClSignalSetUpExceptionFrame(&exception_frame, | |
| 45 &signal_context, | |
| 46 0 /* context_user_addr, what is this? */); | |
| 47 | |
| 48 signal_handler_function_pointer(&exception_frame.context); | |
| 49 } | |
| 50 // TODO(uekawa): Only exit on crash signals? | |
| 51 _exit(-1); | |
| 52 } | |
| 53 | |
| 54 static int IrtExceptionHandler(NaClExceptionHandler handler, | |
| 55 NaClExceptionHandler *old_handler) { | |
| 56 // TODO(uekawa): Do I need to have a mutex lock? | |
| 57 if (old_handler) { | |
| 58 *old_handler = signal_handler_function_pointer; | |
| 59 } | |
| 60 signal_handler_function_pointer = handler; | |
| 61 return 0; | |
| 62 } | |
| 63 | |
| 64 static int IrtExceptionStack(void *stack, size_t size) { | |
| 65 // TODO(uekawa): I think we shouldn't implement this until we really | |
| 66 // need it. IrtThreadCreate allocates sigaltstack already, and | |
| 67 // there is no legitimate reason I can think of that you would want | |
| 68 // to reallocate an altstack. Note that | |
| 69 // native_client/src/tests/exception_test/exception_crash_test.c | |
| 70 // wants to use this but we are not running that test yet. | |
| 71 return -EINVAL; | |
| 72 } | |
| 73 | |
| 74 static int IrtExceptionClearFlag(void) { | |
| 75 // TODO(uekawa): I think we shouldn't implement this until we really | |
| 76 // need it. | |
| 77 return -EINVAL; | |
| 78 } | |
| 79 | |
| 80 } // namespace | |
| 81 | |
| 82 const struct nacl_irt_exception_handling kIrtExceptionHandling = { | |
| 83 IrtExceptionHandler, | |
| 84 IrtExceptionStack, | |
| 85 IrtExceptionClearFlag, | |
| 86 }; | |
| 87 | |
| 88 void InitializeSignalHandler(void) { | |
| 89 struct sigaction sa; | |
| 90 unsigned int a; | |
| 91 | |
| 92 memset(&sa, 0, sizeof(sa)); | |
| 93 sigemptyset(&sa.sa_mask); | |
| 94 sa.sa_sigaction = SignalCatch; | |
| 95 sa.sa_flags = SA_ONSTACK | SA_SIGINFO; | |
| 96 | |
| 97 /* | |
| 98 * Mask all signals we catch to prevent re-entry. | |
| 99 */ | |
| 100 for (a = 0; a < NACL_ARRAY_SIZE(kSignals); a++) { | |
| 101 sigaddset(&sa.sa_mask, kSignals[a]); | |
| 102 } | |
| 103 | |
| 104 /* Install all handlers */ | |
| 105 for (a = 0; a < NACL_ARRAY_SIZE(kSignals); a++) { | |
| 106 if (sigaction(kSignals[a], &sa, NULL) != 0) { | |
| 107 perror("sigaction"); | |
|
hidehiko
2014/04/22 00:22:37
PLOG(ERROR) ?
Junichi Uekawa
2014/04/22 04:57:01
Here base/logging.h conflicts with native_client/s
| |
| 108 } | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 } // namespace nonsfi | |
| 113 } // namespace nacl | |
| OLD | NEW |