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_exempt_list_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   // Creates a link from this namespace to target for the ":"-separated list of
59   // libraries in shared_libs. If target is nullptr it creates a link to the
60   // default namespace.
61   Result<void> Link(const NativeLoaderNamespace* target, const std::string& shared_libs) const;
62 
63   Result<void*> Load(const char* lib_name) const;
64 
65   static Result<NativeLoaderNamespace> GetExportedNamespace(const std::string& name,
66                                                             bool is_bridged);
67   static Result<NativeLoaderNamespace> GetSystemNamespace(bool is_bridged);
68 
69  private:
NativeLoaderNamespaceNativeLoaderNamespace70   explicit NativeLoaderNamespace(const std::string& name, android_namespace_t* ns)
71       : name_(name), raw_(ns) {}
NativeLoaderNamespaceNativeLoaderNamespace72   explicit NativeLoaderNamespace(const std::string& name, native_bridge_namespace_t* ns)
73       : name_(name), raw_(ns) {}
74 
75   std::string name_;
76   std::variant<android_namespace_t*, native_bridge_namespace_t*> raw_;
77 };
78 
79 }  // namespace android
80 #endif  // #if defined(ART_TARGET_ANDROID)
81 
82 #endif  // ART_LIBNATIVELOADER_NATIVE_LOADER_NAMESPACE_H_
83