Use a different VNDK namespace for product apps

As product partition may have a different VNDK version than that of
vendor partition, they may not share the same VNDK namespace for
their apps.
Define a new vndk_product namespace in the system section for product
apps that uses ro.product.vndk.version.

Bug: 149063221
Test: atest libnativeloader_test
Change-Id: I1bb76617104a49b0d11af13d2f116959a18390a3
diff --git a/libnativeloader/library_namespaces.cpp b/libnativeloader/library_namespaces.cpp
index 3d74e2d..dfbdefd 100644
--- a/libnativeloader/library_namespaces.cpp
+++ b/libnativeloader/library_namespaces.cpp
@@ -42,6 +42,7 @@
 // vendor and system namespaces.
 constexpr const char* kVendorNamespaceName = "sphal";
 constexpr const char* kVndkNamespaceName = "vndk";
+constexpr const char* kVndkProductNamespaceName = "vndk_product";
 constexpr const char* kArtNamespaceName = "com_android_art";
 constexpr const char* kNeuralNetworksNamespaceName = "com_android_neuralnetworks";
 constexpr const char* kCronetNamespaceName = "com_android_cronet";
@@ -172,12 +173,12 @@
 
   std::string system_exposed_libraries = default_public_libraries();
   std::string namespace_name = kClassloaderNamespaceName;
-  bool unbundled_vendor_or_product_app = false;
+  ApkOrigin unbundled_app_origin = APK_ORIGIN_DEFAULT;
   if ((apk_origin == APK_ORIGIN_VENDOR ||
        (apk_origin == APK_ORIGIN_PRODUCT &&
         is_product_vndk_version_defined())) &&
       !is_shared) {
-    unbundled_vendor_or_product_app = true;
+    unbundled_app_origin = apk_origin;
     // For vendor / product apks, give access to the vendor / product lib even though
     // they are treated as unbundled; the libs and apks are still bundled
     // together in the vendor / product partition.
@@ -275,11 +276,22 @@
     }
   }
 
-  // Give access to VNDK-SP libraries from the 'vndk' namespace.
-  if (unbundled_vendor_or_product_app && !vndksp_libraries().empty()) {
+  // Give access to VNDK-SP libraries from the 'vndk' namespace for unbundled vendor apps.
+  if (unbundled_app_origin == APK_ORIGIN_VENDOR && !vndksp_libraries_vendor().empty()) {
     auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
     if (vndk_ns.ok()) {
-      linked = app_ns->Link(*vndk_ns, vndksp_libraries());
+      linked = app_ns->Link(*vndk_ns, vndksp_libraries_vendor());
+      if (!linked.ok()) {
+        return linked.error();
+      }
+    }
+  }
+
+  // Give access to VNDK-SP libraries from the 'vndk_product' namespace for unbundled product apps.
+  if (unbundled_app_origin == APK_ORIGIN_PRODUCT && !vndksp_libraries_product().empty()) {
+    auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkProductNamespaceName, is_bridged);
+    if (vndk_ns.ok()) {
+      linked = app_ns->Link(*vndk_ns, vndksp_libraries_product());
       if (!linked.ok()) {
         return linked.error();
       }