diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h --- a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h @@ -393,6 +393,9 @@ } #endif +#define CONCAT2(x, y) x##y +#define CONCAT(x, y) CONCAT2(x, y) + #define HANDLE_EINTR(res, f) \ { \ int rverrno; \ diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.h b/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.h --- a/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.h @@ -229,10 +229,11 @@ uptr local_stack; \ uptr sp = (uptr)&local_stack -#define GET_CURRENT_PC() \ - ({ \ - this_pc: \ - StackTrace::GetNextInstructionPc((uptr) && this_pc); \ +#define GET_CURRENT_PC() \ + ({ \ + CONCAT(this_pc, __LINE__) \ + : StackTrace::GetNextInstructionPc((uptr) && \ + CONCAT(this_pc, __LINE__)); \ }) #endif // SANITIZER_STACKTRACE_H diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cpp --- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cpp +++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cpp @@ -271,6 +271,22 @@ EXPECT_EQ(bp, stack.top_frame_bp); } +TEST(SlowUnwindTest, GET_CURRENT_PC) { + BufferedStackTrace stack; + uptr pc = GET_CURRENT_PC(); + uptr bp = GET_CURRENT_FRAME(); + stack.Unwind(pc, bp, nullptr, false, /*max_depth=*/0); + EXPECT_EQ(0U, stack.size); + EXPECT_EQ(0U, stack.top_frame_bp); + // Ensure that GET_CURRENT_PC() macro can be used + // multiple times in a single function. + pc = GET_CURRENT_PC(); + stack.Unwind(pc, bp, nullptr, false, /*max_depth=*/1); + EXPECT_EQ(1U, stack.size); + EXPECT_EQ(pc, stack.trace[0]); + EXPECT_EQ(bp, stack.top_frame_bp); +} + // Dummy implementation. This should never be called, but is required to link // non-optimized builds of this test. void BufferedStackTrace::UnwindImpl(uptr pc, uptr bp, void *context,