OSDN Git Service

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