Skip to content

Commit c8c7adb

Browse files
yfeldblummeta-codesync[bot]
authored andcommitted
Make signal stack-trace test TSAN-compatible
Summary: [🤖 Metamate] Make `StackTraceTest.Signal` signal-safe so it can run under TSAN rather than report a runtime skip or be compiled out. The handler captures only `getStackTraceSafe()` into fixed storage and publishes its count with a statically verified lock-free atomic. Preserve `errno` in the handler and restore the previous signal action after the test. Perform symbolization and assertions after `raise()` returns, checking errno preservation and that the captured trace includes the handler and interrupted caller in order. `Simple` continues comparing all three stack-trace APIs outside signal context. ___ landed-with-radar-review Differential Revision: D127472550 fbshipit-source-id: da7c1eb3b5bdc5bdf8fd6a12c88492aa6a8747c4
1 parent 73433ee commit c8c7adb

1 file changed

Lines changed: 57 additions & 19 deletions

File tree

‎folly/debugging/symbolizer/test/StackTraceTest.cpp‎

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include <algorithm>
1718
#include <array>
1819
#include <atomic>
20+
#include <cerrno>
21+
#include <csignal>
1922
#include <cstdint>
2023
#include <cstring>
2124
#include <limits>
@@ -651,31 +654,66 @@ void foo2() {
651654
verifyStackTraces();
652655
}
653656

654-
volatile bool handled = false;
655-
void handler(int /* num */, siginfo_t* /* info */, void* /* ctx */) {
656-
// Yes, getStackTrace and VLOG aren't async-signal-safe, but signals
657-
// raised with raise() aren't "async" signals.
657+
TEST(StackTraceTest, Simple) {
658658
foo1();
659-
handled = true;
660659
}
661660

662-
TEST(StackTraceTest, Simple) {
663-
foo1();
661+
namespace {
662+
663+
// NOLINTNEXTLINE
664+
std::array<uintptr_t, 100> signalAddresses{};
665+
std::atomic<ssize_t> signalFrameCount{-1};
666+
static_assert(decltype(signalFrameCount)::is_always_lock_free);
667+
668+
FOLLY_NOINLINE void captureSignalStackTrace(int) {
669+
const int savedErrno = errno;
670+
signalFrameCount.store(
671+
getStackTraceSafe(signalAddresses.data(), signalAddresses.size()),
672+
std::memory_order_release);
673+
errno = savedErrno;
674+
}
675+
676+
FOLLY_NOINLINE int raiseSignalForStackTrace() {
677+
const int result = raise(SIGUSR1);
678+
compiler_must_not_elide(result);
679+
return result;
664680
}
665681

682+
} // namespace
683+
666684
TEST(StackTraceTest, Signal) {
667-
if (folly::kIsSanitizeThread) {
668-
// TSAN doesn't like signal-unsafe functions in a signal handler regardless
669-
// of how the signal is raised. So skip the test in that case.
670-
SKIP() << "Not supported for TSAN";
671-
}
672-
struct sigaction sa;
673-
memset(&sa, 0, sizeof(sa));
674-
sa.sa_sigaction = handler;
675-
sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
676-
CHECK_ERR(sigaction(SIGUSR1, &sa, nullptr));
677-
raise(SIGUSR1);
678-
EXPECT_TRUE(handled);
685+
struct sigaction sa{};
686+
sa.sa_handler = captureSignalStackTrace;
687+
sigemptyset(&sa.sa_mask);
688+
struct sigaction previous{};
689+
ASSERT_EQ(sigaction(SIGUSR1, &sa, &previous), 0);
690+
SCOPE_EXIT {
691+
CHECK_ERR(sigaction(SIGUSR1, &previous, nullptr));
692+
};
693+
694+
signalFrameCount.store(-1, std::memory_order_relaxed);
695+
errno = EDOM;
696+
const int result = raiseSignalForStackTrace();
697+
const int signalErrno = errno;
698+
ASSERT_EQ(result, 0);
699+
EXPECT_EQ(signalErrno, EDOM);
700+
const auto count = signalFrameCount.load(std::memory_order_acquire);
701+
ASSERT_GT(count, 0);
702+
ASSERT_LE(count, static_cast<ssize_t>(signalAddresses.size()));
703+
704+
FrameArray<signalAddresses.size()> frames;
705+
frames.frameCount = static_cast<size_t>(count);
706+
std::copy_n(signalAddresses.begin(), frames.frameCount, frames.addresses);
707+
Symbolizer symbolizer;
708+
symbolizer.symbolize(frames);
709+
StringSymbolizePrinter printer;
710+
printer.println(frames);
711+
const auto stackTrace = printer.str();
712+
const auto handler = stackTrace.find("captureSignalStackTrace");
713+
const auto caller = stackTrace.find("raiseSignalForStackTrace");
714+
ASSERT_NE(handler, std::string::npos) << stackTrace;
715+
ASSERT_NE(caller, std::string::npos) << stackTrace;
716+
EXPECT_LT(handler, caller) << stackTrace;
679717
}
680718

681719
ssize_t read_all(int fd, uint8_t* buffer, size_t size) {

0 commit comments

Comments
 (0)