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