Skip to content

[compiler-rt][rtsan] Record pc and bp higher up in the stack #107014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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*}}
}
Loading