Files
sdk/runtime/vm/thread_interrupter_android.cc
T
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

102 lines
3.0 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.
#include "platform/globals.h"
#if defined(DART_HOST_OS_ANDROID)
#include <errno.h> // NOLINT
#include <sys/syscall.h> // NOLINT
#include "vm/flags.h"
#include "vm/os.h"
#include "vm/profiler.h"
#include "vm/signal_handler.h"
#include "vm/thread_interrupter.h"
namespace dart {
#ifndef PRODUCT
// Old linux kernels on ARM might require a trampoline to
// work around incorrect Thumb -> ARM transitions.
// See thread_interrupted_android_arm.S for more details.
#if defined(HOST_ARCH_ARM) && \
(defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)) && \
!defined(__thumb__)
#define USE_SIGNAL_HANDLER_TRAMPOLINE
#endif
DECLARE_FLAG(bool, trace_thread_interrupter);
namespace {
#if defined(USE_SIGNAL_HANDLER_TRAMPOLINE)
extern "C" {
#endif
void ThreadInterruptSignalHandler(int signal, siginfo_t* info, void* context_) {
if (signal != SIGPROF) {
return;
}
Thread* thread = Thread::Current();
if (thread == nullptr) {
return;
}
ThreadInterruptScope signal_handler_scope;
// Extract thread state.
ucontext_t* context = reinterpret_cast<ucontext_t*>(context_);
mcontext_t mcontext = context->uc_mcontext;
InterruptedThreadState its;
its.pc = SignalHandler::GetProgramCounter(mcontext);
its.fp = SignalHandler::GetFramePointer(mcontext);
its.csp = SignalHandler::GetCStackPointer(mcontext);
its.dsp = SignalHandler::GetDartStackPointer(mcontext);
its.lr = SignalHandler::GetLinkRegister(mcontext);
Profiler::SampleThread(thread, its);
}
#if defined(USE_SIGNAL_HANDLER_TRAMPOLINE)
} // extern "C"
#endif
} // namespace
void ThreadInterrupter::InterruptThread(OSThread* thread) {
if (FLAG_trace_thread_interrupter) {
OS::PrintErr("ThreadInterrupter interrupting %p\n",
reinterpret_cast<void*>(thread->id()));
}
int result = syscall(__NR_tgkill, getpid(), thread->id(), SIGPROF);
ASSERT((result == 0) || (result == ESRCH));
}
#if defined(USE_SIGNAL_HANDLER_TRAMPOLINE)
// Defined in thread_interrupted_android_arm.S
extern "C" void ThreadInterruptSignalHandlerTrampoline(int signal,
siginfo_t* info,
void* context_);
#endif
void ThreadInterrupter::InstallSignalHandler() {
#if defined(USE_SIGNAL_HANDLER_TRAMPOLINE)
SignalHandler::Install(&ThreadInterruptSignalHandlerTrampoline);
#else
SignalHandler::Install(&ThreadInterruptSignalHandler);
#endif
}
void ThreadInterrupter::RemoveSignalHandler() {
SignalHandler::Remove();
}
void* ThreadInterrupter::PrepareCurrentThread() {
return SignalHandler::PrepareCurrentThread();
}
void ThreadInterrupter::CleanupCurrentThreadState(void* state) {
SignalHandler::CleanupCurrentThreadState(state);
}
#endif // !PRODUCT
} // namespace dart
#endif // defined(DART_HOST_OS_ANDROID)