Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 1 | /* |
| 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 Stepanov | beb3eb1 | 2017-01-31 17:10:03 -0800 | [diff] [blame] | 17 | #include <dlfcn.h> |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
| 19 | #include <sys/stat.h> |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 20 | |
| 21 | #include "BionicDeathTest.h" |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 22 | #include "gtest_globals.h" |
| 23 | #include "utils.h" |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 24 | |
| 25 | // Private libdl interface. |
| 26 | extern "C" { |
| 27 | void __cfi_slowpath(uint64_t CallSiteTypeId, void* Ptr); |
| 28 | void __cfi_slowpath_diag(uint64_t CallSiteTypeId, void* Ptr, void* DiagData); |
Evgenii Stepanov | 97c16f8 | 2017-08-02 16:34:44 -0700 | [diff] [blame] | 29 | size_t __cfi_shadow_size(); |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static void f() {} |
| 33 | |
| 34 | TEST(cfi_test, basic) { |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 35 | #if defined(__BIONIC__) |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 36 | void* handle; |
| 37 | handle = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL); |
| 38 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 39 | |
Evgenii Stepanov | 97c16f8 | 2017-08-02 16:34:44 -0700 | [diff] [blame] | 40 | EXPECT_NE(0U, __cfi_shadow_size()); |
| 41 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 42 | #define SYM(type, name) auto name = reinterpret_cast<type>(dlsym(handle, #name)) |
| 43 | SYM(int (*)(), get_count); |
| 44 | SYM(uint64_t(*)(), get_last_type_id); |
| 45 | SYM(void* (*)(), get_last_address); |
| 46 | SYM(void* (*)(), get_last_diag); |
| 47 | SYM(void* (*)(), get_global_address); |
| 48 | SYM(void (*)(uint64_t, void*, void*), __cfi_check); |
| 49 | #undef SYM |
| 50 | |
| 51 | int c = get_count(); |
| 52 | |
| 53 | // CFI check for code inside the DSO. Can't use just any function address - this is only |
| 54 | // guaranteed to work for code addresses above __cfi_check. |
| 55 | void* code_ptr = reinterpret_cast<char*>(__cfi_check) + 1234; |
| 56 | void* diag_ptr = reinterpret_cast<void*>(5678); |
| 57 | __cfi_slowpath_diag(42, code_ptr, diag_ptr); |
| 58 | EXPECT_EQ(42U, get_last_type_id()); |
| 59 | EXPECT_EQ(code_ptr, get_last_address()); |
| 60 | EXPECT_EQ(diag_ptr, get_last_diag()); |
| 61 | EXPECT_EQ(++c, get_count()); |
| 62 | |
| 63 | // __cfi_slowpath passes nullptr for the Diag argument. |
| 64 | __cfi_slowpath(42, code_ptr); |
| 65 | EXPECT_EQ(42U, get_last_type_id()); |
| 66 | EXPECT_EQ(code_ptr, get_last_address()); |
| 67 | EXPECT_EQ(nullptr, get_last_diag()); |
| 68 | EXPECT_EQ(++c, get_count()); |
| 69 | |
| 70 | // CFI check for a data address inside the DSO. |
| 71 | __cfi_slowpath(43, get_global_address()); |
| 72 | EXPECT_EQ(43U, get_last_type_id()); |
| 73 | EXPECT_EQ(get_global_address(), get_last_address()); |
| 74 | EXPECT_EQ(++c, get_count()); |
| 75 | |
| 76 | // CFI check for a function inside _this_ DSO. It either goes to this DSO's __cfi_check, |
| 77 | // or (if missing) is simply ignored. Any way, it does not affect the test lib's counters. |
| 78 | __cfi_slowpath(44, reinterpret_cast<void*>(&f)); |
| 79 | EXPECT_EQ(43U, get_last_type_id()); |
| 80 | EXPECT_EQ(get_global_address(), get_last_address()); |
| 81 | EXPECT_EQ(c, get_count()); |
| 82 | |
| 83 | // CFI check for a stack address. This is always invalid and gets the process killed. |
| 84 | EXPECT_DEATH(__cfi_slowpath(45, reinterpret_cast<void*>(&c)), ""); |
| 85 | |
| 86 | // CFI check for a heap address. This is always invalid and gets the process killed. |
| 87 | void* p = malloc(4096); |
| 88 | EXPECT_DEATH(__cfi_slowpath(46, p), ""); |
| 89 | free(p); |
| 90 | |
| 91 | // Load the same library again. |
| 92 | void* handle2 = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL); |
| 93 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 94 | EXPECT_EQ(handle2, handle); |
| 95 | |
| 96 | // Check that it is still there. |
| 97 | __cfi_slowpath(43, get_global_address()); |
| 98 | EXPECT_EQ(43U, get_last_type_id()); |
| 99 | EXPECT_EQ(get_global_address(), get_last_address()); |
| 100 | EXPECT_EQ(++c, get_count()); |
| 101 | |
| 102 | dlclose(handle); |
| 103 | dlclose(handle2); |
| 104 | |
| 105 | // CFI check for a function inside the unloaded DSO. This is always invalid and gets the process |
| 106 | // killed. |
| 107 | EXPECT_DEATH(__cfi_slowpath(45, reinterpret_cast<void*>(code_ptr)), ""); |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 108 | #endif |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TEST(cfi_test, invalid) { |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 112 | #if defined(__BIONIC__) |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 113 | void* handle; |
| 114 | handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL); |
| 115 | ASSERT_FALSE(handle != nullptr) << dlerror(); |
| 116 | |
| 117 | handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL); |
| 118 | ASSERT_FALSE(handle != nullptr) << dlerror(); |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 119 | #endif |
| 120 | } |
| 121 | |
| 122 | // cfi_test_helper exports __cfi_check, which triggers CFI initialization at startup. |
| 123 | TEST(cfi_test, early_init) { |
| 124 | #if defined(__BIONIC__) |
| 125 | std::string helper = get_testlib_root() + "/cfi_test_helper/cfi_test_helper"; |
| 126 | chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607 |
| 127 | ExecTestHelper eth; |
| 128 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 129 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | // cfi_test_helper2 depends on a library that exports __cfi_check, which triggers CFI initialization |
| 134 | // at startup. |
| 135 | TEST(cfi_test, early_init2) { |
| 136 | #if defined(__BIONIC__) |
| 137 | std::string helper = get_testlib_root() + "/cfi_test_helper2/cfi_test_helper2"; |
| 138 | chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607 |
| 139 | ExecTestHelper eth; |
| 140 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 141 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
| 142 | #endif |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 143 | } |