1 /* 2 * Copyright (C) 2016 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_RUNTIME_PLUGIN_H_ 18 #define ART_RUNTIME_PLUGIN_H_ 19 20 #include <string> 21 22 #include <android-base/logging.h> 23 24 #include "base/macros.h" 25 26 namespace art HIDDEN { 27 28 // This function is loaded from the plugin (if present) and called during runtime initialization. 29 // By the time this has been called the runtime has been fully initialized but not other native 30 // libraries have been loaded yet. Failure to initialize is considered a fatal error. 31 // TODO might want to give initialization function some arguments 32 using PluginInitializationFunction = bool (*)(); 33 using PluginDeinitializationFunction = bool (*)(); 34 35 // A class encapsulating a plugin. There is no stable plugin ABI or API and likely never will be. 36 // TODO Might want to put some locking in this but ATM we only load these at initialization in a 37 // single-threaded fashion so not much need 38 class Plugin { 39 public: Create(const std::string & lib)40 static Plugin Create(const std::string& lib) { 41 return Plugin(lib); 42 } 43 IsLoaded()44 bool IsLoaded() const { 45 return dlopen_handle_ != nullptr; 46 } 47 GetLibrary()48 const std::string& GetLibrary() const { 49 return library_; 50 } 51 52 bool Load(/*out*/std::string* error_msg); 53 bool Unload(); 54 55 ~Plugin()56 ~Plugin() { 57 if (IsLoaded() && !Unload()) { 58 LOG(ERROR) << "Error unloading " << this; 59 } 60 } 61 62 Plugin(const Plugin& other); 63 64 // Create move constructor for putting this in a list Plugin(Plugin && other)65 Plugin(Plugin&& other) noexcept 66 : library_(other.library_), 67 dlopen_handle_(other.dlopen_handle_) { 68 other.dlopen_handle_ = nullptr; 69 } 70 71 private: Plugin(const std::string & library)72 explicit Plugin(const std::string& library) : library_(library), dlopen_handle_(nullptr) { } 73 74 std::string library_; 75 void* dlopen_handle_; 76 77 friend std::ostream& operator<<(std::ostream &os, Plugin const& m); 78 }; 79 80 std::ostream& operator<<(std::ostream &os, Plugin const& m); 81 std::ostream& operator<<(std::ostream &os, const Plugin* m); 82 83 } // namespace art 84 85 #endif // ART_RUNTIME_PLUGIN_H_ 86