xref: /aosp_15_r20/art/runtime/plugin.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "plugin.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <dlfcn.h>
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/locks.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
24*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker const char* PLUGIN_INITIALIZATION_FUNCTION_NAME = "ArtPlugin_Initialize";
31*795d594fSAndroid Build Coastguard Worker const char* PLUGIN_DEINITIALIZATION_FUNCTION_NAME = "ArtPlugin_Deinitialize";
32*795d594fSAndroid Build Coastguard Worker 
Plugin(const Plugin & other)33*795d594fSAndroid Build Coastguard Worker Plugin::Plugin(const Plugin& other) : library_(other.library_), dlopen_handle_(nullptr) {
34*795d594fSAndroid Build Coastguard Worker   CHECK(!other.IsLoaded()) << "Should not copy loaded plugins.";
35*795d594fSAndroid Build Coastguard Worker }
36*795d594fSAndroid Build Coastguard Worker 
Load(std::string * error_msg)37*795d594fSAndroid Build Coastguard Worker bool Plugin::Load(/*out*/std::string* error_msg) {
38*795d594fSAndroid Build Coastguard Worker   Locks::mutator_lock_->AssertNotHeld(Thread::Current());
39*795d594fSAndroid Build Coastguard Worker   DCHECK(!IsLoaded());
40*795d594fSAndroid Build Coastguard Worker   void* res = dlopen(library_.c_str(), RTLD_LAZY);
41*795d594fSAndroid Build Coastguard Worker   if (res == nullptr) {
42*795d594fSAndroid Build Coastguard Worker     *error_msg = StringPrintf("dlopen failed: %s", dlerror());
43*795d594fSAndroid Build Coastguard Worker     return false;
44*795d594fSAndroid Build Coastguard Worker   }
45*795d594fSAndroid Build Coastguard Worker   // Get the initializer function
46*795d594fSAndroid Build Coastguard Worker   PluginInitializationFunction init = reinterpret_cast<PluginInitializationFunction>(
47*795d594fSAndroid Build Coastguard Worker       dlsym(res, PLUGIN_INITIALIZATION_FUNCTION_NAME));
48*795d594fSAndroid Build Coastguard Worker   if (init != nullptr) {
49*795d594fSAndroid Build Coastguard Worker     if (!init()) {
50*795d594fSAndroid Build Coastguard Worker       dlclose(res);
51*795d594fSAndroid Build Coastguard Worker       *error_msg = StringPrintf("Initialization of plugin failed");
52*795d594fSAndroid Build Coastguard Worker       return false;
53*795d594fSAndroid Build Coastguard Worker     }
54*795d594fSAndroid Build Coastguard Worker   } else {
55*795d594fSAndroid Build Coastguard Worker     LOG(WARNING) << this << " does not include an initialization function";
56*795d594fSAndroid Build Coastguard Worker   }
57*795d594fSAndroid Build Coastguard Worker   dlopen_handle_ = res;
58*795d594fSAndroid Build Coastguard Worker   return true;
59*795d594fSAndroid Build Coastguard Worker }
60*795d594fSAndroid Build Coastguard Worker 
Unload()61*795d594fSAndroid Build Coastguard Worker bool Plugin::Unload() {
62*795d594fSAndroid Build Coastguard Worker   Locks::mutator_lock_->AssertNotHeld(Thread::Current());
63*795d594fSAndroid Build Coastguard Worker   DCHECK(IsLoaded());
64*795d594fSAndroid Build Coastguard Worker   bool ret = true;
65*795d594fSAndroid Build Coastguard Worker   void* handle = dlopen_handle_;
66*795d594fSAndroid Build Coastguard Worker   PluginDeinitializationFunction deinit = reinterpret_cast<PluginDeinitializationFunction>(
67*795d594fSAndroid Build Coastguard Worker       dlsym(handle, PLUGIN_DEINITIALIZATION_FUNCTION_NAME));
68*795d594fSAndroid Build Coastguard Worker   if (deinit != nullptr) {
69*795d594fSAndroid Build Coastguard Worker     if (!deinit()) {
70*795d594fSAndroid Build Coastguard Worker       LOG(WARNING) << this << " failed deinitialization";
71*795d594fSAndroid Build Coastguard Worker       ret = false;
72*795d594fSAndroid Build Coastguard Worker     }
73*795d594fSAndroid Build Coastguard Worker   } else {
74*795d594fSAndroid Build Coastguard Worker     LOG(WARNING) << this << " does not include a deinitialization function";
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker   dlopen_handle_ = nullptr;
77*795d594fSAndroid Build Coastguard Worker   // Don't bother to actually dlclose since we are shutting down anyway and there might be small
78*795d594fSAndroid Build Coastguard Worker   // amounts of processing still being done.
79*795d594fSAndroid Build Coastguard Worker   return ret;
80*795d594fSAndroid Build Coastguard Worker }
81*795d594fSAndroid Build Coastguard Worker 
operator <<(std::ostream & os,const Plugin * m)82*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream &os, const Plugin* m) {
83*795d594fSAndroid Build Coastguard Worker   return os << *m;
84*795d594fSAndroid Build Coastguard Worker }
85*795d594fSAndroid Build Coastguard Worker 
operator <<(std::ostream & os,Plugin const & m)86*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream &os, Plugin const& m) {
87*795d594fSAndroid Build Coastguard Worker   return os << "Plugin { library=\"" << m.library_ << "\", handle=" << m.dlopen_handle_ << " }";
88*795d594fSAndroid Build Coastguard Worker }
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker }  // namespace art
91