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 ToRawAndroidNamespaceNativeLoaderNamespace51 android_namespace_t* ToRawAndroidNamespace() const { return std::get<0>(raw_); } ToRawNativeBridgeNamespaceNativeLoaderNamespace52 native_bridge_namespace_t* ToRawNativeBridgeNamespace() const { return std::get<1>(raw_); } 53 nameNativeLoaderNamespace54 std::string name() const { return name_; } IsBridgedNativeLoaderNamespace55 bool IsBridged() const { return raw_.index() == 1; } 56 57 // Creates a link from this namespace to target for the ":"-separated list of 58 // libraries in shared_libs. If target is nullptr it creates a link to the 59 // default namespace. 60 Result<void> Link(const NativeLoaderNamespace* target, const std::string& shared_libs) const; 61 62 Result<void*> Load(const char* lib_name) const; 63 64 static Result<NativeLoaderNamespace> GetExportedNamespace(const std::string& name, 65 bool is_bridged); 66 static Result<NativeLoaderNamespace> GetSystemNamespace(bool is_bridged); 67 68 private: NativeLoaderNamespaceNativeLoaderNamespace69 explicit NativeLoaderNamespace(const std::string& name, android_namespace_t* ns) 70 : name_(name), raw_(ns) {} NativeLoaderNamespaceNativeLoaderNamespace71 explicit NativeLoaderNamespace(const std::string& name, native_bridge_namespace_t* ns) 72 : name_(name), raw_(ns) {} 73 74 const std::string name_; 75 const std::variant<android_namespace_t*, native_bridge_namespace_t*> raw_; 76 }; 77 78 } // namespace android 79 #endif // #if defined(ART_TARGET_ANDROID) 80 81 #endif // ART_LIBNATIVELOADER_NATIVE_LOADER_NAMESPACE_H_ 82