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