Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
58 changes: 25 additions & 33 deletions compiler-rt/lib/rtsan/rtsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,33 @@ static InitializationState GetInitializationState() {
atomic_load(&rtsan_initialized, memory_order_acquire));
}

static auto OnViolationAction(DiagnosticsInfo info) {
return [info]() {
IncrementTotalErrorCount();
static auto OnViolationAction(const BufferedStackTrace &stack,
const DiagnosticsInfo &info) {
IncrementTotalErrorCount();

BufferedStackTrace stack;
// If in the future we interop with other sanitizers, we will
// need to make our own stackdepot
StackDepotHandle handle = StackDepotPut_WithHandle(stack);

// We use the unwind_on_fatal flag here because of precedent with other
// sanitizers, this action is not necessarily fatal if halt_on_error=false
stack.Unwind(info.pc, info.bp, nullptr,
common_flags()->fast_unwind_on_fatal);
const bool is_stack_novel = handle.use_count() == 0;

// If in the future we interop with other sanitizers, we will
// need to make our own stackdepot
StackDepotHandle handle = StackDepotPut_WithHandle(stack);
// Marked UNLIKELY as if user is runing with halt_on_error=false
// we expect a high number of duplicate stacks. We are willing
// To pay for the first insertion.
if (UNLIKELY(is_stack_novel)) {
IncrementUniqueErrorCount();

const bool is_stack_novel = handle.use_count() == 0;
PrintDiagnostics(info);
stack.Print();

// Marked UNLIKELY as if user is runing with halt_on_error=false
// we expect a high number of duplicate stacks. We are willing
// To pay for the first insertion.
if (UNLIKELY(is_stack_novel)) {
IncrementUniqueErrorCount();
handle.inc_use_count_unsafe();
}

PrintDiagnostics(info);
stack.Print();

handle.inc_use_count_unsafe();
}

if (flags().halt_on_error) {
if (flags().print_stats_on_exit)
PrintStatisticsSummary();
Die();
}
};
if (flags().halt_on_error) {
if (flags().print_stats_on_exit)
PrintStatisticsSummary();
Die();
}
}

extern "C" {
Expand Down Expand Up @@ -141,17 +133,17 @@ __rtsan_notify_intercepted_call(const char *func_name) {
__rtsan_ensure_initialized();
GET_CALLER_PC_BP;
ExpectNotRealtime(GetContextForThisThread(),
OnViolationAction({DiagnosticsInfoType::InterceptedCall,
func_name, pc, bp}));
{DiagnosticsInfoType::InterceptedCall, func_name, pc, bp},
OnViolationAction);
}

SANITIZER_INTERFACE_ATTRIBUTE void
__rtsan_notify_blocking_call(const char *func_name) {
__rtsan_ensure_initialized();
GET_CALLER_PC_BP;
ExpectNotRealtime(GetContextForThisThread(),
OnViolationAction({DiagnosticsInfoType::BlockingCall,
func_name, pc, bp}));
{DiagnosticsInfoType::BlockingCall, func_name, pc, bp},
OnViolationAction);
}

} // extern "C"
16 changes: 14 additions & 2 deletions compiler-rt/lib/rtsan/rtsan_assertions.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,27 @@

#include "rtsan/rtsan.h"
#include "rtsan/rtsan_context.h"
#include "rtsan/rtsan_diagnostics.h"

#include "sanitizer_common/sanitizer_stacktrace.h"

namespace __rtsan {

template <typename OnViolationAction>
void ExpectNotRealtime(Context &context, OnViolationAction &&OnViolation) {
void ExpectNotRealtime(Context &context, const DiagnosticsInfo &info,
OnViolationAction &&OnViolation) {
CHECK(__rtsan_is_initialized());
if (context.InRealtimeContext() && !context.IsBypassed()) {
ScopedBypass sb{context};
OnViolation();

__sanitizer::BufferedStackTrace stack;

// We use the unwind_on_fatal flag here because of precedent with other
// sanitizers, this action is not necessarily fatal if halt_on_error=false
stack.Unwind(info.pc, info.bp, nullptr,
__sanitizer::common_flags()->fast_unwind_on_fatal);

OnViolation(stack, info);
}
}

Expand Down
10 changes: 8 additions & 2 deletions compiler-rt/lib/rtsan/tests/rtsan_test_assertions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

#include "rtsan/rtsan_assertions.h"

#include "sanitizer_common/sanitizer_stacktrace.h"

#include <gmock/gmock.h>

using namespace __sanitizer;
using namespace __rtsan;

class TestRtsanAssertions : public ::testing::Test {
Expand All @@ -25,9 +28,12 @@ class TestRtsanAssertions : public ::testing::Test {

static void ExpectViolationAction(Context &context,
bool expect_violation_callback) {
::testing::MockFunction<void()> mock_on_violation;
::testing::MockFunction<void(const BufferedStackTrace &stack,
const DiagnosticsInfo &info)>
mock_on_violation;
EXPECT_CALL(mock_on_violation, Call).Times(expect_violation_callback ? 1 : 0);
ExpectNotRealtime(context, mock_on_violation.AsStdFunction());
DiagnosticsInfo info{};
ExpectNotRealtime(context, info, mock_on_violation.AsStdFunction());
}

TEST_F(TestRtsanAssertions,
Expand Down
Loading