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