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