Skip to content

Commit 9ef9acb

Browse files
authored
[rtsan] Introduce halt_on_error flag (llvm#109832)
1 parent b15bd3f commit 9ef9acb

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

compiler-rt/lib/rtsan/rtsan.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ static InitializationState GetInitializationState() {
4444
atomic_load(&rtsan_initialized, memory_order_acquire));
4545
}
4646

47-
static auto PrintDiagnosticsAndDieAction(DiagnosticsInfo info) {
47+
static auto OnViolationAction(DiagnosticsInfo info) {
4848
return [info]() {
4949
__rtsan::PrintDiagnostics(info);
50-
Die();
50+
if (flags().halt_on_error)
51+
Die();
5152
};
5253
}
5354

@@ -105,20 +106,18 @@ __rtsan_notify_intercepted_call(const char *func_name) {
105106

106107
__rtsan_ensure_initialized();
107108
GET_CALLER_PC_BP;
108-
ExpectNotRealtime(
109-
GetContextForThisThread(),
110-
PrintDiagnosticsAndDieAction(
111-
{DiagnosticsInfoType::InterceptedCall, func_name, pc, bp}));
109+
ExpectNotRealtime(GetContextForThisThread(),
110+
OnViolationAction({DiagnosticsInfoType::InterceptedCall,
111+
func_name, pc, bp}));
112112
}
113113

114114
SANITIZER_INTERFACE_ATTRIBUTE void
115115
__rtsan_notify_blocking_call(const char *func_name) {
116116
__rtsan_ensure_initialized();
117117
GET_CALLER_PC_BP;
118-
ExpectNotRealtime(
119-
GetContextForThisThread(),
120-
PrintDiagnosticsAndDieAction(
121-
{DiagnosticsInfoType::BlockingCall, func_name, pc, bp}));
118+
ExpectNotRealtime(GetContextForThisThread(),
119+
OnViolationAction({DiagnosticsInfoType::BlockingCall,
120+
func_name, pc, bp}));
122121
}
123122

124123
} // extern "C"

compiler-rt/lib/rtsan/rtsan_flags.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@
1616
// RTSAN_FLAG(Type, Name, DefaultValue, Description)
1717
// See COMMON_FLAG in sanitizer_flags.inc for more details.
1818

19-
// Example flag, until we get a real one
20-
// RTSAN_FLAG(bool, halt_on_error, true, "If true, halt the program on error")
19+
RTSAN_FLAG(bool, halt_on_error, true, "Exit after first reported error.")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clangxx -fsanitize=realtime %s -o %t
2+
// RUN: %env_rtsan_opts="halt_on_error=true" not %run %t 2>&1 | FileCheck %s
3+
// RUN: %env_rtsan_opts="halt_on_error=false" %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-NO-HALT,CHECK
4+
// UNSUPPORTED: ios
5+
6+
// Intent: Ensure that halt_on_error does not exit on the first violation.
7+
8+
#include <stdlib.h>
9+
10+
void *MallocViolation() { return malloc(10); }
11+
12+
void FreeViolation(void *Ptr) { free(Ptr); }
13+
14+
void process() [[clang::nonblocking]] {
15+
void *Ptr = MallocViolation();
16+
FreeViolation(Ptr);
17+
}
18+
19+
int main() {
20+
process();
21+
return 0;
22+
// CHECK: ==ERROR: RealtimeSanitizer
23+
// CHECK-NEXT: {{.*`malloc`.*}}
24+
// CHECK-NO-HALT: ==ERROR: RealtimeSanitizer
25+
// CHECK-NO-HALT-NEXT: {{.*`free`.*}}
26+
}

0 commit comments

Comments
 (0)