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