Skip to content

Commit ce0c41c

Browse files
[rtsan][NFC] Move assertions and diagnostics into own impl files (#109500)
In preparation for providing more information to rtsan's diagnostics output (via `__rtsan_expect_not_realtime`), this PR separates out all logic for i) making rtsan's assertions about real-time context state and ii) displaying diagnostics to the user - disentangling them both from the rtsan `Context`. We'll follow up this PR with a simplification to the unit tests that reflect this new separation.
1 parent 008999e commit ce0c41c

File tree

9 files changed

+95
-79
lines changed

9 files changed

+95
-79
lines changed

compiler-rt/lib/rtsan/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ include_directories(..)
22

33
set(RTSAN_CXX_SOURCES
44
rtsan.cpp
5+
rtsan_assertions.cpp
56
rtsan_context.cpp
7+
rtsan_diagnostics.cpp
68
rtsan_flags.cpp
7-
rtsan_stack.cpp
89
rtsan_interceptors.cpp)
910

1011
set(RTSAN_PREINIT_SOURCES
1112
rtsan_preinit.cpp)
1213

1314
set(RTSAN_HEADERS
1415
rtsan.h
16+
rtsan_assertions.h
1517
rtsan_context.h
18+
rtsan_diagnostics.h
1619
rtsan_flags.h
17-
rtsan_flags.inc
18-
rtsan_stack.h
19-
)
20+
rtsan_flags.inc)
2021

2122
set(RTSAN_DEPS)
2223

compiler-rt/lib/rtsan/rtsan.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include <rtsan/rtsan.h>
12-
#include <rtsan/rtsan_context.h>
12+
#include <rtsan/rtsan_assertions.h>
1313
#include <rtsan/rtsan_flags.h>
1414
#include <rtsan/rtsan_interceptors.h>
1515

@@ -77,5 +77,4 @@ __rtsan_expect_not_realtime(const char *intercepted_function_name) {
7777
__rtsan_ensure_initialized();
7878
ExpectNotRealtime(GetContextForThisThread(), intercepted_function_name);
7979
}
80-
8180
} // extern "C"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===--- rtsan_assertions.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+
#include "rtsan/rtsan_assertions.h"
13+
14+
#include "rtsan/rtsan.h"
15+
#include "rtsan/rtsan_diagnostics.h"
16+
17+
#include "sanitizer_common/sanitizer_stacktrace.h"
18+
19+
using namespace __sanitizer;
20+
21+
void __rtsan::ExpectNotRealtime(Context &context,
22+
const char *intercepted_function_name) {
23+
CHECK(__rtsan_is_initialized());
24+
if (context.InRealtimeContext() && !context.IsBypassed()) {
25+
context.BypassPush();
26+
27+
GET_CALLER_PC_BP;
28+
PrintDiagnostics(intercepted_function_name, pc, bp);
29+
Die();
30+
context.BypassPop();
31+
}
32+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
//===--- rtsan_stack.h - Realtime Sanitizer ---------------------*- C++ -*-===//
1+
//===--- rtsan_assertions.h - Realtime Sanitizer ----------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9+
// Part of the RealtimeSanitizer runtime library
10+
//
911
//===----------------------------------------------------------------------===//
1012

1113
#pragma once
1214

13-
#include <sanitizer_common/sanitizer_internal_defs.h>
15+
#include "rtsan/rtsan_context.h"
1416

1517
namespace __rtsan {
16-
void PrintStackTrace(__sanitizer::uptr pc, __sanitizer::uptr bp);
18+
void ExpectNotRealtime(Context &context, const char *intercepted_function_name);
1719
} // namespace __rtsan

compiler-rt/lib/rtsan/rtsan_context.cpp

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,10 @@
1111
#include <rtsan/rtsan.h>
1212
#include <rtsan/rtsan_context.h>
1313

14-
#include <rtsan/rtsan_stack.h>
15-
1614
#include <sanitizer_common/sanitizer_allocator_internal.h>
17-
#include <sanitizer_common/sanitizer_report_decorator.h>
18-
#include <sanitizer_common/sanitizer_stacktrace.h>
1915

2016
#include <new>
2117
#include <pthread.h>
22-
#include <stdio.h>
23-
#include <stdlib.h>
2418

2519
using namespace __sanitizer;
2620

@@ -49,22 +43,6 @@ static __rtsan::Context &GetContextForThisThreadImpl() {
4943
return *current_thread_context;
5044
}
5145

52-
/*
53-
This is a placeholder stub for a future feature that will allow
54-
a user to configure RTSan's behaviour when a real-time safety
55-
violation is detected. The RTSan developers intend for the
56-
following choices to be made available, via a RTSAN_OPTIONS
57-
environment variable, in a future PR:
58-
59-
i) exit,
60-
ii) continue, or
61-
iii) wait for user input from stdin.
62-
63-
Until then, and to keep the first PRs small, only the exit mode
64-
is available.
65-
*/
66-
static void InvokeViolationDetectedAction() { Die(); }
67-
6846
__rtsan::Context::Context() = default;
6947

7048
void __rtsan::Context::RealtimePush() { realtime_depth_++; }
@@ -75,48 +53,10 @@ void __rtsan::Context::BypassPush() { bypass_depth_++; }
7553

7654
void __rtsan::Context::BypassPop() { bypass_depth_--; }
7755

78-
void __rtsan::ExpectNotRealtime(Context &context,
79-
const char *intercepted_function_name) {
80-
CHECK(__rtsan_is_initialized());
81-
if (context.InRealtimeContext() && !context.IsBypassed()) {
82-
context.BypassPush();
83-
84-
GET_CALLER_PC_BP;
85-
PrintDiagnostics(intercepted_function_name, pc, bp);
86-
InvokeViolationDetectedAction();
87-
context.BypassPop();
88-
}
89-
}
90-
9156
bool __rtsan::Context::InRealtimeContext() const { return realtime_depth_ > 0; }
9257

9358
bool __rtsan::Context::IsBypassed() const { return bypass_depth_ > 0; }
9459

95-
namespace {
96-
class Decorator : public __sanitizer::SanitizerCommonDecorator {
97-
public:
98-
Decorator() : SanitizerCommonDecorator() {}
99-
const char *FunctionName() { return Green(); }
100-
const char *Reason() { return Blue(); }
101-
};
102-
} // namespace
103-
104-
void __rtsan::PrintDiagnostics(const char *intercepted_function_name, uptr pc,
105-
uptr bp) {
106-
ScopedErrorReportLock l;
107-
108-
Decorator d;
109-
Printf("%s", d.Error());
110-
Report("ERROR: RealtimeSanitizer: unsafe-library-call\n");
111-
Printf("%s", d.Reason());
112-
Printf("Intercepted call to real-time unsafe function "
113-
"`%s%s%s` in real-time context!\n",
114-
d.FunctionName(), intercepted_function_name, d.Reason());
115-
116-
Printf("%s", d.Default());
117-
__rtsan::PrintStackTrace(pc, bp);
118-
}
119-
12060
__rtsan::Context &__rtsan::GetContextForThisThread() {
12161
return GetContextForThisThreadImpl();
12262
}

compiler-rt/lib/rtsan/rtsan_context.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,4 @@ class Context {
3838
};
3939

4040
Context &GetContextForThisThread();
41-
42-
void ExpectNotRealtime(Context &context, const char *intercepted_function_name);
43-
void PrintDiagnostics(const char *intercepted_function_name,
44-
__sanitizer::uptr pc, __sanitizer::uptr bp);
45-
4641
} // namespace __rtsan

compiler-rt/lib/rtsan/rtsan_stack.cpp renamed to compiler-rt/lib/rtsan/rtsan_diagnostics.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- rtsan_stack.cpp - Realtime Sanitizer -------------------*- C++ -*-===//
1+
//===--- rtsan_diagnostics.cpp - Realtime Sanitizer -------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -8,10 +8,11 @@
88
//
99
//===----------------------------------------------------------------------===//
1010

11-
#include "rtsan_stack.h"
11+
#include "rtsan/rtsan_diagnostics.h"
1212

13-
#include <sanitizer_common/sanitizer_flags.h>
14-
#include <sanitizer_common/sanitizer_stacktrace.h>
13+
#include "sanitizer_common/sanitizer_flags.h"
14+
#include "sanitizer_common/sanitizer_report_decorator.h"
15+
#include "sanitizer_common/sanitizer_stacktrace.h"
1516

1617
using namespace __sanitizer;
1718
using namespace __rtsan;
@@ -29,9 +30,34 @@ void BufferedStackTrace::UnwindImpl(uptr pc, uptr bp, void *context,
2930
}
3031
} // namespace __sanitizer
3132

32-
void __rtsan::PrintStackTrace(uptr pc, uptr bp) {
33+
namespace {
34+
class Decorator : public __sanitizer::SanitizerCommonDecorator {
35+
public:
36+
Decorator() : SanitizerCommonDecorator() {}
37+
const char *FunctionName() { return Green(); }
38+
const char *Reason() { return Blue(); }
39+
};
40+
} // namespace
41+
42+
static void PrintStackTrace(uptr pc, uptr bp) {
3343
BufferedStackTrace stack{};
3444

3545
stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal);
3646
stack.Print();
3747
}
48+
49+
void __rtsan::PrintDiagnostics(const char *intercepted_function_name, uptr pc,
50+
uptr bp) {
51+
ScopedErrorReportLock l;
52+
53+
Decorator d;
54+
Printf("%s", d.Error());
55+
Report("ERROR: RealtimeSanitizer: unsafe-library-call\n");
56+
Printf("%s", d.Reason());
57+
Printf("Intercepted call to real-time unsafe function "
58+
"`%s%s%s` in real-time context!\n",
59+
d.FunctionName(), intercepted_function_name, d.Reason());
60+
61+
Printf("%s", d.Default());
62+
PrintStackTrace(pc, bp);
63+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===--- rtsan_diagnostics.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+
#include "sanitizer_common/sanitizer_internal_defs.h"
16+
17+
namespace __rtsan {
18+
void PrintDiagnostics(const char *intercepted_function_name,
19+
__sanitizer::uptr pc, __sanitizer::uptr bp);
20+
} // namespace __rtsan

compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "rtsan_test_utilities.h"
1212

1313
#include "rtsan/rtsan.h"
14+
#include "rtsan/rtsan_assertions.h"
1415
#include "rtsan/rtsan_context.h"
1516

1617
#include <gtest/gtest.h>

0 commit comments

Comments
 (0)