Path: blob/main/contrib/llvm-project/compiler-rt/lib/ubsan/ubsan_signals_standalone.cpp
35233 views
//=-- ubsan_signals_standalone.cpp ----------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Installs signal handlers and related interceptors for UBSan standalone.9//10//===----------------------------------------------------------------------===//1112#include "ubsan_platform.h"13#include "sanitizer_common/sanitizer_platform.h"14#if CAN_SANITIZE_UB15#include "interception/interception.h"16#include "sanitizer_common/sanitizer_stacktrace.h"17#include "ubsan_diag.h"18#include "ubsan_init.h"1920// Interception of signals breaks too many things on Android.21// * It requires that ubsan is the first dependency of the main executable for22// the interceptors to work correctly. This complicates deployment, as it23// prevents us from enabling ubsan on random platform modules independently.24// * For this to work with ART VM, ubsan signal handler has to be set after the25// debuggerd handler, but before the ART handler.26// * Interceptors don't work at all when ubsan runtime is loaded late, ex. when27// it is part of an APK that does not use wrap.sh method.28#if SANITIZER_FUCHSIA || SANITIZER_ANDROID2930namespace __ubsan {31void InitializeDeadlySignals() {}32}3334#else3536namespace __ubsan {37void InitializeDeadlySignals();38} // namespace __ubsan3940#define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)41#define SIGNAL_INTERCEPTOR_ENTER() __ubsan::InitializeDeadlySignals()42#include "sanitizer_common/sanitizer_signal_interceptors.inc"4344// TODO(yln): Temporary workaround. Will be removed.45void ubsan_GetStackTrace(BufferedStackTrace *stack, uptr max_depth,46uptr pc, uptr bp, void *context, bool fast);4748namespace __ubsan {4950static void OnStackUnwind(const SignalContext &sig, const void *,51BufferedStackTrace *stack) {52ubsan_GetStackTrace(stack, kStackTraceMax,53StackTrace::GetNextInstructionPc(sig.pc), sig.bp,54sig.context, common_flags()->fast_unwind_on_fatal);55}5657static void UBsanOnDeadlySignal(int signo, void *siginfo, void *context) {58HandleDeadlySignal(siginfo, context, GetTid(), &OnStackUnwind, nullptr);59}6061static bool is_initialized = false;6263void InitializeDeadlySignals() {64if (is_initialized)65return;66is_initialized = true;67InitializeSignalInterceptors();68#if SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION69// REAL(sigaction_symname) is nullptr in a static link. Bail out.70if (!REAL(sigaction_symname))71return;72#endif73InstallDeadlySignalHandlers(&UBsanOnDeadlySignal);74}7576} // namespace __ubsan7778#endif7980#endif // CAN_SANITIZE_UB818283