[compiler-rt] [rtsan] Add exit statistics (PR #109885)
Chris Apple via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 25 11:57:29 PDT 2024
https://github.com/cjappl updated https://github.com/llvm/llvm-project/pull/109885
>From c768fb47e9f945dd08b1237eeabd009726bfbde1 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Fri, 13 Sep 2024 14:38:35 -0600
Subject: [PATCH 1/3] [rtsan] Add exit statistics
---
compiler-rt/lib/rtsan/CMakeLists.txt | 8 ++++--
compiler-rt/lib/rtsan/rtsan.cpp | 12 ++++++++-
compiler-rt/lib/rtsan/rtsan_flags.inc | 1 +
compiler-rt/lib/rtsan/rtsan_stats.cpp | 35 +++++++++++++++++++++++++++
compiler-rt/lib/rtsan/rtsan_stats.h | 21 ++++++++++++++++
compiler-rt/test/rtsan/exit_stats.cpp | 23 ++++++++++++++++++
6 files changed, 97 insertions(+), 3 deletions(-)
create mode 100644 compiler-rt/lib/rtsan/rtsan_stats.cpp
create mode 100644 compiler-rt/lib/rtsan/rtsan_stats.h
create mode 100644 compiler-rt/test/rtsan/exit_stats.cpp
diff --git a/compiler-rt/lib/rtsan/CMakeLists.txt b/compiler-rt/lib/rtsan/CMakeLists.txt
index d4296f56acd30d..a44f4fb2d2884d 100644
--- a/compiler-rt/lib/rtsan/CMakeLists.txt
+++ b/compiler-rt/lib/rtsan/CMakeLists.txt
@@ -5,7 +5,9 @@ set(RTSAN_CXX_SOURCES
rtsan_context.cpp
rtsan_diagnostics.cpp
rtsan_flags.cpp
- rtsan_interceptors.cpp)
+ rtsan_interceptors.cpp
+ rtsan_stats.cpp
+ )
set(RTSAN_PREINIT_SOURCES
rtsan_preinit.cpp)
@@ -16,7 +18,9 @@ set(RTSAN_HEADERS
rtsan_context.h
rtsan_diagnostics.h
rtsan_flags.h
- rtsan_flags.inc)
+ rtsan_flags.inc
+ rtsan_stats.h
+ )
set(RTSAN_DEPS)
diff --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index f02e89421035ca..fc8def408f08b9 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -13,6 +13,7 @@
#include "rtsan/rtsan_diagnostics.h"
#include "rtsan/rtsan_flags.h"
#include "rtsan/rtsan_interceptors.h"
+#include "rtsan/rtsan_stats.h"
#include "sanitizer_common/sanitizer_atomic.h"
#include "sanitizer_common/sanitizer_common.h"
@@ -44,9 +45,15 @@ static InitializationState GetInitializationState() {
atomic_load(&rtsan_initialized, memory_order_acquire));
}
+static void RtsanAtexit() { PrintStatisticsSummary(); }
+
static auto OnViolationAction(DiagnosticsInfo info) {
return [info]() {
- __rtsan::PrintDiagnostics(info);
+ if (flags().print_stats_on_exit)
+ IncrementTotalErrorCount();
+
+ PrintDiagnostics(info);
+
if (flags().halt_on_error)
Die();
};
@@ -62,6 +69,9 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init() {
InitializeFlags();
InitializeInterceptors();
+ if (flags().print_stats_on_exit)
+ Atexit(RtsanAtexit);
+
SetInitializationState(InitializationState::Initialized);
}
diff --git a/compiler-rt/lib/rtsan/rtsan_flags.inc b/compiler-rt/lib/rtsan/rtsan_flags.inc
index 25d62cf0a60fb0..1df71127d19d37 100644
--- a/compiler-rt/lib/rtsan/rtsan_flags.inc
+++ b/compiler-rt/lib/rtsan/rtsan_flags.inc
@@ -17,3 +17,4 @@
// See COMMON_FLAG in sanitizer_flags.inc for more details.
RTSAN_FLAG(bool, halt_on_error, true, "Exit after first reported error.")
+RTSAN_FLAG(bool, print_stats_on_exit, false, "Print stats on exit.")
diff --git a/compiler-rt/lib/rtsan/rtsan_stats.cpp b/compiler-rt/lib/rtsan/rtsan_stats.cpp
new file mode 100644
index 00000000000000..7c1ccf2876f081
--- /dev/null
+++ b/compiler-rt/lib/rtsan/rtsan_stats.cpp
@@ -0,0 +1,35 @@
+//===--- rtsan_stats.cpp - Realtime Sanitizer -------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Part of the RealtimeSanitizer runtime library
+//
+//===----------------------------------------------------------------------===//
+
+#include "rtsan/rtsan_stats.h"
+
+#include "sanitizer_common/sanitizer_atomic.h"
+#include "sanitizer_common/sanitizer_common.h"
+
+using namespace __sanitizer;
+using namespace __rtsan;
+
+static atomic_uint32_t rtsan_total_error_count{0};
+
+void __rtsan::IncrementTotalErrorCount() {
+ atomic_fetch_add(&rtsan_total_error_count, 1, memory_order_relaxed);
+}
+
+static u32 GetTotalErrorCount() {
+ return atomic_load(&rtsan_total_error_count, memory_order_relaxed);
+}
+
+void __rtsan::PrintStatisticsSummary() {
+ ScopedErrorReportLock l;
+ Printf("RealtimeSanitizer exit stats:\n");
+ Printf(" Total error count: %u\n", GetTotalErrorCount());
+}
diff --git a/compiler-rt/lib/rtsan/rtsan_stats.h b/compiler-rt/lib/rtsan/rtsan_stats.h
new file mode 100644
index 00000000000000..3aa30f6a5db76a
--- /dev/null
+++ b/compiler-rt/lib/rtsan/rtsan_stats.h
@@ -0,0 +1,21 @@
+//===--- rtsan_stats.h - Realtime Sanitizer ---------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Part of the RealtimeSanitizer runtime library
+//
+//===----------------------------------------------------------------------===//
+
+#pragma once
+
+namespace __rtsan {
+
+void IncrementTotalErrorCount();
+
+void PrintStatisticsSummary();
+
+} // namespace __rtsan
diff --git a/compiler-rt/test/rtsan/exit_stats.cpp b/compiler-rt/test/rtsan/exit_stats.cpp
new file mode 100644
index 00000000000000..b46a0fd62bac1a
--- /dev/null
+++ b/compiler-rt/test/rtsan/exit_stats.cpp
@@ -0,0 +1,23 @@
+// RUN: %clangxx -fsanitize=realtime %s -o %t
+// RUN: env RTSAN_OPTIONS="halt_on_error=false,print_stats_on_exit=true" %run %t 2>&1 | FileCheck %s
+
+// UNSUPPORTED: ios
+
+// Intent: Ensure exits stats are printed on exit.
+
+#include <unistd.h>
+
+void violation() [[clang::nonblocking]] {
+ const int kNumViolations = 10;
+ for (int i = 0; i < kNumViolations; i++) {
+ usleep(1);
+ }
+}
+
+int main() {
+ violation();
+ return 0;
+}
+
+// CHECK: RealtimeSanitizer exit stats:
+// CHECK-NEXT: Total error count: 10
>From 310bada151c81c2ce9774316d440fb17f86ac171 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Wed, 25 Sep 2024 06:02:31 -0700
Subject: [PATCH 2/3] [PR] fmayer - Add PrintStatistics Directly
---
compiler-rt/lib/rtsan/rtsan.cpp | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index fc8def408f08b9..079b21cb1767b1 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -45,8 +45,6 @@ static InitializationState GetInitializationState() {
atomic_load(&rtsan_initialized, memory_order_acquire));
}
-static void RtsanAtexit() { PrintStatisticsSummary(); }
-
static auto OnViolationAction(DiagnosticsInfo info) {
return [info]() {
if (flags().print_stats_on_exit)
@@ -70,7 +68,7 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init() {
InitializeInterceptors();
if (flags().print_stats_on_exit)
- Atexit(RtsanAtexit);
+ Atexit(PrintStatisticsSummary);
SetInitializationState(InitializationState::Initialized);
}
>From 6be01312b9bb4807d5dabaf37767a0884fff9f02 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Wed, 25 Sep 2024 11:57:17 -0700
Subject: [PATCH 3/3] [PR] fmayer - always increment stats
---
compiler-rt/lib/rtsan/rtsan.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index 079b21cb1767b1..87c3611935ee5f 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -47,8 +47,7 @@ static InitializationState GetInitializationState() {
static auto OnViolationAction(DiagnosticsInfo info) {
return [info]() {
- if (flags().print_stats_on_exit)
- IncrementTotalErrorCount();
+ IncrementTotalErrorCount();
PrintDiagnostics(info);
More information about the llvm-commits
mailing list