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..707cd9180eaa4364861fdc6ba6548b77f352599e |
| --- /dev/null |
| +++ b/components/nacl/loader/nonsfi/irt_exception_handling.cc |
| @@ -0,0 +1,108 @@ |
| +// 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> |
|
Mark Seaborn
2014/04/23 03:49:50
Nit: add empty line between boilerplate and first
Junichi Uekawa
2014/04/23 07:56:15
Done.
|
| +#include <signal.h> |
| + |
| +#include <map> |
|
Mark Seaborn
2014/04/23 03:49:50
Not used?
Junichi Uekawa
2014/04/23 07:56:15
Done.
|
| + |
| +#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 |
| + |
| +// TODO(uekawa): The list of signals to be handled might need updating. |
|
Mark Seaborn
2014/04/23 03:49:50
It should be OK (so you can remove this TODO). An
Junichi Uekawa
2014/04/23 07:56:15
Done.
|
| +// NonSFI NaCl does not use NACL_THREAD_SUSPEND_SIGNAL (==SIGUSR1) (??check??) |
|
Mark Seaborn
2014/04/23 03:49:50
Correct. That's used for thread suspension, curre
Junichi Uekawa
2014/04/23 07:56:15
I've re-wrote the comment with the context.
|
| +// 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) { |
|
Mark Seaborn
2014/04/23 03:49:50
Nit: use Chromium-style "* " spacing
Junichi Uekawa
2014/04/23 07:56:15
Done.
|
| + if (signal_handler_function_pointer) { |
| + // TODO(uekawa): Whether to add dependency or copy the implementation? |
|
Mark Seaborn
2014/04/23 03:49:50
Adding the dependency isn't ideal but it's OK for
Junichi Uekawa
2014/04/23 07:56:15
I've pondered of not adding a dependency; it intro
|
| + NaClSignalContext signal_context; |
| + NaClSignalContextFromHandler(&signal_context, uc); |
| + // Is this safe to allocate this on stack ? |
|
Mark Seaborn
2014/04/23 03:49:50
Yes
Junichi Uekawa
2014/04/23 07:56:15
thanks
|
| + NaClExceptionFrame exception_frame; |
| + NaClSignalSetUpExceptionFrame(&exception_frame, |
| + &signal_context, |
| + 0 /* context_user_addr, what is this? */); |
|
Mark Seaborn
2014/04/23 03:49:50
This parameter doesn't apply if we're calling user
Junichi Uekawa
2014/04/23 07:56:15
I think we don't need this for nonsfi NaCl, but pr
|
| + |
| + signal_handler_function_pointer(&exception_frame.context); |
| + } |
| + // TODO(uekawa): Only exit on crash signals? |
|
Mark Seaborn
2014/04/23 03:49:50
All the signals you're handling are crash signals
Junichi Uekawa
2014/04/23 07:56:15
Removed the comment.
|
| + _exit(-1); |
| +} |
| + |
| +static int IrtExceptionHandler(NaClExceptionHandler handler, |
| + NaClExceptionHandler *old_handler) { |
| + // TODO(uekawa): Do I need to have a mutex lock? |
|
Mark Seaborn
2014/04/23 03:49:50
You could use AtomicExchange from base/atomicops.h
Junichi Uekawa
2014/04/23 07:56:15
after staring at AtomicExchange, I don't think thi
Mark Seaborn
2014/04/24 23:39:27
But setting the new handler and getting the old on
Junichi Uekawa
2014/04/25 01:06:09
Done.
|
| + 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; |
|
Mark Seaborn
2014/04/23 03:49:50
This could return 0, since it's mostly-OK as a no-
Junichi Uekawa
2014/04/23 07:56:15
Done.
|
| +} |
| + |
| +static int IrtExceptionClearFlag(void) { |
| + // TODO(uekawa): I think we shouldn't implement this until we really |
| + // need it. |
| + return -EINVAL; |
|
Mark Seaborn
2014/04/23 03:49:50
ENOSYS would be more appropriate
Junichi Uekawa
2014/04/23 07:56:15
Done.
|
| +} |
| + |
| +} // namespace |
| + |
| +const struct nacl_irt_exception_handling kIrtExceptionHandling = { |
| + IrtExceptionHandler, |
| + IrtExceptionStack, |
| + IrtExceptionClearFlag, |
| +}; |
| + |
| +void InitializeSignalHandler(void) { |
|
Mark Seaborn
2014/04/23 03:49:50
Nit: Should be "()" rather than "(void)" in C++.
Junichi Uekawa
2014/04/23 07:56:15
Done.
|
| + 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"); |
|
Mark Seaborn
2014/04/23 03:49:50
This should be a fatal error, otherwise this would
Junichi Uekawa
2014/04/23 07:56:15
Done.
|
| + } |
| +} |
| + |
| +} // namespace nonsfi |
| +} // namespace nacl |