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_NATIVE_LOADER_NAMESPACE_H_
18 #define ART_LIBNATIVELOADER_NATIVE_LOADER_NAMESPACE_H_
19 
20 #if defined(ART_TARGET_ANDROID)
21 
22 #include <string>
23 #include <variant>
24 #include <vector>
25 
26 #include <android-base/logging.h>
27 #include <android-base/result.h>
28 #include <android/dlext.h>
29 #include <log/log.h>
30 #include <nativebridge/native_bridge.h>
31 
32 namespace android {
33 
34 using android::base::Result;
35 
36 // NativeLoaderNamespace abstracts a linker namespace for the native
37 // architecture (ex: arm on arm) or the translated architecture (ex: arm on
38 // x86). Instances of this class are managed by LibraryNamespaces object.
39 struct NativeLoaderNamespace {
40  public:
41   static Result<NativeLoaderNamespace> Create(const std::string& name,
42                                               const std::string& search_paths,
43                                               const std::string& permitted_paths,
44                                               const NativeLoaderNamespace* parent, bool is_shared,
45                                               bool is_greylist_enabled,
46                                               bool also_used_as_anonymous);
47 
48   NativeLoaderNamespace(NativeLoaderNamespace&&) = default;
49   NativeLoaderNamespace(const NativeLoaderNamespace&) = default;
50   NativeLoaderNamespace& operator=(const NativeLoaderNamespace&) = default;
51 
ToRawAndroidNamespaceNativeLoaderNamespace52   android_namespace_t* ToRawAndroidNamespace() const { return std::get<0>(raw_); }
ToRawNativeBridgeNamespaceNativeLoaderNamespace53   native_bridge_namespace_t* ToRawNativeBridgeNamespace() const { return std::get<1>(raw_); }
54 
nameNativeLoaderNamespace55   std::string name() const { return name_; }
IsBridgedNativeLoaderNamespace56   bool IsBridged() const { return raw_.index() == 1; }
57 
58   Result<void> Link(const NativeLoaderNamespace& target, const std::string& shared_libs) const;
59   Result<void*> Load(const char* lib_name) const;
60 
61   static Result<NativeLoaderNamespace> GetExportedNamespace(const std::string& name,
62                                                             bool is_bridged);
63   static Result<NativeLoaderNamespace> GetSystemNamespace(bool is_bridged);
64 
65  private:
NativeLoaderNamespaceNativeLoaderNamespace66   explicit NativeLoaderNamespace(const std::string& name, android_namespace_t* ns)
67       : name_(name), raw_(ns) {}
NativeLoaderNamespaceNativeLoaderNamespace68   explicit NativeLoaderNamespace(const std::string& name, native_bridge_namespace_t* ns)
69       : name_(name), raw_(ns) {}
70 
71   std::string name_;
72   std::variant<android_namespace_t*, native_bridge_namespace_t*> raw_;
73 };
74 
75 }  // namespace android
76 #endif  // #if defined(ART_TARGET_ANDROID)
77 
78 #endif  // ART_LIBNATIVELOADER_NATIVE_LOADER_NAMESPACE_H_
79