Path: blob/main/contrib/llvm-project/compiler-rt/lib/rtsan/rtsan_diagnostics.cpp
213766 views
//===--- rtsan_diagnostics.cpp - Realtime Sanitizer -------------*- C++ -*-===//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//===----------------------------------------------------------------------===//910#include "rtsan/rtsan_diagnostics.h"1112#include "sanitizer_common/sanitizer_flags.h"13#include "sanitizer_common/sanitizer_report_decorator.h"14#include "sanitizer_common/sanitizer_stacktrace.h"1516using namespace __sanitizer;17using namespace __rtsan;1819// 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,23bool request_fast, u32 max_depth) {24uptr top = 0;25uptr bottom = 0;26GetThreadStackTopAndBottom(false, &top, &bottom);27bool fast = StackTrace::WillUseFastUnwind(request_fast);28Unwind(max_depth, pc, bp, context, top, bottom, fast);29}30} // namespace __sanitizer3132namespace {33class Decorator : public SanitizerCommonDecorator {34public:35Decorator() : SanitizerCommonDecorator() {}36const char *FunctionName() const { return Green(); }37const char *Reason() const { return Blue(); }38};39} // namespace4041static const char *GetErrorTypeStr(const DiagnosticsInfo &info) {42switch (info.type) {43case DiagnosticsInfoType::InterceptedCall:44return "unsafe-library-call";45case DiagnosticsInfoType::BlockingCall:46return "blocking-call";47}48CHECK(false);49return "(unknown error)";50}5152static void PrintError(const Decorator &decorator,53const DiagnosticsInfo &info) {5455Printf("%s", decorator.Error());56Report("ERROR: RealtimeSanitizer: %s\n", GetErrorTypeStr(info));57}5859static void PrintReason(const Decorator &decorator,60const DiagnosticsInfo &info) {61Printf("%s", decorator.Reason());6263switch (info.type) {64case DiagnosticsInfoType::InterceptedCall: {65Printf("Intercepted call to real-time unsafe function "66"`%s%s%s` in real-time context!",67decorator.FunctionName(), info.func_name, decorator.Reason());68break;69}70case DiagnosticsInfoType::BlockingCall: {71Printf("Call to blocking function "72"`%s%s%s` in real-time context!",73decorator.FunctionName(), info.func_name, decorator.Reason());74break;75}76}7778Printf("\n");79}8081void __rtsan::PrintDiagnostics(const DiagnosticsInfo &info) {82ScopedErrorReportLock::CheckLocked();8384Decorator d;85PrintError(d, info);86PrintReason(d, info);87Printf("%s", d.Default());88}8990void __rtsan::PrintErrorSummary(const DiagnosticsInfo &info,91const BufferedStackTrace &stack) {92ScopedErrorReportLock::CheckLocked();93ReportErrorSummary(GetErrorTypeStr(info), &stack);94}959697