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" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 42 | #include "class_linker.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 43 | #include "dex/dex_file.h" |
| 44 | #include "dex/dex_file_types.h" |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 45 | #include "events-inl.h" |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame^] | 46 | #include "fault_handler.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 47 | #include "gc_root-inl.h" |
| 48 | #include "globals.h" |
| 49 | #include "jni_env_ext-inl.h" |
Alex Light | 6a65631 | 2017-03-29 17:18:00 -0700 | [diff] [blame] | 50 | #include "jvalue.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 51 | #include "jvmti.h" |
| 52 | #include "linear_alloc.h" |
| 53 | #include "mem_map.h" |
| 54 | #include "mirror/array.h" |
| 55 | #include "mirror/class-inl.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 56 | #include "mirror/class_ext.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 57 | #include "mirror/class_loader-inl.h" |
| 58 | #include "mirror/string-inl.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 59 | #include "oat_file.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 60 | #include "scoped_thread_state_change-inl.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 61 | #include "stack.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 62 | #include "thread_list.h" |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 63 | #include "ti_redefine.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 64 | #include "transform.h" |
| 65 | #include "utf.h" |
| 66 | #include "utils/dex_cache_arrays_layout-inl.h" |
| 67 | |
| 68 | namespace openjdkjvmti { |
| 69 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame^] | 70 | // A FaultHandler that will deal with initializing ClassDefinitions when they are actually needed. |
| 71 | class TransformationFaultHandler FINAL : public art::FaultHandler { |
| 72 | public: |
| 73 | explicit TransformationFaultHandler(art::FaultManager* manager) |
| 74 | : art::FaultHandler(manager), |
| 75 | uninitialized_class_definitions_lock_("JVMTI Initialized class definitions lock", |
| 76 | art::LockLevel::kSignalHandlingLock), |
| 77 | class_definition_initialized_cond_("JVMTI Initialized class definitions condition", |
| 78 | uninitialized_class_definitions_lock_) { |
| 79 | manager->AddHandler(this, /* generated_code */ false); |
| 80 | } |
| 81 | |
| 82 | ~TransformationFaultHandler() { |
| 83 | art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_); |
| 84 | uninitialized_class_definitions_.clear(); |
| 85 | } |
| 86 | |
| 87 | bool Action(int sig, siginfo_t* siginfo, void* context ATTRIBUTE_UNUSED) OVERRIDE { |
| 88 | DCHECK_EQ(sig, SIGSEGV); |
| 89 | art::Thread* self = art::Thread::Current(); |
| 90 | if (UNLIKELY(uninitialized_class_definitions_lock_.IsExclusiveHeld(self))) { |
| 91 | if (self != nullptr) { |
| 92 | LOG(FATAL) << "Recursive call into Transformation fault handler!"; |
| 93 | UNREACHABLE(); |
| 94 | } else { |
| 95 | LOG(ERROR) << "Possible deadlock due to recursive signal delivery of segv."; |
| 96 | } |
| 97 | } |
| 98 | uintptr_t ptr = reinterpret_cast<uintptr_t>(siginfo->si_addr); |
| 99 | ArtClassDefinition* res = nullptr; |
| 100 | |
| 101 | { |
| 102 | // NB Technically using a mutex and condition variables here is non-posix compliant but |
| 103 | // everything should be fine since both glibc and bionic implementations of mutexs and |
| 104 | // condition variables work fine so long as the thread was not interrupted during a |
| 105 | // lock/unlock (which it wasn't) on all architectures we care about. |
| 106 | art::MutexLock mu(self, uninitialized_class_definitions_lock_); |
| 107 | auto it = std::find_if(uninitialized_class_definitions_.begin(), |
| 108 | uninitialized_class_definitions_.end(), |
| 109 | [&](const auto op) { return op->ContainsAddress(ptr); }); |
| 110 | if (it != uninitialized_class_definitions_.end()) { |
| 111 | res = *it; |
| 112 | // Remove the class definition. |
| 113 | uninitialized_class_definitions_.erase(it); |
| 114 | // Put it in the initializing list |
| 115 | initializing_class_definitions_.push_back(res); |
| 116 | } else { |
| 117 | // Wait for the ptr to be initialized (if it is currently initializing). |
| 118 | while (DefinitionIsInitializing(ptr)) { |
| 119 | WaitForClassInitializationToFinish(); |
| 120 | } |
| 121 | // Return true (continue with user code) if we find that the definition has been |
| 122 | // initialized. Return false (continue on to next signal handler) if the definition is not |
| 123 | // initialized or found. |
| 124 | return std::find_if(initialized_class_definitions_.begin(), |
| 125 | initialized_class_definitions_.end(), |
| 126 | [&](const auto op) { return op->ContainsAddress(ptr); }) != |
| 127 | uninitialized_class_definitions_.end(); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | VLOG(signals) << "Lazy initialization of dex file for transformation of " << res->GetName() |
| 132 | << " during SEGV"; |
| 133 | res->InitializeMemory(); |
| 134 | |
| 135 | { |
| 136 | art::MutexLock mu(self, uninitialized_class_definitions_lock_); |
| 137 | // Move to initialized state and notify waiters. |
| 138 | initializing_class_definitions_.erase(std::find(initializing_class_definitions_.begin(), |
| 139 | initializing_class_definitions_.end(), |
| 140 | res)); |
| 141 | initialized_class_definitions_.push_back(res); |
| 142 | class_definition_initialized_cond_.Broadcast(self); |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | void RemoveDefinition(ArtClassDefinition* def) REQUIRES(!uninitialized_class_definitions_lock_) { |
| 149 | art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_); |
| 150 | auto it = std::find(uninitialized_class_definitions_.begin(), |
| 151 | uninitialized_class_definitions_.end(), |
| 152 | def); |
| 153 | if (it != uninitialized_class_definitions_.end()) { |
| 154 | uninitialized_class_definitions_.erase(it); |
| 155 | return; |
| 156 | } |
| 157 | while (std::find(initializing_class_definitions_.begin(), |
| 158 | initializing_class_definitions_.end(), |
| 159 | def) != initializing_class_definitions_.end()) { |
| 160 | WaitForClassInitializationToFinish(); |
| 161 | } |
| 162 | it = std::find(initialized_class_definitions_.begin(), |
| 163 | initialized_class_definitions_.end(), |
| 164 | def); |
| 165 | CHECK(it != initialized_class_definitions_.end()) << "Could not find class definition for " |
| 166 | << def->GetName(); |
| 167 | initialized_class_definitions_.erase(it); |
| 168 | } |
| 169 | |
| 170 | void AddArtDefinition(ArtClassDefinition* def) REQUIRES(!uninitialized_class_definitions_lock_) { |
| 171 | DCHECK(def->IsLazyDefinition()); |
| 172 | art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_); |
| 173 | uninitialized_class_definitions_.push_back(def); |
| 174 | } |
| 175 | |
| 176 | private: |
| 177 | bool DefinitionIsInitializing(uintptr_t ptr) REQUIRES(uninitialized_class_definitions_lock_) { |
| 178 | return std::find_if(initializing_class_definitions_.begin(), |
| 179 | initializing_class_definitions_.end(), |
| 180 | [&](const auto op) { return op->ContainsAddress(ptr); }) != |
| 181 | initializing_class_definitions_.end(); |
| 182 | } |
| 183 | |
| 184 | void WaitForClassInitializationToFinish() REQUIRES(uninitialized_class_definitions_lock_) { |
| 185 | class_definition_initialized_cond_.Wait(art::Thread::Current()); |
| 186 | } |
| 187 | |
| 188 | art::Mutex uninitialized_class_definitions_lock_ ACQUIRED_BEFORE(art::Locks::abort_lock_); |
| 189 | art::ConditionVariable class_definition_initialized_cond_ |
| 190 | GUARDED_BY(uninitialized_class_definitions_lock_); |
| 191 | |
| 192 | // A list of the class definitions that have a non-readable map. |
| 193 | std::vector<ArtClassDefinition*> uninitialized_class_definitions_ |
| 194 | GUARDED_BY(uninitialized_class_definitions_lock_); |
| 195 | |
| 196 | // A list of class definitions that are currently undergoing unquickening. Threads should wait |
| 197 | // until the definition is no longer in this before returning. |
| 198 | std::vector<ArtClassDefinition*> initializing_class_definitions_ |
| 199 | GUARDED_BY(uninitialized_class_definitions_lock_); |
| 200 | |
| 201 | // A list of class definitions that are already unquickened. Threads should immediately return if |
| 202 | // it is here. |
| 203 | std::vector<ArtClassDefinition*> initialized_class_definitions_ |
| 204 | GUARDED_BY(uninitialized_class_definitions_lock_); |
| 205 | }; |
| 206 | |
| 207 | static TransformationFaultHandler* gTransformFaultHandler = nullptr; |
| 208 | |
| 209 | void Transformer::Setup() { |
| 210 | // Although we create this the fault handler is actually owned by the 'art::fault_manager' which |
| 211 | // will take care of destroying it. |
| 212 | if (art::MemMap::kCanReplaceMapping && ArtClassDefinition::kEnableOnDemandDexDequicken) { |
| 213 | gTransformFaultHandler = new TransformationFaultHandler(&art::fault_manager); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Simple helper to add and remove the class definition from the fault handler. |
| 218 | class ScopedDefinitionHandler { |
| 219 | public: |
| 220 | explicit ScopedDefinitionHandler(ArtClassDefinition* def) |
| 221 | : def_(def), is_lazy_(def_->IsLazyDefinition()) { |
| 222 | if (is_lazy_) { |
| 223 | gTransformFaultHandler->AddArtDefinition(def_); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | ~ScopedDefinitionHandler() { |
| 228 | if (is_lazy_) { |
| 229 | gTransformFaultHandler->RemoveDefinition(def_); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | ArtClassDefinition* def_; |
| 235 | bool is_lazy_; |
| 236 | }; |
| 237 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 238 | // Initialize templates. |
| 239 | template |
| 240 | void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>( |
| 241 | EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def); |
| 242 | template |
| 243 | void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>( |
| 244 | EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def); |
| 245 | |
| 246 | template<ArtJvmtiEvent kEvent> |
| 247 | void Transformer::TransformSingleClassDirect(EventHandler* event_handler, |
| 248 | art::Thread* self, |
| 249 | /*in-out*/ArtClassDefinition* def) { |
| 250 | static_assert(kEvent == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable || |
| 251 | kEvent == ArtJvmtiEvent::kClassFileLoadHookRetransformable, |
| 252 | "bad event type"); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame^] | 253 | ScopedDefinitionHandler handler(def); |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 254 | jint new_len = -1; |
| 255 | unsigned char* new_data = nullptr; |
| 256 | art::ArrayRef<const unsigned char> dex_data = def->GetDexData(); |
| 257 | event_handler->DispatchEvent<kEvent>( |
| 258 | self, |
| 259 | static_cast<JNIEnv*>(self->GetJniEnv()), |
| 260 | def->GetClass(), |
| 261 | def->GetLoader(), |
| 262 | def->GetName().c_str(), |
| 263 | def->GetProtectionDomain(), |
| 264 | static_cast<jint>(dex_data.size()), |
| 265 | dex_data.data(), |
| 266 | /*out*/&new_len, |
| 267 | /*out*/&new_data); |
| 268 | def->SetNewDexData(new_len, new_data); |
| 269 | } |
| 270 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 271 | jvmtiError Transformer::RetransformClassesDirect( |
Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 272 | EventHandler* event_handler, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 273 | art::Thread* self, |
| 274 | /*in-out*/std::vector<ArtClassDefinition>* definitions) { |
| 275 | for (ArtClassDefinition& def : *definitions) { |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 276 | TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(event_handler, |
| 277 | self, |
| 278 | &def); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 279 | } |
| 280 | return OK; |
| 281 | } |
| 282 | |
| 283 | jvmtiError Transformer::RetransformClasses(ArtJvmTiEnv* env, |
Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 284 | EventHandler* event_handler, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 285 | art::Runtime* runtime, |
| 286 | art::Thread* self, |
| 287 | jint class_count, |
| 288 | const jclass* classes, |
| 289 | /*out*/std::string* error_msg) { |
| 290 | if (env == nullptr) { |
| 291 | *error_msg = "env was null!"; |
| 292 | return ERR(INVALID_ENVIRONMENT); |
| 293 | } else if (class_count < 0) { |
| 294 | *error_msg = "class_count was less then 0"; |
| 295 | return ERR(ILLEGAL_ARGUMENT); |
| 296 | } else if (class_count == 0) { |
| 297 | // We don't actually need to do anything. Just return OK. |
| 298 | return OK; |
| 299 | } else if (classes == nullptr) { |
| 300 | *error_msg = "null classes!"; |
| 301 | return ERR(NULL_POINTER); |
| 302 | } |
| 303 | // A holder that will Deallocate all the class bytes buffers on destruction. |
| 304 | std::vector<ArtClassDefinition> definitions; |
| 305 | jvmtiError res = OK; |
| 306 | for (jint i = 0; i < class_count; i++) { |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 307 | jboolean is_modifiable = JNI_FALSE; |
| 308 | res = env->IsModifiableClass(classes[i], &is_modifiable); |
| 309 | if (res != OK) { |
| 310 | return res; |
| 311 | } else if (!is_modifiable) { |
| 312 | return ERR(UNMODIFIABLE_CLASS); |
| 313 | } |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 314 | ArtClassDefinition def; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 315 | res = def.Init(self, classes[i]); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 316 | if (res != OK) { |
| 317 | return res; |
| 318 | } |
| 319 | definitions.push_back(std::move(def)); |
| 320 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 321 | res = RetransformClassesDirect(event_handler, self, &definitions); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 322 | if (res != OK) { |
| 323 | return res; |
| 324 | } |
| 325 | return Redefiner::RedefineClassesDirect(env, runtime, self, definitions, error_msg); |
| 326 | } |
| 327 | |
| 328 | // TODO Move this somewhere else, ti_class? |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 329 | jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) { |
| 330 | JNIEnv* jni_env = nullptr; |
| 331 | jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1); |
| 332 | if (ret != JNI_OK) { |
| 333 | // TODO Different error might be better? |
| 334 | return ERR(INTERNAL); |
| 335 | } |
| 336 | art::ScopedObjectAccess soa(jni_env); |
| 337 | art::StackHandleScope<1> hs(art::Thread::Current()); |
| 338 | art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass))); |
| 339 | const art::DexFile& dex = hs_klass->GetDexFile(); |
| 340 | *location = dex.GetLocation(); |
| 341 | return OK; |
| 342 | } |
| 343 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 344 | } // namespace openjdkjvmti |