Translate full apex lib path to native_bridge path am: dc8bbbb645 am: e0b5bcf26b am: d5ef9527d0 am: 55147c617e

Original change: https://android-review.googlesource.com/c/platform/frameworks/libs/native_bridge_support/+/1433311

Change-Id: Id8a2467ffb427e61d5cdb56b20806f2e484bf8d0
diff --git a/linker/linker_translate_path.cpp b/linker/linker_translate_path.cpp
index 02a501c..b5f5816 100644
--- a/linker/linker_translate_path.cpp
+++ b/linker/linker_translate_path.cpp
@@ -15,75 +15,116 @@
  */
 
 #include "linker_translate_path.h"
-#include "linker.h"
+
+#include <string>
 
 #include <android/api-level.h>
 
-#include <string>
+#include "linker.h"
+
+// Handle dlopen by full path.
+//
+// 1. Translate original path to native_bridge path.
+//
+// Native bridge libraries reside in $LIB/$ABI subdirectory. For example:
+//   /system/lib/liblog.so -> /system/lib/arm/liblog.so
+//
+// Native bridge libraries do not use apex. For example:
+//   /apex/com.android.i18n/lib/libicuuc.so -> /system/lib/arm/libicuuc.so
+//
+// 2. Repeat linker workaround to open apex libraries by system path (see http://b/121248172).
+//
+// For older target SDK versions, linker allows to open apex libraries by system path, so it does:
+//   /system/lib/libicuuc.so -> /apex/com.android.art/lib/libicuuc.so
+//
+// Adding native bridge path translation, we get:
+//   /system/lib/libicuuc.so -> /apex/com.android.art/lib/libicuuc.so -> /system/lib/arm/libicuuc.so
 
 #if defined(__arm__)
 #define SYSTEM_LIB(name) \
   { "/system/lib/" name, "/system/lib/arm/" name }
+#define APEX_LIB(apex, name) \
+  { "/apex/" apex "/lib/" name, "/system/lib/arm/" name }
 #elif defined(__aarch64__)
 #define SYSTEM_LIB(name) \
   { "/system/lib64/" name, "/system/lib64/arm64/" name }
+#define APEX_LIB(apex, name) \
+  { "/apex/" apex "/lib64/" name, "/system/lib64/arm64/" name }
 #else
 #error "Unknown guest arch"
 #endif
 
-// Workaround for dlopen(/system/lib(64)/<soname>) when .so is in /apex. http://b/121248172
 /**
- * Translate /system path to /apex path if needed
- * The workaround should work only when targetSdkVersion < Q.
+ * Translate /system path or /apex path to native_bridge path
+ * Function name is misleading, as it overrides the corresponding function in original linker.
  *
- * param out_name_to_apex pointing to /apex path
+ * param out_name pointing to native_bridge path
  * return true if translation is needed
  */
-bool translateSystemPathToApexPath(const char* name, std::string* out_name_to_apex) {
+bool translateSystemPathToApexPath(const char* name, std::string* out_name) {
   static constexpr const char* kPathTranslation[][2] = {
-      SYSTEM_LIB("libEGL.so"),
-      SYSTEM_LIB("libGLESv1_CM.so"),
-      SYSTEM_LIB("libGLESv2.so"),
-      SYSTEM_LIB("libGLESv3.so"),
-      SYSTEM_LIB("libOpenMAXAL.so"),
-      SYSTEM_LIB("libOpenSLES.so"),
-      SYSTEM_LIB("libRS.so"),
-      SYSTEM_LIB("libaaudio.so"),
-      SYSTEM_LIB("libamidi.so"),
-      SYSTEM_LIB("libandroid.so"),
-      SYSTEM_LIB("libbinder_ndk.so"),
-      SYSTEM_LIB("libc.so"),
-      SYSTEM_LIB("libcamera2ndk.so"),
-      SYSTEM_LIB("libdl.so"),
-      SYSTEM_LIB("libjnigraphics.so"),
-      SYSTEM_LIB("liblog.so"),
-      SYSTEM_LIB("libm.so"),
-      SYSTEM_LIB("libmediandk.so"),
-      SYSTEM_LIB("libnativewindow.so"),
-      SYSTEM_LIB("libstdc++.so"),
-      SYSTEM_LIB("libsync.so"),
-      SYSTEM_LIB("libvulkan.so"),
-      SYSTEM_LIB("libwebviewchromium_plat_support.so"),
-      SYSTEM_LIB("libz.so")};
+    // Libraries accessible by system path.
+    SYSTEM_LIB("libEGL.so"),
+    SYSTEM_LIB("libGLESv1_CM.so"),
+    SYSTEM_LIB("libGLESv2.so"),
+    SYSTEM_LIB("libGLESv3.so"),
+    SYSTEM_LIB("libOpenMAXAL.so"),
+    SYSTEM_LIB("libOpenSLES.so"),
+    SYSTEM_LIB("libRS.so"),
+    SYSTEM_LIB("libaaudio.so"),
+    SYSTEM_LIB("libamidi.so"),
+    SYSTEM_LIB("libandroid.so"),
+    SYSTEM_LIB("libbinder_ndk.so"),
+    SYSTEM_LIB("libc.so"),
+    SYSTEM_LIB("libcamera2ndk.so"),
+    SYSTEM_LIB("libdl.so"),
+    SYSTEM_LIB("libjnigraphics.so"),
+    SYSTEM_LIB("liblog.so"),
+    SYSTEM_LIB("libm.so"),
+    SYSTEM_LIB("libmediandk.so"),
+    SYSTEM_LIB("libnativewindow.so"),
+    SYSTEM_LIB("libstdc++.so"),
+    SYSTEM_LIB("libsync.so"),
+    SYSTEM_LIB("libvulkan.so"),
+    SYSTEM_LIB("libwebviewchromium_plat_support.so"),
+    SYSTEM_LIB("libz.so"),
+    // Apex/system after R.
+    APEX_LIB("com.android.i18n", "libandroidicu.so"),
+    APEX_LIB("com.android.i18n", "libicui18n.so"),
+    APEX_LIB("com.android.i18n", "libicuuc.so"),
+    // Apex/system on R (see http://b/161958857).
+    APEX_LIB("com.android.art", "libicui18n.so"),
+    APEX_LIB("com.android.art", "libicuuc.so"),
+    APEX_LIB("com.android.art", "libnativehelper.so"),
+    // Apex/system on Q.
+    APEX_LIB("com.android.runtime", "libicui18n.so"),
+    APEX_LIB("com.android.runtime", "libicuuc.so"),
+  };
 
   static constexpr const char* kPathTranslationQ[][2] = {
-      SYSTEM_LIB("libicui18n.so"), SYSTEM_LIB("libicuuc.so"), SYSTEM_LIB("libneuralnetworks.so")};
+    // Apps targeting below Q can open apex libraries by system path.
+    SYSTEM_LIB("libicui18n.so"),
+    SYSTEM_LIB("libicuuc.so"),
+    SYSTEM_LIB("libneuralnetworks.so"),
+  };
 
-  // Libraries from greylist.  Only convert these for apps targeting N or below.
-  static constexpr const char* kPathTranslationN[][2] = {SYSTEM_LIB("libandroid_runtime.so"),
-                                                         SYSTEM_LIB("libbinder.so"),
-                                                         SYSTEM_LIB("libcrypto.so"),
-                                                         SYSTEM_LIB("libcutils.so"),
-                                                         SYSTEM_LIB("libexpat.so"),
-                                                         SYSTEM_LIB("libgui.so"),
-                                                         SYSTEM_LIB("libmedia.so"),
-                                                         SYSTEM_LIB("libnativehelper.so"),
-                                                         SYSTEM_LIB("libssl.so"),
-                                                         SYSTEM_LIB("libstagefright.so"),
-                                                         SYSTEM_LIB("libsqlite.so"),
-                                                         SYSTEM_LIB("libui.so"),
-                                                         SYSTEM_LIB("libutils.so"),
-                                                         SYSTEM_LIB("libvorbisidec.so")};
+  static constexpr const char* kPathTranslationN[][2] = {
+    // Apps targeting below N can open greylisted libraries.
+    SYSTEM_LIB("libandroid_runtime.so"),
+    SYSTEM_LIB("libbinder.so"),
+    SYSTEM_LIB("libcrypto.so"),
+    SYSTEM_LIB("libcutils.so"),
+    SYSTEM_LIB("libexpat.so"),
+    SYSTEM_LIB("libgui.so"),
+    SYSTEM_LIB("libmedia.so"),
+    SYSTEM_LIB("libnativehelper.so"),
+    SYSTEM_LIB("libssl.so"),
+    SYSTEM_LIB("libstagefright.so"),
+    SYSTEM_LIB("libsqlite.so"),
+    SYSTEM_LIB("libui.so"),
+    SYSTEM_LIB("libutils.so"),
+    SYSTEM_LIB("libvorbisidec.so"),
+  };
 
   if (name == nullptr) {
     return false;
@@ -93,7 +134,7 @@
 
   if (auto it = std::find_if(std::begin(kPathTranslation), std::end(kPathTranslation), comparator);
       it != std::end(kPathTranslation)) {
-    *out_name_to_apex = (*it)[1];
+    *out_name = (*it)[1];
     return true;
   }
 
@@ -101,7 +142,7 @@
     if (auto it =
             std::find_if(std::begin(kPathTranslationQ), std::end(kPathTranslationQ), comparator);
         it != std::end(kPathTranslationQ)) {
-      *out_name_to_apex = (*it)[1];
+      *out_name = (*it)[1];
       return true;
     }
   }
@@ -110,11 +151,10 @@
     if (auto it =
             std::find_if(std::begin(kPathTranslationN), std::end(kPathTranslationN), comparator);
         it != std::end(kPathTranslationN)) {
-      *out_name_to_apex = (*it)[1];
+      *out_name = (*it)[1];
       return true;
     }
   }
 
   return false;
 }
-// End Workaround for dlopen(/system/lib/<soname>) when .so is in /apex.