1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2012 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker *
4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker *
8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker *
10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker */
16*8d67ca89SAndroid Build Coastguard Worker
17*8d67ca89SAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*8d67ca89SAndroid Build Coastguard Worker
19*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
20*8d67ca89SAndroid Build Coastguard Worker #include <android-base/properties.h>
21*8d67ca89SAndroid Build Coastguard Worker #endif
22*8d67ca89SAndroid Build Coastguard Worker
23*8d67ca89SAndroid Build Coastguard Worker #include <dlfcn.h>
24*8d67ca89SAndroid Build Coastguard Worker #include <libgen.h>
25*8d67ca89SAndroid Build Coastguard Worker #include <limits.h>
26*8d67ca89SAndroid Build Coastguard Worker #include <stdio.h>
27*8d67ca89SAndroid Build Coastguard Worker #include <stdint.h>
28*8d67ca89SAndroid Build Coastguard Worker #include <sys/stat.h>
29*8d67ca89SAndroid Build Coastguard Worker
30*8d67ca89SAndroid Build Coastguard Worker #include <fstream>
31*8d67ca89SAndroid Build Coastguard Worker #include <iostream>
32*8d67ca89SAndroid Build Coastguard Worker #include <regex>
33*8d67ca89SAndroid Build Coastguard Worker #include <string>
34*8d67ca89SAndroid Build Coastguard Worker
35*8d67ca89SAndroid Build Coastguard Worker #include <android-base/file.h>
36*8d67ca89SAndroid Build Coastguard Worker #include <android-base/macros.h>
37*8d67ca89SAndroid Build Coastguard Worker #include <android-base/test_utils.h>
38*8d67ca89SAndroid Build Coastguard Worker #include "gtest_globals.h"
39*8d67ca89SAndroid Build Coastguard Worker #include "utils.h"
40*8d67ca89SAndroid Build Coastguard Worker
main_global_default_serial()41*8d67ca89SAndroid Build Coastguard Worker extern "C" int main_global_default_serial() {
42*8d67ca89SAndroid Build Coastguard Worker return 3370318;
43*8d67ca89SAndroid Build Coastguard Worker }
44*8d67ca89SAndroid Build Coastguard Worker
main_global_protected_serial()45*8d67ca89SAndroid Build Coastguard Worker extern "C" int main_global_protected_serial() {
46*8d67ca89SAndroid Build Coastguard Worker return 2716057;
47*8d67ca89SAndroid Build Coastguard Worker }
48*8d67ca89SAndroid Build Coastguard Worker
49*8d67ca89SAndroid Build Coastguard Worker // The following functions are defined in DT_NEEDED
50*8d67ca89SAndroid Build Coastguard Worker // libdl_preempt_test.so library.
51*8d67ca89SAndroid Build Coastguard Worker
52*8d67ca89SAndroid Build Coastguard Worker // This one calls main_global_default_serial
53*8d67ca89SAndroid Build Coastguard Worker extern "C" int main_global_default_get_serial();
54*8d67ca89SAndroid Build Coastguard Worker
55*8d67ca89SAndroid Build Coastguard Worker // This one calls main_global_protected_serial
56*8d67ca89SAndroid Build Coastguard Worker extern "C" int main_global_protected_get_serial();
57*8d67ca89SAndroid Build Coastguard Worker
58*8d67ca89SAndroid Build Coastguard Worker // This one calls lib_global_default_serial
59*8d67ca89SAndroid Build Coastguard Worker extern "C" int lib_global_default_get_serial();
60*8d67ca89SAndroid Build Coastguard Worker
61*8d67ca89SAndroid Build Coastguard Worker // This one calls lib_global_protected_serial
62*8d67ca89SAndroid Build Coastguard Worker extern "C" int lib_global_protected_get_serial();
63*8d67ca89SAndroid Build Coastguard Worker
64*8d67ca89SAndroid Build Coastguard Worker // This test verifies that the global default function
65*8d67ca89SAndroid Build Coastguard Worker // main_global_default_serial() is preempted by
66*8d67ca89SAndroid Build Coastguard Worker // the function defined above.
TEST(dl,main_preempts_global_default)67*8d67ca89SAndroid Build Coastguard Worker TEST(dl, main_preempts_global_default) {
68*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(3370318, main_global_default_get_serial());
69*8d67ca89SAndroid Build Coastguard Worker }
70*8d67ca89SAndroid Build Coastguard Worker
71*8d67ca89SAndroid Build Coastguard Worker // This one makes sure that the global protected
72*8d67ca89SAndroid Build Coastguard Worker // symbols do not get preempted
TEST(dl,main_does_not_preempt_global_protected)73*8d67ca89SAndroid Build Coastguard Worker TEST(dl, main_does_not_preempt_global_protected) {
74*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(3370318, main_global_protected_get_serial());
75*8d67ca89SAndroid Build Coastguard Worker }
76*8d67ca89SAndroid Build Coastguard Worker
77*8d67ca89SAndroid Build Coastguard Worker // check same things for lib
TEST(dl,lib_preempts_global_default)78*8d67ca89SAndroid Build Coastguard Worker TEST(dl, lib_preempts_global_default) {
79*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(3370318, lib_global_default_get_serial());
80*8d67ca89SAndroid Build Coastguard Worker }
81*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,lib_does_not_preempt_global_protected)82*8d67ca89SAndroid Build Coastguard Worker TEST(dl, lib_does_not_preempt_global_protected) {
83*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(3370318, lib_global_protected_get_serial());
84*8d67ca89SAndroid Build Coastguard Worker }
85*8d67ca89SAndroid Build Coastguard Worker
86*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
87*8d67ca89SAndroid Build Coastguard Worker #if defined(__LP64__)
88*8d67ca89SAndroid Build Coastguard Worker #define LINKER_NAME "linker64"
89*8d67ca89SAndroid Build Coastguard Worker #else
90*8d67ca89SAndroid Build Coastguard Worker #define LINKER_NAME "linker"
91*8d67ca89SAndroid Build Coastguard Worker #endif
92*8d67ca89SAndroid Build Coastguard Worker static constexpr const char* kPathToLinker = "/system/bin/" LINKER_NAME;
93*8d67ca89SAndroid Build Coastguard Worker static constexpr const char* kAlternatePathToLinker = "/system/bin/" ABI_STRING "/" LINKER_NAME;
94*8d67ca89SAndroid Build Coastguard Worker #undef LINKER_NAME
95*8d67ca89SAndroid Build Coastguard Worker
PathToLinker()96*8d67ca89SAndroid Build Coastguard Worker const char* PathToLinker() {
97*8d67ca89SAndroid Build Coastguard Worker // On the systems with emulated architecture linker would be of different
98*8d67ca89SAndroid Build Coastguard Worker // architecture. Try to use alternate paths first.
99*8d67ca89SAndroid Build Coastguard Worker struct stat buffer;
100*8d67ca89SAndroid Build Coastguard Worker if (stat(kAlternatePathToLinker, &buffer) == 0) {
101*8d67ca89SAndroid Build Coastguard Worker return kAlternatePathToLinker;
102*8d67ca89SAndroid Build Coastguard Worker }
103*8d67ca89SAndroid Build Coastguard Worker return kPathToLinker;
104*8d67ca89SAndroid Build Coastguard Worker }
105*8d67ca89SAndroid Build Coastguard Worker #endif // defined(__BIONIC__)
106*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,exec_linker)107*8d67ca89SAndroid Build Coastguard Worker TEST(dl, exec_linker) {
108*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
109*8d67ca89SAndroid Build Coastguard Worker const char* path_to_linker = PathToLinker();
110*8d67ca89SAndroid Build Coastguard Worker std::string usage_prefix = std::string("Usage: ") + path_to_linker;
111*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
112*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ path_to_linker, nullptr });
113*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
114*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(0u, eth.GetOutput().find(usage_prefix)) << "Test output:\n" << eth.GetOutput();
115*8d67ca89SAndroid Build Coastguard Worker #endif
116*8d67ca89SAndroid Build Coastguard Worker }
117*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,exec_linker_load_file)118*8d67ca89SAndroid Build Coastguard Worker TEST(dl, exec_linker_load_file) {
119*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
120*8d67ca89SAndroid Build Coastguard Worker const char* path_to_linker = PathToLinker();
121*8d67ca89SAndroid Build Coastguard Worker std::string helper = GetTestLibRoot() + "/exec_linker_helper";
122*8d67ca89SAndroid Build Coastguard Worker std::string expected_output =
123*8d67ca89SAndroid Build Coastguard Worker "ctor: argc=1 argv[0]=" + helper + "\n" +
124*8d67ca89SAndroid Build Coastguard Worker "main: argc=1 argv[0]=" + helper + "\n" +
125*8d67ca89SAndroid Build Coastguard Worker "__progname=exec_linker_helper\n" +
126*8d67ca89SAndroid Build Coastguard Worker "helper_func called\n";
127*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
128*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ path_to_linker, helper.c_str(), nullptr });
129*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
130*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(expected_output, eth.GetOutput());
131*8d67ca89SAndroid Build Coastguard Worker #endif
132*8d67ca89SAndroid Build Coastguard Worker }
133*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,exec_linker_load_from_zip)134*8d67ca89SAndroid Build Coastguard Worker TEST(dl, exec_linker_load_from_zip) {
135*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
136*8d67ca89SAndroid Build Coastguard Worker const char* path_to_linker = PathToLinker();
137*8d67ca89SAndroid Build Coastguard Worker std::string helper = GetTestLibRoot() +
138*8d67ca89SAndroid Build Coastguard Worker "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip!/libdir/exec_linker_helper";
139*8d67ca89SAndroid Build Coastguard Worker std::string expected_output =
140*8d67ca89SAndroid Build Coastguard Worker "ctor: argc=1 argv[0]=" + helper + "\n" +
141*8d67ca89SAndroid Build Coastguard Worker "main: argc=1 argv[0]=" + helper + "\n" +
142*8d67ca89SAndroid Build Coastguard Worker "__progname=exec_linker_helper\n" +
143*8d67ca89SAndroid Build Coastguard Worker "helper_func called\n";
144*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
145*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ path_to_linker, helper.c_str(), nullptr });
146*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
147*8d67ca89SAndroid Build Coastguard Worker ASSERT_EQ(expected_output, eth.GetOutput());
148*8d67ca89SAndroid Build Coastguard Worker #endif
149*8d67ca89SAndroid Build Coastguard Worker }
150*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,exec_linker_load_self)151*8d67ca89SAndroid Build Coastguard Worker TEST(dl, exec_linker_load_self) {
152*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
153*8d67ca89SAndroid Build Coastguard Worker const char* path_to_linker = PathToLinker();
154*8d67ca89SAndroid Build Coastguard Worker std::string error_message = "error: linker cannot load itself\n";
155*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
156*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ path_to_linker, path_to_linker, nullptr });
157*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
158*8d67ca89SAndroid Build Coastguard Worker #endif
159*8d67ca89SAndroid Build Coastguard Worker }
160*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,preinit_system_calls)161*8d67ca89SAndroid Build Coastguard Worker TEST(dl, preinit_system_calls) {
162*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
163*8d67ca89SAndroid Build Coastguard Worker SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
164*8d67ca89SAndroid Build Coastguard Worker std::string helper = GetTestLibRoot() + "/preinit_syscall_test_helper";
165*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
166*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ helper.c_str(), nullptr });
167*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
168*8d67ca89SAndroid Build Coastguard Worker #endif
169*8d67ca89SAndroid Build Coastguard Worker }
170*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,preinit_getauxval)171*8d67ca89SAndroid Build Coastguard Worker TEST(dl, preinit_getauxval) {
172*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
173*8d67ca89SAndroid Build Coastguard Worker SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
174*8d67ca89SAndroid Build Coastguard Worker std::string helper = GetTestLibRoot() + "/preinit_getauxval_test_helper";
175*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
176*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ helper.c_str(), nullptr });
177*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
178*8d67ca89SAndroid Build Coastguard Worker #else
179*8d67ca89SAndroid Build Coastguard Worker // Force a failure when not compiled for bionic so the test is considered a pass.
180*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(false);
181*8d67ca89SAndroid Build Coastguard Worker #endif
182*8d67ca89SAndroid Build Coastguard Worker }
183*8d67ca89SAndroid Build Coastguard Worker
184*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,exec_without_ld_preload)185*8d67ca89SAndroid Build Coastguard Worker TEST(dl, exec_without_ld_preload) {
186*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
187*8d67ca89SAndroid Build Coastguard Worker std::string helper = GetTestLibRoot() + "/ld_preload_test_helper";
188*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
189*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ helper.c_str(), nullptr });
190*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
191*8d67ca89SAndroid Build Coastguard Worker #endif
192*8d67ca89SAndroid Build Coastguard Worker }
193*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,exec_with_ld_preload)194*8d67ca89SAndroid Build Coastguard Worker TEST(dl, exec_with_ld_preload) {
195*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
196*8d67ca89SAndroid Build Coastguard Worker std::string helper = GetTestLibRoot() + "/ld_preload_test_helper";
197*8d67ca89SAndroid Build Coastguard Worker std::string env = std::string("LD_PRELOAD=") + GetTestLibRoot() + "/ld_preload_test_helper_lib2.so";
198*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
199*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ helper.c_str(), nullptr });
200*8d67ca89SAndroid Build Coastguard Worker eth.SetEnv({ env.c_str(), nullptr });
201*8d67ca89SAndroid Build Coastguard Worker // ld_preload_test_helper calls get_value_from_lib() and returns the value.
202*8d67ca89SAndroid Build Coastguard Worker // The symbol is defined by two libs: ld_preload_test_helper_lib.so and
203*8d67ca89SAndroid Build Coastguard Worker // ld_preloaded_lib.so. The former is DT_NEEDED and the latter is LD_PRELOADED
204*8d67ca89SAndroid Build Coastguard Worker // via this execution. The main executable is linked to the LD_PRELOADED lib
205*8d67ca89SAndroid Build Coastguard Worker // and the value given from the lib is returned.
206*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
207*8d67ca89SAndroid Build Coastguard Worker #endif
208*8d67ca89SAndroid Build Coastguard Worker }
209*8d67ca89SAndroid Build Coastguard Worker
210*8d67ca89SAndroid Build Coastguard Worker
211*8d67ca89SAndroid Build Coastguard Worker // ld_config_test_helper must fail because it is depending on a lib which is not
212*8d67ca89SAndroid Build Coastguard Worker // in the search path
213*8d67ca89SAndroid Build Coastguard Worker //
214*8d67ca89SAndroid Build Coastguard Worker // Call sequence is...
215*8d67ca89SAndroid Build Coastguard Worker // _helper -- (get_value_from_lib()) -->
216*8d67ca89SAndroid Build Coastguard Worker // _lib1.so -- (get_value_from_another_lib()) -->
217*8d67ca89SAndroid Build Coastguard Worker // _lib2.so (returns 12345)
218*8d67ca89SAndroid Build Coastguard Worker // The two libs are in ns2/ subdir.
TEST(dl,exec_without_ld_config_file)219*8d67ca89SAndroid Build Coastguard Worker TEST(dl, exec_without_ld_config_file) {
220*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
221*8d67ca89SAndroid Build Coastguard Worker std::string error_message = "CANNOT LINK EXECUTABLE \"" + GetTestLibRoot() +
222*8d67ca89SAndroid Build Coastguard Worker "/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" "
223*8d67ca89SAndroid Build Coastguard Worker "not found: needed by main executable\n";
224*8d67ca89SAndroid Build Coastguard Worker std::string helper = GetTestLibRoot() + "/ld_config_test_helper";
225*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
226*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ helper.c_str(), nullptr });
227*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
228*8d67ca89SAndroid Build Coastguard Worker #endif
229*8d67ca89SAndroid Build Coastguard Worker }
230*8d67ca89SAndroid Build Coastguard Worker
231*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
232*8d67ca89SAndroid Build Coastguard Worker extern "C" void android_get_LD_LIBRARY_PATH(char*, size_t);
create_ld_config_file(const char * config_file)233*8d67ca89SAndroid Build Coastguard Worker static void create_ld_config_file(const char* config_file) {
234*8d67ca89SAndroid Build Coastguard Worker char default_search_paths[PATH_MAX];
235*8d67ca89SAndroid Build Coastguard Worker android_get_LD_LIBRARY_PATH(default_search_paths, sizeof(default_search_paths));
236*8d67ca89SAndroid Build Coastguard Worker
237*8d67ca89SAndroid Build Coastguard Worker std::ofstream fout(config_file, std::ios::out);
238*8d67ca89SAndroid Build Coastguard Worker fout << "dir.test = " << GetTestLibRoot() << "/" << std::endl
239*8d67ca89SAndroid Build Coastguard Worker << "[test]" << std::endl
240*8d67ca89SAndroid Build Coastguard Worker << "additional.namespaces = ns2" << std::endl
241*8d67ca89SAndroid Build Coastguard Worker << "namespace.default.search.paths = " << GetTestLibRoot() << std::endl
242*8d67ca89SAndroid Build Coastguard Worker << "namespace.default.links = ns2" << std::endl
243*8d67ca89SAndroid Build Coastguard Worker << "namespace.default.link.ns2.shared_libs = "
244*8d67ca89SAndroid Build Coastguard Worker "libc.so:libm.so:libdl.so:ld_config_test_helper_lib1.so"
245*8d67ca89SAndroid Build Coastguard Worker << std::endl
246*8d67ca89SAndroid Build Coastguard Worker << "namespace.ns2.search.paths = " << default_search_paths << ":" << GetTestLibRoot()
247*8d67ca89SAndroid Build Coastguard Worker << "/ns2" << std::endl;
248*8d67ca89SAndroid Build Coastguard Worker fout.close();
249*8d67ca89SAndroid Build Coastguard Worker }
250*8d67ca89SAndroid Build Coastguard Worker #endif
251*8d67ca89SAndroid Build Coastguard Worker
252*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
253*8d67ca89SAndroid Build Coastguard Worker // This test can't rely on ro.debuggable, because it might have been forced on
254*8d67ca89SAndroid Build Coastguard Worker // in a user build ("Force Debuggable"). In that configuration, ro.debuggable is
255*8d67ca89SAndroid Build Coastguard Worker // true, but Bionic's LD_CONFIG_FILE testing support is still disabled.
is_user_build()256*8d67ca89SAndroid Build Coastguard Worker static bool is_user_build() {
257*8d67ca89SAndroid Build Coastguard Worker return android::base::GetProperty("ro.build.type", "user") == std::string("user");
258*8d67ca89SAndroid Build Coastguard Worker }
259*8d67ca89SAndroid Build Coastguard Worker #endif
260*8d67ca89SAndroid Build Coastguard Worker
261*8d67ca89SAndroid Build Coastguard Worker // lib1.so and lib2.so are now searchable by having another namespace 'ns2'
262*8d67ca89SAndroid Build Coastguard Worker // whose search paths include the 'ns2/' subdir.
263*8d67ca89SAndroid Build Coastguard Worker //
264*8d67ca89SAndroid Build Coastguard Worker // lib1.so is linked with DF_1_GLOBAL, so both it and the executable are added
265*8d67ca89SAndroid Build Coastguard Worker // to every namespace.
266*8d67ca89SAndroid Build Coastguard Worker //
267*8d67ca89SAndroid Build Coastguard Worker // namespace configuration ('*' indicates primary ns)
268*8d67ca89SAndroid Build Coastguard Worker // - default: exe[*], lib1.so
269*8d67ca89SAndroid Build Coastguard Worker // - ns2: exe, lib1.so[*], lib2.so[*]
270*8d67ca89SAndroid Build Coastguard Worker //
TEST(dl,exec_with_ld_config_file)271*8d67ca89SAndroid Build Coastguard Worker TEST(dl, exec_with_ld_config_file) {
272*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
273*8d67ca89SAndroid Build Coastguard Worker SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
274*8d67ca89SAndroid Build Coastguard Worker if (is_user_build()) {
275*8d67ca89SAndroid Build Coastguard Worker GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
276*8d67ca89SAndroid Build Coastguard Worker }
277*8d67ca89SAndroid Build Coastguard Worker std::string helper = GetTestLibRoot() + "/ld_config_test_helper";
278*8d67ca89SAndroid Build Coastguard Worker TemporaryFile config_file;
279*8d67ca89SAndroid Build Coastguard Worker create_ld_config_file(config_file.path);
280*8d67ca89SAndroid Build Coastguard Worker std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
281*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
282*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ helper.c_str(), nullptr });
283*8d67ca89SAndroid Build Coastguard Worker eth.SetEnv({ env.c_str(), nullptr });
284*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0,
285*8d67ca89SAndroid Build Coastguard Worker "foo lib1\n"
286*8d67ca89SAndroid Build Coastguard Worker "lib1_call_funcs\n"
287*8d67ca89SAndroid Build Coastguard Worker "foo lib1\n"
288*8d67ca89SAndroid Build Coastguard Worker "bar lib2\n");
289*8d67ca89SAndroid Build Coastguard Worker #endif
290*8d67ca89SAndroid Build Coastguard Worker }
291*8d67ca89SAndroid Build Coastguard Worker
292*8d67ca89SAndroid Build Coastguard Worker // lib3.so has same foo and bar symbols as lib2.so. lib3.so is LD_PRELOADed.
293*8d67ca89SAndroid Build Coastguard Worker // This test ensures that LD_PRELOADed libs are available to all namespaces.
294*8d67ca89SAndroid Build Coastguard Worker //
295*8d67ca89SAndroid Build Coastguard Worker // namespace configuration ('*' indicates primary ns)
296*8d67ca89SAndroid Build Coastguard Worker // - default: exe[*], lib3.so[*], lib1.so
297*8d67ca89SAndroid Build Coastguard Worker // - ns2: exe, lib3.so, lib1.so[*], lib2.so[*]
298*8d67ca89SAndroid Build Coastguard Worker //
299*8d67ca89SAndroid Build Coastguard Worker // Ensure that, in both namespaces, a call to foo calls the lib3.so symbol,
300*8d67ca89SAndroid Build Coastguard Worker // which then calls the lib1.so symbol using RTLD_NEXT. Ensure that RTLD_NEXT
301*8d67ca89SAndroid Build Coastguard Worker // finds nothing when called from lib1.so.
302*8d67ca89SAndroid Build Coastguard Worker //
303*8d67ca89SAndroid Build Coastguard Worker // For the bar symbol, lib3.so's primary namespace is the default namespace, but
304*8d67ca89SAndroid Build Coastguard Worker // lib2.so is not in the default namespace, so using RTLD_NEXT from lib3.so
305*8d67ca89SAndroid Build Coastguard Worker // doesn't find the symbol in lib2.so.
TEST(dl,exec_with_ld_config_file_with_ld_preload)306*8d67ca89SAndroid Build Coastguard Worker TEST(dl, exec_with_ld_config_file_with_ld_preload) {
307*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
308*8d67ca89SAndroid Build Coastguard Worker SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
309*8d67ca89SAndroid Build Coastguard Worker if (is_user_build()) {
310*8d67ca89SAndroid Build Coastguard Worker GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
311*8d67ca89SAndroid Build Coastguard Worker }
312*8d67ca89SAndroid Build Coastguard Worker std::string helper = GetTestLibRoot() + "/ld_config_test_helper";
313*8d67ca89SAndroid Build Coastguard Worker TemporaryFile config_file;
314*8d67ca89SAndroid Build Coastguard Worker create_ld_config_file(config_file.path);
315*8d67ca89SAndroid Build Coastguard Worker std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
316*8d67ca89SAndroid Build Coastguard Worker std::string env2 = std::string("LD_PRELOAD=") + GetTestLibRoot() + "/ld_config_test_helper_lib3.so";
317*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
318*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ helper.c_str(), nullptr });
319*8d67ca89SAndroid Build Coastguard Worker eth.SetEnv({ env.c_str(), env2.c_str(), nullptr });
320*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0,
321*8d67ca89SAndroid Build Coastguard Worker "foo lib3\n"
322*8d67ca89SAndroid Build Coastguard Worker "foo lib1\n"
323*8d67ca89SAndroid Build Coastguard Worker "lib1_call_funcs\n"
324*8d67ca89SAndroid Build Coastguard Worker "foo lib3\n"
325*8d67ca89SAndroid Build Coastguard Worker "foo lib1\n"
326*8d67ca89SAndroid Build Coastguard Worker "bar lib3\n"
327*8d67ca89SAndroid Build Coastguard Worker "lib3_call_funcs\n"
328*8d67ca89SAndroid Build Coastguard Worker "foo lib3\n"
329*8d67ca89SAndroid Build Coastguard Worker "foo lib1\n"
330*8d67ca89SAndroid Build Coastguard Worker "bar lib3\n");
331*8d67ca89SAndroid Build Coastguard Worker #endif
332*8d67ca89SAndroid Build Coastguard Worker }
333*8d67ca89SAndroid Build Coastguard Worker
334*8d67ca89SAndroid Build Coastguard Worker // ensures that LD_CONFIG_FILE env var does not work for production builds.
335*8d67ca89SAndroid Build Coastguard Worker // The test input is the same as exec_with_ld_config_file, but it must fail in
336*8d67ca89SAndroid Build Coastguard Worker // this case.
TEST(dl,disable_ld_config_file)337*8d67ca89SAndroid Build Coastguard Worker TEST(dl, disable_ld_config_file) {
338*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
339*8d67ca89SAndroid Build Coastguard Worker if (getuid() == 0) {
340*8d67ca89SAndroid Build Coastguard Worker // when executed from the shell (e.g. not as part of CTS), skip the test.
341*8d67ca89SAndroid Build Coastguard Worker // This test is only for CTS.
342*8d67ca89SAndroid Build Coastguard Worker GTEST_SKIP() << "test is not supported with root uid";
343*8d67ca89SAndroid Build Coastguard Worker }
344*8d67ca89SAndroid Build Coastguard Worker if (!is_user_build()) {
345*8d67ca89SAndroid Build Coastguard Worker GTEST_SKIP() << "test requires user build";
346*8d67ca89SAndroid Build Coastguard Worker }
347*8d67ca89SAndroid Build Coastguard Worker
348*8d67ca89SAndroid Build Coastguard Worker std::string error_message =
349*8d67ca89SAndroid Build Coastguard Worker std::string("CANNOT LINK EXECUTABLE ") + "\"" + GetTestLibRoot() +
350*8d67ca89SAndroid Build Coastguard Worker "/ld_config_test_helper\": " +
351*8d67ca89SAndroid Build Coastguard Worker "library \"ld_config_test_helper_lib1.so\" not found: needed by main executable\n";
352*8d67ca89SAndroid Build Coastguard Worker std::string helper = GetTestLibRoot() + "/ld_config_test_helper";
353*8d67ca89SAndroid Build Coastguard Worker TemporaryFile config_file;
354*8d67ca89SAndroid Build Coastguard Worker create_ld_config_file(config_file.path);
355*8d67ca89SAndroid Build Coastguard Worker std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
356*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
357*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ helper.c_str(), nullptr });
358*8d67ca89SAndroid Build Coastguard Worker eth.SetEnv({ env.c_str(), nullptr });
359*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
360*8d67ca89SAndroid Build Coastguard Worker #endif
361*8d67ca89SAndroid Build Coastguard Worker }
362*8d67ca89SAndroid Build Coastguard Worker
RelocationsTest(const char * lib,const char * expectation)363*8d67ca89SAndroid Build Coastguard Worker static void RelocationsTest(const char* lib, const char* expectation) {
364*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
365*8d67ca89SAndroid Build Coastguard Worker // Does readelf think the .so file looks right?
366*8d67ca89SAndroid Build Coastguard Worker const std::string path = GetTestLibRoot() + "/" + lib;
367*8d67ca89SAndroid Build Coastguard Worker ExecTestHelper eth;
368*8d67ca89SAndroid Build Coastguard Worker eth.SetArgs({ "readelf", "-SW", path.c_str(), nullptr });
369*8d67ca89SAndroid Build Coastguard Worker eth.Run([&]() { execvpe("readelf", eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
370*8d67ca89SAndroid Build Coastguard Worker
371*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(std::regex_search(eth.GetOutput(), std::regex(expectation))) << eth.GetOutput();
372*8d67ca89SAndroid Build Coastguard Worker
373*8d67ca89SAndroid Build Coastguard Worker // Can we load it?
374*8d67ca89SAndroid Build Coastguard Worker void* handle = dlopen(lib, RTLD_NOW);
375*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(handle != nullptr) << dlerror();
376*8d67ca89SAndroid Build Coastguard Worker #else
377*8d67ca89SAndroid Build Coastguard Worker UNUSED(lib);
378*8d67ca89SAndroid Build Coastguard Worker UNUSED(expectation);
379*8d67ca89SAndroid Build Coastguard Worker GTEST_SKIP() << "test is not supported on glibc";
380*8d67ca89SAndroid Build Coastguard Worker #endif
381*8d67ca89SAndroid Build Coastguard Worker }
382*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,relocations_RELR)383*8d67ca89SAndroid Build Coastguard Worker TEST(dl, relocations_RELR) {
384*8d67ca89SAndroid Build Coastguard Worker RelocationsTest("librelocations-RELR.so", "\\.relr\\.dyn * RELR");
385*8d67ca89SAndroid Build Coastguard Worker }
386*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,relocations_ANDROID_RELR)387*8d67ca89SAndroid Build Coastguard Worker TEST(dl, relocations_ANDROID_RELR) {
388*8d67ca89SAndroid Build Coastguard Worker RelocationsTest("librelocations-ANDROID_RELR.so", "\\.relr\\.dyn * ANDROID_RELR");
389*8d67ca89SAndroid Build Coastguard Worker }
390*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,relocations_ANDROID_REL)391*8d67ca89SAndroid Build Coastguard Worker TEST(dl, relocations_ANDROID_REL) {
392*8d67ca89SAndroid Build Coastguard Worker RelocationsTest("librelocations-ANDROID_REL.so",
393*8d67ca89SAndroid Build Coastguard Worker #if __LP64__
394*8d67ca89SAndroid Build Coastguard Worker "\\.rela\\.dyn * ANDROID_RELA"
395*8d67ca89SAndroid Build Coastguard Worker #else
396*8d67ca89SAndroid Build Coastguard Worker "\\.rel\\.dyn * ANDROID_REL"
397*8d67ca89SAndroid Build Coastguard Worker #endif
398*8d67ca89SAndroid Build Coastguard Worker );
399*8d67ca89SAndroid Build Coastguard Worker }
400*8d67ca89SAndroid Build Coastguard Worker
TEST(dl,relocations_fat)401*8d67ca89SAndroid Build Coastguard Worker TEST(dl, relocations_fat) {
402*8d67ca89SAndroid Build Coastguard Worker RelocationsTest("librelocations-fat.so",
403*8d67ca89SAndroid Build Coastguard Worker #if __LP64__
404*8d67ca89SAndroid Build Coastguard Worker "\\.rela\\.dyn * RELA"
405*8d67ca89SAndroid Build Coastguard Worker #else
406*8d67ca89SAndroid Build Coastguard Worker "\\.rel\\.dyn * REL"
407*8d67ca89SAndroid Build Coastguard Worker #endif
408*8d67ca89SAndroid Build Coastguard Worker );
409*8d67ca89SAndroid Build Coastguard Worker }
410