Evgenii Stepanov | 4ccd431 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 1 | #include <gtest/gtest.h> |
Evgenii Stepanov | beb3eb1 | 2017-01-31 17:10:03 -0800 | [diff] [blame] | 2 | #include <dlfcn.h> |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 3 | |
| 4 | #include "BionicDeathTest.h" |
| 5 | |
| 6 | // Private libdl interface. |
| 7 | extern "C" { |
| 8 | void __cfi_slowpath(uint64_t CallSiteTypeId, void* Ptr); |
| 9 | void __cfi_slowpath_diag(uint64_t CallSiteTypeId, void* Ptr, void* DiagData); |
| 10 | } |
| 11 | |
| 12 | static void f() {} |
| 13 | |
| 14 | TEST(cfi_test, basic) { |
| 15 | void* handle; |
| 16 | handle = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL); |
| 17 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 18 | |
| 19 | #define SYM(type, name) auto name = reinterpret_cast<type>(dlsym(handle, #name)) |
| 20 | SYM(int (*)(), get_count); |
| 21 | SYM(uint64_t(*)(), get_last_type_id); |
| 22 | SYM(void* (*)(), get_last_address); |
| 23 | SYM(void* (*)(), get_last_diag); |
| 24 | SYM(void* (*)(), get_global_address); |
| 25 | SYM(void (*)(uint64_t, void*, void*), __cfi_check); |
| 26 | #undef SYM |
| 27 | |
| 28 | int c = get_count(); |
| 29 | |
| 30 | // CFI check for code inside the DSO. Can't use just any function address - this is only |
| 31 | // guaranteed to work for code addresses above __cfi_check. |
| 32 | void* code_ptr = reinterpret_cast<char*>(__cfi_check) + 1234; |
| 33 | void* diag_ptr = reinterpret_cast<void*>(5678); |
| 34 | __cfi_slowpath_diag(42, code_ptr, diag_ptr); |
| 35 | EXPECT_EQ(42U, get_last_type_id()); |
| 36 | EXPECT_EQ(code_ptr, get_last_address()); |
| 37 | EXPECT_EQ(diag_ptr, get_last_diag()); |
| 38 | EXPECT_EQ(++c, get_count()); |
| 39 | |
| 40 | // __cfi_slowpath passes nullptr for the Diag argument. |
| 41 | __cfi_slowpath(42, code_ptr); |
| 42 | EXPECT_EQ(42U, get_last_type_id()); |
| 43 | EXPECT_EQ(code_ptr, get_last_address()); |
| 44 | EXPECT_EQ(nullptr, get_last_diag()); |
| 45 | EXPECT_EQ(++c, get_count()); |
| 46 | |
| 47 | // CFI check for a data address inside the DSO. |
| 48 | __cfi_slowpath(43, get_global_address()); |
| 49 | EXPECT_EQ(43U, get_last_type_id()); |
| 50 | EXPECT_EQ(get_global_address(), get_last_address()); |
| 51 | EXPECT_EQ(++c, get_count()); |
| 52 | |
| 53 | // CFI check for a function inside _this_ DSO. It either goes to this DSO's __cfi_check, |
| 54 | // or (if missing) is simply ignored. Any way, it does not affect the test lib's counters. |
| 55 | __cfi_slowpath(44, reinterpret_cast<void*>(&f)); |
| 56 | EXPECT_EQ(43U, get_last_type_id()); |
| 57 | EXPECT_EQ(get_global_address(), get_last_address()); |
| 58 | EXPECT_EQ(c, get_count()); |
| 59 | |
| 60 | // CFI check for a stack address. This is always invalid and gets the process killed. |
| 61 | EXPECT_DEATH(__cfi_slowpath(45, reinterpret_cast<void*>(&c)), ""); |
| 62 | |
| 63 | // CFI check for a heap address. This is always invalid and gets the process killed. |
| 64 | void* p = malloc(4096); |
| 65 | EXPECT_DEATH(__cfi_slowpath(46, p), ""); |
| 66 | free(p); |
| 67 | |
| 68 | // Load the same library again. |
| 69 | void* handle2 = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL); |
| 70 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 71 | EXPECT_EQ(handle2, handle); |
| 72 | |
| 73 | // Check that it is still there. |
| 74 | __cfi_slowpath(43, get_global_address()); |
| 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 | dlclose(handle); |
| 80 | dlclose(handle2); |
| 81 | |
| 82 | // CFI check for a function inside the unloaded DSO. This is always invalid and gets the process |
| 83 | // killed. |
| 84 | EXPECT_DEATH(__cfi_slowpath(45, reinterpret_cast<void*>(code_ptr)), ""); |
| 85 | } |
| 86 | |
| 87 | TEST(cfi_test, invalid) { |
| 88 | void* handle; |
| 89 | handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL); |
| 90 | ASSERT_FALSE(handle != nullptr) << dlerror(); |
| 91 | |
| 92 | handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL); |
| 93 | ASSERT_FALSE(handle != nullptr) << dlerror(); |
| 94 | } |