Path: blob/main/contrib/llvm-project/compiler-rt/lib/ubsan/ubsan_monitor.cpp
35233 views
//===-- ubsan_monitor.cpp ---------------------------------------*- 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// Hooks which allow a monitor process to inspect UBSan's diagnostics.9//10//===----------------------------------------------------------------------===//1112#include "ubsan_monitor.h"1314using namespace __ubsan;1516UndefinedBehaviorReport::UndefinedBehaviorReport(const char *IssueKind,17Location &Loc,18InternalScopedString &Msg)19: IssueKind(IssueKind), Loc(Loc) {20// We have the common sanitizer reporting lock, so it's safe to register a21// new UB report.22RegisterUndefinedBehaviorReport(this);2324// Make a copy of the diagnostic.25if (Msg.length())26Buffer.Append(Msg.data());2728// Let the monitor know that a report is available.29__ubsan_on_report();30}3132static UndefinedBehaviorReport *CurrentUBR;3334void __ubsan::RegisterUndefinedBehaviorReport(UndefinedBehaviorReport *UBR) {35CurrentUBR = UBR;36}3738SANITIZER_WEAK_DEFAULT_IMPL39void __ubsan::__ubsan_on_report(void) {}4041void __ubsan::__ubsan_get_current_report_data(const char **OutIssueKind,42const char **OutMessage,43const char **OutFilename,44unsigned *OutLine,45unsigned *OutCol,46char **OutMemoryAddr) {47if (!OutIssueKind || !OutMessage || !OutFilename || !OutLine || !OutCol ||48!OutMemoryAddr)49UNREACHABLE("Invalid arguments passed to __ubsan_get_current_report_data");5051InternalScopedString &Buf = CurrentUBR->Buffer;5253// Ensure that the first character of the diagnostic text can't start with a54// lowercase letter.55char FirstChar = *Buf.data();56if (FirstChar >= 'a' && FirstChar <= 'z')57*Buf.data() += 'A' - 'a';5859*OutIssueKind = CurrentUBR->IssueKind;60*OutMessage = Buf.data();61if (!CurrentUBR->Loc.isSourceLocation()) {62*OutFilename = "<unknown>";63*OutLine = *OutCol = 0;64} else {65SourceLocation SL = CurrentUBR->Loc.getSourceLocation();66*OutFilename = SL.getFilename();67*OutLine = SL.getLine();68*OutCol = SL.getColumn();69}7071if (CurrentUBR->Loc.isMemoryLocation())72*OutMemoryAddr = (char *)CurrentUBR->Loc.getMemoryLocation();73else74*OutMemoryAddr = nullptr;75}767778