1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ART_LIBNATIVELOADER_LIBRARY_NAMESPACES_H_ 18 #define ART_LIBNATIVELOADER_LIBRARY_NAMESPACES_H_ 19 20 #if !defined(ART_TARGET_ANDROID) 21 #error "Not available for host or linux target" 22 #endif 23 24 #include <list> 25 #include <optional> 26 #include <string> 27 #include <string_view> 28 29 #include "android-base/result.h" 30 #include "jni.h" 31 #include "native_loader_namespace.h" 32 33 namespace android::nativeloader { 34 35 using android::base::Result; 36 37 // The device may be configured to have the vendor libraries loaded to a separate namespace. 38 // For historical reasons this namespace was named sphal but effectively it is intended 39 // to use to load vendor libraries to separate namespace with controlled interface between 40 // vendor and system namespaces. 41 constexpr const char* kVendorNamespaceName = "sphal"; 42 // Similar to sphal namespace, product namespace provides some product libraries. 43 constexpr const char* kProductNamespaceName = "product"; 44 45 // vndk namespace for unbundled vendor apps 46 constexpr const char* kVndkNamespaceName = "vndk"; 47 // vndk_product namespace for unbundled product apps 48 constexpr const char* kVndkProductNamespaceName = "vndk_product"; 49 50 // API domains, roughly corresponding to partitions. Interdependencies between 51 // these must follow API restrictions, while intradependencies do not. 52 using ApiDomain = enum { 53 API_DOMAIN_DEFAULT = 0, // Locations other than those below, in particular for ordinary apps 54 API_DOMAIN_VENDOR = 1, // Vendor partition 55 API_DOMAIN_PRODUCT = 2, // Product partition 56 API_DOMAIN_SYSTEM = 3, // System and system_ext partitions 57 }; 58 59 ApiDomain GetApiDomainFromPath(const std::string_view path); 60 Result<ApiDomain> GetApiDomainFromPathList(const std::string& path_list); 61 62 // LibraryNamespaces is a singleton object that manages NativeLoaderNamespace 63 // objects for an app process. Its main job is to create (and configure) a new 64 // NativeLoaderNamespace object for a Java ClassLoader, and to find an existing 65 // object for a given ClassLoader. 66 class LibraryNamespaces { 67 public: LibraryNamespaces()68 LibraryNamespaces() : initialized_(false), app_main_namespace_(nullptr) {} 69 70 LibraryNamespaces(LibraryNamespaces&&) = default; 71 LibraryNamespaces(const LibraryNamespaces&) = delete; 72 LibraryNamespaces& operator=(const LibraryNamespaces&) = delete; 73 74 void Initialize(); Reset()75 void Reset() { 76 namespaces_.clear(); 77 initialized_ = false; 78 app_main_namespace_ = nullptr; 79 } 80 Result<NativeLoaderNamespace*> Create(JNIEnv* env, 81 uint32_t target_sdk_version, 82 jobject class_loader, 83 ApiDomain api_domain, 84 bool is_shared, 85 const std::string& dex_path, 86 jstring library_path_j, 87 jstring permitted_path_j, 88 jstring uses_library_list_j); 89 NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader); 90 91 private: 92 Result<void> InitPublicNamespace(const char* library_path); 93 NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader); 94 95 bool initialized_; 96 NativeLoaderNamespace* app_main_namespace_; 97 std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_; 98 }; 99 100 std::optional<std::string> FindApexNamespaceName(const std::string& location); 101 102 } // namespace android::nativeloader 103 104 #endif // ART_LIBNATIVELOADER_LIBRARY_NAMESPACES_H_ 105