Merge "Fix handling mkdir failure"
diff --git a/contents/common/system_links.cc b/contents/common/system_links.cc
index ecca2e4..1056678 100644
--- a/contents/common/system_links.cc
+++ b/contents/common/system_links.cc
@@ -16,9 +16,12 @@
 
 #include <string>
 
+#include <android-base/strings.h>
+
 #include "linkerconfig/common.h"
 #include "linkerconfig/context.h"
 #include "linkerconfig/section.h"
+#include "linkerconfig/variables.h"
 
 namespace android {
 namespace linkerconfig {
@@ -35,6 +38,14 @@
           .AddSharedLib("@{STUB_LIBRARIES}", "@{SANITIZER_RUNTIME_LIBRARIES}");
     }
   });
+
+  Namespace* system_ns = section->GetNamespace(system_ns_name);
+  if (system_ns) {
+    std::optional<std::string> stub_libraries_var =
+        android::linkerconfig::modules::Variables::GetValue("STUB_LIBRARIES");
+    system_ns->AddProvides(
+        android::base::Split(stub_libraries_var.value_or(""), ":"));
+  }
 }
 }  // namespace contents
 }  // namespace linkerconfig
diff --git a/contents/namespace/adbd.cc b/contents/namespace/adbd.cc
index 15c30e6..cb8c9a9 100644
--- a/contents/namespace/adbd.cc
+++ b/contents/namespace/adbd.cc
@@ -31,6 +31,16 @@
   Namespace ns("adbd", /*is_isolated=*/true, /*is_visible=*/false);
   ns.AddSearchPath("/apex/com.android.adbd/${LIB}", AsanPath::SAME_PATH);
   ns.AddPermittedPath("/system/${LIB}");
+
+  ns.AddProvides(std::vector{
+      "libadbconnection_client.so",
+  });
+  ns.AddRequires(std::vector{
+      "libc.so",
+      "libdl.so",
+      "liblog.so",
+      "libm.so",
+  });
   return ns;
 }
 }  // namespace contents
diff --git a/contents/namespace/art.cc b/contents/namespace/art.cc
index 5b73fdc..89b7bf4 100644
--- a/contents/namespace/art.cc
+++ b/contents/namespace/art.cc
@@ -47,9 +47,31 @@
   // classloader-namespace for oat files, and tighten this up.
   ns.GetLink(ctx.GetSystemNamespaceName()).AllowAllSharedLibs();
 
-  ns.GetLink("adbd").AddSharedLib("libadbconnection_client.so");
-  ns.GetLink("neuralnetworks").AddSharedLib("libneuralnetworks.so");
-
+  ns.AddProvides(std::vector{
+      "libandroidicu.so",
+      "libandroidio.so",
+      "libdexfile_external.so",
+      "libdexfiled_external.so",
+      "libnativebridge.so",
+      "libnativehelper.so",
+      "libnativeloader.so",
+      // TODO(b/122876336): Remove libpac.so once it's migrated to Webview
+      "libpac.so",
+      // TODO(b/120786417 or b/134659294): libicuuc.so
+      // and libicui18n.so are kept for app compat.
+      "libicui18n.so",
+      "libicuuc.so",
+  });
+  ns.AddRequires(std::vector{
+      "libadbconnection_client.so",
+      "libc.so",
+      "libdl.so",
+      "libdl_android.so",
+      "liblog.so",
+      "libm.so",
+      // not listed in the manifest, but add here to preserve original configuration
+      "libneuralnetworks.so",
+  });
   return ns;
 }
 
diff --git a/contents/namespace/conscrypt.cc b/contents/namespace/conscrypt.cc
index 0356ed2..b40acfb 100644
--- a/contents/namespace/conscrypt.cc
+++ b/contents/namespace/conscrypt.cc
@@ -31,13 +31,19 @@
 namespace linkerconfig {
 namespace contents {
 Namespace BuildConscryptNamespace([[maybe_unused]] const Context& ctx) {
-  Namespace ns("conscrypt", /*is_isolated=*/true,
+  Namespace ns("conscrypt",
+               /*is_isolated=*/true,
                /*is_visible=*/true);
 
   ns.AddSearchPath("/apex/com.android.conscrypt/${LIB}", AsanPath::SAME_PATH);
   ns.AddPermittedPath("/system/${LIB}");
-  ns.GetLink("art").AddSharedLib("libandroidio.so");
-
+  ns.AddRequires(std::vector{
+      "libandroidio.so",
+      "libc.so",
+      "libdl.so",
+      "liblog.so",
+      "libm.so",
+  });
   return ns;
 }
 }  // namespace contents
diff --git a/contents/namespace/neuralnetworks.cc b/contents/namespace/neuralnetworks.cc
index 2638b86..4daa4a1 100644
--- a/contents/namespace/neuralnetworks.cc
+++ b/contents/namespace/neuralnetworks.cc
@@ -32,7 +32,19 @@
   ns.AddSearchPath("/apex/com.android.neuralnetworks/${LIB}",
                    AsanPath::SAME_PATH);
   ns.AddPermittedPath("/system/${LIB}");
-
+  ns.AddProvides(std::vector{
+      "libneuralnetworks.so",
+  });
+  ns.AddRequires(std::vector{
+      "libc.so",
+      "libcgrouprc.so",
+      "libdl.so",
+      "liblog.so",
+      "libm.so",
+      "libnativewindow.so",
+      "libneuralnetworks_packageinfo.so",
+      "libvndksupport.so",
+  });
   return ns;
 }
 }  // namespace contents
diff --git a/contents/namespace/resolv.cc b/contents/namespace/resolv.cc
index 545f5d9..e89b3a6 100644
--- a/contents/namespace/resolv.cc
+++ b/contents/namespace/resolv.cc
@@ -32,6 +32,15 @@
   ns.AddSearchPath("/apex/com.android.resolv/${LIB}", AsanPath::SAME_PATH);
   ns.AddPermittedPath("/system/${LIB}");
 
+  ns.AddProvides(std::vector{
+      "libnetd_resolv.so",
+  });
+  ns.AddRequires(std::vector{
+      "libbinder_ndk.so",
+      "libc.so",
+      "libdl.so",
+      "libm.so",
+  });
   return ns;
 }
 }  // namespace contents
diff --git a/contents/namespace/systemdefault.cc b/contents/namespace/systemdefault.cc
index 8f76ede..71bc341 100644
--- a/contents/namespace/systemdefault.cc
+++ b/contents/namespace/systemdefault.cc
@@ -27,22 +27,6 @@
 
 namespace {
 
-// Keep in sync with the "platform" namespace in art/build/apex/ld.config.txt.
-const std::vector<std::string> kLibsFromArt = {
-    "libdexfile_external.so",
-    "libdexfiled_external.so",
-    "libnativebridge.so",
-    "libnativehelper.so",
-    "libnativeloader.so",
-    "libandroidicu.so",
-    // TODO(b/122876336): Remove libpac.so once it's migrated to Webview
-    "libpac.so",
-    // TODO(b/120786417 or b/134659294): libicuuc.so
-    // and libicui18n.so are kept for app compat.
-    "libicui18n.so",
-    "libicuuc.so",
-};
-
 // We can't have entire /system/${LIB} as permitted paths because doing so makes
 // it possible to load libs in /system/${LIB}/vndk* directories by their
 // absolute paths, e.g. dlopen("/system/lib/vndk/libbase.so"). VNDK libs are
@@ -121,11 +105,25 @@
     BuildPermittedPath(ns);
   }
 
-  ns.GetLink("art").AddSharedLib(kLibsFromArt);
-
-  ns.GetLink("resolv").AddSharedLib("libnetd_resolv.so");
-  ns.GetLink("neuralnetworks").AddSharedLib("libneuralnetworks.so");
-
+  ns.AddRequires(std::vector{
+      // Keep in sync with the "platform" namespace in art/build/apex/ld.config.txt.
+      "libdexfile_external.so",
+      "libdexfiled_external.so",
+      "libnativebridge.so",
+      "libnativehelper.so",
+      "libnativeloader.so",
+      "libandroidicu.so",
+      // TODO(b/122876336): Remove libpac.so once it's migrated to Webview
+      "libpac.so",
+      // TODO(b/120786417 or b/134659294): libicuuc.so
+      // and libicui18n.so are kept for app compat.
+      "libicui18n.so",
+      "libicuuc.so",
+      // resolv
+      "libnetd_resolv.so",
+      // nn
+      "libneuralnetworks.so",
+  });
   return ns;
 }
 }  // namespace contents
diff --git a/contents/namespace/unrestricteddefault.cc b/contents/namespace/unrestricteddefault.cc
index 24c1474..6a8423a 100644
--- a/contents/namespace/unrestricteddefault.cc
+++ b/contents/namespace/unrestricteddefault.cc
@@ -22,24 +22,6 @@
 using android::linkerconfig::modules::AsanPath;
 using android::linkerconfig::modules::Namespace;
 
-namespace {
-
-// Keep in sync with the "platform" namespace in art/build/apex/ld.config.txt.
-const std::vector<std::string> kLibsFromArt = {
-    "libdexfile_external.so",
-    "libdexfiled_external.so",
-    "libnativebridge.so",
-    "libnativehelper.so",
-    "libnativeloader.so",
-    "libandroidicu.so",
-    "libpac.so",
-    // TODO(b/120786417 or b/134659294): libicuuc.so and libicui18n.so are kept
-    // for app compat.
-    "libicui18n.so",
-    "libicuuc.so"};
-
-}  // namespace
-
 namespace android {
 namespace linkerconfig {
 namespace contents {
@@ -50,10 +32,25 @@
   ns.AddSearchPath("/odm/${LIB}", AsanPath::WITH_DATA_ASAN);
   ns.AddSearchPath("/vendor/${LIB}", AsanPath::WITH_DATA_ASAN);
 
-  ns.GetLink("art").AddSharedLib(kLibsFromArt);
-  ns.GetLink("resolv").AddSharedLib("libnetd_resolv.so");
-  ns.GetLink("neuralnetworks").AddSharedLib("libneuralnetworks.so");
-
+  ns.AddRequires(std::vector{
+      // Keep in sync with the "platform" namespace in art/build/apex/ld.config.txt.
+      "libdexfile_external.so",
+      "libdexfiled_external.so",
+      "libnativebridge.so",
+      "libnativehelper.so",
+      "libnativeloader.so",
+      "libandroidicu.so",
+      // TODO(b/122876336): Remove libpac.so once it's migrated to Webview
+      "libpac.so",
+      // TODO(b/120786417 or b/134659294): libicuuc.so
+      // and libicui18n.so are kept for app compat.
+      "libicui18n.so",
+      "libicuuc.so",
+      // resolv
+      "libnetd_resolv.so",
+      // nn
+      "libneuralnetworks.so",
+  });
   return ns;
 }
 }  // namespace contents
diff --git a/contents/section/legacy.cc b/contents/section/legacy.cc
index 39aac53..dc20b32 100644
--- a/contents/section/legacy.cc
+++ b/contents/section/legacy.cc
@@ -43,6 +43,9 @@
 
   Section section("legacy", std::move(namespaces));
   AddStandardSystemLinks(ctx, &section);
+  if (auto res = section.Resolve(); !res) {
+    LOG(ERROR) << res.error();
+  }
   return section;
 }
 }  // namespace contents
diff --git a/contents/section/product.cc b/contents/section/product.cc
index 46ae84d..019d44d 100644
--- a/contents/section/product.cc
+++ b/contents/section/product.cc
@@ -49,6 +49,9 @@
 
   Section section("product", std::move(namespaces));
   AddStandardSystemLinks(ctx, &section);
+  if (auto res = section.Resolve(); !res) {
+    LOG(ERROR) << res.error();
+  }
   return section;
 }
 }  // namespace contents
diff --git a/contents/section/system.cc b/contents/section/system.cc
index a6af86a..f58310f 100644
--- a/contents/section/system.cc
+++ b/contents/section/system.cc
@@ -47,6 +47,9 @@
 
   Section section("system", std::move(namespaces));
   AddStandardSystemLinks(ctx, &section);
+  if (auto res = section.Resolve(); !res) {
+    LOG(ERROR) << res.error();
+  }
   return section;
 }
 }  // namespace contents
diff --git a/contents/section/unrestricted.cc b/contents/section/unrestricted.cc
index 4e947da..b866924 100644
--- a/contents/section/unrestricted.cc
+++ b/contents/section/unrestricted.cc
@@ -49,6 +49,9 @@
 
   Section section("unrestricted", std::move(namespaces));
   AddStandardSystemLinks(ctx, &section);
+  if (auto res = section.Resolve(); !res) {
+    LOG(ERROR) << res.error();
+  }
   return section;
 }
 }  // namespace contents
diff --git a/contents/section/vendor.cc b/contents/section/vendor.cc
index 9cfdce7..92eb2fb 100644
--- a/contents/section/vendor.cc
+++ b/contents/section/vendor.cc
@@ -56,6 +56,9 @@
 
   Section section("vendor", std::move(namespaces));
   AddStandardSystemLinks(ctx, &section);
+  if (auto res = section.Resolve(); !res) {
+    LOG(ERROR) << res.error();
+  }
   return section;
 }
 }  // namespace contents