Skip to content

Commit a424b79

Browse files
authored
[compiler-rt][rtsan] Record pc and bp higher up in the stack (llvm#107014)
Functionally, this change affects only our printed stack traces. New version does not expose any internal rtsan interworking
1 parent ce8ec31 commit a424b79

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

compiler-rt/lib/rtsan/rtsan_context.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <stdio.h>
2121
#include <stdlib.h>
2222

23+
using namespace __sanitizer;
24+
2325
static pthread_key_t context_key;
2426
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
2527

@@ -75,7 +77,9 @@ void __rtsan::ExpectNotRealtime(Context &context,
7577
const char *intercepted_function_name) {
7678
if (context.InRealtimeContext() && !context.IsBypassed()) {
7779
context.BypassPush();
78-
PrintDiagnostics(intercepted_function_name);
80+
81+
GET_CALLER_PC_BP;
82+
PrintDiagnostics(intercepted_function_name, pc, bp);
7983
InvokeViolationDetectedAction();
8084
context.BypassPop();
8185
}
@@ -85,12 +89,13 @@ bool __rtsan::Context::InRealtimeContext() const { return realtime_depth_ > 0; }
8589

8690
bool __rtsan::Context::IsBypassed() const { return bypass_depth_ > 0; }
8791

88-
void __rtsan::PrintDiagnostics(const char *intercepted_function_name) {
92+
void __rtsan::PrintDiagnostics(const char *intercepted_function_name, uptr pc,
93+
uptr bp) {
8994
fprintf(stderr,
9095
"Real-time violation: intercepted call to real-time unsafe function "
9196
"`%s` in real-time context! Stack trace:\n",
9297
intercepted_function_name);
93-
__rtsan::PrintStackTrace();
98+
__rtsan::PrintStackTrace(pc, bp);
9499
}
95100

96101
__rtsan::Context &__rtsan::GetContextForThisThread() {

compiler-rt/lib/rtsan/rtsan_context.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#pragma once
1212

13+
#include <sanitizer_common/sanitizer_internal_defs.h>
14+
1315
namespace __rtsan {
1416

1517
class Context {
@@ -38,6 +40,7 @@ class Context {
3840
Context &GetContextForThisThread();
3941

4042
void ExpectNotRealtime(Context &context, const char *intercepted_function_name);
41-
void PrintDiagnostics(const char *intercepted_function_name);
43+
void PrintDiagnostics(const char *intercepted_function_name,
44+
__sanitizer::uptr pc, __sanitizer::uptr bp);
4245

4346
} // namespace __rtsan

compiler-rt/lib/rtsan/rtsan_stack.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ static void SetGlobalStackTraceFormat() {
3838
OverrideCommonFlags(cf);
3939
}
4040

41-
void __rtsan::PrintStackTrace() {
41+
void __rtsan::PrintStackTrace(uptr pc, uptr bp) {
4242

4343
BufferedStackTrace stack{};
4444

45-
GET_CURRENT_PC_BP;
4645
stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal);
4746

4847
SetGlobalStackTraceFormat();

compiler-rt/lib/rtsan/rtsan_stack.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#pragma once
1212

13+
#include <sanitizer_common/sanitizer_internal_defs.h>
14+
1315
namespace __rtsan {
14-
void PrintStackTrace();
16+
void PrintStackTrace(__sanitizer::uptr pc, __sanitizer::uptr bp);
1517
} // namespace __rtsan

compiler-rt/test/rtsan/basic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ void violation() [[clang::nonblocking]] {
1616
int main() {
1717
violation();
1818
return 0;
19-
// CHECK: {{.*Real-time violation.*}}
20-
// CHECK: {{.*malloc*}}
19+
// CHECK: Real-time violation: intercepted call to real-time unsafe function `malloc` in real-time context! Stack trace:
20+
// CHECK-NEXT: {{.*malloc*}}
2121
}

0 commit comments

Comments
 (0)