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