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