Skip to content

Commit

Permalink
[compiler-rt][rtsan] Record pc and bp higher up in the stack (llvm#10…
Browse files Browse the repository at this point in the history
…7014)

Functionally, this change affects only our printed stack traces. New
version does not expose any internal rtsan interworking
  • Loading branch information
cjappl committed Sep 3, 2024
1 parent ce8ec31 commit a424b79
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
11 changes: 8 additions & 3 deletions compiler-rt/lib/rtsan/rtsan_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <stdio.h>
#include <stdlib.h>

using namespace __sanitizer;

static pthread_key_t context_key;
static pthread_once_t key_once = PTHREAD_ONCE_INIT;

Expand Down Expand Up @@ -75,7 +77,9 @@ void __rtsan::ExpectNotRealtime(Context &context,
const char *intercepted_function_name) {
if (context.InRealtimeContext() && !context.IsBypassed()) {
context.BypassPush();
PrintDiagnostics(intercepted_function_name);

GET_CALLER_PC_BP;
PrintDiagnostics(intercepted_function_name, pc, bp);
InvokeViolationDetectedAction();
context.BypassPop();
}
Expand All @@ -85,12 +89,13 @@ bool __rtsan::Context::InRealtimeContext() const { return realtime_depth_ > 0; }

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

void __rtsan::PrintDiagnostics(const char *intercepted_function_name) {
void __rtsan::PrintDiagnostics(const char *intercepted_function_name, uptr pc,
uptr bp) {
fprintf(stderr,
"Real-time violation: intercepted call to real-time unsafe function "
"`%s` in real-time context! Stack trace:\n",
intercepted_function_name);
__rtsan::PrintStackTrace();
__rtsan::PrintStackTrace(pc, bp);
}

__rtsan::Context &__rtsan::GetContextForThisThread() {
Expand Down
5 changes: 4 additions & 1 deletion compiler-rt/lib/rtsan/rtsan_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#pragma once

#include <sanitizer_common/sanitizer_internal_defs.h>

namespace __rtsan {

class Context {
Expand Down Expand Up @@ -38,6 +40,7 @@ class Context {
Context &GetContextForThisThread();

void ExpectNotRealtime(Context &context, const char *intercepted_function_name);
void PrintDiagnostics(const char *intercepted_function_name);
void PrintDiagnostics(const char *intercepted_function_name,
__sanitizer::uptr pc, __sanitizer::uptr bp);

} // namespace __rtsan
3 changes: 1 addition & 2 deletions compiler-rt/lib/rtsan/rtsan_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ static void SetGlobalStackTraceFormat() {
OverrideCommonFlags(cf);
}

void __rtsan::PrintStackTrace() {
void __rtsan::PrintStackTrace(uptr pc, uptr bp) {

BufferedStackTrace stack{};

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

SetGlobalStackTraceFormat();
Expand Down
4 changes: 3 additions & 1 deletion compiler-rt/lib/rtsan/rtsan_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#pragma once

#include <sanitizer_common/sanitizer_internal_defs.h>

namespace __rtsan {
void PrintStackTrace();
void PrintStackTrace(__sanitizer::uptr pc, __sanitizer::uptr bp);
} // namespace __rtsan
4 changes: 2 additions & 2 deletions compiler-rt/test/rtsan/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ void violation() [[clang::nonblocking]] {
int main() {
violation();
return 0;
// CHECK: {{.*Real-time violation.*}}
// CHECK: {{.*malloc*}}
// CHECK: Real-time violation: intercepted call to real-time unsafe function `malloc` in real-time context! Stack trace:
// CHECK-NEXT: {{.*malloc*}}
}

0 comments on commit a424b79

Please sign in to comment.