dart-sdk/runtime/vm/signal_handler.h
Vyacheslav Egorov a013de84e0 [vm] Reserve larger alternative signal stack if needed
Commit 18bdb28ef6 have configured `SIGPROF` handler on Android
to use alternative signal stack to workaround a [bug][1] in Bionic
implementation of `setjmp`. However when implementing the
workaround we have misinterpreted `sigaltstack` documentation
and assumed that `sigaltstack` configures alternative stack
*globally*, similar to how `sigaction` configures the signal handler.
This is not correct: `sigaltstack` configures alternative stack
for the current thread only.

Nevertheless our workaround kinda worked as intended because Bionic's
`pthread_create` actually assigns an individual alternative
stack to each new thread since [Android L][2].

However older version of Bionic (pre [Android 6.0.1][3]) configured
alternative signal stack which was too small for ARM64. This meant
that non-trivial signal handler could easily overflow the stack
and cause a SIGSEGV when hitting a guard page.

This is what we observe in the flutter/flutter#130003: launching
Flutter application with profiler enabled in a ARM64 emulator
running Android 6.0 causes an immediate segfault in the
signal handler.

This CL changes our code to make sure that alternative signal
stack associated with the current thread is present and is large
enough and allocate a new one if it is not.

[1]: b/152210274
[2]: https://android-review.git.corp.google.com/c/platform/bionic/+/62238
[3]: https://android-review.git.corp.google.com/c/platform/bionic/+/172213
[4]: https://github.com/flutter/flutter/issues/130003

TEST=manually on an Android 6.0.0 ARM64 emulator

Cq-Include-Trybots: luci.dart.try:vm-aot-android-release-arm64c-try,vm-ffi-android-release-arm64c-try,vm-ffi-android-release-arm-try
Change-Id: Ib87df25e72ad3f486e90cf2fc6d7f980a8e0b1dc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/327866
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Slava Egorov <vegorov@google.com>
2023-09-27 15:11:05 +00:00

70 lines
2.2 KiB
C++

// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_SIGNAL_HANDLER_H_
#define RUNTIME_VM_SIGNAL_HANDLER_H_
#include "vm/allocation.h"
#include "vm/globals.h"
#if defined(DART_HOST_OS_LINUX)
#include <signal.h> // NOLINT
#include <ucontext.h> // NOLINT
#elif defined(DART_HOST_OS_ANDROID)
#include <signal.h> // NOLINT
#if !defined(__BIONIC_HAVE_UCONTEXT_T)
#include <asm/sigcontext.h> // NOLINT
// If ucontext_t is not defined on Android, define it here.
typedef struct sigcontext mcontext_t;
typedef struct ucontext {
uint32_t uc_flags;
struct ucontext* uc_link;
stack_t uc_stack;
struct sigcontext uc_mcontext;
uint32_t uc_sigmask;
} ucontext_t;
#endif // !defined(__BIONIC_HAVE_UCONTEXT_T)
#elif defined(DART_HOST_OS_MACOS)
#include <signal.h> // NOLINT
#include <sys/ucontext.h> // NOLINT
#elif defined(DART_HOST_OS_WINDOWS)
// Stub out for windows.
struct siginfo_t;
struct mcontext_t;
struct sigset_t {};
#elif defined(DART_HOST_OS_FUCHSIA)
#include <signal.h> // NOLINT
#include <ucontext.h> // NOLINT
#endif
namespace dart {
typedef void (*SignalAction)(int signal, siginfo_t* info, void* context);
class SignalHandler : public AllStatic {
public:
static void Install(SignalAction action);
static void Remove();
static uintptr_t GetProgramCounter(const mcontext_t& mcontext);
static uintptr_t GetFramePointer(const mcontext_t& mcontext);
static uintptr_t GetCStackPointer(const mcontext_t& mcontext);
static uintptr_t GetDartStackPointer(const mcontext_t& mcontext);
static uintptr_t GetLinkRegister(const mcontext_t& mcontext);
#if defined(DART_HOST_OS_ANDROID)
// Prepare current thread for handling interrupts. Returns
// opaque pointer to the allocated state (if any).
static void* PrepareCurrentThread();
// Cleanup any state which was created by |PrepareCurrentThread|.
static void CleanupCurrentThreadState(void* state);
#endif
};
#undef USE_SIGNAL_HANDLER_TRAMPOLINE
} // namespace dart
#endif // RUNTIME_VM_SIGNAL_HANDLER_H_