• 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 
29 #include <string>
30 #include <iostream>
31 #include <fstream>
32 
33 #include "gtest_globals.h"
34 #include <android-base/file.h>
35 #include "utils.h"
36 
main_global_default_serial()37 extern "C" int main_global_default_serial() {
38   return 3370318;
39 }
40 
main_global_protected_serial()41 extern "C" int main_global_protected_serial() {
42   return 2716057;
43 }
44 
45 // The following functions are defined in DT_NEEDED
46 // libdl_preempt_test.so library.
47 
48 // This one calls main_global_default_serial
49 extern "C" int main_global_default_get_serial();
50 
51 // This one calls main_global_protected_serial
52 extern "C" int main_global_protected_get_serial();
53 
54 // This one calls lib_global_default_serial
55 extern "C" int lib_global_default_get_serial();
56 
57 // This one calls lib_global_protected_serial
58 extern "C" int lib_global_protected_get_serial();
59 
60 // This test verifies that the global default function
61 // main_global_default_serial() is preempted by
62 // the function defined above.
TEST(dl,main_preempts_global_default)63 TEST(dl, main_preempts_global_default) {
64   ASSERT_EQ(3370318, main_global_default_get_serial());
65 }
66 
67 // This one makes sure that the global protected
68 // symbols do not get preempted
TEST(dl,main_does_not_preempt_global_protected)69 TEST(dl, main_does_not_preempt_global_protected) {
70   ASSERT_EQ(3370318, main_global_protected_get_serial());
71 }
72 
73 // check same things for lib
TEST(dl,lib_preempts_global_default)74 TEST(dl, lib_preempts_global_default) {
75   ASSERT_EQ(3370318, lib_global_default_get_serial());
76 }
77 
TEST(dl,lib_does_not_preempt_global_protected)78 TEST(dl, lib_does_not_preempt_global_protected) {
79   ASSERT_EQ(3370318, lib_global_protected_get_serial());
80 }
81 
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 #endif
89 
TEST(dl,exec_linker)90 TEST(dl, exec_linker) {
91 #if defined(__BIONIC__)
92   std::string usage_prefix = std::string("Usage: ") + kPathToLinker;
93   ExecTestHelper eth;
94   eth.SetArgs({ kPathToLinker, nullptr });
95   eth.Run([&]() { execve(kPathToLinker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
96   ASSERT_EQ(0u, eth.GetOutput().find(usage_prefix)) << "Test output:\n" << eth.GetOutput();
97 #endif
98 }
99 
TEST(dl,exec_linker_load_file)100 TEST(dl, exec_linker_load_file) {
101 #if defined(__BIONIC__)
102   std::string helper = GetTestlibRoot() +
103       "/exec_linker_helper/exec_linker_helper";
104   std::string expected_output =
105       "ctor: argc=1 argv[0]=" + helper + "\n" +
106       "main: argc=1 argv[0]=" + helper + "\n" +
107       "__progname=" + helper + "\n" +
108       "helper_func called\n";
109   ExecTestHelper eth;
110   eth.SetArgs({ kPathToLinker, helper.c_str(), nullptr });
111   eth.Run([&]() { execve(kPathToLinker, eth.GetArgs(), eth.GetEnv()); }, 0, expected_output.c_str());
112 #endif
113 }
114 
TEST(dl,exec_linker_load_from_zip)115 TEST(dl, exec_linker_load_from_zip) {
116 #if defined(__BIONIC__)
117   std::string helper = GetTestlibRoot() +
118       "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip!/libdir/exec_linker_helper";
119   std::string expected_output =
120       "ctor: argc=1 argv[0]=" + helper + "\n" +
121       "main: argc=1 argv[0]=" + helper + "\n" +
122       "__progname=" + helper + "\n" +
123       "helper_func called\n";
124   ExecTestHelper eth;
125   eth.SetArgs({ kPathToLinker, helper.c_str(), nullptr });
126   eth.Run([&]() { execve(kPathToLinker, eth.GetArgs(), eth.GetEnv()); }, 0, expected_output.c_str());
127 #endif
128 }
129 
TEST(dl,exec_linker_load_self)130 TEST(dl, exec_linker_load_self) {
131 #if defined(__BIONIC__)
132   std::string error_message = "error: linker cannot load itself\n";
133   ExecTestHelper eth;
134   eth.SetArgs({ kPathToLinker, kPathToLinker, nullptr });
135   eth.Run([&]() { execve(kPathToLinker, eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
136 #endif
137 }
138 
TEST(dl,preinit_system_calls)139 TEST(dl, preinit_system_calls) {
140 #if defined(__BIONIC__)
141   SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
142   std::string helper = GetTestlibRoot() +
143       "/preinit_syscall_test_helper/preinit_syscall_test_helper";
144   chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
145   ExecTestHelper eth;
146   eth.SetArgs({ helper.c_str(), nullptr });
147   eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
148 #endif
149 }
150 
TEST(dl,preinit_getauxval)151 TEST(dl, preinit_getauxval) {
152 #if defined(__BIONIC__)
153   SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
154   std::string helper = GetTestlibRoot() +
155       "/preinit_getauxval_test_helper/preinit_getauxval_test_helper";
156   chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
157   ExecTestHelper eth;
158   eth.SetArgs({ helper.c_str(), nullptr });
159   eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
160 #else
161   // Force a failure when not compiled for bionic so the test is considered a pass.
162   ASSERT_TRUE(false);
163 #endif
164 }
165 
166 
TEST(dl,exec_without_ld_preload)167 TEST(dl, exec_without_ld_preload) {
168 #if defined(__BIONIC__)
169   std::string helper = GetTestlibRoot() +
170       "/ld_preload_test_helper/ld_preload_test_helper";
171   chmod(helper.c_str(), 0755);
172   ExecTestHelper eth;
173   eth.SetArgs({ helper.c_str(), nullptr });
174   eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
175 #endif
176 }
177 
TEST(dl,exec_with_ld_preload)178 TEST(dl, exec_with_ld_preload) {
179 #if defined(__BIONIC__)
180   std::string helper = GetTestlibRoot() +
181       "/ld_preload_test_helper/ld_preload_test_helper";
182   std::string env = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_preload_test_helper_lib2.so";
183   chmod(helper.c_str(), 0755);
184   ExecTestHelper eth;
185   eth.SetArgs({ helper.c_str(), nullptr });
186   eth.SetEnv({ env.c_str(), nullptr });
187   // ld_preload_test_helper calls get_value_from_lib() and returns the value.
188   // The symbol is defined by two libs: ld_preload_test_helper_lib.so and
189   // ld_preloaded_lib.so. The former is DT_NEEDED and the latter is LD_PRELOADED
190   // via this execution. The main executable is linked to the LD_PRELOADED lib
191   // and the value given from the lib is returned.
192   eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
193 #endif
194 }
195 
196 
197 // ld_config_test_helper must fail because it is depending on a lib which is not
198 // in the search path
199 //
200 // Call sequence is...
201 // _helper -- (get_value_from_lib()) -->
202 //     _lib1.so -- (get_value_from_another_lib()) -->
203 //       _lib2.so (returns 12345)
204 // The two libs are in ns2/ subdir.
TEST(dl,exec_without_ld_config_file)205 TEST(dl, exec_without_ld_config_file) {
206 #if defined(__BIONIC__)
207   std::string error_message = "CANNOT LINK EXECUTABLE \"" + GetTestlibRoot() + "/ld_config_test_helper/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" not found\n";
208   std::string helper = GetTestlibRoot() +
209       "/ld_config_test_helper/ld_config_test_helper";
210   chmod(helper.c_str(), 0755);
211   ExecTestHelper eth;
212   eth.SetArgs({ helper.c_str(), nullptr });
213   eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
214 #endif
215 }
216 
217 #if defined(__BIONIC__)
218 extern "C" void android_get_LD_LIBRARY_PATH(char*, size_t);
create_ld_config_file(const char * config_file)219 static void create_ld_config_file(const char* config_file) {
220   char default_search_paths[PATH_MAX];
221   android_get_LD_LIBRARY_PATH(default_search_paths, sizeof(default_search_paths));
222 
223   std::ofstream fout(config_file, std::ios::out);
224   fout << "dir.test = " << GetTestlibRoot() << "/ld_config_test_helper/" << std::endl
225        << "[test]" << std::endl
226        << "additional.namespaces = ns2" << std::endl
227        << "namespace.default.search.paths = " << GetTestlibRoot() << std::endl
228        << "namespace.default.links = ns2" << std::endl
229        << "namespace.default.link.ns2.shared_libs = libc.so:libm.so:libdl.so:ld_config_test_helper_lib1.so" << std::endl
230        << "namespace.ns2.search.paths = " << default_search_paths << ":" << GetTestlibRoot() << "/ns2" << std::endl;
231   fout.close();
232 }
233 #endif
234 
235 #if defined(__BIONIC__)
is_debuggable_build()236 static bool is_debuggable_build() {
237   return android::base::GetBoolProperty("ro.debuggable", false);
238 }
239 #endif
240 
241 // _lib1.so and _lib2.so are now searchable by having another namespace 'ns2'
242 // whose search paths include the 'ns2/' subdir.
TEST(dl,exec_with_ld_config_file)243 TEST(dl, exec_with_ld_config_file) {
244 #if defined(__BIONIC__)
245   SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
246   if (!is_debuggable_build()) {
247     // LD_CONFIG_FILE is not supported on user build
248     return;
249   }
250   std::string helper = GetTestlibRoot() +
251       "/ld_config_test_helper/ld_config_test_helper";
252   TemporaryFile config_file;
253   create_ld_config_file(config_file.path);
254   std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
255   chmod(helper.c_str(), 0755);
256   ExecTestHelper eth;
257   eth.SetArgs({ helper.c_str(), nullptr });
258   eth.SetEnv({ env.c_str(), nullptr });
259   eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
260 #endif
261 }
262 
263 // _lib3.so has same symbol as lib2.so but returns 54321. _lib3.so is
264 // LD_PRELOADed. This test is to ensure LD_PRELOADed libs are available to
265 // additional namespaces other than the default namespace.
TEST(dl,exec_with_ld_config_file_with_ld_preload)266 TEST(dl, exec_with_ld_config_file_with_ld_preload) {
267 #if defined(__BIONIC__)
268   SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
269   if (!is_debuggable_build()) {
270     // LD_CONFIG_FILE is not supported on user build
271     return;
272   }
273   std::string helper = GetTestlibRoot() +
274       "/ld_config_test_helper/ld_config_test_helper";
275   TemporaryFile config_file;
276   create_ld_config_file(config_file.path);
277   std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
278   std::string env2 = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_config_test_helper_lib3.so";
279   chmod(helper.c_str(), 0755);
280   ExecTestHelper eth;
281   eth.SetArgs({ helper.c_str(), nullptr });
282   eth.SetEnv({ env.c_str(), env2.c_str(), nullptr });
283   eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
284 #endif
285 }
286 
287 // ensures that LD_CONFIG_FILE env var does not work for production builds.
288 // The test input is the same as exec_with_ld_config_file, but it must fail in
289 // this case.
TEST(dl,disable_ld_config_file)290 TEST(dl, disable_ld_config_file) {
291 #if defined(__BIONIC__)
292   if (getuid() == 0) {
293     // when executed from the shell (e.g. not as part of CTS), skip the test.
294     // This test is only for CTS.
295     return;
296   }
297   if (is_debuggable_build()) {
298     // Skip the test for non production devices
299     return;
300   }
301 
302   std::string error_message = "CANNOT LINK EXECUTABLE \"" + GetTestlibRoot() + "/ld_config_test_helper/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" not found\n";
303   std::string helper = GetTestlibRoot() +
304       "/ld_config_test_helper/ld_config_test_helper";
305   TemporaryFile config_file;
306   create_ld_config_file(config_file.path);
307   std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
308   chmod(helper.c_str(), 0755);
309   ExecTestHelper eth;
310   eth.SetArgs({ helper.c_str(), nullptr });
311   eth.SetEnv({ env.c_str(), nullptr });
312   eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
313 #endif
314 }
315