// setjmp/longjmp do save/restore the signal mask on bionic, but not on glibc. // This is a BSD versus System V historical accident. POSIX leaves the // behavior unspecified, so any code that cares needs to use sigsetjmp.
SigSets ss;
sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
jmp_buf jb; if (setjmp(jb) == 0) {
sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
longjmp(jb, 1);
FAIL(); // Unreachable.
} else { #ifdefined(__BIONIC__) // bionic behaves like BSD and does save/restore the signal mask.
AssertSigmaskEquals(ss.one); #else // glibc behaves like System V and doesn't save/restore the signal mask.
AssertSigmaskEquals(ss.two); #endif
}
}
// sigsetjmp(1)/siglongjmp does save/restore the signal mask.
SigSets ss;
sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
sigjmp_buf sjb; if (sigsetjmp(sjb, 1) == 0) {
sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
siglongjmp(sjb, 1);
FAIL(); // Unreachable.
} else {
AssertSigmaskEquals(ss.one);
}
}
#ifdefined(__arm__)
// arm callee saves: r4-r11, d8-d15
// In practice, like x86, we've seen clang clobber the integer callee saves and break this test. // Since 32-bit is deprecated (to the extent that we can't run 32-bit code on current devices), // we just test the easy floating point callee saves.
// x86 has so few registers it's basically impossible to do anything before // they get clobbered. // (x86 has no floating point callee saves so there's nothing to check there.) #define SET_REGS() #define CLEAR_REGS() #define CHECK_REGS()
TEST(setjmp, setjmp_registers) {
[[maybe_unused]] long regs[32];
[[maybe_unused]] double fregs[32];
jmp_buf jb;
SET_REGS(); int value = setjmp(jb); if (value == 0) { // We got here from the original setjmp() call.
CLEAR_REGS();
longjmp(jb, 123);
FAIL(); // Unreachable.
} else { // We got here from the longjmp() call.
CHECK_REGS();
ASSERT_EQ(123, value);
}
}
// Corrupt the cookie.
raw_words[JB_SIGFLAG_OFFSET] = 0xfeedface; // Recompute the checksum. // This assumes that the checksum is the last non-reserved word, // and that we don't checksum the reserved words, // which is true for all our architectures. long cs = 0; for (size_t i = 0; i < JB_CHECKSUM_OFFSET; i++) {
cs ^= raw_words[i];
}
raw_words[JB_CHECKSUM_OFFSET] = cs;
EXPECT_EXIT(longjmp(jb, 0), testing::KilledBySignal(SIGABRT), "setjmp cookie mismatch"); #else // TODO: we can't make this EXPECT_EXIT() because the implementations are wrong! // TODO: we need to fix longjmp() to check the cookie _before_ corrupting sp + pc!
EXPECT_DEATH(longjmp(jb, 0), ""); #endif #else
GTEST_SKIP() << "bionic-only test"; #endif
}
TEST_F(setjmp_DeathTest, setjmp_checksum) { #ifdefined(__BIONIC__)
jmp_buf jb; int value = setjmp(jb);
ASSERT_EQ(0, value);
// Flip a bit. reinterpret_cast<long*>(jb)[1] ^= 1;
TEST(setjmp, setjmp_stack) {
jmp_buf buf; int value = setjmp(buf); if (value == 0) call_longjmp(buf);
EXPECT_EQ(123, value);
}
TEST(setjmp, bug_152210274) { // Ensure that we never have a mangled value in the stack pointer. #ifdefined(__BIONIC__) struct sigaction sa = {.sa_flags = SA_SIGINFO, .sa_sigaction = [](int, siginfo_t*, void*) {}};
ASSERT_EQ(0, sigaction(SIGPROF, &sa, 0));
constexpr size_t kNumThreads = 20;
// Start a bunch of threads calling setjmp/longjmp. auto jumper = [](void* arg) -> void* {
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGPROF);
pthread_sigmask(SIG_UNBLOCK, &set, nullptr);
jmp_buf buf; for (size_t count = 0; count < 100000; ++count) { if (setjmp(buf) != 0) {
perror("setjmp");
abort();
} // This will never be true, but the compiler doesn't know that, so the // setjmp won't be removed by DCE. With HWASan/MTE this also acts as a // kind of enforcement that the threads are done before leaving the test. if (*static_cast<size_t*>(arg) != 123) longjmp(buf, 1);
} return nullptr;
};
pthread_t threads[kNumThreads];
pid_t tids[kNumThreads] = {};
size_t var = 123; for (size_t i = 0; i < kNumThreads; ++i) {
ASSERT_EQ(0, pthread_create(&threads[i], nullptr, jumper, &var));
tids[i] = pthread_gettid_np(threads[i]);
}
// Start the interrupter thread. auto interrupter = [](void* arg) -> void* {
pid_t* tids = static_cast<pid_t*>(arg); for (size_t count = 0; count < 1000; ++count) { for (size_t i = 0; i < kNumThreads; i++) { if (tgkill(getpid(), tids[i], SIGPROF) == -1 && errno != ESRCH) {
perror("tgkill failed");
abort();
}
}
usleep(100);
} return nullptr;
};
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, nullptr, interrupter, tids));
pthread_join(t, nullptr); for (size_t i = 0; i < kNumThreads; i++) {
pthread_join(threads[i], nullptr);
} #else
GTEST_SKIP() << "tests uses functions not in glibc"; #endif
}
#ifdefined(__aarch64__) // Call sigsetjmp and verify SME ZA state staticvoid sigsetjmp_helper() {
sigjmp_buf jb;
sigsetjmp(jb, 0); bool za_state = sme_is_za_on();
sme_disable_za(); // Turn ZA off anyway.
ASSERT_FALSE(za_state);
}
// Call siglongjmp and verify SME ZA state staticvoid siglongjmp_helper() { int value;
sigjmp_buf jb; if ((value = sigsetjmp(jb, 0)) == 0) {
siglongjmp(jb, 789);
sme_disable_za();
FAIL(); // Unreachable.
} else { bool za_state = sme_is_za_on();
sme_disable_za(); // Turn ZA off anyway.
ASSERT_EQ(789, value);
ASSERT_FALSE(za_state);
}
}
TEST(setjmp, sigsetjmp_sme) { if (!sme_is_enabled()) {
GTEST_SKIP() << "SME is not enabled on device.";
}
__arm_za_disable();
sme_dormant_caller(&sigsetjmp_helper);
}
TEST(setjmp, siglongjmp_sme) { if (!sme_is_enabled()) {
GTEST_SKIP() << "SME is not enabled on device.";
}
__arm_za_disable();
sme_dormant_caller(&siglongjmp_helper);
} #endif
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet am 2026-06-28)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.