Merge "Fix fdsan death tests."

This commit is contained in:
Elliott Hughes 2021-02-20 21:26:29 +00:00 committed by Gerrit Code Review
commit a0d3da5999

View file

@ -27,6 +27,7 @@
#if defined(__BIONIC__) #if defined(__BIONIC__)
#include <android/fdsan.h> #include <android/fdsan.h>
#include <bionic/reserved_signals.h>
#endif #endif
#include <unordered_map> #include <unordered_map>
@ -38,7 +39,7 @@
EXPECT_DEATH((android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL), expression), \ EXPECT_DEATH((android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL), expression), \
(regex)) (regex))
struct FdsanTest : public ::testing::Test { struct fdsan : public ::testing::Test {
void SetUp() override { void SetUp() override {
#if defined(__BIONIC__) #if defined(__BIONIC__)
// The bionic unit test running forks for each test by default, which turns // The bionic unit test running forks for each test by default, which turns
@ -48,28 +49,38 @@ struct FdsanTest : public ::testing::Test {
} }
}; };
TEST_F(FdsanTest, unowned_untagged_close) { struct fdsan_DeathTest : public BionicDeathTest {
#if defined(__BIONIC__)
void SetUp() override {
android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
signal(BIONIC_SIGNAL_DEBUGGER, SIG_DFL); // Disable debuggerd.
BionicDeathTest::SetUp();
}
#endif
};
TEST_F(fdsan, unowned_untagged_close) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
int fd = open("/dev/null", O_RDONLY); int fd = open("/dev/null", O_RDONLY);
ASSERT_EQ(0, close(fd)); ASSERT_EQ(0, close(fd));
#endif #endif
} }
TEST_F(FdsanTest, unowned_tagged_close) { TEST_F(fdsan, unowned_tagged_close) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
int fd = open("/dev/null", O_RDONLY); int fd = open("/dev/null", O_RDONLY);
ASSERT_EQ(0, android_fdsan_close_with_tag(fd, 0)); ASSERT_EQ(0, android_fdsan_close_with_tag(fd, 0));
#endif #endif
} }
TEST_F(FdsanTest, unowned_improperly_tagged_close) { TEST_F(fdsan_DeathTest, unowned_improperly_tagged_close) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
int fd = open("/dev/null", O_RDONLY); int fd = open("/dev/null", O_RDONLY);
EXPECT_FDSAN_DEATH(android_fdsan_close_with_tag(fd, 0xdeadbeef), "actually unowned"); EXPECT_FDSAN_DEATH(android_fdsan_close_with_tag(fd, 0xdeadbeef), "actually unowned");
#endif #endif
} }
TEST_F(FdsanTest, unowned_incorrect_exchange) { TEST_F(fdsan_DeathTest, unowned_incorrect_exchange) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
int fd = open("/dev/null", O_RDONLY); int fd = open("/dev/null", O_RDONLY);
EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef), EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef),
@ -77,7 +88,7 @@ TEST_F(FdsanTest, unowned_incorrect_exchange) {
#endif #endif
} }
TEST_F(FdsanTest, owned_untagged_close) { TEST_F(fdsan_DeathTest, owned_untagged_close) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
int fd = open("/dev/null", O_RDONLY); int fd = open("/dev/null", O_RDONLY);
android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
@ -85,7 +96,7 @@ TEST_F(FdsanTest, owned_untagged_close) {
#endif #endif
} }
TEST_F(FdsanTest, owned_tagged_close) { TEST_F(fdsan, owned_tagged_close) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
int fd = open("/dev/null", O_RDONLY); int fd = open("/dev/null", O_RDONLY);
android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
@ -93,7 +104,7 @@ TEST_F(FdsanTest, owned_tagged_close) {
#endif #endif
} }
TEST_F(FdsanTest, owned_improperly_tagged_close) { TEST_F(fdsan_DeathTest, owned_improperly_tagged_close) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
int fd = open("/dev/null", O_RDONLY); int fd = open("/dev/null", O_RDONLY);
android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
@ -101,7 +112,7 @@ TEST_F(FdsanTest, owned_improperly_tagged_close) {
#endif #endif
} }
TEST_F(FdsanTest, owned_incorrect_exchange) { TEST_F(fdsan_DeathTest, owned_incorrect_exchange) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
int fd = open("/dev/null", O_RDONLY); int fd = open("/dev/null", O_RDONLY);
android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
@ -110,7 +121,7 @@ TEST_F(FdsanTest, owned_incorrect_exchange) {
#endif #endif
} }
TEST_F(FdsanTest, fopen) { TEST_F(fdsan_DeathTest, fopen) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
FILE* f = fopen("/dev/null", "r"); FILE* f = fopen("/dev/null", "r");
ASSERT_TRUE(f); ASSERT_TRUE(f);
@ -118,7 +129,7 @@ TEST_F(FdsanTest, fopen) {
#endif #endif
} }
TEST_F(FdsanTest, closedir) { TEST_F(fdsan_DeathTest, closedir) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
DIR* dir = opendir("/dev/"); DIR* dir = opendir("/dev/");
ASSERT_TRUE(dir); ASSERT_TRUE(dir);
@ -126,7 +137,7 @@ TEST_F(FdsanTest, closedir) {
#endif #endif
} }
TEST_F(FdsanTest, overflow) { TEST_F(fdsan, overflow) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
std::unordered_map<int, uint64_t> fds; std::unordered_map<int, uint64_t> fds;
for (int i = 0; i < 4096; ++i) { for (int i = 0; i < 4096; ++i) {
@ -142,7 +153,7 @@ TEST_F(FdsanTest, overflow) {
#endif #endif
} }
TEST_F(FdsanTest, owner_value_high) { TEST_F(fdsan_DeathTest, owner_value_high) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
int fd = open("/dev/null", O_RDONLY); int fd = open("/dev/null", O_RDONLY);
uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, ~0ULL); uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, ~0ULL);
@ -152,7 +163,7 @@ TEST_F(FdsanTest, owner_value_high) {
#endif #endif
} }
TEST_F(FdsanTest, owner_value_low) { TEST_F(fdsan_DeathTest, owner_value_low) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
int fd = open("/dev/null", O_RDONLY); int fd = open("/dev/null", O_RDONLY);
uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, 1); uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, 1);
@ -162,7 +173,7 @@ TEST_F(FdsanTest, owner_value_low) {
#endif #endif
} }
TEST_F(FdsanTest, unique_fd_unowned_close) { TEST_F(fdsan_DeathTest, unique_fd_unowned_close) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
android::base::unique_fd fd(open("/dev/null", O_RDONLY)); android::base::unique_fd fd(open("/dev/null", O_RDONLY));
android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL); android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
@ -170,19 +181,19 @@ TEST_F(FdsanTest, unique_fd_unowned_close) {
#endif #endif
} }
TEST_F(FdsanTest, unique_fd_untag_on_release) { TEST_F(fdsan, unique_fd_untag_on_release) {
android::base::unique_fd fd(open("/dev/null", O_RDONLY)); android::base::unique_fd fd(open("/dev/null", O_RDONLY));
close(fd.release()); close(fd.release());
} }
TEST_F(FdsanTest, unique_fd_move) { TEST_F(fdsan, unique_fd_move) {
android::base::unique_fd fd(open("/dev/null", O_RDONLY)); android::base::unique_fd fd(open("/dev/null", O_RDONLY));
android::base::unique_fd fd_moved = std::move(fd); android::base::unique_fd fd_moved = std::move(fd);
ASSERT_EQ(-1, fd.get()); ASSERT_EQ(-1, fd.get());
ASSERT_GT(fd_moved.get(), -1); ASSERT_GT(fd_moved.get(), -1);
} }
TEST_F(FdsanTest, unique_fd_unowned_close_after_move) { TEST_F(fdsan_DeathTest, unique_fd_unowned_close_after_move) {
#if defined(__BIONIC__) #if defined(__BIONIC__)
android::base::unique_fd fd(open("/dev/null", O_RDONLY)); android::base::unique_fd fd(open("/dev/null", O_RDONLY));
android::base::unique_fd fd_moved = std::move(fd); android::base::unique_fd fd_moved = std::move(fd);
@ -194,7 +205,7 @@ TEST_F(FdsanTest, unique_fd_unowned_close_after_move) {
#endif #endif
} }
TEST_F(FdsanTest, vfork) { TEST_F(fdsan, vfork) {
android::base::unique_fd fd(open("/dev/null", O_RDONLY)); android::base::unique_fd fd(open("/dev/null", O_RDONLY));
pid_t rc = vfork(); pid_t rc = vfork();