| 6 |
| 7 | #include <android/dlext.h> |
| 8 | #include <dlfcn.h> |
| 9 |
| 10 | #include "base/android/jni_android.h" |
| 11 | #include "base/android/jni_string.h" |
| 12 | #include "base/base_jni_headers/BundleUtils_jni.h" |
| 13 | #include "base/files/file_path.h" |
| 14 | #include "base/logging.h" |
| 15 |
| 16 | // These symbols are added by the lld linker when creating a partitioned shared |
| 17 | // library. The symbols live in the base library, and are used to properly load |
| 18 | // the other partitions (feature libraries) when needed. |
| 19 | struct PartitionIndexEntry { |
| 20 | int32_t name_relptr; |
| 21 | int32_t addr_relptr; |
| 22 | uint32_t size; |
| 23 | }; |
| 24 | static_assert(sizeof(PartitionIndexEntry) == 12U, |
| 25 | "Unexpected PartitionIndexEntry size"); |
| 26 |
| 27 | // Marked as weak_import because these symbols are lld-specific. The method that |
| 28 | // uses them will only be invoked in builds that have lld-generated partitions. |
| 29 | extern PartitionIndexEntry __part_index_begin[] __attribute__((weak_import)); |
| 30 | extern PartitionIndexEntry __part_index_end[] __attribute__((weak_import)); |
| 31 |
| 32 | extern "C" { |
| 33 | // Marked as weak_import because this symbol is either supplied by the system |
| 34 | // (on Android N+), or by Crazy Linker (Android M and prior). |
| 35 | extern void* android_dlopen_ext(const char* __filename, |
| 36 | int __flags, |
| 37 | const android_dlextinfo* __info) |
| 38 | __attribute__((weak_import)); |
| 39 | } // extern "C" |
| 40 |
| 41 | namespace base { |
| 42 | namespace android { |
| 43 |
| 44 | namespace { |
| 45 |
| 46 | const void* ReadRelPtr(const int32_t* relptr) { |
| 47 | return reinterpret_cast<const char*>(relptr) + *relptr; |
| 48 | } |
| 49 |
| 50 | } // namespace |
| 51 |
| 52 | // static |
| 53 | std::string BundleUtils::ResolveLibraryPath(const std::string& library_name) { |
| 54 | JNIEnv* env = AttachCurrentThread(); |
| 55 | ScopedJavaLocalRef<jstring> java_path = Java_BundleUtils_getNativeLibraryPath( |
| 56 | env, base::android::ConvertUTF8ToJavaString(env, library_name)); |
| 57 | // TODO(https://crbug.com/1019853): Remove this tolerance. |
| 58 | if (!java_path) { |
| 59 | return std::string(); |
| 60 | } |
| 61 | return base::android::ConvertJavaStringToUTF8(env, java_path); |
| 62 | } |
| 63 |
| 64 | // static |
| 65 | bool BundleUtils::IsBundle() { |
| 66 | return Java_BundleUtils_isBundleForNative( |
| 67 | base::android::AttachCurrentThread()); |
| 68 | } |
| 69 |
| 70 | // static |
| 71 | void* BundleUtils::DlOpenModuleLibraryPartition(const std::string& library_name, |
| 72 | const std::string& partition) { |
| 73 | // TODO(https://crbug.com/1019853): Remove this tolerance. |
| 74 | std::string library_path = ResolveLibraryPath(library_name); |
| 75 | if (library_path.empty()) { |
| 76 | return nullptr; |
| 77 | } |
| 78 |
| 79 | // Linear search is required here because the partition descriptors are not |
| 80 | // ordered. If a large number of partitions come into existence, lld could be |
| 81 | // modified to sort the partitions. |
| 82 | DCHECK(__part_index_begin != nullptr); |
| 83 | DCHECK(__part_index_end != nullptr); |
| 84 | for (const PartitionIndexEntry* part = __part_index_begin; |
| 85 | part != __part_index_end; ++part) { |
| 86 | std::string name( |
| 87 | reinterpret_cast<const char*>(ReadRelPtr(&part->name_relptr))); |
| 88 | if (name == partition) { |
| 89 | android_dlextinfo info = {}; |
| 90 | info.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 91 | info.reserved_addr = const_cast<void*>(ReadRelPtr(&part->addr_relptr)); |
| 92 | info.reserved_size = part->size; |
| 93 |
| 94 | DCHECK(android_dlopen_ext != nullptr); |
| 95 | return android_dlopen_ext(library_path.c_str(), RTLD_LOCAL, &info); |
| 96 | } |
| 97 | } |
| 98 |
| 99 | NOTREACHED(); |
| 100 | return nullptr; |
| 101 | } |
| 102 |
| 103 | } // namespace android |
| 104 | } // namespace base |
| 105 |