Reland: Fix libnativeloader to correctly link to the platform namespace.

This relands http://r.android.com/951440:

This affected binaries in the Runtime APEX, where the platform namespace is
"platform" rather than "default".

Also extend ANDROID_ADDITIONAL_PUBLIC_LIBRARIES to create links to both to
platform and runtime namespaces, so that it can be used to open up access to
internal libraries in the Runtime APEX as well, which is used by ART gtests
and run tests.

Also update some comments in the ld.config*.txt files to accurately explain
why some namespaces need to be visible, and some other minor changes for
consistency. There are no semantically significant changes in those files.

Test: Flash and boot
Test: Run an ART run test with the internal libarttest.so library
Test: lunch aosp_cf_x86_phone-eng;
  atest android.compilation.cts.AdbRootDependentCompilationTest#testCompile_curProfile \
    com.android.cts.dexmetadata.InstallDexMetadataHostTest#testProfileSnapshotAfterInstall \
    installd_dexopt_test
Bug: 130293232
Bug: 121117762
Change-Id: I3d9f2102a03e83843e15bc78b5ad347220c52769
diff --git a/libnativeloader/native_loader.cpp b/libnativeloader/native_loader.cpp
index e460b1a..1012ea0 100644
--- a/libnativeloader/native_loader.cpp
+++ b/libnativeloader/native_loader.cpp
@@ -126,7 +126,7 @@
 // classloader, the classloader-namespace namespace associated with that
 // classloader is selected for dlopen. The namespace is configured so that its
 // search path is set to the app-local JNI directory and it is linked to the
-// default namespace with the names of libs listed in the public.libraries.txt.
+// platform namespace with the names of libs listed in the public.libraries.txt.
 // This way an app can only load its own JNI libraries along with the public libs.
 static constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
 // Same thing for vendor APKs.
@@ -309,21 +309,24 @@
       }
     }
 
-    std::string runtime_exposed_libraries = base::Join(kRuntimePublicLibraries, ":");
+    std::string runtime_exposed_libraries = runtime_public_libraries_;
 
     NativeLoaderNamespace native_loader_ns;
     if (!is_native_bridge) {
+      // The platform namespace is called "default" for binaries in /system and
+      // "platform" for those in the Runtime APEX. Try "platform" first since
+      // "default" always exists.
+      android_namespace_t* platform_ns = android_get_exported_namespace(kPlatformNamespaceName);
+      if (platform_ns == nullptr) {
+        platform_ns = android_get_exported_namespace(kDefaultNamespaceName);
+      }
+
       android_namespace_t* android_parent_ns;
       if (parent_ns != nullptr) {
         android_parent_ns = parent_ns->get_android_ns();
       } else {
-        // Fall back to the platform namespace if no parent is found. It is
-        // called "default" for binaries in /system and "platform" for those in
-        // the Runtime APEX. Try "platform" first since "default" always exists.
-        android_parent_ns = android_get_exported_namespace(kPlatformNamespaceName);
-        if (android_parent_ns == nullptr) {
-          android_parent_ns = android_get_exported_namespace(kDefaultNamespaceName);
-        }
+        // Fall back to the platform namespace if no parent is found.
+        android_parent_ns = platform_ns;
       }
 
       android_namespace_t* ns = android_create_namespace(namespace_name,
@@ -344,7 +347,7 @@
 
       android_namespace_t* runtime_ns = android_get_exported_namespace(kRuntimeNamespaceName);
 
-      if (!android_link_namespaces(ns, nullptr, system_exposed_libraries.c_str())) {
+      if (!android_link_namespaces(ns, platform_ns, system_exposed_libraries.c_str())) {
         *error_msg = dlerror();
         return nullptr;
       }
@@ -374,14 +377,19 @@
 
       native_loader_ns = NativeLoaderNamespace(ns);
     } else {
+      // Same functionality as in the branch above, but calling through native bridge.
+
+      native_bridge_namespace_t* platform_ns =
+          NativeBridgeGetExportedNamespace(kPlatformNamespaceName);
+      if (platform_ns == nullptr) {
+        platform_ns = NativeBridgeGetExportedNamespace(kDefaultNamespaceName);
+      }
+
       native_bridge_namespace_t* native_bridge_parent_namespace;
       if (parent_ns != nullptr) {
         native_bridge_parent_namespace = parent_ns->get_native_bridge_ns();
       } else {
-        native_bridge_parent_namespace = NativeBridgeGetExportedNamespace(kPlatformNamespaceName);
-        if (native_bridge_parent_namespace == nullptr) {
-          native_bridge_parent_namespace = NativeBridgeGetExportedNamespace(kDefaultNamespaceName);
-        }
+        native_bridge_parent_namespace = platform_ns;
       }
 
       native_bridge_namespace_t* ns = NativeBridgeCreateNamespace(namespace_name,
@@ -399,7 +407,7 @@
       native_bridge_namespace_t* runtime_ns =
           NativeBridgeGetExportedNamespace(kRuntimeNamespaceName);
 
-      if (!NativeBridgeLinkNamespaces(ns, nullptr, system_exposed_libraries.c_str())) {
+      if (!NativeBridgeLinkNamespaces(ns, platform_ns, system_exposed_libraries.c_str())) {
         *error_msg = NativeBridgeGetError();
         return nullptr;
       }
@@ -451,6 +459,7 @@
     std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
     std::string public_native_libraries_system_config =
             root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
+    std::string runtime_public_libraries = base::Join(kRuntimePublicLibraries, ":");
     std::string llndk_native_libraries_system_config =
             root_dir + kLlndkNativeLibrariesSystemConfigPathFromRoot;
     std::string vndksp_native_libraries_system_config =
@@ -472,6 +481,10 @@
         std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
         std::copy(additional_libs_vector.begin(), additional_libs_vector.end(),
                   std::back_inserter(sonames));
+        // Apply the same list to the runtime namespace, since some libraries
+        // might reside there.
+        CHECK(sizeof(kRuntimePublicLibraries) > 0);
+        runtime_public_libraries = runtime_public_libraries + ':' + additional_libs;
       }
     }
 
@@ -497,6 +510,7 @@
     }
 
     system_public_libraries_ = base::Join(sonames, ':');
+    runtime_public_libraries_ = runtime_public_libraries;
 
     // read /system/etc/public.libraries-<companyname>.txt which contain partner defined
     // system libs that are exposed to apps. The libs in the txt files must be
@@ -724,6 +738,7 @@
   bool initialized_;
   std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
   std::string system_public_libraries_;
+  std::string runtime_public_libraries_;
   std::string vendor_public_libraries_;
   std::string oem_public_libraries_;
   std::string product_public_libraries_;