Skip to content

[rtsan] Add exit statistics #109885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler-rt/lib/rtsan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
9 changes: 8 additions & 1 deletion compiler-rt/lib/rtsan/rtsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -46,7 +47,10 @@ static InitializationState GetInitializationState() {

static auto OnViolationAction(DiagnosticsInfo info) {
return [info]() {
__rtsan::PrintDiagnostics(info);
IncrementTotalErrorCount();

PrintDiagnostics(info);

if (flags().halt_on_error)
Die();
};
Expand All @@ -62,6 +66,9 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init() {
InitializeFlags();
InitializeInterceptors();

if (flags().print_stats_on_exit)
Atexit(PrintStatisticsSummary);

SetInitializationState(InitializationState::Initialized);
}

Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/rtsan/rtsan_flags.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
35 changes: 35 additions & 0 deletions compiler-rt/lib/rtsan/rtsan_stats.cpp
Original file line number Diff line number Diff line change
@@ -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());
}
21 changes: 21 additions & 0 deletions compiler-rt/lib/rtsan/rtsan_stats.h
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions compiler-rt/test/rtsan/exit_stats.cpp
Original file line number Diff line number Diff line change
@@ -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
Loading