Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2016 The Android Open Source Project |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | * |
| 4 | * This file implements interfaces from the file jvmti.h. This implementation |
| 5 | * is licensed under the same terms as the file jvmti.h. The |
| 6 | * copyright and license information for the file jvmti.h follows. |
| 7 | * |
| 8 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
| 9 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 10 | * |
| 11 | * This code is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License version 2 only, as |
| 13 | * published by the Free Software Foundation. Oracle designates this |
| 14 | * particular file as subject to the "Classpath" exception as provided |
| 15 | * by Oracle in the LICENSE file that accompanied this code. |
| 16 | * |
| 17 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * version 2 for more details (a copy is included in the LICENSE file that |
| 21 | * accompanied this code). |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License version |
| 24 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 25 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | * |
| 27 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 28 | * or visit www.oracle.com if you need additional information or have any |
| 29 | * questions. |
| 30 | */ |
| 31 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 32 | #include <stddef.h> |
| 33 | #include <sys/types.h> |
| 34 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 35 | #include <unordered_map> |
| 36 | #include <unordered_set> |
| 37 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 38 | #include "transform.h" |
| 39 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 40 | #include "art_method.h" |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 41 | #include "base/array_ref.h" |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 42 | #include "base/globals.h" |
Andreas Gampe | 85f1c57 | 2018-11-21 13:52:48 -0800 | [diff] [blame^] | 43 | #include "base/logging.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 44 | #include "base/mem_map.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 45 | #include "class_linker.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 46 | #include "dex/dex_file.h" |
| 47 | #include "dex/dex_file_types.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 48 | #include "dex/utf.h" |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 49 | #include "events-inl.h" |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 50 | #include "fault_handler.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 51 | #include "gc_root-inl.h" |
Andreas Gampe | e15b9b1 | 2018-10-29 12:54:27 -0700 | [diff] [blame] | 52 | #include "handle_scope-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 53 | #include "jni/jni_env_ext-inl.h" |
Alex Light | 6a65631 | 2017-03-29 17:18:00 -0700 | [diff] [blame] | 54 | #include "jvalue.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 55 | #include "jvmti.h" |
| 56 | #include "linear_alloc.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 57 | #include "mirror/array.h" |
| 58 | #include "mirror/class-inl.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 59 | #include "mirror/class_ext.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 60 | #include "mirror/class_loader-inl.h" |
| 61 | #include "mirror/string-inl.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 62 | #include "oat_file.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 63 | #include "scoped_thread_state_change-inl.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 64 | #include "stack.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 65 | #include "thread_list.h" |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 66 | #include "ti_redefine.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 67 | #include "transform.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 68 | #include "utils/dex_cache_arrays_layout-inl.h" |
| 69 | |
| 70 | namespace openjdkjvmti { |
| 71 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 72 | // A FaultHandler that will deal with initializing ClassDefinitions when they are actually needed. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 73 | class TransformationFaultHandler final : public art::FaultHandler { |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 74 | public: |
| 75 | explicit TransformationFaultHandler(art::FaultManager* manager) |
| 76 | : art::FaultHandler(manager), |
| 77 | uninitialized_class_definitions_lock_("JVMTI Initialized class definitions lock", |
| 78 | art::LockLevel::kSignalHandlingLock), |
| 79 | class_definition_initialized_cond_("JVMTI Initialized class definitions condition", |
| 80 | uninitialized_class_definitions_lock_) { |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 81 | manager->AddHandler(this, /* generated_code= */ false); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | ~TransformationFaultHandler() { |
| 85 | art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_); |
| 86 | uninitialized_class_definitions_.clear(); |
| 87 | } |
| 88 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 89 | bool Action(int sig, siginfo_t* siginfo, void* context ATTRIBUTE_UNUSED) override { |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 90 | DCHECK_EQ(sig, SIGSEGV); |
| 91 | art::Thread* self = art::Thread::Current(); |
| 92 | if (UNLIKELY(uninitialized_class_definitions_lock_.IsExclusiveHeld(self))) { |
| 93 | if (self != nullptr) { |
| 94 | LOG(FATAL) << "Recursive call into Transformation fault handler!"; |
| 95 | UNREACHABLE(); |
| 96 | } else { |
| 97 | LOG(ERROR) << "Possible deadlock due to recursive signal delivery of segv."; |
| 98 | } |
| 99 | } |
| 100 | uintptr_t ptr = reinterpret_cast<uintptr_t>(siginfo->si_addr); |
| 101 | ArtClassDefinition* res = nullptr; |
| 102 | |
| 103 | { |
| 104 | // NB Technically using a mutex and condition variables here is non-posix compliant but |
| 105 | // everything should be fine since both glibc and bionic implementations of mutexs and |
| 106 | // condition variables work fine so long as the thread was not interrupted during a |
| 107 | // lock/unlock (which it wasn't) on all architectures we care about. |
| 108 | art::MutexLock mu(self, uninitialized_class_definitions_lock_); |
| 109 | auto it = std::find_if(uninitialized_class_definitions_.begin(), |
| 110 | uninitialized_class_definitions_.end(), |
| 111 | [&](const auto op) { return op->ContainsAddress(ptr); }); |
| 112 | if (it != uninitialized_class_definitions_.end()) { |
| 113 | res = *it; |
| 114 | // Remove the class definition. |
| 115 | uninitialized_class_definitions_.erase(it); |
| 116 | // Put it in the initializing list |
| 117 | initializing_class_definitions_.push_back(res); |
| 118 | } else { |
| 119 | // Wait for the ptr to be initialized (if it is currently initializing). |
| 120 | while (DefinitionIsInitializing(ptr)) { |
| 121 | WaitForClassInitializationToFinish(); |
| 122 | } |
| 123 | // Return true (continue with user code) if we find that the definition has been |
| 124 | // initialized. Return false (continue on to next signal handler) if the definition is not |
| 125 | // initialized or found. |
| 126 | return std::find_if(initialized_class_definitions_.begin(), |
| 127 | initialized_class_definitions_.end(), |
| 128 | [&](const auto op) { return op->ContainsAddress(ptr); }) != |
Alex Light | 12ee56b | 2018-02-12 13:00:34 -0800 | [diff] [blame] | 129 | initialized_class_definitions_.end(); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Alex Light | fe2a39d | 2018-02-05 11:08:08 -0800 | [diff] [blame] | 133 | if (LIKELY(self != nullptr)) { |
| 134 | CHECK_EQ(self->GetState(), art::ThreadState::kNative) |
| 135 | << "Transformation fault handler occurred outside of native mode"; |
| 136 | } |
| 137 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 138 | VLOG(signals) << "Lazy initialization of dex file for transformation of " << res->GetName() |
| 139 | << " during SEGV"; |
| 140 | res->InitializeMemory(); |
| 141 | |
| 142 | { |
| 143 | art::MutexLock mu(self, uninitialized_class_definitions_lock_); |
| 144 | // Move to initialized state and notify waiters. |
| 145 | initializing_class_definitions_.erase(std::find(initializing_class_definitions_.begin(), |
| 146 | initializing_class_definitions_.end(), |
| 147 | res)); |
| 148 | initialized_class_definitions_.push_back(res); |
| 149 | class_definition_initialized_cond_.Broadcast(self); |
| 150 | } |
| 151 | |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | void RemoveDefinition(ArtClassDefinition* def) REQUIRES(!uninitialized_class_definitions_lock_) { |
| 156 | art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_); |
| 157 | auto it = std::find(uninitialized_class_definitions_.begin(), |
| 158 | uninitialized_class_definitions_.end(), |
| 159 | def); |
| 160 | if (it != uninitialized_class_definitions_.end()) { |
| 161 | uninitialized_class_definitions_.erase(it); |
| 162 | return; |
| 163 | } |
| 164 | while (std::find(initializing_class_definitions_.begin(), |
| 165 | initializing_class_definitions_.end(), |
| 166 | def) != initializing_class_definitions_.end()) { |
| 167 | WaitForClassInitializationToFinish(); |
| 168 | } |
| 169 | it = std::find(initialized_class_definitions_.begin(), |
| 170 | initialized_class_definitions_.end(), |
| 171 | def); |
| 172 | CHECK(it != initialized_class_definitions_.end()) << "Could not find class definition for " |
| 173 | << def->GetName(); |
| 174 | initialized_class_definitions_.erase(it); |
| 175 | } |
| 176 | |
| 177 | void AddArtDefinition(ArtClassDefinition* def) REQUIRES(!uninitialized_class_definitions_lock_) { |
| 178 | DCHECK(def->IsLazyDefinition()); |
| 179 | art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_); |
| 180 | uninitialized_class_definitions_.push_back(def); |
| 181 | } |
| 182 | |
| 183 | private: |
| 184 | bool DefinitionIsInitializing(uintptr_t ptr) REQUIRES(uninitialized_class_definitions_lock_) { |
| 185 | return std::find_if(initializing_class_definitions_.begin(), |
| 186 | initializing_class_definitions_.end(), |
| 187 | [&](const auto op) { return op->ContainsAddress(ptr); }) != |
| 188 | initializing_class_definitions_.end(); |
| 189 | } |
| 190 | |
| 191 | void WaitForClassInitializationToFinish() REQUIRES(uninitialized_class_definitions_lock_) { |
| 192 | class_definition_initialized_cond_.Wait(art::Thread::Current()); |
| 193 | } |
| 194 | |
| 195 | art::Mutex uninitialized_class_definitions_lock_ ACQUIRED_BEFORE(art::Locks::abort_lock_); |
| 196 | art::ConditionVariable class_definition_initialized_cond_ |
| 197 | GUARDED_BY(uninitialized_class_definitions_lock_); |
| 198 | |
| 199 | // A list of the class definitions that have a non-readable map. |
| 200 | std::vector<ArtClassDefinition*> uninitialized_class_definitions_ |
| 201 | GUARDED_BY(uninitialized_class_definitions_lock_); |
| 202 | |
| 203 | // A list of class definitions that are currently undergoing unquickening. Threads should wait |
| 204 | // until the definition is no longer in this before returning. |
| 205 | std::vector<ArtClassDefinition*> initializing_class_definitions_ |
| 206 | GUARDED_BY(uninitialized_class_definitions_lock_); |
| 207 | |
| 208 | // A list of class definitions that are already unquickened. Threads should immediately return if |
| 209 | // it is here. |
| 210 | std::vector<ArtClassDefinition*> initialized_class_definitions_ |
| 211 | GUARDED_BY(uninitialized_class_definitions_lock_); |
| 212 | }; |
| 213 | |
| 214 | static TransformationFaultHandler* gTransformFaultHandler = nullptr; |
| 215 | |
| 216 | void Transformer::Setup() { |
| 217 | // Although we create this the fault handler is actually owned by the 'art::fault_manager' which |
| 218 | // will take care of destroying it. |
| 219 | if (art::MemMap::kCanReplaceMapping && ArtClassDefinition::kEnableOnDemandDexDequicken) { |
| 220 | gTransformFaultHandler = new TransformationFaultHandler(&art::fault_manager); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Simple helper to add and remove the class definition from the fault handler. |
| 225 | class ScopedDefinitionHandler { |
| 226 | public: |
| 227 | explicit ScopedDefinitionHandler(ArtClassDefinition* def) |
| 228 | : def_(def), is_lazy_(def_->IsLazyDefinition()) { |
| 229 | if (is_lazy_) { |
| 230 | gTransformFaultHandler->AddArtDefinition(def_); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | ~ScopedDefinitionHandler() { |
| 235 | if (is_lazy_) { |
| 236 | gTransformFaultHandler->RemoveDefinition(def_); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | private: |
| 241 | ArtClassDefinition* def_; |
| 242 | bool is_lazy_; |
| 243 | }; |
| 244 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 245 | // Initialize templates. |
| 246 | template |
| 247 | void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>( |
| 248 | EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def); |
| 249 | template |
| 250 | void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>( |
| 251 | EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def); |
| 252 | |
| 253 | template<ArtJvmtiEvent kEvent> |
| 254 | void Transformer::TransformSingleClassDirect(EventHandler* event_handler, |
| 255 | art::Thread* self, |
| 256 | /*in-out*/ArtClassDefinition* def) { |
| 257 | static_assert(kEvent == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable || |
| 258 | kEvent == ArtJvmtiEvent::kClassFileLoadHookRetransformable, |
| 259 | "bad event type"); |
Alex Light | fe2a39d | 2018-02-05 11:08:08 -0800 | [diff] [blame] | 260 | // We don't want to do transitions between calling the event and setting the new data so change to |
| 261 | // native state early. This also avoids any problems that the FaultHandler might have in |
| 262 | // determining if an access to the dex_data is from generated code or not. |
| 263 | art::ScopedThreadStateChange stsc(self, art::ThreadState::kNative); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 264 | ScopedDefinitionHandler handler(def); |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 265 | jint new_len = -1; |
| 266 | unsigned char* new_data = nullptr; |
| 267 | art::ArrayRef<const unsigned char> dex_data = def->GetDexData(); |
| 268 | event_handler->DispatchEvent<kEvent>( |
| 269 | self, |
| 270 | static_cast<JNIEnv*>(self->GetJniEnv()), |
| 271 | def->GetClass(), |
| 272 | def->GetLoader(), |
| 273 | def->GetName().c_str(), |
| 274 | def->GetProtectionDomain(), |
| 275 | static_cast<jint>(dex_data.size()), |
| 276 | dex_data.data(), |
| 277 | /*out*/&new_len, |
| 278 | /*out*/&new_data); |
| 279 | def->SetNewDexData(new_len, new_data); |
| 280 | } |
| 281 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 282 | jvmtiError Transformer::RetransformClassesDirect( |
Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 283 | EventHandler* event_handler, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 284 | art::Thread* self, |
| 285 | /*in-out*/std::vector<ArtClassDefinition>* definitions) { |
| 286 | for (ArtClassDefinition& def : *definitions) { |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 287 | TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(event_handler, |
| 288 | self, |
| 289 | &def); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 290 | } |
| 291 | return OK; |
| 292 | } |
| 293 | |
| 294 | jvmtiError Transformer::RetransformClasses(ArtJvmTiEnv* env, |
Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 295 | EventHandler* event_handler, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 296 | art::Runtime* runtime, |
| 297 | art::Thread* self, |
| 298 | jint class_count, |
| 299 | const jclass* classes, |
| 300 | /*out*/std::string* error_msg) { |
| 301 | if (env == nullptr) { |
| 302 | *error_msg = "env was null!"; |
| 303 | return ERR(INVALID_ENVIRONMENT); |
| 304 | } else if (class_count < 0) { |
| 305 | *error_msg = "class_count was less then 0"; |
| 306 | return ERR(ILLEGAL_ARGUMENT); |
| 307 | } else if (class_count == 0) { |
| 308 | // We don't actually need to do anything. Just return OK. |
| 309 | return OK; |
| 310 | } else if (classes == nullptr) { |
| 311 | *error_msg = "null classes!"; |
| 312 | return ERR(NULL_POINTER); |
| 313 | } |
| 314 | // A holder that will Deallocate all the class bytes buffers on destruction. |
| 315 | std::vector<ArtClassDefinition> definitions; |
| 316 | jvmtiError res = OK; |
| 317 | for (jint i = 0; i < class_count; i++) { |
Alex Light | 9e7859c | 2018-04-05 13:49:43 -0700 | [diff] [blame] | 318 | res = Redefiner::GetClassRedefinitionError(classes[i], error_msg); |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 319 | if (res != OK) { |
| 320 | return res; |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 321 | } |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 322 | ArtClassDefinition def; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 323 | res = def.Init(self, classes[i]); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 324 | if (res != OK) { |
| 325 | return res; |
| 326 | } |
| 327 | definitions.push_back(std::move(def)); |
| 328 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 329 | res = RetransformClassesDirect(event_handler, self, &definitions); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 330 | if (res != OK) { |
| 331 | return res; |
| 332 | } |
| 333 | return Redefiner::RedefineClassesDirect(env, runtime, self, definitions, error_msg); |
| 334 | } |
| 335 | |
| 336 | // TODO Move this somewhere else, ti_class? |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 337 | jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) { |
| 338 | JNIEnv* jni_env = nullptr; |
| 339 | jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1); |
| 340 | if (ret != JNI_OK) { |
| 341 | // TODO Different error might be better? |
| 342 | return ERR(INTERNAL); |
| 343 | } |
| 344 | art::ScopedObjectAccess soa(jni_env); |
| 345 | art::StackHandleScope<1> hs(art::Thread::Current()); |
| 346 | art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass))); |
| 347 | const art::DexFile& dex = hs_klass->GetDexFile(); |
| 348 | *location = dex.GetLocation(); |
| 349 | return OK; |
| 350 | } |
| 351 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 352 | } // namespace openjdkjvmti |