Chromium Code Reviews| 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..cdd33aff3f6164caa9d7a2d0f7d01ba23e80765e |
| --- /dev/null |
| +++ b/components/nacl/loader/nonsfi/irt_exception_handling.cc |
| @@ -0,0 +1,113 @@ |
| +// 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 <map> |
| + |
| +#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 |
|
hidehiko
2014/04/22 00:22:37
nit: // comment style.
Ditto for below.
Junichi Uekawa
2014/04/22 04:57:01
Done.
|
| + * things resides in: |
| + * native_client/src/trusted/service_runtime/linux/nacl_signal.c |
| + * native_client/src/trusted/service_runtime/sys_exception.c |
| + */ |
| + |
| +// TODO(uekawa): The list of signals to be handled might need updating. |
| +// NonSFI NaCl does not use NACL_THREAD_SUSPEND_SIGNAL (==SIGUSR1) (??check??) |
| +// 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 |
| +}; |
| + |
| +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) { |
| + // TODO(uekawa): Whether to add dependency or copy the implementation? |
| + NaClSignalContext signal_context; |
| + NaClSignalContextFromHandler(&signal_context, uc); |
| + // Is this safe to allocate this on stack ? |
| + NaClExceptionFrame exception_frame; |
| + NaClSignalSetUpExceptionFrame(&exception_frame, |
| + &signal_context, |
| + 0 /* context_user_addr, what is this? */); |
| + |
| + signal_handler_function_pointer(&exception_frame.context); |
| + } |
| + // TODO(uekawa): Only exit on crash signals? |
| + _exit(-1); |
| +} |
| + |
| +static int IrtExceptionHandler(NaClExceptionHandler handler, |
| + NaClExceptionHandler *old_handler) { |
| + // TODO(uekawa): Do I need to have a mutex lock? |
| + 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 |
| + // need it. IrtThreadCreate allocates sigaltstack already, and |
| + // 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. |
| + return -EINVAL; |
| +} |
| + |
| +static int IrtExceptionClearFlag(void) { |
| + // TODO(uekawa): I think we shouldn't implement this until we really |
| + // need it. |
| + return -EINVAL; |
| +} |
| + |
| +} // namespace |
| + |
| +const struct nacl_irt_exception_handling kIrtExceptionHandling = { |
| + IrtExceptionHandler, |
| + IrtExceptionStack, |
| + IrtExceptionClearFlag, |
| +}; |
| + |
| +void InitializeSignalHandler(void) { |
| + 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) { |
| + 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
|
| + } |
| + } |
| +} |
| + |
| +} // namespace nonsfi |
| +} // namespace nacl |