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