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