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 #define LOG_TAG "nativeloader"
25 
26 #include "native_loader_namespace.h"
27 
28 #include <list>
29 #include <string>
30 
31 #include <android-base/result.h>
32 #include <jni.h>
33 
34 namespace android::nativeloader {
35 
36 using android::base::Result;
37 
38 // LibraryNamespaces is a singleton object that manages NativeLoaderNamespace
39 // objects for an app process. Its main job is to create (and configure) a new
40 // NativeLoaderNamespace object for a Java ClassLoader, and to find an existing
41 // object for a given ClassLoader.
42 class LibraryNamespaces {
43  public:
LibraryNamespaces()44   LibraryNamespaces() : initialized_(false), app_main_namespace_(nullptr) {}
45 
46   LibraryNamespaces(LibraryNamespaces&&) = default;
47   LibraryNamespaces(const LibraryNamespaces&) = delete;
48   LibraryNamespaces& operator=(const LibraryNamespaces&) = delete;
49 
50   void Initialize();
Reset()51   void Reset() {
52     namespaces_.clear();
53     initialized_ = false;
54     app_main_namespace_ = nullptr;
55   }
56   Result<NativeLoaderNamespace*> Create(JNIEnv* env, uint32_t target_sdk_version,
57                                         jobject class_loader, bool is_shared, jstring dex_path,
58                                         jstring java_library_path, jstring java_permitted_path,
59                                         jstring uses_library_list);
60   NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader);
61 
62  private:
63   Result<void> InitPublicNamespace(const char* library_path);
64   NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader);
65 
66   bool initialized_;
67   NativeLoaderNamespace* app_main_namespace_;
68   std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
69 };
70 
71 Result<std::string> FindApexNamespaceName(const std::string& location);
72 
73 }  // namespace android::nativeloader
74 
75 #endif  // ART_LIBNATIVELOADER_LIBRARY_NAMESPACES_H_
76