Skip to content

Commit e55c442

Browse files
committed
[sanitizer_common] Rename OnPrint to __sanitizer_on_print.
Summary: https://reviews.llvm.org/D28596 exposed OnPrint in the global namespace, which can cause collisions with user-defined OnPrint() functions. Reviewers: vitalybuka, dvyukov Reviewed By: vitalybuka, dvyukov Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D67987 llvm-svn: 373518
1 parent 925d9d2 commit e55c442

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,21 @@ void SetPrintfAndReportCallback(void (*callback)(const char *)) {
229229
// Can be overriden in frontend.
230230
#if SANITIZER_GO && defined(TSAN_EXTERNAL_HOOKS)
231231
// Implementation must be defined in frontend.
232+
// TODO(morehouse): Remove OnPrint after migrating Go to __sanitizer_on_print.
232233
extern "C" void OnPrint(const char *str);
234+
extern "C" void __sanitizer_on_print(const char *str);
233235
#else
234-
SANITIZER_INTERFACE_WEAK_DEF(void, OnPrint, const char *str) {
236+
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_on_print, const char *str) {
235237
(void)str;
236238
}
237239
#endif
238240

239241
static void CallPrintfAndReportCallback(const char *str) {
242+
#if SANITIZER_GO && defined(TSAN_EXTERNAL_HOOKS)
243+
// TODO(morehouse): Remove OnPrint after migrating Go to __sanitizer_on_print.
240244
OnPrint(str);
245+
#endif
246+
__sanitizer_on_print(str);
241247
if (PrintfAndReportCallback)
242248
PrintfAndReportCallback(str);
243249
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Checks that the __sanitizer_on_print hook gets the exact same sanitizer
2+
// report as what is printed to stderr.
3+
//
4+
// RUN: %clangxx %s -o %t
5+
// RUN: %run %t %t-onprint.txt 2>%t-stderr.txt || true
6+
// RUN: diff %t-onprint.txt %t-stderr.txt
7+
8+
#include <cassert>
9+
#include <cstdio>
10+
#include <cstdlib>
11+
12+
FILE *f;
13+
volatile void *buf;
14+
volatile char sink;
15+
16+
extern "C" void __sanitizer_on_print(const char *str) {
17+
fprintf(f, "%s", str);
18+
fflush(f);
19+
}
20+
21+
int main(int argc, char *argv[]) {
22+
assert(argc >= 2);
23+
f = fopen(argv[1], "w");
24+
25+
// Use-after-free to trigger ASan/TSan reports.
26+
void *ptr = malloc(1);
27+
buf = ptr;
28+
free(ptr);
29+
sink = *static_cast<char *>(ptr);
30+
return 0;
31+
}

0 commit comments

Comments
 (0)