1//===--- rtsan_stack.cpp - Realtime Sanitizer -------------------*- C++ -*-===//
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//===----------------------------------------------------------------------===//
10
11#include "rtsan_stack.h"
12
13#include <sanitizer_common/sanitizer_flags.h>
14#include <sanitizer_common/sanitizer_stacktrace.h>
15
16using namespace __sanitizer;
17using namespace __rtsan;
18
19// We must define our own implementation of this method for our runtime.
20// This one is just copied from UBSan.
21namespace __sanitizer {
22void BufferedStackTrace::UnwindImpl(uptr pc, uptr bp, void *context,
23 bool request_fast, u32 max_depth) {
24 uptr top = 0;
25 uptr bottom = 0;
26 GetThreadStackTopAndBottom(at_initialization: false, stack_top: &top, stack_bottom: &bottom);
27 bool fast = StackTrace::WillUseFastUnwind(request_fast_unwind: request_fast);
28 Unwind(max_depth, pc, bp, context, stack_top: top, stack_bottom: bottom, request_fast_unwind: fast);
29}
30} // namespace __sanitizer
31
32static void SetGlobalStackTraceFormat() {
33 SetCommonFlagsDefaults();
34 CommonFlags cf;
35 cf.CopyFrom(other: *common_flags());
36 cf.stack_trace_format = "DEFAULT";
37 cf.external_symbolizer_path = GetEnv(name: "RTSAN_SYMBOLIZER_PATH");
38 OverrideCommonFlags(cf);
39}
40
41void __rtsan::PrintStackTrace() {
42
43 BufferedStackTrace stack{};
44
45 GET_CURRENT_PC_BP;
46 stack.Unwind(pc, bp, context: nullptr, request_fast: common_flags()->fast_unwind_on_fatal);
47
48 SetGlobalStackTraceFormat();
49 stack.Print();
50}
51