Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 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 | #include "agent.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 18 | |
| 19 | #include "android-base/stringprintf.h" |
Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 20 | #include "nativehelper/scoped_local_ref.h" |
| 21 | #include "nativeloader/native_loader.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 22 | |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 23 | #include "base/strlcpy.h" |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 24 | #include "java_vm_ext.h" |
| 25 | #include "runtime.h" |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 26 | #include "thread-current-inl.h" |
| 27 | #include "scoped_thread_state_change-inl.h" |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | namespace ti { |
| 31 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 32 | using android::base::StringPrintf; |
| 33 | |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 34 | const char* AGENT_ON_LOAD_FUNCTION_NAME = "Agent_OnLoad"; |
| 35 | const char* AGENT_ON_ATTACH_FUNCTION_NAME = "Agent_OnAttach"; |
| 36 | const char* AGENT_ON_UNLOAD_FUNCTION_NAME = "Agent_OnUnload"; |
| 37 | |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 38 | AgentSpec::AgentSpec(const std::string& arg) { |
| 39 | size_t eq = arg.find_first_of('='); |
| 40 | if (eq == std::string::npos) { |
| 41 | name_ = arg; |
| 42 | } else { |
| 43 | name_ = arg.substr(0, eq); |
| 44 | args_ = arg.substr(eq + 1, arg.length()); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | std::unique_ptr<Agent> AgentSpec::Load(/*out*/jint* call_res, |
| 49 | /*out*/LoadError* error, |
| 50 | /*out*/std::string* error_msg) { |
| 51 | VLOG(agents) << "Loading agent: " << name_ << " " << args_; |
Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 52 | return DoLoadHelper(nullptr, false, nullptr, call_res, error, error_msg); |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Tries to attach the agent using its OnAttach method. Returns true on success. |
Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 56 | std::unique_ptr<Agent> AgentSpec::Attach(JNIEnv* env, |
| 57 | jobject class_loader, |
| 58 | /*out*/jint* call_res, |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 59 | /*out*/LoadError* error, |
| 60 | /*out*/std::string* error_msg) { |
| 61 | VLOG(agents) << "Attaching agent: " << name_ << " " << args_; |
Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 62 | return DoLoadHelper(env, true, class_loader, call_res, error, error_msg); |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 66 | // TODO We need to acquire some locks probably. |
Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 67 | std::unique_ptr<Agent> AgentSpec::DoLoadHelper(JNIEnv* env, |
| 68 | bool attaching, |
| 69 | jobject class_loader, |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 70 | /*out*/jint* call_res, |
| 71 | /*out*/LoadError* error, |
| 72 | /*out*/std::string* error_msg) { |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 73 | ScopedThreadStateChange stsc(Thread::Current(), ThreadState::kNative); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 74 | DCHECK(call_res != nullptr); |
| 75 | DCHECK(error_msg != nullptr); |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 76 | |
Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 77 | std::unique_ptr<Agent> agent = DoDlOpen(env, class_loader, error, error_msg); |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 78 | if (agent == nullptr) { |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 79 | VLOG(agents) << "err: " << *error_msg; |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 80 | return nullptr; |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 81 | } |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 82 | AgentOnLoadFunction callback = attaching ? agent->onattach_ : agent->onload_; |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 83 | if (callback == nullptr) { |
| 84 | *error_msg = StringPrintf("Unable to start agent %s: No %s callback found", |
| 85 | (attaching ? "attach" : "load"), |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 86 | name_.c_str()); |
| 87 | VLOG(agents) << "err: " << *error_msg; |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 88 | *error = kLoadingError; |
| 89 | return nullptr; |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 90 | } |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 91 | // Need to let the function fiddle with the array. |
| 92 | std::unique_ptr<char[]> copied_args(new char[args_.size() + 1]); |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 93 | strlcpy(copied_args.get(), args_.c_str(), args_.size() + 1); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 94 | // TODO Need to do some checks that we are at a good spot etc. |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 95 | *call_res = callback(Runtime::Current()->GetJavaVM(), |
| 96 | copied_args.get(), |
| 97 | nullptr); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 98 | if (*call_res != 0) { |
| 99 | *error_msg = StringPrintf("Initialization of %s returned non-zero value of %d", |
| 100 | name_.c_str(), *call_res); |
| 101 | VLOG(agents) << "err: " << *error_msg; |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 102 | *error = kInitializationError; |
| 103 | return nullptr; |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 104 | } |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 105 | return agent; |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 108 | std::unique_ptr<Agent> AgentSpec::DoDlOpen(JNIEnv* env, |
| 109 | jobject class_loader, |
| 110 | /*out*/LoadError* error, |
| 111 | /*out*/std::string* error_msg) { |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 112 | DCHECK(error_msg != nullptr); |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 113 | |
Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 114 | ScopedLocalRef<jstring> library_path(env, |
| 115 | class_loader == nullptr |
| 116 | ? nullptr |
| 117 | : JavaVMExt::GetLibrarySearchPath(env, class_loader)); |
| 118 | |
| 119 | bool needs_native_bridge = false; |
Andreas Gampe | 61c5e7c | 2018-01-18 20:34:24 -0800 | [diff] [blame] | 120 | std::string nativeloader_error_msg; |
Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 121 | void* dlopen_handle = android::OpenNativeLibrary(env, |
| 122 | Runtime::Current()->GetTargetSdkVersion(), |
| 123 | name_.c_str(), |
| 124 | class_loader, |
| 125 | library_path.get(), |
| 126 | &needs_native_bridge, |
Andreas Gampe | 61c5e7c | 2018-01-18 20:34:24 -0800 | [diff] [blame] | 127 | &nativeloader_error_msg); |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 128 | if (dlopen_handle == nullptr) { |
Andreas Gampe | 61c5e7c | 2018-01-18 20:34:24 -0800 | [diff] [blame] | 129 | *error_msg = StringPrintf("Unable to dlopen %s: %s", |
| 130 | name_.c_str(), |
| 131 | nativeloader_error_msg.c_str()); |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 132 | *error = kLoadingError; |
| 133 | return nullptr; |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 134 | } |
Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 135 | if (needs_native_bridge) { |
| 136 | // TODO: Consider support? |
| 137 | android::CloseNativeLibrary(dlopen_handle, needs_native_bridge); |
| 138 | *error_msg = StringPrintf("Native-bridge agents unsupported: %s", name_.c_str()); |
| 139 | *error = kLoadingError; |
| 140 | return nullptr; |
| 141 | } |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 142 | |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 143 | std::unique_ptr<Agent> agent(new Agent(name_, dlopen_handle)); |
| 144 | agent->PopulateFunctions(); |
| 145 | *error = kNoError; |
| 146 | return agent; |
| 147 | } |
| 148 | |
| 149 | std::ostream& operator<<(std::ostream &os, AgentSpec const& m) { |
| 150 | return os << "AgentSpec { name=\"" << m.name_ << "\", args=\"" << m.args_ << "\" }"; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | void* Agent::FindSymbol(const std::string& name) const { |
| 155 | CHECK(dlopen_handle_ != nullptr) << "Cannot find symbols in an unloaded agent library " << this; |
| 156 | return dlsym(dlopen_handle_, name.c_str()); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // TODO Lock some stuff probably. |
| 160 | void Agent::Unload() { |
| 161 | if (dlopen_handle_ != nullptr) { |
| 162 | if (onunload_ != nullptr) { |
| 163 | onunload_(Runtime::Current()->GetJavaVM()); |
| 164 | } |
Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 165 | // Don't actually android::CloseNativeLibrary since some agents assume they will never get |
| 166 | // unloaded. Since this only happens when the runtime is shutting down anyway this isn't a big |
| 167 | // deal. |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 168 | dlopen_handle_ = nullptr; |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 169 | onload_ = nullptr; |
| 170 | onattach_ = nullptr; |
| 171 | onunload_ = nullptr; |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 172 | } else { |
| 173 | VLOG(agents) << this << " is not currently loaded!"; |
| 174 | } |
| 175 | } |
| 176 | |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 177 | Agent::Agent(Agent&& other) |
| 178 | : dlopen_handle_(nullptr), |
| 179 | onload_(nullptr), |
| 180 | onattach_(nullptr), |
| 181 | onunload_(nullptr) { |
| 182 | *this = std::move(other); |
| 183 | } |
| 184 | |
| 185 | Agent& Agent::operator=(Agent&& other) { |
| 186 | if (this != &other) { |
| 187 | if (dlopen_handle_ != nullptr) { |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 188 | Unload(); |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 189 | } |
| 190 | name_ = std::move(other.name_); |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 191 | dlopen_handle_ = other.dlopen_handle_; |
| 192 | onload_ = other.onload_; |
| 193 | onattach_ = other.onattach_; |
| 194 | onunload_ = other.onunload_; |
| 195 | other.dlopen_handle_ = nullptr; |
| 196 | other.onload_ = nullptr; |
| 197 | other.onattach_ = nullptr; |
| 198 | other.onunload_ = nullptr; |
| 199 | } |
| 200 | return *this; |
| 201 | } |
| 202 | |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 203 | void Agent::PopulateFunctions() { |
| 204 | onload_ = reinterpret_cast<AgentOnLoadFunction>(FindSymbol(AGENT_ON_LOAD_FUNCTION_NAME)); |
| 205 | if (onload_ == nullptr) { |
| 206 | VLOG(agents) << "Unable to find 'Agent_OnLoad' symbol in " << this; |
| 207 | } |
| 208 | onattach_ = reinterpret_cast<AgentOnLoadFunction>(FindSymbol(AGENT_ON_ATTACH_FUNCTION_NAME)); |
| 209 | if (onattach_ == nullptr) { |
| 210 | VLOG(agents) << "Unable to find 'Agent_OnAttach' symbol in " << this; |
| 211 | } |
| 212 | onunload_ = reinterpret_cast<AgentOnUnloadFunction>(FindSymbol(AGENT_ON_UNLOAD_FUNCTION_NAME)); |
| 213 | if (onunload_ == nullptr) { |
| 214 | VLOG(agents) << "Unable to find 'Agent_OnUnload' symbol in " << this; |
| 215 | } |
| 216 | } |
| 217 | |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 218 | Agent::~Agent() { |
| 219 | if (dlopen_handle_ != nullptr) { |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 220 | Unload(); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | std::ostream& operator<<(std::ostream &os, const Agent* m) { |
| 225 | return os << *m; |
| 226 | } |
| 227 | |
| 228 | std::ostream& operator<<(std::ostream &os, Agent const& m) { |
Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 229 | return os << "Agent { name=\"" << m.name_ << "\", handle=" << m.dlopen_handle_ << " }"; |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | } // namespace ti |
| 233 | } // namespace art |