Index: components/nacl/loader/nonsfi/irt_exception_handling.cc |
diff --git a/components/nacl/loader/nonsfi/irt_exception_handling.cc b/components/nacl/loader/nonsfi/irt_exception_handling.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..143a2874b9aa25fc5d3efb27265e4172ae145191 |
--- /dev/null |
+++ b/components/nacl/loader/nonsfi/irt_exception_handling.cc |
@@ -0,0 +1,105 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <errno.h> |
+#include <signal.h> |
+ |
+#include "base/atomicops.h" |
+#include "components/nacl/loader/nonsfi/irt_interfaces.h" |
+#include "native_client/src/include/nacl_macros.h" |
+#include "native_client/src/trusted/service_runtime/nacl_exception.h" |
+#include "native_client/src/trusted/service_runtime/nacl_signal.h" |
+ |
+namespace nacl { |
+namespace nonsfi { |
+namespace { |
+ |
+// This is NonSFI version of exception handling codebase, NaCl side of |
+// things resides in: |
+// native_client/src/trusted/service_runtime/linux/nacl_signal.c |
+// native_client/src/trusted/service_runtime/sys_exception.c |
+ |
+// Crash signals to handle. The differences from SFI NaCl are that |
+// NonSFI NaCl does not use NACL_THREAD_SUSPEND_SIGNAL (==SIGUSR1), |
+// and SIGSYS is reserved for seccomp-bpf. |
+static const int kSignals[] = { |
+ SIGSTKFLT, |
+ SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGBUS, SIGFPE, SIGSEGV, |
+ // Handle SIGABRT in case someone sends it asynchronously using kill(). |
+ SIGABRT |
+}; |
+ |
+volatile NaClExceptionHandler signal_handler_function_pointer = NULL; |
+ |
+// Signal handler, responsible for calling the registered handlers. |
+static void SignalCatch(int sig, siginfo_t* info, void* uc) { |
+ if (signal_handler_function_pointer) { |
+ NaClSignalContext signal_context; |
+ NaClSignalContextFromHandler(&signal_context, uc); |
+ NaClExceptionFrame exception_frame; |
+ NaClSignalSetUpExceptionFrame(&exception_frame, |
+ &signal_context, |
+ 0 /* context_user_addr, |
+ not useful for NonSFI NaCl. */); |
+ signal_handler_function_pointer(&exception_frame.context); |
+ } |
+ _exit(-1); |
+} |
+ |
+static int IrtExceptionHandler(NaClExceptionHandler handler, |
+ NaClExceptionHandler* old_handler) { |
+ if (old_handler) |
+ *old_handler = signal_handler_function_pointer; |
+ signal_handler_function_pointer = handler; |
+ return 0; |
+} |
+ |
+static int IrtExceptionStack(void* stack, size_t size) { |
+ // 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.
|
+ // 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.
|
+ // there is no legitimate reason I can think of that you would want |
+ // to reallocate an altstack. Note that |
+ // native_client/src/tests/exception_test/exception_crash_test.c |
+ // wants to use this but we are not running that test yet. Instead |
+ // of returning an error, ignore and succeed. |
+ return 0; |
+} |
+ |
+static int IrtExceptionClearFlag(void) { |
+ // 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.
|
+ // need it. |
+ return -ENOSYS; |
+} |
+ |
+} // namespace |
+ |
+const struct nacl_irt_exception_handling kIrtExceptionHandling = { |
+ IrtExceptionHandler, |
+ IrtExceptionStack, |
+ IrtExceptionClearFlag, |
+}; |
+ |
+void InitializeSignalHandler() { |
+ struct sigaction sa; |
+ unsigned int a; |
+ |
+ memset(&sa, 0, sizeof(sa)); |
+ sigemptyset(&sa.sa_mask); |
+ sa.sa_sigaction = SignalCatch; |
+ sa.sa_flags = SA_ONSTACK | SA_SIGINFO; |
+ |
+ // Mask all signals we catch to prevent re-entry. |
+ for (a = 0; a < NACL_ARRAY_SIZE(kSignals); a++) { |
+ sigaddset(&sa.sa_mask, kSignals[a]); |
+ } |
+ |
+ // Install all handlers. |
+ for (a = 0; a < NACL_ARRAY_SIZE(kSignals); a++) { |
+ if (sigaction(kSignals[a], &sa, NULL) != 0) |
+ NaClLog(LOG_FATAL, "sigaction to register signals failed."); |
+ } |
+} |
+ |
+} // namespace nonsfi |
+} // namespace nacl |