blob: 79a9e35eb63ce067f60d96d26e67e89832ce245c [file] [log] [blame]
Evgenii Stepanov68ecec12017-01-31 13:19:30 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Evgenii Stepanovbeb3eb12017-01-31 17:10:03 -080017#include <dlfcn.h>
Evgenii Stepanov68ecec12017-01-31 13:19:30 -080018#include <sys/stat.h>
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070019
Christopher Ferrisf3224832020-04-01 16:59:57 -070020#include <vector>
21
Elliott Hughes141b9172021-04-09 17:13:09 -070022#include <gtest/gtest.h>
23
Evgenii Stepanov68ecec12017-01-31 13:19:30 -080024#include "gtest_globals.h"
25#include "utils.h"
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070026
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -070027#if defined(__BIONIC__)
28#include "private/CFIShadow.h"
29#endif
30
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070031// Private libdl interface.
32extern "C" {
33void __cfi_slowpath(uint64_t CallSiteTypeId, void* Ptr);
34void __cfi_slowpath_diag(uint64_t CallSiteTypeId, void* Ptr, void* DiagData);
Evgenii Stepanov97c16f82017-08-02 16:34:44 -070035size_t __cfi_shadow_size();
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070036}
37
Elliott Hughes4411b942022-02-09 15:56:27 -080038// Disables debuggerd stack traces to speed up death tests, make them less
39// noisy in logcat, and avoid expected deaths from showing up in stability
40// metrics.
41// We don't use the usual libbase class because (a) we don't care about most
42// of the signals it blocks but (b) we do need to block SIGILL, which normal
43// death tests shouldn't ever hit. (It's possible that a design where a
44// deathtest always declares its expected signals up front is a better one,
45// and maybe that's an interesting future direction for libbase.)
Elliott Hughes82e24a52022-02-15 10:12:23 -080046//
47// We include SIGSEGV because there's a test that passes heap addresses to
48// __cfi_slowpath and we only map the executable code shadow as readable.
49// We don't always get SIGSEGV there though: if the heap allocation happens
50// to be close enough to an executable mapping that its shadow is in the
51// same page as the executable shadow, we'll get SIGILL/SIGTRAP.
Elliott Hughes4411b942022-02-09 15:56:27 -080052class cfi_test_DeathTest : public testing::Test {
53 protected:
54 void SetUp() override {
55 struct sigaction64 action = {.sa_handler = SIG_DFL};
Elliott Hughes4411b942022-02-09 15:56:27 -080056 sigaction64(SIGILL, &action, &previous_sigill_);
Elliott Hughes82e24a52022-02-15 10:12:23 -080057 sigaction64(SIGSEGV, &action, &previous_sigsegv_);
58 sigaction64(SIGTRAP, &action, &previous_sigtrap_);
Elliott Hughes4411b942022-02-09 15:56:27 -080059 }
60
61 void TearDown() override {
Elliott Hughes82e24a52022-02-15 10:12:23 -080062 sigaction64(SIGTRAP, &previous_sigtrap_, nullptr);
Elliott Hughes4411b942022-02-09 15:56:27 -080063 sigaction64(SIGSEGV, &previous_sigsegv_, nullptr);
Elliott Hughes82e24a52022-02-15 10:12:23 -080064 sigaction64(SIGILL, &previous_sigill_, nullptr);
Elliott Hughes4411b942022-02-09 15:56:27 -080065 }
66
67 private:
Elliott Hughes4411b942022-02-09 15:56:27 -080068 struct sigaction64 previous_sigill_;
Elliott Hughes82e24a52022-02-15 10:12:23 -080069 struct sigaction64 previous_sigsegv_;
70 struct sigaction64 previous_sigtrap_;
Elliott Hughes4411b942022-02-09 15:56:27 -080071};
72
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070073static void f() {}
74
Elliott Hughese657eb42021-02-18 17:11:56 -080075TEST_F(cfi_test_DeathTest, basic) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -080076#if defined(__BIONIC__)
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070077 void* handle;
78 handle = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL);
79 ASSERT_TRUE(handle != nullptr) << dlerror();
80
Evgenii Stepanov97c16f82017-08-02 16:34:44 -070081 EXPECT_NE(0U, __cfi_shadow_size());
82
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070083#define SYM(type, name) auto name = reinterpret_cast<type>(dlsym(handle, #name))
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -070084 SYM(size_t (*)(), get_count);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070085 SYM(uint64_t(*)(), get_last_type_id);
86 SYM(void* (*)(), get_last_address);
87 SYM(void* (*)(), get_last_diag);
88 SYM(void* (*)(), get_global_address);
89 SYM(void (*)(uint64_t, void*, void*), __cfi_check);
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -070090 SYM(char*, bss);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070091#undef SYM
92
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -070093 size_t c = get_count();
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070094
95 // CFI check for code inside the DSO. Can't use just any function address - this is only
96 // guaranteed to work for code addresses above __cfi_check.
97 void* code_ptr = reinterpret_cast<char*>(__cfi_check) + 1234;
98 void* diag_ptr = reinterpret_cast<void*>(5678);
99 __cfi_slowpath_diag(42, code_ptr, diag_ptr);
100 EXPECT_EQ(42U, get_last_type_id());
101 EXPECT_EQ(code_ptr, get_last_address());
102 EXPECT_EQ(diag_ptr, get_last_diag());
103 EXPECT_EQ(++c, get_count());
104
105 // __cfi_slowpath passes nullptr for the Diag argument.
106 __cfi_slowpath(42, code_ptr);
107 EXPECT_EQ(42U, get_last_type_id());
108 EXPECT_EQ(code_ptr, get_last_address());
109 EXPECT_EQ(nullptr, get_last_diag());
110 EXPECT_EQ(++c, get_count());
111
112 // CFI check for a data address inside the DSO.
113 __cfi_slowpath(43, get_global_address());
114 EXPECT_EQ(43U, get_last_type_id());
115 EXPECT_EQ(get_global_address(), get_last_address());
116 EXPECT_EQ(++c, get_count());
117
118 // CFI check for a function inside _this_ DSO. It either goes to this DSO's __cfi_check,
119 // or (if missing) is simply ignored. Any way, it does not affect the test lib's counters.
120 __cfi_slowpath(44, reinterpret_cast<void*>(&f));
121 EXPECT_EQ(43U, get_last_type_id());
122 EXPECT_EQ(get_global_address(), get_last_address());
123 EXPECT_EQ(c, get_count());
124
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -0700125 // Check all the addresses.
126 const size_t bss_size = 1024 * 1024;
127 static_assert(bss_size >= kLibraryAlignment * 2, "test range not big enough");
128 for (size_t i = 0; i < bss_size; ++i) {
129 __cfi_slowpath(47, bss + i);
130 EXPECT_EQ(++c, get_count());
131 }
132
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700133 // Load the same library again.
134 void* handle2 = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL);
135 ASSERT_TRUE(handle2 != nullptr) << dlerror();
136 EXPECT_EQ(handle2, handle);
137
138 // Check that it is still there.
139 __cfi_slowpath(43, get_global_address());
140 EXPECT_EQ(43U, get_last_type_id());
141 EXPECT_EQ(get_global_address(), get_last_address());
142 EXPECT_EQ(++c, get_count());
143
144 dlclose(handle);
145 dlclose(handle2);
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800146#endif
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700147}
148
149TEST(cfi_test, invalid) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800150#if defined(__BIONIC__)
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700151 void* handle;
152 handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL);
153 ASSERT_FALSE(handle != nullptr) << dlerror();
154
155 handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL);
156 ASSERT_FALSE(handle != nullptr) << dlerror();
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800157#endif
158}
159
160// cfi_test_helper exports __cfi_check, which triggers CFI initialization at startup.
161TEST(cfi_test, early_init) {
162#if defined(__BIONIC__)
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000163 std::string helper = GetTestLibRoot() + "/cfi_test_helper";
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800164 ExecTestHelper eth;
165 eth.SetArgs({ helper.c_str(), nullptr });
166 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
167#endif
168}
169
170// cfi_test_helper2 depends on a library that exports __cfi_check, which triggers CFI initialization
171// at startup.
172TEST(cfi_test, early_init2) {
173#if defined(__BIONIC__)
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000174 std::string helper = GetTestLibRoot() + "/cfi_test_helper2";
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800175 ExecTestHelper eth;
176 eth.SetArgs({ helper.c_str(), nullptr });
177 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
178#endif
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700179}