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 | |
5 #include <errno.h> | |
6 #include <signal.h> | |
7 | |
8 #include "base/atomicops.h" | |
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 | |
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 // Crash signals to handle. The differences from SFI NaCl are that | |
24 // NonSFI NaCl does not use NACL_THREAD_SUSPEND_SIGNAL (==SIGUSR1), | |
25 // and SIGSYS is reserved for seccomp-bpf. | |
26 static const int kSignals[] = { | |
27 SIGSTKFLT, | |
28 SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGBUS, SIGFPE, SIGSEGV, | |
29 // Handle SIGABRT in case someone sends it asynchronously using kill(). | |
30 SIGABRT | |
31 }; | |
32 | |
33 volatile NaClExceptionHandler signal_handler_function_pointer = NULL; | |
34 | |
35 // Signal handler, responsible for calling the registered handlers. | |
36 static void SignalCatch(int sig, siginfo_t* info, void* uc) { | |
37 if (signal_handler_function_pointer) { | |
38 NaClSignalContext signal_context; | |
39 NaClSignalContextFromHandler(&signal_context, uc); | |
40 NaClExceptionFrame exception_frame; | |
41 NaClSignalSetUpExceptionFrame(&exception_frame, | |
42 &signal_context, | |
43 0 /* context_user_addr, | |
44 not useful for NonSFI NaCl. */); | |
45 signal_handler_function_pointer(&exception_frame.context); | |
46 } | |
47 _exit(-1); | |
48 } | |
49 | |
50 static int IrtExceptionHandler(NaClExceptionHandler handler, | |
51 NaClExceptionHandler* old_handler) { | |
52 if (old_handler) | |
53 *old_handler = signal_handler_function_pointer; | |
54 signal_handler_function_pointer = handler; | |
55 return 0; | |
56 } | |
57 | |
58 static int IrtExceptionStack(void* stack, size_t size) { | |
59 // TODO(uekawa): I think we shouldn't implement this until we really | |
Mark Seaborn
2014/04/24 23:39:27
Can you state the TODO a bit more formally, please
Junichi Uekawa
2014/04/25 01:06:10
Done.
| |
60 // need it. IrtThreadCreate allocates sigaltstack already, and | |
Mark Seaborn
2014/04/24 23:39:27
BTW, this part doesn't reflect the code currently.
Junichi Uekawa
2014/04/25 01:06:10
Done.
| |
61 // there is no legitimate reason I can think of that you would want | |
62 // to reallocate an altstack. Note that | |
63 // native_client/src/tests/exception_test/exception_crash_test.c | |
64 // wants to use this but we are not running that test yet. Instead | |
65 // of returning an error, ignore and succeed. | |
66 return 0; | |
67 } | |
68 | |
69 static int IrtExceptionClearFlag(void) { | |
70 // TODO(uekawa): I think we shouldn't implement this until we really | |
Mark Seaborn
2014/04/24 23:39:27
Ditto -- can you state it a bit more formally? i.
Junichi Uekawa
2014/04/25 01:06:10
Done.
| |
71 // need it. | |
72 return -ENOSYS; | |
73 } | |
74 | |
75 } // namespace | |
76 | |
77 const struct nacl_irt_exception_handling kIrtExceptionHandling = { | |
78 IrtExceptionHandler, | |
79 IrtExceptionStack, | |
80 IrtExceptionClearFlag, | |
81 }; | |
82 | |
83 void InitializeSignalHandler() { | |
84 struct sigaction sa; | |
85 unsigned int a; | |
86 | |
87 memset(&sa, 0, sizeof(sa)); | |
88 sigemptyset(&sa.sa_mask); | |
89 sa.sa_sigaction = SignalCatch; | |
90 sa.sa_flags = SA_ONSTACK | SA_SIGINFO; | |
91 | |
92 // Mask all signals we catch to prevent re-entry. | |
93 for (a = 0; a < NACL_ARRAY_SIZE(kSignals); a++) { | |
94 sigaddset(&sa.sa_mask, kSignals[a]); | |
95 } | |
96 | |
97 // Install all handlers. | |
98 for (a = 0; a < NACL_ARRAY_SIZE(kSignals); a++) { | |
99 if (sigaction(kSignals[a], &sa, NULL) != 0) | |
100 NaClLog(LOG_FATAL, "sigaction to register signals failed."); | |
101 } | |
102 } | |
103 | |
104 } // namespace nonsfi | |
105 } // namespace nacl | |
OLD | NEW |