1 //===-- ubsan_monitor.cpp ---------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Hooks which allow a monitor process to inspect UBSan's diagnostics.
11 //===----------------------------------------------------------------------===//
13 #include "ubsan_monitor.h"
15 using namespace __ubsan
;
17 UndefinedBehaviorReport::UndefinedBehaviorReport(const char *IssueKind
,
19 InternalScopedString
&Msg
)
20 : IssueKind(IssueKind
), Loc(Loc
) {
21 // We have the common sanitizer reporting lock, so it's safe to register a
23 RegisterUndefinedBehaviorReport(this);
25 // Make a copy of the diagnostic.
26 Buffer
.append("%s", Msg
.data());
28 // Let the monitor know that a report is available.
32 static UndefinedBehaviorReport
*CurrentUBR
;
34 void __ubsan::RegisterUndefinedBehaviorReport(UndefinedBehaviorReport
*UBR
) {
38 SANITIZER_WEAK_DEFAULT_IMPL
39 void __ubsan::__ubsan_on_report(void) {}
41 void __ubsan::__ubsan_get_current_report_data(const char **OutIssueKind
,
42 const char **OutMessage
,
43 const char **OutFilename
,
46 char **OutMemoryAddr
) {
47 if (!OutIssueKind
|| !OutMessage
|| !OutFilename
|| !OutLine
|| !OutCol
||
49 UNREACHABLE("Invalid arguments passed to __ubsan_get_current_report_data");
51 InternalScopedString
&Buf
= CurrentUBR
->Buffer
;
53 // Ensure that the first character of the diagnostic text can't start with a
55 char FirstChar
= *Buf
.data();
56 if (FirstChar
>= 'a' && FirstChar
<= 'z')
57 *Buf
.data() += 'A' - 'a';
59 *OutIssueKind
= CurrentUBR
->IssueKind
;
60 *OutMessage
= Buf
.data();
61 if (!CurrentUBR
->Loc
.isSourceLocation()) {
62 *OutFilename
= "<unknown>";
63 *OutLine
= *OutCol
= 0;
65 SourceLocation SL
= CurrentUBR
->Loc
.getSourceLocation();
66 *OutFilename
= SL
.getFilename();
67 *OutLine
= SL
.getLine();
68 *OutCol
= SL
.getColumn();
71 if (CurrentUBR
->Loc
.isMemoryLocation())
72 *OutMemoryAddr
= (char *)CurrentUBR
->Loc
.getMemoryLocation();
74 *OutMemoryAddr
= nullptr;