Skip to content

Commit c3334da

Browse files
authored
[rtsan] Add exit statistics (#109885)
adds the flag `print_stats_on_exit` which mirrors nsan's same flag. # Why? Not only is this nice for the end users, this gives us a very trivial way to test deduplication which is next up Currently the style is something like: ``` RealtimeSanitizer exit stats: Total error count: 488 ```
1 parent 0f52193 commit c3334da

File tree

6 files changed

+94
-3
lines changed

6 files changed

+94
-3
lines changed

compiler-rt/lib/rtsan/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ set(RTSAN_CXX_SOURCES
55
rtsan_context.cpp
66
rtsan_diagnostics.cpp
77
rtsan_flags.cpp
8-
rtsan_interceptors.cpp)
8+
rtsan_interceptors.cpp
9+
rtsan_stats.cpp
10+
)
911

1012
set(RTSAN_PREINIT_SOURCES
1113
rtsan_preinit.cpp)
@@ -16,7 +18,9 @@ set(RTSAN_HEADERS
1618
rtsan_context.h
1719
rtsan_diagnostics.h
1820
rtsan_flags.h
19-
rtsan_flags.inc)
21+
rtsan_flags.inc
22+
rtsan_stats.h
23+
)
2024

2125
set(RTSAN_DEPS)
2226

compiler-rt/lib/rtsan/rtsan.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "rtsan/rtsan_diagnostics.h"
1414
#include "rtsan/rtsan_flags.h"
1515
#include "rtsan/rtsan_interceptors.h"
16+
#include "rtsan/rtsan_stats.h"
1617

1718
#include "sanitizer_common/sanitizer_atomic.h"
1819
#include "sanitizer_common/sanitizer_common.h"
@@ -46,7 +47,10 @@ static InitializationState GetInitializationState() {
4647

4748
static auto OnViolationAction(DiagnosticsInfo info) {
4849
return [info]() {
49-
__rtsan::PrintDiagnostics(info);
50+
IncrementTotalErrorCount();
51+
52+
PrintDiagnostics(info);
53+
5054
if (flags().halt_on_error)
5155
Die();
5256
};
@@ -62,6 +66,9 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init() {
6266
InitializeFlags();
6367
InitializeInterceptors();
6468

69+
if (flags().print_stats_on_exit)
70+
Atexit(PrintStatisticsSummary);
71+
6572
SetInitializationState(InitializationState::Initialized);
6673
}
6774

compiler-rt/lib/rtsan/rtsan_flags.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
// See COMMON_FLAG in sanitizer_flags.inc for more details.
1818

1919
RTSAN_FLAG(bool, halt_on_error, true, "Exit after first reported error.")
20+
RTSAN_FLAG(bool, print_stats_on_exit, false, "Print stats on exit.")

compiler-rt/lib/rtsan/rtsan_stats.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- rtsan_stats.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+
// Part of the RealtimeSanitizer runtime library
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "rtsan/rtsan_stats.h"
14+
15+
#include "sanitizer_common/sanitizer_atomic.h"
16+
#include "sanitizer_common/sanitizer_common.h"
17+
18+
using namespace __sanitizer;
19+
using namespace __rtsan;
20+
21+
static atomic_uint32_t rtsan_total_error_count{0};
22+
23+
void __rtsan::IncrementTotalErrorCount() {
24+
atomic_fetch_add(&rtsan_total_error_count, 1, memory_order_relaxed);
25+
}
26+
27+
static u32 GetTotalErrorCount() {
28+
return atomic_load(&rtsan_total_error_count, memory_order_relaxed);
29+
}
30+
31+
void __rtsan::PrintStatisticsSummary() {
32+
ScopedErrorReportLock l;
33+
Printf("RealtimeSanitizer exit stats:\n");
34+
Printf(" Total error count: %u\n", GetTotalErrorCount());
35+
}

compiler-rt/lib/rtsan/rtsan_stats.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===--- rtsan_stats.h - 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+
// Part of the RealtimeSanitizer runtime library
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#pragma once
14+
15+
namespace __rtsan {
16+
17+
void IncrementTotalErrorCount();
18+
19+
void PrintStatisticsSummary();
20+
21+
} // namespace __rtsan

compiler-rt/test/rtsan/exit_stats.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clangxx -fsanitize=realtime %s -o %t
2+
// RUN: env RTSAN_OPTIONS="halt_on_error=false,print_stats_on_exit=true" %run %t 2>&1 | FileCheck %s
3+
4+
// UNSUPPORTED: ios
5+
6+
// Intent: Ensure exits stats are printed on exit.
7+
8+
#include <unistd.h>
9+
10+
void violation() [[clang::nonblocking]] {
11+
const int kNumViolations = 10;
12+
for (int i = 0; i < kNumViolations; i++) {
13+
usleep(1);
14+
}
15+
}
16+
17+
int main() {
18+
violation();
19+
return 0;
20+
}
21+
22+
// CHECK: RealtimeSanitizer exit stats:
23+
// CHECK-NEXT: Total error count: 10

0 commit comments

Comments
 (0)