Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -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 | |
| 32 | #include "ti_class.h" |
| 33 | |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 34 | #include "android-base/stringprintf.h" |
| 35 | |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 36 | #include <mutex> |
| 37 | #include <unordered_set> |
| 38 | |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 39 | #include "art_jvmti.h" |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 40 | #include "base/macros.h" |
Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 41 | #include "class_table-inl.h" |
| 42 | #include "class_linker.h" |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 43 | #include "common_throws.h" |
Andreas Gampe | 0eb3643 | 2017-02-15 18:36:14 -0800 | [diff] [blame] | 44 | #include "dex_file_annotations.h" |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 45 | #include "events-inl.h" |
Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 46 | #include "gc/heap.h" |
| 47 | #include "gc_root.h" |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 48 | #include "handle.h" |
| 49 | #include "jni_env_ext-inl.h" |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 50 | #include "jni_internal.h" |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 51 | #include "mirror/array-inl.h" |
| 52 | #include "mirror/class-inl.h" |
| 53 | #include "mirror/class_ext.h" |
Andreas Gampe | 0eb3643 | 2017-02-15 18:36:14 -0800 | [diff] [blame] | 54 | #include "mirror/object_array-inl.h" |
Andreas Gampe | a67354b | 2017-02-10 16:18:30 -0800 | [diff] [blame] | 55 | #include "mirror/object_reference.h" |
| 56 | #include "mirror/object-inl.h" |
Andreas Gampe | 52784ac | 2017-02-13 18:10:09 -0800 | [diff] [blame] | 57 | #include "mirror/reference.h" |
Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 58 | #include "runtime.h" |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 59 | #include "runtime_callbacks.h" |
| 60 | #include "ScopedLocalRef.h" |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 61 | #include "scoped_thread_state_change-inl.h" |
| 62 | #include "thread-inl.h" |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 63 | #include "thread_list.h" |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 64 | #include "ti_class_loader.h" |
Alex Light | d8ce4e7 | 2017-02-27 10:52:29 -0800 | [diff] [blame^] | 65 | #include "ti_phase.h" |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 66 | #include "ti_redefine.h" |
| 67 | #include "utils.h" |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 68 | |
| 69 | namespace openjdkjvmti { |
| 70 | |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 71 | using android::base::StringPrintf; |
| 72 | |
| 73 | static std::unique_ptr<const art::DexFile> MakeSingleDexFile(art::Thread* self, |
| 74 | const char* descriptor, |
| 75 | const std::string& orig_location, |
| 76 | jint final_len, |
| 77 | const unsigned char* final_dex_data) |
| 78 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 79 | // Make the mmap |
| 80 | std::string error_msg; |
| 81 | std::unique_ptr<art::MemMap> map(Redefiner::MoveDataToMemMap(orig_location, |
| 82 | final_len, |
| 83 | final_dex_data, |
| 84 | &error_msg)); |
| 85 | if (map.get() == nullptr) { |
| 86 | LOG(WARNING) << "Unable to allocate mmap for redefined dex file! Error was: " << error_msg; |
| 87 | self->ThrowOutOfMemoryError(StringPrintf( |
| 88 | "Unable to allocate dex file for transformation of %s", descriptor).c_str()); |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
| 92 | // Make a dex-file |
| 93 | if (map->Size() < sizeof(art::DexFile::Header)) { |
| 94 | LOG(WARNING) << "Could not read dex file header because dex_data was too short"; |
| 95 | art::ThrowClassFormatError(nullptr, |
| 96 | "Unable to read transformed dex file of %s", |
| 97 | descriptor); |
| 98 | return nullptr; |
| 99 | } |
| 100 | uint32_t checksum = reinterpret_cast<const art::DexFile::Header*>(map->Begin())->checksum_; |
| 101 | std::unique_ptr<const art::DexFile> dex_file(art::DexFile::Open(map->GetName(), |
| 102 | checksum, |
| 103 | std::move(map), |
| 104 | /*verify*/true, |
| 105 | /*verify_checksum*/true, |
| 106 | &error_msg)); |
| 107 | if (dex_file.get() == nullptr) { |
| 108 | LOG(WARNING) << "Unable to load modified dex file for " << descriptor << ": " << error_msg; |
| 109 | art::ThrowClassFormatError(nullptr, |
| 110 | "Unable to read transformed dex file of %s because %s", |
| 111 | descriptor, |
| 112 | error_msg.c_str()); |
| 113 | return nullptr; |
| 114 | } |
| 115 | if (dex_file->NumClassDefs() != 1) { |
| 116 | LOG(WARNING) << "Dex file contains more than 1 class_def. Ignoring."; |
| 117 | // TODO Throw some other sort of error here maybe? |
| 118 | art::ThrowClassFormatError( |
| 119 | nullptr, |
| 120 | "Unable to use transformed dex file of %s because it contained too many classes", |
| 121 | descriptor); |
| 122 | return nullptr; |
| 123 | } |
| 124 | return dex_file; |
| 125 | } |
| 126 | |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 127 | struct ClassCallback : public art::ClassLoadCallback { |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 128 | void ClassPreDefine(const char* descriptor, |
| 129 | art::Handle<art::mirror::Class> klass, |
| 130 | art::Handle<art::mirror::ClassLoader> class_loader, |
| 131 | const art::DexFile& initial_dex_file, |
| 132 | const art::DexFile::ClassDef& initial_class_def ATTRIBUTE_UNUSED, |
| 133 | /*out*/art::DexFile const** final_dex_file, |
| 134 | /*out*/art::DexFile::ClassDef const** final_class_def) |
| 135 | OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 136 | bool is_enabled = |
| 137 | event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassFileLoadHookRetransformable) || |
| 138 | event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassFileLoadHookNonRetransformable); |
| 139 | if (!is_enabled) { |
| 140 | return; |
| 141 | } |
| 142 | if (descriptor[0] != 'L') { |
| 143 | // It is a primitive or array. Just return |
| 144 | return; |
| 145 | } |
Alex Light | d8ce4e7 | 2017-02-27 10:52:29 -0800 | [diff] [blame^] | 146 | jvmtiPhase phase = PhaseUtil::GetPhaseUnchecked(); |
| 147 | if (UNLIKELY(phase != JVMTI_PHASE_START && phase != JVMTI_PHASE_LIVE)) { |
| 148 | // We want to wait until we are at least in the START phase so that all WellKnownClasses and |
| 149 | // mirror classes have been initialized and loaded. The runtime relies on these classes having |
| 150 | // specific fields and methods present. Since PreDefine hooks don't need to abide by this |
| 151 | // restriction we will simply not send the event for these classes. |
| 152 | LOG(WARNING) << "Ignoring load of class <" << descriptor << "> as it is being loaded during " |
| 153 | << "runtime initialization."; |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | // Strip the 'L' and ';' from the descriptor |
Alex Light | 2802712 | 2017-01-26 17:21:51 -0800 | [diff] [blame] | 158 | std::string name(std::string(descriptor).substr(1, strlen(descriptor) - 2)); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 159 | |
| 160 | art::Thread* self = art::Thread::Current(); |
| 161 | art::JNIEnvExt* env = self->GetJniEnv(); |
| 162 | ScopedLocalRef<jobject> loader( |
| 163 | env, class_loader.IsNull() ? nullptr : env->AddLocalReference<jobject>(class_loader.Get())); |
| 164 | // Go back to native. |
| 165 | art::ScopedThreadSuspension sts(self, art::ThreadState::kNative); |
| 166 | // Call all Non-retransformable agents. |
| 167 | jint post_no_redefine_len = 0; |
| 168 | unsigned char* post_no_redefine_dex_data = nullptr; |
| 169 | std::unique_ptr<const unsigned char> post_no_redefine_unique_ptr(nullptr); |
| 170 | event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>( |
| 171 | self, |
| 172 | static_cast<JNIEnv*>(env), |
| 173 | static_cast<jclass>(nullptr), // The class doesn't really exist yet so send null. |
| 174 | loader.get(), |
| 175 | name.c_str(), |
| 176 | static_cast<jobject>(nullptr), // Android doesn't seem to have protection domains |
| 177 | static_cast<jint>(initial_dex_file.Size()), |
| 178 | static_cast<const unsigned char*>(initial_dex_file.Begin()), |
| 179 | static_cast<jint*>(&post_no_redefine_len), |
| 180 | static_cast<unsigned char**>(&post_no_redefine_dex_data)); |
| 181 | if (post_no_redefine_dex_data == nullptr) { |
| 182 | DCHECK_EQ(post_no_redefine_len, 0); |
| 183 | post_no_redefine_dex_data = const_cast<unsigned char*>(initial_dex_file.Begin()); |
| 184 | post_no_redefine_len = initial_dex_file.Size(); |
| 185 | } else { |
| 186 | post_no_redefine_unique_ptr = std::unique_ptr<const unsigned char>(post_no_redefine_dex_data); |
| 187 | DCHECK_GT(post_no_redefine_len, 0); |
| 188 | } |
| 189 | // Call all retransformable agents. |
| 190 | jint final_len = 0; |
| 191 | unsigned char* final_dex_data = nullptr; |
| 192 | std::unique_ptr<const unsigned char> final_dex_unique_ptr(nullptr); |
| 193 | event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>( |
| 194 | self, |
| 195 | static_cast<JNIEnv*>(env), |
| 196 | static_cast<jclass>(nullptr), // The class doesn't really exist yet so send null. |
| 197 | loader.get(), |
| 198 | name.c_str(), |
| 199 | static_cast<jobject>(nullptr), // Android doesn't seem to have protection domains |
| 200 | static_cast<jint>(post_no_redefine_len), |
| 201 | static_cast<const unsigned char*>(post_no_redefine_dex_data), |
| 202 | static_cast<jint*>(&final_len), |
| 203 | static_cast<unsigned char**>(&final_dex_data)); |
| 204 | if (final_dex_data == nullptr) { |
| 205 | DCHECK_EQ(final_len, 0); |
| 206 | final_dex_data = post_no_redefine_dex_data; |
| 207 | final_len = post_no_redefine_len; |
| 208 | } else { |
| 209 | final_dex_unique_ptr = std::unique_ptr<const unsigned char>(final_dex_data); |
| 210 | DCHECK_GT(final_len, 0); |
| 211 | } |
| 212 | |
| 213 | if (final_dex_data != initial_dex_file.Begin()) { |
| 214 | LOG(WARNING) << "Changing class " << descriptor; |
| 215 | art::ScopedObjectAccess soa(self); |
| 216 | art::StackHandleScope<2> hs(self); |
| 217 | // Save the results of all the non-retransformable agents. |
| 218 | // First allocate the ClassExt |
| 219 | art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->EnsureExtDataPresent(self))); |
| 220 | // Make sure we have a ClassExt. This is fine even though we are a temporary since it will |
| 221 | // get copied. |
| 222 | if (ext.IsNull()) { |
| 223 | // We will just return failure if we fail to allocate |
| 224 | LOG(WARNING) << "Could not allocate ext-data for class '" << descriptor << "'. " |
| 225 | << "Aborting transformation since we will be unable to store it."; |
| 226 | self->AssertPendingOOMException(); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | // Allocate the byte array to store the dex file bytes in. |
| 231 | art::Handle<art::mirror::ByteArray> arr(hs.NewHandle( |
| 232 | art::mirror::ByteArray::AllocateAndFill( |
| 233 | self, |
| 234 | reinterpret_cast<const signed char*>(post_no_redefine_dex_data), |
| 235 | post_no_redefine_len))); |
| 236 | if (arr.IsNull()) { |
| 237 | LOG(WARNING) << "Unable to allocate byte array for initial dex-file bytes. Aborting " |
| 238 | << "transformation"; |
| 239 | self->AssertPendingOOMException(); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | std::unique_ptr<const art::DexFile> dex_file(MakeSingleDexFile(self, |
| 244 | descriptor, |
| 245 | initial_dex_file.GetLocation(), |
| 246 | final_len, |
| 247 | final_dex_data)); |
| 248 | if (dex_file.get() == nullptr) { |
| 249 | return; |
| 250 | } |
| 251 | |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 252 | // TODO Check Redefined dex file for all invariants. |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 253 | LOG(WARNING) << "Dex file created by class-definition time transformation of " |
| 254 | << descriptor << " is not checked for all retransformation invariants."; |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 255 | |
| 256 | if (!ClassLoaderHelper::AddToClassLoader(self, class_loader, dex_file.get())) { |
| 257 | LOG(ERROR) << "Unable to add " << descriptor << " to class loader!"; |
| 258 | return; |
| 259 | } |
| 260 | |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 261 | // Actually set the ClassExt's original bytes once we have actually succeeded. |
| 262 | ext->SetOriginalDexFileBytes(arr.Get()); |
| 263 | // Set the return values |
| 264 | *final_class_def = &dex_file->GetClassDef(0); |
| 265 | *final_dex_file = dex_file.release(); |
| 266 | } |
| 267 | } |
| 268 | |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 269 | void ClassLoad(art::Handle<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 270 | if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassLoad)) { |
| 271 | art::Thread* thread = art::Thread::Current(); |
| 272 | ScopedLocalRef<jclass> jklass(thread->GetJniEnv(), |
| 273 | thread->GetJniEnv()->AddLocalReference<jclass>(klass.Get())); |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 274 | ScopedLocalRef<jthread> thread_jni( |
| 275 | thread->GetJniEnv(), thread->GetJniEnv()->AddLocalReference<jthread>(thread->GetPeer())); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 276 | { |
| 277 | art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative); |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 278 | event_handler->DispatchEvent<ArtJvmtiEvent::kClassLoad>( |
| 279 | thread, |
| 280 | static_cast<JNIEnv*>(thread->GetJniEnv()), |
| 281 | thread_jni.get(), |
| 282 | jklass.get()); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 283 | } |
Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 284 | if (klass->IsTemp()) { |
| 285 | AddTempClass(thread, jklass.get()); |
| 286 | } |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 290 | void ClassPrepare(art::Handle<art::mirror::Class> temp_klass, |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 291 | art::Handle<art::mirror::Class> klass) |
| 292 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 293 | if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassPrepare)) { |
| 294 | art::Thread* thread = art::Thread::Current(); |
Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 295 | if (temp_klass.Get() != klass.Get()) { |
| 296 | DCHECK(temp_klass->IsTemp()); |
| 297 | DCHECK(temp_klass->IsRetired()); |
| 298 | HandleTempClass(thread, temp_klass, klass); |
| 299 | } |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 300 | ScopedLocalRef<jclass> jklass(thread->GetJniEnv(), |
| 301 | thread->GetJniEnv()->AddLocalReference<jclass>(klass.Get())); |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 302 | ScopedLocalRef<jthread> thread_jni( |
| 303 | thread->GetJniEnv(), thread->GetJniEnv()->AddLocalReference<jthread>(thread->GetPeer())); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 304 | art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative); |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 305 | event_handler->DispatchEvent<ArtJvmtiEvent::kClassPrepare>( |
| 306 | thread, |
| 307 | static_cast<JNIEnv*>(thread->GetJniEnv()), |
| 308 | thread_jni.get(), |
| 309 | jklass.get()); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Andreas Gampe | 7619b5b | 2017-02-10 11:49:12 -0800 | [diff] [blame] | 313 | // To support parallel class-loading, we need to perform some locking dances here. Namely, |
| 314 | // the fixup stage must not be holding the temp_classes lock when it fixes up the system |
| 315 | // (as that requires suspending all mutators). |
| 316 | |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 317 | void AddTempClass(art::Thread* self, jclass klass) { |
| 318 | std::unique_lock<std::mutex> mu(temp_classes_lock); |
Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 319 | jclass global_klass = reinterpret_cast<jclass>(self->GetJniEnv()->NewGlobalRef(klass)); |
| 320 | temp_classes.push_back(global_klass); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 323 | void HandleTempClass(art::Thread* self, |
| 324 | art::Handle<art::mirror::Class> temp_klass, |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 325 | art::Handle<art::mirror::Class> klass) |
| 326 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 7619b5b | 2017-02-10 11:49:12 -0800 | [diff] [blame] | 327 | bool requires_fixup = false; |
| 328 | { |
| 329 | std::unique_lock<std::mutex> mu(temp_classes_lock); |
| 330 | if (temp_classes.empty()) { |
| 331 | return; |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 332 | } |
Andreas Gampe | 7619b5b | 2017-02-10 11:49:12 -0800 | [diff] [blame] | 333 | |
| 334 | for (auto it = temp_classes.begin(); it != temp_classes.end(); ++it) { |
| 335 | if (temp_klass.Get() == art::ObjPtr<art::mirror::Class>::DownCast(self->DecodeJObject(*it))) { |
| 336 | self->GetJniEnv()->DeleteGlobalRef(*it); |
| 337 | temp_classes.erase(it); |
| 338 | requires_fixup = true; |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | if (requires_fixup) { |
| 344 | FixupTempClass(self, temp_klass, klass); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 348 | void FixupTempClass(art::Thread* self, |
| 349 | art::Handle<art::mirror::Class> temp_klass, |
| 350 | art::Handle<art::mirror::Class> klass) |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 351 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 352 | // Suspend everything. |
| 353 | art::gc::Heap* heap = art::Runtime::Current()->GetHeap(); |
| 354 | if (heap->IsGcConcurrentAndMoving()) { |
| 355 | // Need to take a heap dump while GC isn't running. See the |
| 356 | // comment in Heap::VisitObjects(). |
| 357 | heap->IncrementDisableMovingGC(self); |
| 358 | } |
| 359 | { |
| 360 | art::ScopedThreadSuspension sts(self, art::kWaitingForVisitObjects); |
| 361 | art::ScopedSuspendAll ssa("FixupTempClass"); |
| 362 | |
| 363 | art::mirror::Class* input = temp_klass.Get(); |
| 364 | art::mirror::Class* output = klass.Get(); |
| 365 | |
| 366 | FixupGlobalReferenceTables(input, output); |
Andreas Gampe | 94dda93 | 2017-02-09 18:19:21 -0800 | [diff] [blame] | 367 | FixupLocalReferenceTables(self, input, output); |
Andreas Gampe | a67354b | 2017-02-10 16:18:30 -0800 | [diff] [blame] | 368 | FixupHeap(input, output); |
Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 369 | } |
| 370 | if (heap->IsGcConcurrentAndMoving()) { |
| 371 | heap->DecrementDisableMovingGC(self); |
| 372 | } |
| 373 | } |
| 374 | |
Andreas Gampe | 94dda93 | 2017-02-09 18:19:21 -0800 | [diff] [blame] | 375 | class RootUpdater : public art::RootVisitor { |
| 376 | public: |
| 377 | RootUpdater(const art::mirror::Class* input, art::mirror::Class* output) |
| 378 | : input_(input), output_(output) {} |
| 379 | |
| 380 | void VisitRoots(art::mirror::Object*** roots, |
| 381 | size_t count, |
| 382 | const art::RootInfo& info ATTRIBUTE_UNUSED) |
| 383 | OVERRIDE { |
| 384 | for (size_t i = 0; i != count; ++i) { |
| 385 | if (*roots[i] == input_) { |
| 386 | *roots[i] = output_; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots, |
| 392 | size_t count, |
| 393 | const art::RootInfo& info ATTRIBUTE_UNUSED) |
| 394 | OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 395 | for (size_t i = 0; i != count; ++i) { |
| 396 | if (roots[i]->AsMirrorPtr() == input_) { |
| 397 | roots[i]->Assign(output_); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | private: |
| 403 | const art::mirror::Class* input_; |
| 404 | art::mirror::Class* output_; |
| 405 | }; |
| 406 | |
Andreas Gampe | a67354b | 2017-02-10 16:18:30 -0800 | [diff] [blame] | 407 | void FixupGlobalReferenceTables(art::mirror::Class* input, art::mirror::Class* output) |
Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 408 | REQUIRES(art::Locks::mutator_lock_) { |
| 409 | art::JavaVMExt* java_vm = art::Runtime::Current()->GetJavaVM(); |
| 410 | |
| 411 | // Fix up the global table with a root visitor. |
Andreas Gampe | 94dda93 | 2017-02-09 18:19:21 -0800 | [diff] [blame] | 412 | RootUpdater global_update(input, output); |
Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 413 | java_vm->VisitRoots(&global_update); |
| 414 | |
| 415 | class WeakGlobalUpdate : public art::IsMarkedVisitor { |
| 416 | public: |
| 417 | WeakGlobalUpdate(art::mirror::Class* root_input, art::mirror::Class* root_output) |
| 418 | : input_(root_input), output_(root_output) {} |
| 419 | |
| 420 | art::mirror::Object* IsMarked(art::mirror::Object* obj) OVERRIDE { |
| 421 | if (obj == input_) { |
| 422 | return output_; |
| 423 | } |
| 424 | return obj; |
| 425 | } |
| 426 | |
| 427 | private: |
| 428 | const art::mirror::Class* input_; |
| 429 | art::mirror::Class* output_; |
| 430 | }; |
| 431 | WeakGlobalUpdate weak_global_update(input, output); |
| 432 | java_vm->SweepJniWeakGlobals(&weak_global_update); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Andreas Gampe | 94dda93 | 2017-02-09 18:19:21 -0800 | [diff] [blame] | 435 | void FixupLocalReferenceTables(art::Thread* self, |
| 436 | art::mirror::Class* input, |
| 437 | art::mirror::Class* output) |
| 438 | REQUIRES(art::Locks::mutator_lock_) { |
| 439 | class LocalUpdate { |
| 440 | public: |
| 441 | LocalUpdate(const art::mirror::Class* root_input, art::mirror::Class* root_output) |
| 442 | : input_(root_input), output_(root_output) {} |
| 443 | |
| 444 | static void Callback(art::Thread* t, void* arg) REQUIRES(art::Locks::mutator_lock_) { |
| 445 | LocalUpdate* local = reinterpret_cast<LocalUpdate*>(arg); |
| 446 | |
| 447 | // Fix up the local table with a root visitor. |
| 448 | RootUpdater local_update(local->input_, local->output_); |
| 449 | t->GetJniEnv()->locals.VisitRoots( |
| 450 | &local_update, art::RootInfo(art::kRootJNILocal, t->GetThreadId())); |
| 451 | } |
| 452 | |
| 453 | private: |
| 454 | const art::mirror::Class* input_; |
| 455 | art::mirror::Class* output_; |
| 456 | }; |
| 457 | LocalUpdate local_upd(input, output); |
| 458 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
| 459 | art::Runtime::Current()->GetThreadList()->ForEach(LocalUpdate::Callback, &local_upd); |
| 460 | } |
| 461 | |
Andreas Gampe | a67354b | 2017-02-10 16:18:30 -0800 | [diff] [blame] | 462 | void FixupHeap(art::mirror::Class* input, art::mirror::Class* output) |
| 463 | REQUIRES(art::Locks::mutator_lock_) { |
| 464 | class HeapFixupVisitor { |
| 465 | public: |
| 466 | HeapFixupVisitor(const art::mirror::Class* root_input, art::mirror::Class* root_output) |
| 467 | : input_(root_input), output_(root_output) {} |
| 468 | |
| 469 | void operator()(art::mirror::Object* src, |
| 470 | art::MemberOffset field_offset, |
| 471 | bool is_static ATTRIBUTE_UNUSED) const |
| 472 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 473 | art::mirror::HeapReference<art::mirror::Object>* trg = |
| 474 | src->GetFieldObjectReferenceAddr(field_offset); |
| 475 | if (trg->AsMirrorPtr() == input_) { |
| 476 | DCHECK_NE(field_offset.Uint32Value(), 0u); // This shouldn't be the class field of |
| 477 | // an object. |
| 478 | trg->Assign(output_); |
| 479 | } |
| 480 | } |
| 481 | |
Andreas Gampe | 52784ac | 2017-02-13 18:10:09 -0800 | [diff] [blame] | 482 | void operator()(art::ObjPtr<art::mirror::Class> klass ATTRIBUTE_UNUSED, |
| 483 | art::ObjPtr<art::mirror::Reference> reference) const |
| 484 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 485 | art::mirror::Object* val = reference->GetReferent(); |
| 486 | if (val == input_) { |
| 487 | reference->SetReferent<false>(output_); |
| 488 | } |
| 489 | } |
| 490 | |
Andreas Gampe | a67354b | 2017-02-10 16:18:30 -0800 | [diff] [blame] | 491 | void VisitRoot(art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED) |
Andreas Gampe | 52784ac | 2017-02-13 18:10:09 -0800 | [diff] [blame] | 492 | const { |
Andreas Gampe | a67354b | 2017-02-10 16:18:30 -0800 | [diff] [blame] | 493 | LOG(FATAL) << "Unreachable"; |
| 494 | } |
| 495 | |
| 496 | void VisitRootIfNonNull( |
| 497 | art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED) const { |
| 498 | LOG(FATAL) << "Unreachable"; |
| 499 | } |
| 500 | |
| 501 | static void AllObjectsCallback(art::mirror::Object* obj, void* arg) |
| 502 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 503 | HeapFixupVisitor* hfv = reinterpret_cast<HeapFixupVisitor*>(arg); |
| 504 | |
| 505 | // Visit references, not native roots. |
Andreas Gampe | 52784ac | 2017-02-13 18:10:09 -0800 | [diff] [blame] | 506 | obj->VisitReferences<false>(*hfv, *hfv); |
Andreas Gampe | a67354b | 2017-02-10 16:18:30 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | private: |
| 510 | const art::mirror::Class* input_; |
| 511 | art::mirror::Class* output_; |
| 512 | }; |
| 513 | HeapFixupVisitor hfv(input, output); |
| 514 | art::Runtime::Current()->GetHeap()->VisitObjectsPaused(HeapFixupVisitor::AllObjectsCallback, |
| 515 | &hfv); |
| 516 | } |
| 517 | |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 518 | // A set of all the temp classes we have handed out. We have to fix up references to these. |
| 519 | // For simplicity, we store the temp classes as JNI global references in a vector. Normally a |
| 520 | // Prepare event will closely follow, so the vector should be small. |
| 521 | std::mutex temp_classes_lock; |
| 522 | std::vector<jclass> temp_classes; |
| 523 | |
| 524 | EventHandler* event_handler = nullptr; |
| 525 | }; |
| 526 | |
| 527 | ClassCallback gClassCallback; |
| 528 | |
| 529 | void ClassUtil::Register(EventHandler* handler) { |
| 530 | gClassCallback.event_handler = handler; |
| 531 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 532 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 533 | art::ScopedSuspendAll ssa("Add load callback"); |
| 534 | art::Runtime::Current()->GetRuntimeCallbacks()->AddClassLoadCallback(&gClassCallback); |
| 535 | } |
| 536 | |
| 537 | void ClassUtil::Unregister() { |
| 538 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 539 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 540 | art::ScopedSuspendAll ssa("Remove thread callback"); |
| 541 | art::Runtime* runtime = art::Runtime::Current(); |
| 542 | runtime->GetRuntimeCallbacks()->RemoveClassLoadCallback(&gClassCallback); |
| 543 | } |
| 544 | |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 545 | jvmtiError ClassUtil::GetClassFields(jvmtiEnv* env, |
| 546 | jclass jklass, |
| 547 | jint* field_count_ptr, |
| 548 | jfieldID** fields_ptr) { |
| 549 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 550 | art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass); |
| 551 | if (klass == nullptr) { |
| 552 | return ERR(INVALID_CLASS); |
| 553 | } |
| 554 | |
| 555 | if (field_count_ptr == nullptr || fields_ptr == nullptr) { |
| 556 | return ERR(NULL_POINTER); |
| 557 | } |
| 558 | |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 559 | art::IterationRange<art::StrideIterator<art::ArtField>> ifields = klass->GetIFields(); |
| 560 | art::IterationRange<art::StrideIterator<art::ArtField>> sfields = klass->GetSFields(); |
| 561 | size_t array_size = klass->NumInstanceFields() + klass->NumStaticFields(); |
| 562 | |
| 563 | unsigned char* out_ptr; |
| 564 | jvmtiError allocError = env->Allocate(array_size * sizeof(jfieldID), &out_ptr); |
| 565 | if (allocError != ERR(NONE)) { |
| 566 | return allocError; |
| 567 | } |
| 568 | jfieldID* field_array = reinterpret_cast<jfieldID*>(out_ptr); |
| 569 | |
| 570 | size_t array_idx = 0; |
| 571 | for (art::ArtField& field : sfields) { |
| 572 | field_array[array_idx] = art::jni::EncodeArtField(&field); |
| 573 | ++array_idx; |
| 574 | } |
| 575 | for (art::ArtField& field : ifields) { |
| 576 | field_array[array_idx] = art::jni::EncodeArtField(&field); |
| 577 | ++array_idx; |
| 578 | } |
| 579 | |
| 580 | *field_count_ptr = static_cast<jint>(array_size); |
| 581 | *fields_ptr = field_array; |
| 582 | |
| 583 | return ERR(NONE); |
| 584 | } |
| 585 | |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 586 | jvmtiError ClassUtil::GetClassMethods(jvmtiEnv* env, |
| 587 | jclass jklass, |
| 588 | jint* method_count_ptr, |
| 589 | jmethodID** methods_ptr) { |
| 590 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 591 | art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass); |
| 592 | if (klass == nullptr) { |
| 593 | return ERR(INVALID_CLASS); |
| 594 | } |
| 595 | |
| 596 | if (method_count_ptr == nullptr || methods_ptr == nullptr) { |
| 597 | return ERR(NULL_POINTER); |
| 598 | } |
| 599 | |
| 600 | size_t array_size = klass->NumDeclaredVirtualMethods() + klass->NumDirectMethods(); |
| 601 | unsigned char* out_ptr; |
| 602 | jvmtiError allocError = env->Allocate(array_size * sizeof(jmethodID), &out_ptr); |
| 603 | if (allocError != ERR(NONE)) { |
| 604 | return allocError; |
| 605 | } |
| 606 | jmethodID* method_array = reinterpret_cast<jmethodID*>(out_ptr); |
| 607 | |
| 608 | if (art::kIsDebugBuild) { |
| 609 | size_t count = 0; |
| 610 | for (auto& m ATTRIBUTE_UNUSED : klass->GetDeclaredMethods(art::kRuntimePointerSize)) { |
| 611 | count++; |
| 612 | } |
| 613 | CHECK_EQ(count, klass->NumDirectMethods() + klass->NumDeclaredVirtualMethods()); |
| 614 | } |
| 615 | |
| 616 | size_t array_idx = 0; |
| 617 | for (auto& m : klass->GetDeclaredMethods(art::kRuntimePointerSize)) { |
| 618 | method_array[array_idx] = art::jni::EncodeArtMethod(&m); |
| 619 | ++array_idx; |
| 620 | } |
| 621 | |
| 622 | *method_count_ptr = static_cast<jint>(array_size); |
| 623 | *methods_ptr = method_array; |
| 624 | |
| 625 | return ERR(NONE); |
| 626 | } |
| 627 | |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 628 | jvmtiError ClassUtil::GetImplementedInterfaces(jvmtiEnv* env, |
| 629 | jclass jklass, |
| 630 | jint* interface_count_ptr, |
| 631 | jclass** interfaces_ptr) { |
| 632 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 633 | art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass); |
| 634 | if (klass == nullptr) { |
| 635 | return ERR(INVALID_CLASS); |
| 636 | } |
| 637 | |
| 638 | if (interface_count_ptr == nullptr || interfaces_ptr == nullptr) { |
| 639 | return ERR(NULL_POINTER); |
| 640 | } |
| 641 | |
| 642 | // Need to handle array specifically. Arrays implement Serializable and Cloneable, but the |
| 643 | // spec says these should not be reported. |
| 644 | if (klass->IsArrayClass()) { |
| 645 | *interface_count_ptr = 0; |
| 646 | *interfaces_ptr = nullptr; // TODO: Should we allocate a dummy here? |
| 647 | return ERR(NONE); |
| 648 | } |
| 649 | |
| 650 | size_t array_size = klass->NumDirectInterfaces(); |
| 651 | unsigned char* out_ptr; |
| 652 | jvmtiError allocError = env->Allocate(array_size * sizeof(jclass), &out_ptr); |
| 653 | if (allocError != ERR(NONE)) { |
| 654 | return allocError; |
| 655 | } |
| 656 | jclass* interface_array = reinterpret_cast<jclass*>(out_ptr); |
| 657 | |
| 658 | art::StackHandleScope<1> hs(soa.Self()); |
| 659 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass)); |
| 660 | |
| 661 | for (uint32_t idx = 0; idx != array_size; ++idx) { |
| 662 | art::ObjPtr<art::mirror::Class> inf_klass = |
| 663 | art::mirror::Class::ResolveDirectInterface(soa.Self(), h_klass, idx); |
| 664 | if (inf_klass == nullptr) { |
| 665 | soa.Self()->ClearException(); |
| 666 | env->Deallocate(out_ptr); |
| 667 | // TODO: What is the right error code here? |
| 668 | return ERR(INTERNAL); |
| 669 | } |
| 670 | interface_array[idx] = soa.AddLocalReference<jclass>(inf_klass); |
| 671 | } |
| 672 | |
| 673 | *interface_count_ptr = static_cast<jint>(array_size); |
| 674 | *interfaces_ptr = interface_array; |
| 675 | |
| 676 | return ERR(NONE); |
| 677 | } |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 678 | |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 679 | jvmtiError ClassUtil::GetClassSignature(jvmtiEnv* env, |
| 680 | jclass jklass, |
| 681 | char** signature_ptr, |
| 682 | char** generic_ptr) { |
| 683 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 684 | art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass); |
| 685 | if (klass == nullptr) { |
| 686 | return ERR(INVALID_CLASS); |
| 687 | } |
| 688 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 689 | JvmtiUniquePtr<char[]> sig_copy; |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 690 | if (signature_ptr != nullptr) { |
| 691 | std::string storage; |
| 692 | const char* descriptor = klass->GetDescriptor(&storage); |
| 693 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 694 | jvmtiError ret; |
| 695 | sig_copy = CopyString(env, descriptor, &ret); |
| 696 | if (sig_copy == nullptr) { |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 697 | return ret; |
| 698 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 699 | *signature_ptr = sig_copy.get(); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 702 | if (generic_ptr != nullptr) { |
| 703 | *generic_ptr = nullptr; |
Andreas Gampe | 0eb3643 | 2017-02-15 18:36:14 -0800 | [diff] [blame] | 704 | if (!klass->IsProxyClass() && klass->GetDexCache() != nullptr) { |
| 705 | art::StackHandleScope<1> hs(soa.Self()); |
| 706 | art::Handle<art::mirror::Class> h_klass = hs.NewHandle(klass); |
| 707 | art::mirror::ObjectArray<art::mirror::String>* str_array = |
| 708 | art::annotations::GetSignatureAnnotationForClass(h_klass); |
| 709 | if (str_array != nullptr) { |
| 710 | std::ostringstream oss; |
| 711 | for (int32_t i = 0; i != str_array->GetLength(); ++i) { |
| 712 | oss << str_array->Get(i)->ToModifiedUtf8(); |
| 713 | } |
| 714 | std::string output_string = oss.str(); |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 715 | jvmtiError ret; |
| 716 | JvmtiUniquePtr<char[]> copy = CopyString(env, output_string.c_str(), &ret); |
| 717 | if (copy == nullptr) { |
Andreas Gampe | 0eb3643 | 2017-02-15 18:36:14 -0800 | [diff] [blame] | 718 | return ret; |
| 719 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 720 | *generic_ptr = copy.release(); |
Andreas Gampe | 0eb3643 | 2017-02-15 18:36:14 -0800 | [diff] [blame] | 721 | } else if (soa.Self()->IsExceptionPending()) { |
| 722 | // TODO: Should we report an error here? |
| 723 | soa.Self()->ClearException(); |
| 724 | } |
| 725 | } |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 726 | } |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 727 | |
| 728 | // Everything is fine, release the buffers. |
| 729 | sig_copy.release(); |
| 730 | |
| 731 | return ERR(NONE); |
| 732 | } |
| 733 | |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 734 | jvmtiError ClassUtil::GetClassStatus(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 735 | jclass jklass, |
| 736 | jint* status_ptr) { |
| 737 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 738 | art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass); |
| 739 | if (klass == nullptr) { |
| 740 | return ERR(INVALID_CLASS); |
| 741 | } |
| 742 | |
| 743 | if (status_ptr == nullptr) { |
| 744 | return ERR(NULL_POINTER); |
| 745 | } |
| 746 | |
| 747 | if (klass->IsArrayClass()) { |
| 748 | *status_ptr = JVMTI_CLASS_STATUS_ARRAY; |
| 749 | } else if (klass->IsPrimitive()) { |
| 750 | *status_ptr = JVMTI_CLASS_STATUS_PRIMITIVE; |
| 751 | } else { |
| 752 | *status_ptr = JVMTI_CLASS_STATUS_VERIFIED; // All loaded classes are structurally verified. |
| 753 | // This is finicky. If there's an error, we'll say it wasn't prepared. |
| 754 | if (klass->IsResolved()) { |
| 755 | *status_ptr |= JVMTI_CLASS_STATUS_PREPARED; |
| 756 | } |
| 757 | if (klass->IsInitialized()) { |
| 758 | *status_ptr |= JVMTI_CLASS_STATUS_INITIALIZED; |
| 759 | } |
| 760 | // Technically the class may be erroneous for other reasons, but we do not have enough info. |
| 761 | if (klass->IsErroneous()) { |
| 762 | *status_ptr |= JVMTI_CLASS_STATUS_ERROR; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | return ERR(NONE); |
| 767 | } |
| 768 | |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 769 | template <typename T> |
| 770 | static jvmtiError ClassIsT(jclass jklass, T test, jboolean* is_t_ptr) { |
| 771 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 772 | art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass); |
| 773 | if (klass == nullptr) { |
| 774 | return ERR(INVALID_CLASS); |
| 775 | } |
| 776 | |
| 777 | if (is_t_ptr == nullptr) { |
| 778 | return ERR(NULL_POINTER); |
| 779 | } |
| 780 | |
| 781 | *is_t_ptr = test(klass) ? JNI_TRUE : JNI_FALSE; |
| 782 | return ERR(NONE); |
| 783 | } |
| 784 | |
| 785 | jvmtiError ClassUtil::IsInterface(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 786 | jclass jklass, |
| 787 | jboolean* is_interface_ptr) { |
| 788 | auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 789 | return klass->IsInterface(); |
| 790 | }; |
| 791 | return ClassIsT(jklass, test, is_interface_ptr); |
| 792 | } |
| 793 | |
| 794 | jvmtiError ClassUtil::IsArrayClass(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 795 | jclass jklass, |
| 796 | jboolean* is_array_class_ptr) { |
| 797 | auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 798 | return klass->IsArrayClass(); |
| 799 | }; |
| 800 | return ClassIsT(jklass, test, is_array_class_ptr); |
| 801 | } |
| 802 | |
Andreas Gampe | 64013e5 | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 803 | // Keep this in sync with Class.getModifiers(). |
| 804 | static uint32_t ClassGetModifiers(art::Thread* self, art::ObjPtr<art::mirror::Class> klass) |
| 805 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 806 | if (klass->IsArrayClass()) { |
| 807 | uint32_t component_modifiers = ClassGetModifiers(self, klass->GetComponentType()); |
| 808 | if ((component_modifiers & art::kAccInterface) != 0) { |
| 809 | component_modifiers &= ~(art::kAccInterface | art::kAccStatic); |
| 810 | } |
| 811 | return art::kAccAbstract | art::kAccFinal | component_modifiers; |
| 812 | } |
| 813 | |
| 814 | uint32_t modifiers = klass->GetAccessFlags() & art::kAccJavaFlagsMask; |
| 815 | |
| 816 | art::StackHandleScope<1> hs(self); |
| 817 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass)); |
| 818 | return art::mirror::Class::GetInnerClassFlags(h_klass, modifiers); |
| 819 | } |
| 820 | |
| 821 | jvmtiError ClassUtil::GetClassModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 822 | jclass jklass, |
| 823 | jint* modifiers_ptr) { |
| 824 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 825 | art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass); |
| 826 | if (klass == nullptr) { |
| 827 | return ERR(INVALID_CLASS); |
| 828 | } |
| 829 | |
| 830 | if (modifiers_ptr == nullptr) { |
| 831 | return ERR(NULL_POINTER); |
| 832 | } |
| 833 | |
| 834 | *modifiers_ptr = ClassGetModifiers(soa.Self(), klass); |
| 835 | |
| 836 | return ERR(NONE); |
| 837 | } |
| 838 | |
Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame] | 839 | jvmtiError ClassUtil::GetClassLoader(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 840 | jclass jklass, |
| 841 | jobject* classloader_ptr) { |
| 842 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 843 | art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass); |
| 844 | if (klass == nullptr) { |
| 845 | return ERR(INVALID_CLASS); |
| 846 | } |
| 847 | |
| 848 | if (classloader_ptr == nullptr) { |
| 849 | return ERR(NULL_POINTER); |
| 850 | } |
| 851 | |
| 852 | *classloader_ptr = soa.AddLocalReference<jobject>(klass->GetClassLoader()); |
| 853 | |
| 854 | return ERR(NONE); |
| 855 | } |
| 856 | |
Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 857 | jvmtiError ClassUtil::GetClassLoaderClasses(jvmtiEnv* env, |
| 858 | jobject initiating_loader, |
| 859 | jint* class_count_ptr, |
| 860 | jclass** classes_ptr) { |
| 861 | UNUSED(env, initiating_loader, class_count_ptr, classes_ptr); |
| 862 | |
| 863 | if (class_count_ptr == nullptr || classes_ptr == nullptr) { |
| 864 | return ERR(NULL_POINTER); |
| 865 | } |
| 866 | art::Thread* self = art::Thread::Current(); |
| 867 | if (!self->GetJniEnv()->IsInstanceOf(initiating_loader, |
| 868 | art::WellKnownClasses::java_lang_ClassLoader)) { |
| 869 | return ERR(ILLEGAL_ARGUMENT); |
| 870 | } |
| 871 | if (self->GetJniEnv()->IsInstanceOf(initiating_loader, |
| 872 | art::WellKnownClasses::java_lang_BootClassLoader)) { |
| 873 | // Need to use null for the BootClassLoader. |
| 874 | initiating_loader = nullptr; |
| 875 | } |
| 876 | |
| 877 | art::ScopedObjectAccess soa(self); |
| 878 | art::ObjPtr<art::mirror::ClassLoader> class_loader = |
| 879 | soa.Decode<art::mirror::ClassLoader>(initiating_loader); |
| 880 | |
| 881 | art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker(); |
| 882 | |
| 883 | art::ReaderMutexLock mu(self, *art::Locks::classlinker_classes_lock_); |
| 884 | |
| 885 | art::ClassTable* class_table = class_linker->ClassTableForClassLoader(class_loader); |
| 886 | if (class_table == nullptr) { |
| 887 | // Nothing loaded. |
| 888 | *class_count_ptr = 0; |
| 889 | *classes_ptr = nullptr; |
| 890 | return ERR(NONE); |
| 891 | } |
| 892 | |
| 893 | struct ClassTableCount { |
| 894 | bool operator()(art::ObjPtr<art::mirror::Class> klass) { |
| 895 | DCHECK(klass != nullptr); |
| 896 | ++count; |
| 897 | return true; |
| 898 | } |
| 899 | |
| 900 | size_t count = 0; |
| 901 | }; |
| 902 | ClassTableCount ctc; |
| 903 | class_table->Visit(ctc); |
| 904 | |
| 905 | if (ctc.count == 0) { |
| 906 | // Nothing loaded. |
| 907 | *class_count_ptr = 0; |
| 908 | *classes_ptr = nullptr; |
| 909 | return ERR(NONE); |
| 910 | } |
| 911 | |
| 912 | unsigned char* data; |
| 913 | jvmtiError data_result = env->Allocate(ctc.count * sizeof(jclass), &data); |
| 914 | if (data_result != ERR(NONE)) { |
| 915 | return data_result; |
| 916 | } |
| 917 | jclass* class_array = reinterpret_cast<jclass*>(data); |
| 918 | |
| 919 | struct ClassTableFill { |
| 920 | bool operator()(art::ObjPtr<art::mirror::Class> klass) |
| 921 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 922 | DCHECK(klass != nullptr); |
| 923 | DCHECK_LT(count, ctc_ref.count); |
| 924 | local_class_array[count++] = soa_ptr->AddLocalReference<jclass>(klass); |
| 925 | return true; |
| 926 | } |
| 927 | |
| 928 | jclass* local_class_array; |
| 929 | const ClassTableCount& ctc_ref; |
| 930 | art::ScopedObjectAccess* soa_ptr; |
| 931 | size_t count; |
| 932 | }; |
| 933 | ClassTableFill ctf = { class_array, ctc, &soa, 0 }; |
| 934 | class_table->Visit(ctf); |
| 935 | DCHECK_EQ(ctc.count, ctf.count); |
| 936 | |
| 937 | *class_count_ptr = ctc.count; |
| 938 | *classes_ptr = class_array; |
| 939 | |
| 940 | return ERR(NONE); |
| 941 | } |
| 942 | |
Andreas Gampe | 812a244 | 2017-01-19 22:04:46 -0800 | [diff] [blame] | 943 | jvmtiError ClassUtil::GetClassVersionNumbers(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 944 | jclass jklass, |
| 945 | jint* minor_version_ptr, |
| 946 | jint* major_version_ptr) { |
| 947 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 948 | if (jklass == nullptr) { |
| 949 | return ERR(INVALID_CLASS); |
| 950 | } |
| 951 | art::ObjPtr<art::mirror::Object> jklass_obj = soa.Decode<art::mirror::Object>(jklass); |
| 952 | if (!jklass_obj->IsClass()) { |
| 953 | return ERR(INVALID_CLASS); |
| 954 | } |
| 955 | art::ObjPtr<art::mirror::Class> klass = jklass_obj->AsClass(); |
| 956 | if (klass->IsPrimitive() || klass->IsArrayClass()) { |
| 957 | return ERR(INVALID_CLASS); |
| 958 | } |
| 959 | |
| 960 | if (minor_version_ptr == nullptr || major_version_ptr == nullptr) { |
| 961 | return ERR(NULL_POINTER); |
| 962 | } |
| 963 | |
| 964 | // Note: proxies will show the dex file version of java.lang.reflect.Proxy, as that is |
| 965 | // what their dex cache copies from. |
| 966 | uint32_t version = klass->GetDexFile().GetHeader().GetVersion(); |
| 967 | |
| 968 | *major_version_ptr = static_cast<jint>(version); |
| 969 | *minor_version_ptr = 0; |
| 970 | |
| 971 | return ERR(NONE); |
| 972 | } |
| 973 | |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 974 | } // namespace openjdkjvmti |