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