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, /* Used to support a seccomp-bpf sandbox. -- is this too ? */ | |
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? */); | |
Junichi Uekawa
2014/04/14 22:51:04
SFI-NaCl converts ucontext -> NaClSignalContext ->
| |
44 | |
45 // NaClExceptionContext | |
46 signal_handler_function_pointer(&exception_frame.context); | |
47 } | |
48 // TODO(uekawa): only exit on crash signals ? | |
49 _exit(-1); | |
50 } | |
51 | |
52 // Based off NaCl version | |
53 // native_client/src/trusted/service_runtime/sys_exception.c | |
54 static int IrtExceptionHandler(NaClExceptionHandler handler, | |
55 NaClExceptionHandler *old_handler) { | |
56 // TODO(uekawa): Do I need to mutex lock? | |
57 | |
58 if (old_handler) { | |
59 *old_handler = signal_handler_function_pointer; | |
60 } | |
61 signal_handler_function_pointer = handler; | |
62 return 0; | |
63 } | |
64 | |
65 static int IrtExceptionStack(void *stack, size_t size) { | |
66 return -EINVAL; | |
67 } | |
68 | |
69 static int IrtExceptionClearFlag(void) { | |
70 return -EINVAL; | |
71 } | |
72 | |
73 } // namespace | |
74 | |
75 const struct nacl_irt_exception_handling kIrtExceptionHandling = { | |
76 IrtExceptionHandler, | |
77 IrtExceptionStack, | |
78 IrtExceptionClearFlag, | |
79 }; | |
80 | |
81 void InitializeSignalHandler(void) { | |
82 struct sigaction sa; | |
83 unsigned int a; | |
84 | |
85 memset(&sa, 0, sizeof(sa)); | |
86 sigemptyset(&sa.sa_mask); | |
87 sa.sa_sigaction = SignalCatch; | |
88 sa.sa_flags = SA_ONSTACK | SA_SIGINFO; | |
89 | |
90 /* | |
91 * Mask all signals we catch to prevent re-entry. | |
92 * | |
93 * In particular, NACL_THREAD_SUSPEND_SIGNAL must be masked while we | |
94 * are handling a fault from untrusted code, otherwise the | |
95 * suspension signal will interrupt the trusted fault handler. That | |
96 * would cause NaClAppThreadGetSuspendedRegisters() to report | |
97 * trusted-code register state rather than untrusted-code register | |
98 * state from the point where the fault occurred. | |
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 // TODO: log something when things fail. This is bad. | |
108 // LOG(FATAL) << "Failed to install signal handler"; | |
109 } | |
110 } | |
111 } | |
112 | |
113 } // namespace nonsfi | |
114 } // namespace nacl | |
OLD | NEW |