Ensure same order of global group members in all NS's
During "step 1" of find_libraries, the linker finds the transitive
closure of dependencies, in BFS order. As it finds each library, it
adds the library to its primary namespace (so that, if some other
library also depends on it, find_loaded_library_by_soname can find the
library in the process of being loaded).
LD_PRELOAD libraries are automatically marked DF_1_GLOBAL, and any
DF_1_GLOBAL library is added to every linker namespace. Previously,
this secondary namespace registration happened after step 1. The result
is that across different namespaces, the order of libraries could vary.
In general, a namespace's primary members will all appear before
secondary members. This is undesirable for libsigchain.so, which we
want to have appear before any other non-preloaded library.
Instead, when an soinfo is added to its primary namespace, immediately
add it to all the other namespaces, too. This ensures that the order of
soinfo objects is the same across namespaces.
Expand the dl.exec_with_ld_config_file_with_ld_preload and
dl.exec_with_ld_config_file tests to cover the new behavior. Mark
lib1.so DF_1_GLOBAL and use a "foo" symbol to mimic the behavior of a
signal API interposed by (e.g.) libsigchain.so and a ASAN preload.
Test: bionic unit tests
Bug: http://b/143219447
Change-Id: I9fd90f6f0d14caf1aca6d414b3e9aab77deca3ff
diff --git a/tests/dl_test.cpp b/tests/dl_test.cpp
index 6adba19..6c9bf3f 100644
--- a/tests/dl_test.cpp
+++ b/tests/dl_test.cpp
@@ -268,8 +268,16 @@
}
#endif
-// _lib1.so and _lib2.so are now searchable by having another namespace 'ns2'
+// lib1.so and lib2.so are now searchable by having another namespace 'ns2'
// whose search paths include the 'ns2/' subdir.
+//
+// lib1.so is linked with DF_1_GLOBAL, so both it and the executable are added
+// to every namespace.
+//
+// namespace configuration ('*' indicates primary ns)
+// - default: exe[*], lib1.so
+// - ns2: exe, lib1.so[*], lib2.so[*]
+//
TEST(dl, exec_with_ld_config_file) {
#if defined(__BIONIC__)
SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
@@ -285,13 +293,28 @@
ExecTestHelper eth;
eth.SetArgs({ helper.c_str(), nullptr });
eth.SetEnv({ env.c_str(), nullptr });
- eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
+ eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0,
+ "foo lib1\n"
+ "lib1_call_funcs\n"
+ "foo lib1\n"
+ "bar lib2\n");
#endif
}
-// _lib3.so has same symbol as lib2.so but returns 54321. _lib3.so is
-// LD_PRELOADed. This test is to ensure LD_PRELOADed libs are available to
-// additional namespaces other than the default namespace.
+// lib3.so has same foo and bar symbols as lib2.so. lib3.so is LD_PRELOADed.
+// This test ensures that LD_PRELOADed libs are available to all namespaces.
+//
+// namespace configuration ('*' indicates primary ns)
+// - default: exe[*], lib3.so[*], lib1.so
+// - ns2: exe, lib3.so, lib1.so[*], lib2.so[*]
+//
+// Ensure that, in both namespaces, a call to foo calls the lib3.so symbol,
+// which then calls the lib1.so symbol using RTLD_NEXT. Ensure that RTLD_NEXT
+// finds nothing when called from lib1.so.
+//
+// For the bar symbol, lib3.so's primary namespace is the default namespace, but
+// lib2.so is not in the default namespace, so using RTLD_NEXT from lib3.so
+// doesn't find the symbol in lib2.so.
TEST(dl, exec_with_ld_config_file_with_ld_preload) {
#if defined(__BIONIC__)
SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
@@ -308,7 +331,17 @@
ExecTestHelper eth;
eth.SetArgs({ helper.c_str(), nullptr });
eth.SetEnv({ env.c_str(), env2.c_str(), nullptr });
- eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
+ eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0,
+ "foo lib3\n"
+ "foo lib1\n"
+ "lib1_call_funcs\n"
+ "foo lib3\n"
+ "foo lib1\n"
+ "bar lib3\n"
+ "lib3_call_funcs\n"
+ "foo lib3\n"
+ "foo lib1\n"
+ "bar lib3\n");
#endif
}
diff --git a/tests/libs/Android.bp b/tests/libs/Android.bp
index 385d120..ba1c198 100644
--- a/tests/libs/Android.bp
+++ b/tests/libs/Android.bp
@@ -1478,6 +1478,8 @@
srcs: ["ld_config_test_helper_lib1.cpp"],
shared_libs: ["ld_config_test_helper_lib2"],
relative_install_path: "bionic-loader-test-libs/ns2",
+ // Mark the library DF_1_GLOBAL so it is added to every linker namespace.
+ ldflags: ["-Wl,-z,global"]
}
cc_test_library {
diff --git a/tests/libs/ld_config_test_helper.cpp b/tests/libs/ld_config_test_helper.cpp
index 87e512e..1249121 100644
--- a/tests/libs/ld_config_test_helper.cpp
+++ b/tests/libs/ld_config_test_helper.cpp
@@ -22,7 +22,9 @@
#endif
#include <unistd.h>
-extern int get_value_from_lib();
+extern "C" void foo();
+void lib1_call_funcs();
+__attribute__((weak)) void lib3_call_funcs();
int main() {
bool skip_vdso_check = false;
@@ -45,6 +47,9 @@
dlclose(handle);
}
- printf("%d", get_value_from_lib());
+ foo();
+ lib1_call_funcs();
+ if (lib3_call_funcs) lib3_call_funcs();
+
return 0;
}
diff --git a/tests/libs/ld_config_test_helper_lib1.cpp b/tests/libs/ld_config_test_helper_lib1.cpp
index fc5401a..ffa9a45 100644
--- a/tests/libs/ld_config_test_helper_lib1.cpp
+++ b/tests/libs/ld_config_test_helper_lib1.cpp
@@ -1,4 +1,19 @@
-extern int get_value_from_another_lib();
-int get_value_from_lib() {
- return get_value_from_another_lib();
+#include <dlfcn.h>
+#include <stdio.h>
+
+// Mark foo and bar weak so that Clang allows the run-time linker to decide which DSO's symbol to
+// use.
+
+__attribute__((weak)) extern "C" void foo() {
+ printf("foo lib1\n");
+ void (*next)(void) = reinterpret_cast<void (*)()>(dlsym(RTLD_NEXT, "foo"));
+ if (next != nullptr) next();
+}
+
+__attribute__((weak)) extern "C" void bar();
+
+void lib1_call_funcs() {
+ printf("lib1_call_funcs\n");
+ foo();
+ bar();
}
diff --git a/tests/libs/ld_config_test_helper_lib2.cpp b/tests/libs/ld_config_test_helper_lib2.cpp
index a620a6c..d5bca2c 100644
--- a/tests/libs/ld_config_test_helper_lib2.cpp
+++ b/tests/libs/ld_config_test_helper_lib2.cpp
@@ -1,3 +1,11 @@
-int get_value_from_another_lib() {
- return 12345;
+#include <dlfcn.h>
+#include <stdio.h>
+
+// Mark foo and bar weak so that Clang allows the run-time linker to decide which DSO's symbol to
+// use.
+
+__attribute__((weak)) extern "C" void bar() {
+ printf("bar lib2\n");
+ void (*next)(void) = reinterpret_cast<void (*)()>(dlsym(RTLD_NEXT, "bar"));
+ if (next != nullptr) next();
}
diff --git a/tests/libs/ld_config_test_helper_lib3.cpp b/tests/libs/ld_config_test_helper_lib3.cpp
index 93d1cd8..94e1570 100644
--- a/tests/libs/ld_config_test_helper_lib3.cpp
+++ b/tests/libs/ld_config_test_helper_lib3.cpp
@@ -1,3 +1,23 @@
-int get_value_from_another_lib() {
- return 54321;
+#include <dlfcn.h>
+#include <stdio.h>
+
+// Mark foo and bar weak so that Clang allows the run-time linker to decide which DSO's symbol to
+// use.
+
+__attribute__((weak)) extern "C" void foo() {
+ printf("foo lib3\n");
+ void (*next)(void) = reinterpret_cast<void (*)()>(dlsym(RTLD_NEXT, "foo"));
+ if (next != nullptr) next();
+}
+
+__attribute__((weak)) extern "C" void bar() {
+ printf("bar lib3\n");
+ void (*next)(void) = reinterpret_cast<void (*)()>(dlsym(RTLD_NEXT, "bar"));
+ if (next != nullptr) next();
+}
+
+void lib3_call_funcs() {
+ printf("lib3_call_funcs\n");
+ foo();
+ bar();
}