ubsan_signals_standalone.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //=-- ubsan_signals_standalone.cpp ----------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Installs signal handlers and related interceptors for UBSan standalone.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ubsan_platform.h"
  13. #include "sanitizer_common/sanitizer_platform.h"
  14. #if CAN_SANITIZE_UB
  15. #include "interception/interception.h"
  16. #include "sanitizer_common/sanitizer_stacktrace.h"
  17. #include "ubsan_diag.h"
  18. #include "ubsan_init.h"
  19. // Interception of signals breaks too many things on Android.
  20. // * It requires that ubsan is the first dependency of the main executable for
  21. // the interceptors to work correctly. This complicates deployment, as it
  22. // prevents us from enabling ubsan on random platform modules independently.
  23. // * For this to work with ART VM, ubsan signal handler has to be set after the
  24. // debuggerd handler, but before the ART handler.
  25. // * Interceptors don't work at all when ubsan runtime is loaded late, ex. when
  26. // it is part of an APK that does not use wrap.sh method.
  27. #if SANITIZER_FUCHSIA || SANITIZER_ANDROID
  28. namespace __ubsan {
  29. void InitializeDeadlySignals() {}
  30. }
  31. #else
  32. #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
  33. #include "sanitizer_common/sanitizer_signal_interceptors.inc"
  34. // TODO(yln): Temporary workaround. Will be removed.
  35. void ubsan_GetStackTrace(BufferedStackTrace *stack, uptr max_depth,
  36. uptr pc, uptr bp, void *context, bool fast);
  37. namespace __ubsan {
  38. static void OnStackUnwind(const SignalContext &sig, const void *,
  39. BufferedStackTrace *stack) {
  40. ubsan_GetStackTrace(stack, kStackTraceMax,
  41. StackTrace::GetNextInstructionPc(sig.pc), sig.bp,
  42. sig.context, common_flags()->fast_unwind_on_fatal);
  43. }
  44. static void UBsanOnDeadlySignal(int signo, void *siginfo, void *context) {
  45. HandleDeadlySignal(siginfo, context, GetTid(), &OnStackUnwind, nullptr);
  46. }
  47. static bool is_initialized = false;
  48. void InitializeDeadlySignals() {
  49. if (is_initialized)
  50. return;
  51. is_initialized = true;
  52. InitializeSignalInterceptors();
  53. InstallDeadlySignalHandlers(&UBsanOnDeadlySignal);
  54. }
  55. } // namespace __ubsan
  56. #endif
  57. #endif // CAN_SANITIZE_UB