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