2014-10-31 09:37:09 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-02-19 02:11:56 +01:00
|
|
|
#include "BionicDeathTest.h"
|
2018-02-07 21:44:45 +01:00
|
|
|
#include "SignalUtils.h"
|
2018-01-31 18:05:26 +01:00
|
|
|
|
2021-02-19 02:11:56 +01:00
|
|
|
using setjmp_DeathTest = BionicDeathTest;
|
|
|
|
|
2014-10-31 09:37:09 +01:00
|
|
|
TEST(setjmp, setjmp_smoke) {
|
|
|
|
int value;
|
|
|
|
jmp_buf jb;
|
|
|
|
if ((value = setjmp(jb)) == 0) {
|
|
|
|
longjmp(jb, 123);
|
|
|
|
FAIL(); // Unreachable.
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(123, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(setjmp, _setjmp_smoke) {
|
|
|
|
int value;
|
|
|
|
jmp_buf jb;
|
|
|
|
if ((value = _setjmp(jb)) == 0) {
|
|
|
|
_longjmp(jb, 456);
|
|
|
|
FAIL(); // Unreachable.
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(456, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(setjmp, sigsetjmp_0_smoke) {
|
|
|
|
int value;
|
|
|
|
sigjmp_buf jb;
|
|
|
|
if ((value = sigsetjmp(jb, 0)) == 0) {
|
|
|
|
siglongjmp(jb, 789);
|
|
|
|
FAIL(); // Unreachable.
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(789, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(setjmp, sigsetjmp_1_smoke) {
|
|
|
|
int value;
|
|
|
|
sigjmp_buf jb;
|
|
|
|
if ((value = sigsetjmp(jb, 0)) == 0) {
|
|
|
|
siglongjmp(jb, 0xabc);
|
|
|
|
FAIL(); // Unreachable.
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(0xabc, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 18:05:26 +01:00
|
|
|
// Two distinct signal sets.
|
2014-12-10 18:31:04 +01:00
|
|
|
struct SigSets {
|
|
|
|
SigSets() : one(MakeSigSet(0)), two(MakeSigSet(1)) {
|
2014-12-06 07:24:49 +01:00
|
|
|
}
|
2014-12-10 18:31:04 +01:00
|
|
|
|
2018-01-31 18:05:26 +01:00
|
|
|
static sigset64_t MakeSigSet(int offset) {
|
|
|
|
sigset64_t ss;
|
|
|
|
sigemptyset64(&ss);
|
|
|
|
sigaddset64(&ss, SIGUSR1 + offset);
|
2018-10-09 02:28:07 +02:00
|
|
|
// TIMER_SIGNAL.
|
|
|
|
sigaddset64(&ss, __SIGRTMIN);
|
2018-01-31 18:05:26 +01:00
|
|
|
sigaddset64(&ss, SIGRTMIN + offset);
|
2014-12-10 18:31:04 +01:00
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
2018-01-31 18:05:26 +01:00
|
|
|
sigset64_t one;
|
|
|
|
sigset64_t two;
|
2014-12-10 18:31:04 +01:00
|
|
|
};
|
2014-10-31 09:37:09 +01:00
|
|
|
|
2018-01-31 18:05:26 +01:00
|
|
|
void AssertSigmaskEquals(const sigset64_t& expected) {
|
|
|
|
sigset64_t actual;
|
2018-08-03 02:31:13 +02:00
|
|
|
sigprocmask64(SIG_SETMASK, nullptr, &actual);
|
2018-01-31 18:05:26 +01:00
|
|
|
size_t end = sizeof(expected) * 8;
|
2014-12-06 07:24:49 +01:00
|
|
|
for (size_t i = 1; i <= end; ++i) {
|
2018-01-31 18:05:26 +01:00
|
|
|
EXPECT_EQ(sigismember64(&expected, i), sigismember64(&actual, i)) << i;
|
2014-12-06 07:24:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-31 09:37:09 +01:00
|
|
|
TEST(setjmp, _setjmp_signal_mask) {
|
2018-01-31 18:05:26 +01:00
|
|
|
SignalMaskRestorer smr;
|
|
|
|
|
2014-10-31 09:37:09 +01:00
|
|
|
// _setjmp/_longjmp do not save/restore the signal mask.
|
2014-12-10 18:31:04 +01:00
|
|
|
SigSets ss;
|
2018-01-31 18:05:26 +01:00
|
|
|
sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
|
2014-10-31 09:37:09 +01:00
|
|
|
jmp_buf jb;
|
|
|
|
if (_setjmp(jb) == 0) {
|
2018-08-03 02:31:13 +02:00
|
|
|
sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
|
2014-10-31 09:37:09 +01:00
|
|
|
_longjmp(jb, 1);
|
|
|
|
FAIL(); // Unreachable.
|
|
|
|
} else {
|
2014-12-10 18:31:04 +01:00
|
|
|
AssertSigmaskEquals(ss.two);
|
2014-10-31 09:37:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(setjmp, setjmp_signal_mask) {
|
2018-01-31 18:05:26 +01:00
|
|
|
SignalMaskRestorer smr;
|
|
|
|
|
2014-10-31 09:37:09 +01:00
|
|
|
// 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.
|
2014-12-10 18:31:04 +01:00
|
|
|
SigSets ss;
|
2018-01-31 18:05:26 +01:00
|
|
|
sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
|
2014-10-31 09:37:09 +01:00
|
|
|
jmp_buf jb;
|
|
|
|
if (setjmp(jb) == 0) {
|
2018-08-03 02:31:13 +02:00
|
|
|
sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
|
2014-10-31 09:37:09 +01:00
|
|
|
longjmp(jb, 1);
|
|
|
|
FAIL(); // Unreachable.
|
|
|
|
} else {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
// bionic behaves like BSD and does save/restore the signal mask.
|
2014-12-10 18:31:04 +01:00
|
|
|
AssertSigmaskEquals(ss.one);
|
2014-10-31 09:37:09 +01:00
|
|
|
#else
|
|
|
|
// glibc behaves like System V and doesn't save/restore the signal mask.
|
2014-12-10 18:31:04 +01:00
|
|
|
AssertSigmaskEquals(ss.two);
|
2014-10-31 09:37:09 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(setjmp, sigsetjmp_0_signal_mask) {
|
2018-01-31 18:05:26 +01:00
|
|
|
SignalMaskRestorer smr;
|
|
|
|
|
2014-10-31 09:37:09 +01:00
|
|
|
// sigsetjmp(0)/siglongjmp do not save/restore the signal mask.
|
2014-12-10 18:31:04 +01:00
|
|
|
SigSets ss;
|
2018-01-31 18:05:26 +01:00
|
|
|
sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
|
2014-10-31 09:37:09 +01:00
|
|
|
sigjmp_buf sjb;
|
|
|
|
if (sigsetjmp(sjb, 0) == 0) {
|
2018-08-03 02:31:13 +02:00
|
|
|
sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
|
2014-10-31 09:37:09 +01:00
|
|
|
siglongjmp(sjb, 1);
|
|
|
|
FAIL(); // Unreachable.
|
|
|
|
} else {
|
2014-12-10 18:31:04 +01:00
|
|
|
AssertSigmaskEquals(ss.two);
|
2014-10-31 09:37:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(setjmp, sigsetjmp_1_signal_mask) {
|
2018-01-31 18:05:26 +01:00
|
|
|
SignalMaskRestorer smr;
|
|
|
|
|
2014-10-31 09:37:09 +01:00
|
|
|
// sigsetjmp(1)/siglongjmp does save/restore the signal mask.
|
2014-12-10 18:31:04 +01:00
|
|
|
SigSets ss;
|
2018-01-31 18:05:26 +01:00
|
|
|
sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
|
2014-10-31 09:37:09 +01:00
|
|
|
sigjmp_buf sjb;
|
|
|
|
if (sigsetjmp(sjb, 1) == 0) {
|
2018-08-03 02:31:13 +02:00
|
|
|
sigprocmask64(SIG_SETMASK, &ss.two, nullptr);
|
2014-10-31 09:37:09 +01:00
|
|
|
siglongjmp(sjb, 1);
|
|
|
|
FAIL(); // Unreachable.
|
|
|
|
} else {
|
2014-12-10 18:31:04 +01:00
|
|
|
AssertSigmaskEquals(ss.one);
|
2014-10-31 09:37:09 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-27 05:52:34 +01:00
|
|
|
|
|
|
|
#if defined(__aarch64__)
|
|
|
|
#define SET_FREG(n, v) asm volatile("fmov d"#n ", "#v : : : "d"#n)
|
|
|
|
#define CLEAR_FREG(n) asm volatile("fmov d"#n ", xzr" : : : "d"#n)
|
|
|
|
#define SET_FREGS \
|
|
|
|
SET_FREG(8, 8.0); SET_FREG(9, 9.0); SET_FREG(10, 10.0); SET_FREG(11, 11.0); \
|
|
|
|
SET_FREG(12, 12.0); SET_FREG(13, 13.0); SET_FREG(14, 14.0); SET_FREG(15, 15.0);
|
|
|
|
#define CLEAR_FREGS \
|
|
|
|
CLEAR_FREG(8); CLEAR_FREG(9); CLEAR_FREG(10); CLEAR_FREG(11); \
|
|
|
|
CLEAR_FREG(12); CLEAR_FREG(13); CLEAR_FREG(14); CLEAR_FREG(15);
|
|
|
|
#define GET_FREG(n) ({ double _r; asm volatile("fmov %0, d"#n : "=r"(_r) : :); _r; })
|
|
|
|
#define CHECK_FREGS \
|
|
|
|
EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
|
|
|
|
EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
|
|
|
|
EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
|
|
|
|
EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
|
|
|
|
#elif defined(__arm__)
|
|
|
|
#define SET_FREG(n, v) \
|
|
|
|
({ const double _v{v}; asm volatile("fcpyd d"#n ", %P0" : : "w"(_v) : "d"#n); })
|
|
|
|
#define SET_FREGS \
|
|
|
|
SET_FREG(8, 8); SET_FREG(9, 9); SET_FREG(10, 10); SET_FREG(11, 11); \
|
|
|
|
SET_FREG(12, 12); SET_FREG(13, 13); SET_FREG(14, 14); SET_FREG(15, 15);
|
|
|
|
#define CLEAR_FREGS \
|
|
|
|
SET_FREG(8, 0); SET_FREG(9, 0); SET_FREG(10, 0); SET_FREG(11, 0); \
|
|
|
|
SET_FREG(12, 0); SET_FREG(13, 0); SET_FREG(14, 0); SET_FREG(15, 0);
|
|
|
|
#define GET_FREG(n) ({ double _r; asm volatile("fcpyd %P0, d"#n : "=w"(_r) : :); _r;})
|
|
|
|
#define CHECK_FREGS \
|
|
|
|
EXPECT_EQ(8.0, GET_FREG(8)); EXPECT_EQ(9.0, GET_FREG(9)); \
|
|
|
|
EXPECT_EQ(10.0, GET_FREG(10)); EXPECT_EQ(11.0, GET_FREG(11)); \
|
|
|
|
EXPECT_EQ(12.0, GET_FREG(12)); EXPECT_EQ(13.0, GET_FREG(13)); \
|
|
|
|
EXPECT_EQ(14.0, GET_FREG(14)); EXPECT_EQ(15.0, GET_FREG(15));
|
|
|
|
#else
|
|
|
|
/* The other architectures don't save/restore fp registers. */
|
|
|
|
#define SET_FREGS
|
|
|
|
#define CLEAR_FREGS
|
|
|
|
#define CHECK_FREGS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST(setjmp, setjmp_fp_registers) {
|
|
|
|
int value;
|
|
|
|
jmp_buf jb;
|
|
|
|
SET_FREGS;
|
|
|
|
if ((value = setjmp(jb)) == 0) {
|
|
|
|
CLEAR_FREGS;
|
|
|
|
longjmp(jb, 123);
|
|
|
|
FAIL(); // Unreachable.
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(123, value);
|
|
|
|
CHECK_FREGS;
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 00:40:24 +02:00
|
|
|
|
|
|
|
#if defined(__arm__)
|
|
|
|
#define __JB_SIGFLAG 0
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
#define __JB_SIGFLAG 0
|
|
|
|
#elif defined(__i386__)
|
2018-01-31 18:05:26 +01:00
|
|
|
#define __JB_SIGFLAG 8
|
2015-09-11 00:40:24 +02:00
|
|
|
#elif defined(__x86_64)
|
|
|
|
#define __JB_SIGFLAG 8
|
|
|
|
#endif
|
|
|
|
|
2021-02-19 02:11:56 +01:00
|
|
|
TEST_F(setjmp_DeathTest, setjmp_cookie) {
|
2015-09-11 00:40:24 +02:00
|
|
|
jmp_buf jb;
|
|
|
|
int value = setjmp(jb);
|
|
|
|
ASSERT_EQ(0, value);
|
|
|
|
|
|
|
|
long* sigflag = reinterpret_cast<long*>(jb) + __JB_SIGFLAG;
|
|
|
|
|
|
|
|
// Make sure there's actually a cookie.
|
|
|
|
EXPECT_NE(0, *sigflag & ~1);
|
|
|
|
|
|
|
|
// Wipe it out
|
|
|
|
*sigflag &= 1;
|
|
|
|
EXPECT_DEATH(longjmp(jb, 0), "");
|
|
|
|
}
|
2016-03-03 04:03:17 +01:00
|
|
|
|
2021-02-19 02:11:56 +01:00
|
|
|
TEST_F(setjmp_DeathTest, setjmp_cookie_checksum) {
|
2016-03-03 04:03:17 +01:00
|
|
|
jmp_buf jb;
|
|
|
|
int value = setjmp(jb);
|
|
|
|
|
|
|
|
if (value == 0) {
|
|
|
|
// Flip a bit.
|
2016-03-16 15:49:12 +01:00
|
|
|
reinterpret_cast<long*>(jb)[1] ^= 1;
|
2016-03-03 04:03:17 +01:00
|
|
|
|
|
|
|
EXPECT_DEATH(longjmp(jb, 1), "checksum mismatch");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "setjmp_cookie_checksum: longjmp succeeded?");
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 21:41:41 +01:00
|
|
|
|
|
|
|
__attribute__((noinline)) void call_longjmp(jmp_buf buf) {
|
|
|
|
longjmp(buf, 123);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(setjmp, setjmp_stack) {
|
|
|
|
jmp_buf buf;
|
|
|
|
int value = setjmp(buf);
|
|
|
|
if (value == 0) call_longjmp(buf);
|
|
|
|
EXPECT_EQ(123, value);
|
|
|
|
}
|