blob: ceb0b4d675056e4977add9e290ded262e609f016 [file] [log] [blame]
Dmitriy Ivanov0416d882014-11-04 09:38:18 -08001/*
2 * Copyright (C) 2012 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 <gtest/gtest.h>
18
Jiyong Park4945d8f2017-08-30 11:30:53 +090019#if defined(__BIONIC__)
20#include <android-base/properties.h>
21#endif
22
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080023#include <dlfcn.h>
24#include <libgen.h>
25#include <limits.h>
26#include <stdio.h>
27#include <stdint.h>
28
29#include <string>
Jiyong Park02586a22017-05-20 01:01:24 +090030#include <iostream>
31#include <fstream>
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080032
Elliott Hugheseb04ed52017-03-29 13:48:02 -070033#include "gtest_globals.h"
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080034#include "utils.h"
35
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080036extern "C" int main_global_default_serial() {
37 return 3370318;
38}
39
40extern "C" int main_global_protected_serial() {
41 return 2716057;
42}
43
44// The following functions are defined in DT_NEEDED
45// libdl_preempt_test.so library.
46
47// This one calls main_global_default_serial
48extern "C" int main_global_default_get_serial();
49
50// This one calls main_global_protected_serial
51extern "C" int main_global_protected_get_serial();
52
53// This one calls lib_global_default_serial
54extern "C" int lib_global_default_get_serial();
55
56// This one calls lib_global_protected_serial
57extern "C" int lib_global_protected_get_serial();
58
59// This test verifies that the global default function
60// main_global_default_serial() is preempted by
61// the function defined above.
62TEST(dl, main_preempts_global_default) {
63 ASSERT_EQ(3370318, main_global_default_get_serial());
64}
65
66// This one makes sure that the global protected
67// symbols do not get preempted
68TEST(dl, main_does_not_preempt_global_protected) {
69 ASSERT_EQ(3370318, main_global_protected_get_serial());
70}
71
72// check same things for lib
73TEST(dl, lib_preempts_global_default) {
74 ASSERT_EQ(3370318, lib_global_default_get_serial());
75}
76
77TEST(dl, lib_does_not_preempt_global_protected) {
78 ASSERT_EQ(3370318, lib_global_protected_get_serial());
79}
80
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080081TEST(dl, exec_linker) {
82#if defined(__BIONIC__)
83#if defined(__LP64__)
84 static constexpr const char* kPathToLinker = "/system/bin/linker64";
85#else
86 static constexpr const char* kPathToLinker = "/system/bin/linker";
87#endif
88 ExecTestHelper eth;
89 std::string expected_output = std::string("This is ") + kPathToLinker +
90 ", the helper program for dynamic executables.\n";
91 eth.SetArgs( { kPathToLinker, nullptr });
92 eth.Run([&]() { execve(kPathToLinker, eth.GetArgs(), eth.GetEnv()); }, 0, expected_output.c_str());
93#endif
94}
95
Elliott Hugheseb04ed52017-03-29 13:48:02 -070096TEST(dl, preinit_system_calls) {
97#if defined(__BIONIC__)
98 std::string helper = get_testlib_root() +
99 "/preinit_syscall_test_helper/preinit_syscall_test_helper";
100 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
101 ExecTestHelper eth;
102 eth.SetArgs({ helper.c_str(), nullptr });
103 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
104#endif
105}
106
107TEST(dl, xfail_preinit_getauxval) {
108#if defined(__BIONIC__)
109 std::string helper = get_testlib_root() +
110 "/preinit_getauxval_test_helper/preinit_getauxval_test_helper";
111 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
112 ExecTestHelper eth;
113 eth.SetArgs({ helper.c_str(), nullptr });
114 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
115#endif
116}
117
Jiyong Park02586a22017-05-20 01:01:24 +0900118
119TEST(dl, exec_without_ld_preload) {
120#if defined(__BIONIC__)
121 std::string helper = get_testlib_root() +
122 "/ld_preload_test_helper/ld_preload_test_helper";
123 chmod(helper.c_str(), 0755);
124 ExecTestHelper eth;
125 eth.SetArgs({ helper.c_str(), nullptr });
126 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
127#endif
128}
129
130TEST(dl, exec_with_ld_preload) {
131#if defined(__BIONIC__)
132 std::string helper = get_testlib_root() +
133 "/ld_preload_test_helper/ld_preload_test_helper";
134 std::string env = std::string("LD_PRELOAD=") + get_testlib_root() + "/ld_preload_test_helper_lib2.so";
135 chmod(helper.c_str(), 0755);
136 ExecTestHelper eth;
137 eth.SetArgs({ helper.c_str(), nullptr });
138 eth.SetEnv({ env.c_str(), nullptr });
139 // ld_preload_test_helper calls get_value_from_lib() and returns the value.
140 // The symbol is defined by two libs: ld_preload_test_helper_lib.so and
141 // ld_preloaded_lib.so. The former is DT_NEEDED and the latter is LD_PRELOADED
142 // via this execution. The main executable is linked to the LD_PRELOADED lib
143 // and the value given from the lib is returned.
144 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
145#endif
146}
147
148
149// ld_config_test_helper must fail because it is depending on a lib which is not
150// in the search path
151//
152// Call sequence is...
153// _helper -- (get_value_from_lib()) -->
154// _lib1.so -- (get_value_from_another_lib()) -->
155// _lib2.so (returns 12345)
156// The two libs are in ns2/ subdir.
157TEST(dl, exec_without_ld_config_file) {
158#if defined(__BIONIC__)
159 std::string error_message = "CANNOT LINK EXECUTABLE \"" + get_testlib_root() + "/ld_config_test_helper/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" not found\n";
160 std::string helper = get_testlib_root() +
161 "/ld_config_test_helper/ld_config_test_helper";
162 chmod(helper.c_str(), 0755);
163 ExecTestHelper eth;
164 eth.SetArgs({ helper.c_str(), nullptr });
dimitry04f7a792017-09-29 11:52:17 +0200165 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Jiyong Park02586a22017-05-20 01:01:24 +0900166#endif
167}
168
169#if defined(__BIONIC__)
170static void create_ld_config_file(std::string& config_file) {
171 std::ofstream fout(config_file.c_str(), std::ios::out);
172 fout << "dir.test = " << get_testlib_root() << "/ld_config_test_helper/" << std::endl
173 << "[test]" << std::endl
174 << "additional.namespaces = ns2" << std::endl
175 << "namespace.default.search.paths = " << get_testlib_root() << std::endl
176 << "namespace.default.links = ns2" << std::endl
177 << "namespace.default.link.ns2.shared_libs = libc.so:libm.so:libdl.so:ld_config_test_helper_lib1.so" << std::endl
178 << "namespace.ns2.search.paths = /system/${LIB}:" << get_testlib_root() << "/ns2" << std::endl;
179 fout.close();
180}
181#endif
182
183#ifdef USE_LD_CONFIG_FILE
184
185// _lib1.so and _lib2.so are now searchable by having another namespace 'ns2'
186// whose search paths include the 'ns2/' subdir.
187TEST(dl, exec_with_ld_config_file) {
188#if defined(__BIONIC__)
189 std::string helper = get_testlib_root() +
190 "/ld_config_test_helper/ld_config_test_helper";
191 std::string config_file = get_testlib_root() + "/ld.config.txt";
192 create_ld_config_file(config_file);
193 std::string env = std::string("LD_CONFIG_FILE=") + config_file;
194 chmod(helper.c_str(), 0755);
195 ExecTestHelper eth;
196 eth.SetArgs({ helper.c_str(), nullptr });
197 eth.SetEnv({ env.c_str(), nullptr });
198 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
199#endif
200}
201
202// _lib3.so has same symbol as lib2.so but returns 54321. _lib3.so is
203// LD_PRELOADed. This test is to ensure LD_PRELOADed libs are available to
204// additional namespaces other than the default namespace.
205TEST(dl, exec_with_ld_config_file_with_ld_preload) {
206#if defined(__BIONIC__)
207 std::string helper = get_testlib_root() +
208 "/ld_config_test_helper/ld_config_test_helper";
209 std::string config_file = get_testlib_root() + "/ld.config.txt";
210 create_ld_config_file(config_file);
211 std::string env = std::string("LD_CONFIG_FILE=") + config_file;
212 std::string env2 = std::string("LD_PRELOAD=") + get_testlib_root() + "/ld_config_test_helper_lib3.so";
213 chmod(helper.c_str(), 0755);
214 ExecTestHelper eth;
215 eth.SetArgs({ helper.c_str(), nullptr });
216 eth.SetEnv({ env.c_str(), env2.c_str(), nullptr });
217 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
218#endif
219}
220
221#endif // USE_LD_CONFIG_FILE
222
223// ensures that LD_CONFIG_FILE env var does not work for production builds.
224// The test input is the same as exec_with_ld_config_file, but it must fail in
225// this case.
226TEST(dl, disable_ld_config_file) {
227#if defined(__BIONIC__)
228 if (getuid() == 0) {
229 // when executed from the shell (e.g. not as part of CTS), skip the test.
230 // This test is only for CTS.
231 return;
232 }
Jiyong Park4945d8f2017-08-30 11:30:53 +0900233 std::string build_type = android::base::GetProperty("ro.build.type", "user");
234 if (build_type == "userdebug" || build_type == "eng") {
235 // Skip the test for non production devices
236 return;
237 }
238
Jiyong Park02586a22017-05-20 01:01:24 +0900239 std::string error_message = "CANNOT LINK EXECUTABLE \"" + get_testlib_root() + "/ld_config_test_helper/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" not found\n";
240 std::string helper = get_testlib_root() +
241 "/ld_config_test_helper/ld_config_test_helper";
242 std::string config_file = get_testlib_root() + "/ld.config.txt";
243 create_ld_config_file(config_file);
244 std::string env = std::string("LD_CONFIG_FILE=") + config_file;
245 chmod(helper.c_str(), 0755);
246 ExecTestHelper eth;
247 eth.SetArgs({ helper.c_str(), nullptr });
248 eth.SetEnv({ env.c_str(), nullptr });
249 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, -6, error_message.c_str());
250#endif
251}