blob: 261fe3e8105d1bdb749ee7bdbd080e010b86802a [file] [log] [blame]
Andreas Gampee492ae32016-10-28 19:34:57 -07001/* 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 Light440b5d92017-01-24 15:32:25 -080034#include "android-base/stringprintf.h"
35
Igor Murashkin5573c372017-11-16 13:34:30 -080036#include <mutex>
Andreas Gampee6377462017-01-20 17:37:50 -080037#include <unordered_set>
38
Andreas Gampee492ae32016-10-28 19:34:57 -070039#include "art_jvmti.h"
Vladimir Markoe1993c72017-06-14 17:01:38 +010040#include "base/array_ref.h"
Andreas Gampee6377462017-01-20 17:37:50 -080041#include "base/macros.h"
David Sehr8f4b0562018-03-02 12:01:51 -080042#include "base/utils.h"
Andreas Gampe70f16392017-01-16 14:20:10 -080043#include "class_linker.h"
Andreas Gampee8320ea2018-02-20 18:24:55 -080044#include "class_loader_utils.h"
Steven Morelande431e272017-07-18 16:53:49 -070045#include "class_table-inl.h"
Alex Light440b5d92017-01-24 15:32:25 -080046#include "common_throws.h"
David Sehr013fd802018-01-11 22:55:24 -080047#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080048#include "dex/dex_file_annotations.h"
49#include "dex/dex_file_loader.h"
David Sehr67bf42e2018-02-26 16:43:04 -080050#include "dex/primitive.h"
Andreas Gampee6377462017-01-20 17:37:50 -080051#include "events-inl.h"
Alex Light40528472017-03-28 09:07:36 -070052#include "fixed_up_dex_file.h"
Andreas Gampe1c158a02017-07-13 17:26:19 -070053#include "gc/heap-visit-objects-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070054#include "gc/heap.h"
Andreas Gampe691051b2017-02-09 09:15:24 -080055#include "gc_root.h"
Andreas Gampee6377462017-01-20 17:37:50 -080056#include "handle.h"
57#include "jni_env_ext-inl.h"
Andreas Gampeac587272017-01-05 15:21:34 -080058#include "jni_internal.h"
Alex Light440b5d92017-01-24 15:32:25 -080059#include "mirror/array-inl.h"
60#include "mirror/class-inl.h"
61#include "mirror/class_ext.h"
Andreas Gampea67354b2017-02-10 16:18:30 -080062#include "mirror/object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080063#include "mirror/object-refvisitor-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070064#include "mirror/object_array-inl.h"
65#include "mirror/object_reference.h"
Andreas Gampe52784ac2017-02-13 18:10:09 -080066#include "mirror/reference.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070067#include "nativehelper/scoped_local_ref.h"
Alex Light6a656312017-03-29 17:18:00 -070068#include "reflection.h"
Andreas Gampe70f16392017-01-16 14:20:10 -080069#include "runtime.h"
Andreas Gampee6377462017-01-20 17:37:50 -080070#include "runtime_callbacks.h"
Andreas Gampee492ae32016-10-28 19:34:57 -070071#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070072#include "thread-current-inl.h"
Andreas Gampee6377462017-01-20 17:37:50 -080073#include "thread_list.h"
Alex Lightc7588752018-02-20 11:15:54 -080074#include "ti_class_loader-inl.h"
Alex Lightd8ce4e72017-02-27 10:52:29 -080075#include "ti_phase.h"
Alex Light440b5d92017-01-24 15:32:25 -080076#include "ti_redefine.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070077#include "well_known_classes.h"
Andreas Gampee492ae32016-10-28 19:34:57 -070078
79namespace openjdkjvmti {
80
Alex Light440b5d92017-01-24 15:32:25 -080081using android::base::StringPrintf;
82
83static std::unique_ptr<const art::DexFile> MakeSingleDexFile(art::Thread* self,
84 const char* descriptor,
85 const std::string& orig_location,
86 jint final_len,
87 const unsigned char* final_dex_data)
88 REQUIRES_SHARED(art::Locks::mutator_lock_) {
89 // Make the mmap
90 std::string error_msg;
Vladimir Markoe1993c72017-06-14 17:01:38 +010091 art::ArrayRef<const unsigned char> final_data(final_dex_data, final_len);
Alex Light440b5d92017-01-24 15:32:25 -080092 std::unique_ptr<art::MemMap> map(Redefiner::MoveDataToMemMap(orig_location,
Alex Lightb7354d52017-03-30 15:17:01 -070093 final_data,
Alex Light440b5d92017-01-24 15:32:25 -080094 &error_msg));
95 if (map.get() == nullptr) {
96 LOG(WARNING) << "Unable to allocate mmap for redefined dex file! Error was: " << error_msg;
97 self->ThrowOutOfMemoryError(StringPrintf(
98 "Unable to allocate dex file for transformation of %s", descriptor).c_str());
99 return nullptr;
100 }
101
102 // Make a dex-file
103 if (map->Size() < sizeof(art::DexFile::Header)) {
104 LOG(WARNING) << "Could not read dex file header because dex_data was too short";
105 art::ThrowClassFormatError(nullptr,
106 "Unable to read transformed dex file of %s",
107 descriptor);
108 return nullptr;
109 }
110 uint32_t checksum = reinterpret_cast<const art::DexFile::Header*>(map->Begin())->checksum_;
Andreas Gampef45d61c2017-06-07 10:29:33 -0700111 std::string map_name = map->GetName();
David Sehr013fd802018-01-11 22:55:24 -0800112 const art::ArtDexFileLoader dex_file_loader;
113 std::unique_ptr<const art::DexFile> dex_file(dex_file_loader.Open(map_name,
114 checksum,
115 std::move(map),
116 /*verify*/true,
117 /*verify_checksum*/true,
118 &error_msg));
Alex Light440b5d92017-01-24 15:32:25 -0800119 if (dex_file.get() == nullptr) {
120 LOG(WARNING) << "Unable to load modified dex file for " << descriptor << ": " << error_msg;
121 art::ThrowClassFormatError(nullptr,
122 "Unable to read transformed dex file of %s because %s",
123 descriptor,
124 error_msg.c_str());
125 return nullptr;
126 }
127 if (dex_file->NumClassDefs() != 1) {
128 LOG(WARNING) << "Dex file contains more than 1 class_def. Ignoring.";
129 // TODO Throw some other sort of error here maybe?
130 art::ThrowClassFormatError(
131 nullptr,
132 "Unable to use transformed dex file of %s because it contained too many classes",
133 descriptor);
134 return nullptr;
135 }
136 return dex_file;
137}
138
Alex Light28b6e7e2017-05-22 16:05:59 -0700139// A deleter that acts like the jvmtiEnv->Deallocate so that asan does not get tripped up.
140// TODO We should everything use the actual jvmtiEnv->Allocate/Deallocate functions once we can
141// figure out which env to use.
142template <typename T>
143class FakeJvmtiDeleter {
144 public:
145 FakeJvmtiDeleter() {}
146
147 FakeJvmtiDeleter(FakeJvmtiDeleter&) = default;
148 FakeJvmtiDeleter(FakeJvmtiDeleter&&) = default;
149 FakeJvmtiDeleter& operator=(const FakeJvmtiDeleter&) = default;
150
151 template <typename U> void operator()(const U* ptr) const {
152 if (ptr != nullptr) {
153 free(const_cast<U*>(ptr));
154 }
155 }
156};
157
Andreas Gampee6377462017-01-20 17:37:50 -0800158struct ClassCallback : public art::ClassLoadCallback {
Alex Light440b5d92017-01-24 15:32:25 -0800159 void ClassPreDefine(const char* descriptor,
160 art::Handle<art::mirror::Class> klass,
161 art::Handle<art::mirror::ClassLoader> class_loader,
162 const art::DexFile& initial_dex_file,
163 const art::DexFile::ClassDef& initial_class_def ATTRIBUTE_UNUSED,
164 /*out*/art::DexFile const** final_dex_file,
165 /*out*/art::DexFile::ClassDef const** final_class_def)
166 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
167 bool is_enabled =
168 event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassFileLoadHookRetransformable) ||
169 event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassFileLoadHookNonRetransformable);
170 if (!is_enabled) {
171 return;
172 }
173 if (descriptor[0] != 'L') {
174 // It is a primitive or array. Just return
175 return;
176 }
Alex Lightd8ce4e72017-02-27 10:52:29 -0800177 jvmtiPhase phase = PhaseUtil::GetPhaseUnchecked();
178 if (UNLIKELY(phase != JVMTI_PHASE_START && phase != JVMTI_PHASE_LIVE)) {
179 // We want to wait until we are at least in the START phase so that all WellKnownClasses and
180 // mirror classes have been initialized and loaded. The runtime relies on these classes having
181 // specific fields and methods present. Since PreDefine hooks don't need to abide by this
182 // restriction we will simply not send the event for these classes.
183 LOG(WARNING) << "Ignoring load of class <" << descriptor << "> as it is being loaded during "
184 << "runtime initialization.";
185 return;
186 }
187
Alex Light440b5d92017-01-24 15:32:25 -0800188 art::Thread* self = art::Thread::Current();
Alex Light64e4c142018-01-30 13:46:37 -0800189 ArtClassDefinition def;
190 def.InitFirstLoad(descriptor, class_loader, initial_dex_file);
Alex Light40528472017-03-28 09:07:36 -0700191
Alex Light64e4c142018-01-30 13:46:37 -0800192 // Call all non-retransformable agents.
193 Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
194 event_handler, self, &def);
195
196 std::vector<unsigned char> post_non_retransform;
197 if (def.IsModified()) {
198 // Copy the dex data after the non-retransformable events.
199 post_non_retransform.resize(def.GetDexData().size());
200 memcpy(post_non_retransform.data(), def.GetDexData().data(), post_non_retransform.size());
Alex Light440b5d92017-01-24 15:32:25 -0800201 }
Alex Light64e4c142018-01-30 13:46:37 -0800202
Alex Light440b5d92017-01-24 15:32:25 -0800203 // Call all retransformable agents.
Alex Light64e4c142018-01-30 13:46:37 -0800204 Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
205 event_handler, self, &def);
Alex Light440b5d92017-01-24 15:32:25 -0800206
Alex Light64e4c142018-01-30 13:46:37 -0800207 if (def.IsModified()) {
Alex Light440b5d92017-01-24 15:32:25 -0800208 LOG(WARNING) << "Changing class " << descriptor;
Alex Light440b5d92017-01-24 15:32:25 -0800209 art::StackHandleScope<2> hs(self);
210 // Save the results of all the non-retransformable agents.
211 // First allocate the ClassExt
212 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->EnsureExtDataPresent(self)));
213 // Make sure we have a ClassExt. This is fine even though we are a temporary since it will
214 // get copied.
215 if (ext.IsNull()) {
216 // We will just return failure if we fail to allocate
217 LOG(WARNING) << "Could not allocate ext-data for class '" << descriptor << "'. "
218 << "Aborting transformation since we will be unable to store it.";
219 self->AssertPendingOOMException();
220 return;
221 }
222
223 // Allocate the byte array to store the dex file bytes in.
Alex Light6a656312017-03-29 17:18:00 -0700224 art::MutableHandle<art::mirror::Object> arr(hs.NewHandle<art::mirror::Object>(nullptr));
Alex Light64e4c142018-01-30 13:46:37 -0800225 if (post_non_retransform.empty() && strcmp(descriptor, "Ljava/lang/Long;") != 0) {
Alex Light6a656312017-03-29 17:18:00 -0700226 // we didn't have any non-retransformable agents. We can just cache a pointer to the
227 // initial_dex_file. It will be kept live by the class_loader.
228 jlong dex_ptr = reinterpret_cast<uintptr_t>(&initial_dex_file);
229 art::JValue val;
230 val.SetJ(dex_ptr);
231 arr.Assign(art::BoxPrimitive(art::Primitive::kPrimLong, val));
232 } else {
233 arr.Assign(art::mirror::ByteArray::AllocateAndFill(
234 self,
Alex Light64e4c142018-01-30 13:46:37 -0800235 reinterpret_cast<const signed char*>(post_non_retransform.data()),
236 post_non_retransform.size()));
Alex Light6a656312017-03-29 17:18:00 -0700237 }
Alex Light440b5d92017-01-24 15:32:25 -0800238 if (arr.IsNull()) {
Alex Light6a656312017-03-29 17:18:00 -0700239 LOG(WARNING) << "Unable to allocate memory for initial dex-file. Aborting transformation";
Alex Light440b5d92017-01-24 15:32:25 -0800240 self->AssertPendingOOMException();
241 return;
242 }
243
244 std::unique_ptr<const art::DexFile> dex_file(MakeSingleDexFile(self,
245 descriptor,
246 initial_dex_file.GetLocation(),
Alex Light64e4c142018-01-30 13:46:37 -0800247 def.GetDexData().size(),
248 def.GetDexData().data()));
Alex Light440b5d92017-01-24 15:32:25 -0800249 if (dex_file.get() == nullptr) {
250 return;
251 }
252
Alex Lighteb98b082017-01-25 13:02:32 -0800253 // TODO Check Redefined dex file for all invariants.
Alex Light440b5d92017-01-24 15:32:25 -0800254 LOG(WARNING) << "Dex file created by class-definition time transformation of "
255 << descriptor << " is not checked for all retransformation invariants.";
Alex Lighteb98b082017-01-25 13:02:32 -0800256
257 if (!ClassLoaderHelper::AddToClassLoader(self, class_loader, dex_file.get())) {
258 LOG(ERROR) << "Unable to add " << descriptor << " to class loader!";
259 return;
260 }
261
Alex Light440b5d92017-01-24 15:32:25 -0800262 // Actually set the ClassExt's original bytes once we have actually succeeded.
Alex Light2f814aa2017-03-24 15:21:34 +0000263 ext->SetOriginalDexFile(arr.Get());
Alex Light440b5d92017-01-24 15:32:25 -0800264 // Set the return values
265 *final_class_def = &dex_file->GetClassDef(0);
266 *final_dex_file = dex_file.release();
267 }
268 }
269
Andreas Gampee6377462017-01-20 17:37:50 -0800270 void ClassLoad(art::Handle<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
271 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassLoad)) {
272 art::Thread* thread = art::Thread::Current();
273 ScopedLocalRef<jclass> jklass(thread->GetJniEnv(),
274 thread->GetJniEnv()->AddLocalReference<jclass>(klass.Get()));
Alex Light72398652017-06-16 09:08:12 -0700275 art::ObjPtr<art::mirror::Object> peer(thread->GetPeer());
Andreas Gampe983c1752017-01-23 19:46:56 -0800276 ScopedLocalRef<jthread> thread_jni(
Alex Light72398652017-06-16 09:08:12 -0700277 thread->GetJniEnv(),
278 peer.IsNull() ? nullptr : thread->GetJniEnv()->AddLocalReference<jthread>(peer));
Alex Light9df79b72017-09-12 08:57:31 -0700279 event_handler->DispatchEvent<ArtJvmtiEvent::kClassLoad>(
280 thread,
281 static_cast<JNIEnv*>(thread->GetJniEnv()),
282 thread_jni.get(),
283 jklass.get());
Andreas Gampe691051b2017-02-09 09:15:24 -0800284 if (klass->IsTemp()) {
285 AddTempClass(thread, jklass.get());
286 }
Andreas Gampee6377462017-01-20 17:37:50 -0800287 }
288 }
289
Andreas Gampe691051b2017-02-09 09:15:24 -0800290 void ClassPrepare(art::Handle<art::mirror::Class> temp_klass,
Andreas Gampee6377462017-01-20 17:37:50 -0800291 art::Handle<art::mirror::Class> klass)
292 REQUIRES_SHARED(art::Locks::mutator_lock_) {
293 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassPrepare)) {
294 art::Thread* thread = art::Thread::Current();
Andreas Gampe691051b2017-02-09 09:15:24 -0800295 if (temp_klass.Get() != klass.Get()) {
296 DCHECK(temp_klass->IsTemp());
297 DCHECK(temp_klass->IsRetired());
298 HandleTempClass(thread, temp_klass, klass);
299 }
Andreas Gampee6377462017-01-20 17:37:50 -0800300 ScopedLocalRef<jclass> jklass(thread->GetJniEnv(),
301 thread->GetJniEnv()->AddLocalReference<jclass>(klass.Get()));
Alex Light72398652017-06-16 09:08:12 -0700302 art::ObjPtr<art::mirror::Object> peer(thread->GetPeer());
Andreas Gampe983c1752017-01-23 19:46:56 -0800303 ScopedLocalRef<jthread> thread_jni(
Alex Light72398652017-06-16 09:08:12 -0700304 thread->GetJniEnv(),
305 peer.IsNull() ? nullptr : thread->GetJniEnv()->AddLocalReference<jthread>(peer));
Andreas Gampe983c1752017-01-23 19:46:56 -0800306 event_handler->DispatchEvent<ArtJvmtiEvent::kClassPrepare>(
307 thread,
308 static_cast<JNIEnv*>(thread->GetJniEnv()),
309 thread_jni.get(),
310 jklass.get());
Andreas Gampee6377462017-01-20 17:37:50 -0800311 }
312 }
313
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800314 // To support parallel class-loading, we need to perform some locking dances here. Namely,
315 // the fixup stage must not be holding the temp_classes lock when it fixes up the system
316 // (as that requires suspending all mutators).
317
Andreas Gampee6377462017-01-20 17:37:50 -0800318 void AddTempClass(art::Thread* self, jclass klass) {
319 std::unique_lock<std::mutex> mu(temp_classes_lock);
Andreas Gampe691051b2017-02-09 09:15:24 -0800320 jclass global_klass = reinterpret_cast<jclass>(self->GetJniEnv()->NewGlobalRef(klass));
321 temp_classes.push_back(global_klass);
Andreas Gampee6377462017-01-20 17:37:50 -0800322 }
323
Andreas Gampe691051b2017-02-09 09:15:24 -0800324 void HandleTempClass(art::Thread* self,
325 art::Handle<art::mirror::Class> temp_klass,
Andreas Gampee6377462017-01-20 17:37:50 -0800326 art::Handle<art::mirror::Class> klass)
327 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800328 bool requires_fixup = false;
329 {
330 std::unique_lock<std::mutex> mu(temp_classes_lock);
331 if (temp_classes.empty()) {
332 return;
Andreas Gampee6377462017-01-20 17:37:50 -0800333 }
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800334
335 for (auto it = temp_classes.begin(); it != temp_classes.end(); ++it) {
336 if (temp_klass.Get() == art::ObjPtr<art::mirror::Class>::DownCast(self->DecodeJObject(*it))) {
337 self->GetJniEnv()->DeleteGlobalRef(*it);
338 temp_classes.erase(it);
339 requires_fixup = true;
340 break;
341 }
342 }
343 }
344 if (requires_fixup) {
345 FixupTempClass(self, temp_klass, klass);
Andreas Gampee6377462017-01-20 17:37:50 -0800346 }
347 }
348
Andreas Gampe691051b2017-02-09 09:15:24 -0800349 void FixupTempClass(art::Thread* self,
350 art::Handle<art::mirror::Class> temp_klass,
351 art::Handle<art::mirror::Class> klass)
Andreas Gampee6377462017-01-20 17:37:50 -0800352 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe691051b2017-02-09 09:15:24 -0800353 // Suspend everything.
354 art::gc::Heap* heap = art::Runtime::Current()->GetHeap();
355 if (heap->IsGcConcurrentAndMoving()) {
356 // Need to take a heap dump while GC isn't running. See the
357 // comment in Heap::VisitObjects().
358 heap->IncrementDisableMovingGC(self);
359 }
360 {
361 art::ScopedThreadSuspension sts(self, art::kWaitingForVisitObjects);
362 art::ScopedSuspendAll ssa("FixupTempClass");
363
364 art::mirror::Class* input = temp_klass.Get();
365 art::mirror::Class* output = klass.Get();
366
367 FixupGlobalReferenceTables(input, output);
Andreas Gampe94dda932017-02-09 18:19:21 -0800368 FixupLocalReferenceTables(self, input, output);
Andreas Gampea67354b2017-02-10 16:18:30 -0800369 FixupHeap(input, output);
Andreas Gampe691051b2017-02-09 09:15:24 -0800370 }
371 if (heap->IsGcConcurrentAndMoving()) {
372 heap->DecrementDisableMovingGC(self);
373 }
374 }
375
Andreas Gampe94dda932017-02-09 18:19:21 -0800376 class RootUpdater : public art::RootVisitor {
377 public:
378 RootUpdater(const art::mirror::Class* input, art::mirror::Class* output)
379 : input_(input), output_(output) {}
380
381 void VisitRoots(art::mirror::Object*** roots,
382 size_t count,
383 const art::RootInfo& info ATTRIBUTE_UNUSED)
384 OVERRIDE {
385 for (size_t i = 0; i != count; ++i) {
386 if (*roots[i] == input_) {
387 *roots[i] = output_;
388 }
389 }
390 }
391
392 void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
393 size_t count,
394 const art::RootInfo& info ATTRIBUTE_UNUSED)
395 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
396 for (size_t i = 0; i != count; ++i) {
397 if (roots[i]->AsMirrorPtr() == input_) {
398 roots[i]->Assign(output_);
399 }
400 }
401 }
402
403 private:
404 const art::mirror::Class* input_;
405 art::mirror::Class* output_;
406 };
407
Andreas Gampea67354b2017-02-10 16:18:30 -0800408 void FixupGlobalReferenceTables(art::mirror::Class* input, art::mirror::Class* output)
Andreas Gampe691051b2017-02-09 09:15:24 -0800409 REQUIRES(art::Locks::mutator_lock_) {
410 art::JavaVMExt* java_vm = art::Runtime::Current()->GetJavaVM();
411
412 // Fix up the global table with a root visitor.
Andreas Gampe94dda932017-02-09 18:19:21 -0800413 RootUpdater global_update(input, output);
Andreas Gampe691051b2017-02-09 09:15:24 -0800414 java_vm->VisitRoots(&global_update);
415
416 class WeakGlobalUpdate : public art::IsMarkedVisitor {
417 public:
418 WeakGlobalUpdate(art::mirror::Class* root_input, art::mirror::Class* root_output)
419 : input_(root_input), output_(root_output) {}
420
421 art::mirror::Object* IsMarked(art::mirror::Object* obj) OVERRIDE {
422 if (obj == input_) {
423 return output_;
424 }
425 return obj;
426 }
427
428 private:
429 const art::mirror::Class* input_;
430 art::mirror::Class* output_;
431 };
432 WeakGlobalUpdate weak_global_update(input, output);
433 java_vm->SweepJniWeakGlobals(&weak_global_update);
Andreas Gampee6377462017-01-20 17:37:50 -0800434 }
435
Andreas Gampe94dda932017-02-09 18:19:21 -0800436 void FixupLocalReferenceTables(art::Thread* self,
437 art::mirror::Class* input,
438 art::mirror::Class* output)
439 REQUIRES(art::Locks::mutator_lock_) {
440 class LocalUpdate {
441 public:
442 LocalUpdate(const art::mirror::Class* root_input, art::mirror::Class* root_output)
443 : input_(root_input), output_(root_output) {}
444
445 static void Callback(art::Thread* t, void* arg) REQUIRES(art::Locks::mutator_lock_) {
446 LocalUpdate* local = reinterpret_cast<LocalUpdate*>(arg);
447
448 // Fix up the local table with a root visitor.
449 RootUpdater local_update(local->input_, local->output_);
Ian Rogers55256cb2017-12-21 17:07:11 -0800450 t->GetJniEnv()->VisitJniLocalRoots(
Andreas Gampe94dda932017-02-09 18:19:21 -0800451 &local_update, art::RootInfo(art::kRootJNILocal, t->GetThreadId()));
452 }
453
454 private:
455 const art::mirror::Class* input_;
456 art::mirror::Class* output_;
457 };
458 LocalUpdate local_upd(input, output);
459 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
460 art::Runtime::Current()->GetThreadList()->ForEach(LocalUpdate::Callback, &local_upd);
461 }
462
Andreas Gampea67354b2017-02-10 16:18:30 -0800463 void FixupHeap(art::mirror::Class* input, art::mirror::Class* output)
464 REQUIRES(art::Locks::mutator_lock_) {
465 class HeapFixupVisitor {
466 public:
467 HeapFixupVisitor(const art::mirror::Class* root_input, art::mirror::Class* root_output)
468 : input_(root_input), output_(root_output) {}
469
470 void operator()(art::mirror::Object* src,
471 art::MemberOffset field_offset,
472 bool is_static ATTRIBUTE_UNUSED) const
473 REQUIRES_SHARED(art::Locks::mutator_lock_) {
474 art::mirror::HeapReference<art::mirror::Object>* trg =
475 src->GetFieldObjectReferenceAddr(field_offset);
476 if (trg->AsMirrorPtr() == input_) {
477 DCHECK_NE(field_offset.Uint32Value(), 0u); // This shouldn't be the class field of
478 // an object.
479 trg->Assign(output_);
480 }
481 }
482
Andreas Gampe52784ac2017-02-13 18:10:09 -0800483 void operator()(art::ObjPtr<art::mirror::Class> klass ATTRIBUTE_UNUSED,
484 art::ObjPtr<art::mirror::Reference> reference) const
485 REQUIRES_SHARED(art::Locks::mutator_lock_) {
486 art::mirror::Object* val = reference->GetReferent();
487 if (val == input_) {
488 reference->SetReferent<false>(output_);
489 }
490 }
491
Andreas Gampea67354b2017-02-10 16:18:30 -0800492 void VisitRoot(art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED)
Andreas Gampe52784ac2017-02-13 18:10:09 -0800493 const {
Andreas Gampea67354b2017-02-10 16:18:30 -0800494 LOG(FATAL) << "Unreachable";
495 }
496
497 void VisitRootIfNonNull(
498 art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED) const {
499 LOG(FATAL) << "Unreachable";
500 }
501
Andreas Gampea67354b2017-02-10 16:18:30 -0800502 private:
503 const art::mirror::Class* input_;
504 art::mirror::Class* output_;
505 };
506 HeapFixupVisitor hfv(input, output);
Andreas Gampe1c158a02017-07-13 17:26:19 -0700507 auto object_visitor = [&](art::mirror::Object* obj) {
508 obj->VisitReferences<false>(hfv, hfv); // Visit references, not native roots.
509 };
510 art::Runtime::Current()->GetHeap()->VisitObjectsPaused(object_visitor);
Andreas Gampea67354b2017-02-10 16:18:30 -0800511 }
512
Andreas Gampee6377462017-01-20 17:37:50 -0800513 // A set of all the temp classes we have handed out. We have to fix up references to these.
514 // For simplicity, we store the temp classes as JNI global references in a vector. Normally a
515 // Prepare event will closely follow, so the vector should be small.
516 std::mutex temp_classes_lock;
517 std::vector<jclass> temp_classes;
518
519 EventHandler* event_handler = nullptr;
520};
521
522ClassCallback gClassCallback;
523
524void ClassUtil::Register(EventHandler* handler) {
525 gClassCallback.event_handler = handler;
526 art::ScopedThreadStateChange stsc(art::Thread::Current(),
527 art::ThreadState::kWaitingForDebuggerToAttach);
528 art::ScopedSuspendAll ssa("Add load callback");
529 art::Runtime::Current()->GetRuntimeCallbacks()->AddClassLoadCallback(&gClassCallback);
530}
531
532void ClassUtil::Unregister() {
533 art::ScopedThreadStateChange stsc(art::Thread::Current(),
534 art::ThreadState::kWaitingForDebuggerToAttach);
535 art::ScopedSuspendAll ssa("Remove thread callback");
536 art::Runtime* runtime = art::Runtime::Current();
537 runtime->GetRuntimeCallbacks()->RemoveClassLoadCallback(&gClassCallback);
538}
539
Andreas Gampeac587272017-01-05 15:21:34 -0800540jvmtiError ClassUtil::GetClassFields(jvmtiEnv* env,
541 jclass jklass,
542 jint* field_count_ptr,
543 jfieldID** fields_ptr) {
544 art::ScopedObjectAccess soa(art::Thread::Current());
545 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
546 if (klass == nullptr) {
547 return ERR(INVALID_CLASS);
548 }
549
Alex Light2c6cd692017-06-30 07:34:40 -0700550 // Check if this class is a temporary class object used for loading. Since we are seeing it the
551 // class must not have been prepared yet since otherwise the fixup would have gotten the jobject
552 // to point to the final class object.
553 if (klass->IsTemp() || klass->IsRetired()) {
554 return ERR(CLASS_NOT_PREPARED);
555 }
556
Andreas Gampeac587272017-01-05 15:21:34 -0800557 if (field_count_ptr == nullptr || fields_ptr == nullptr) {
558 return ERR(NULL_POINTER);
559 }
560
Andreas Gampeac587272017-01-05 15:21:34 -0800561 art::IterationRange<art::StrideIterator<art::ArtField>> ifields = klass->GetIFields();
562 art::IterationRange<art::StrideIterator<art::ArtField>> sfields = klass->GetSFields();
563 size_t array_size = klass->NumInstanceFields() + klass->NumStaticFields();
564
565 unsigned char* out_ptr;
566 jvmtiError allocError = env->Allocate(array_size * sizeof(jfieldID), &out_ptr);
567 if (allocError != ERR(NONE)) {
568 return allocError;
569 }
570 jfieldID* field_array = reinterpret_cast<jfieldID*>(out_ptr);
571
572 size_t array_idx = 0;
573 for (art::ArtField& field : sfields) {
574 field_array[array_idx] = art::jni::EncodeArtField(&field);
575 ++array_idx;
576 }
577 for (art::ArtField& field : ifields) {
578 field_array[array_idx] = art::jni::EncodeArtField(&field);
579 ++array_idx;
580 }
581
582 *field_count_ptr = static_cast<jint>(array_size);
583 *fields_ptr = field_array;
584
585 return ERR(NONE);
586}
587
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800588jvmtiError ClassUtil::GetClassMethods(jvmtiEnv* env,
589 jclass jklass,
590 jint* method_count_ptr,
591 jmethodID** methods_ptr) {
592 art::ScopedObjectAccess soa(art::Thread::Current());
593 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
594 if (klass == nullptr) {
595 return ERR(INVALID_CLASS);
596 }
597
Alex Light2c6cd692017-06-30 07:34:40 -0700598 // Check if this class is a temporary class object used for loading. Since we are seeing it the
599 // class must not have been prepared yet since otherwise the fixup would have gotten the jobject
600 // to point to the final class object.
601 if (klass->IsTemp() || klass->IsRetired()) {
602 return ERR(CLASS_NOT_PREPARED);
603 }
604
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800605 if (method_count_ptr == nullptr || methods_ptr == nullptr) {
606 return ERR(NULL_POINTER);
607 }
608
609 size_t array_size = klass->NumDeclaredVirtualMethods() + klass->NumDirectMethods();
610 unsigned char* out_ptr;
611 jvmtiError allocError = env->Allocate(array_size * sizeof(jmethodID), &out_ptr);
612 if (allocError != ERR(NONE)) {
613 return allocError;
614 }
615 jmethodID* method_array = reinterpret_cast<jmethodID*>(out_ptr);
616
617 if (art::kIsDebugBuild) {
618 size_t count = 0;
619 for (auto& m ATTRIBUTE_UNUSED : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
620 count++;
621 }
622 CHECK_EQ(count, klass->NumDirectMethods() + klass->NumDeclaredVirtualMethods());
623 }
624
625 size_t array_idx = 0;
626 for (auto& m : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
627 method_array[array_idx] = art::jni::EncodeArtMethod(&m);
628 ++array_idx;
629 }
630
631 *method_count_ptr = static_cast<jint>(array_size);
632 *methods_ptr = method_array;
633
634 return ERR(NONE);
635}
636
Andreas Gampe8b07e472017-01-06 14:20:39 -0800637jvmtiError ClassUtil::GetImplementedInterfaces(jvmtiEnv* env,
638 jclass jklass,
639 jint* interface_count_ptr,
640 jclass** interfaces_ptr) {
641 art::ScopedObjectAccess soa(art::Thread::Current());
642 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
643 if (klass == nullptr) {
644 return ERR(INVALID_CLASS);
645 }
646
647 if (interface_count_ptr == nullptr || interfaces_ptr == nullptr) {
648 return ERR(NULL_POINTER);
649 }
650
651 // Need to handle array specifically. Arrays implement Serializable and Cloneable, but the
652 // spec says these should not be reported.
653 if (klass->IsArrayClass()) {
654 *interface_count_ptr = 0;
655 *interfaces_ptr = nullptr; // TODO: Should we allocate a dummy here?
656 return ERR(NONE);
657 }
658
659 size_t array_size = klass->NumDirectInterfaces();
660 unsigned char* out_ptr;
661 jvmtiError allocError = env->Allocate(array_size * sizeof(jclass), &out_ptr);
662 if (allocError != ERR(NONE)) {
663 return allocError;
664 }
665 jclass* interface_array = reinterpret_cast<jclass*>(out_ptr);
666
667 art::StackHandleScope<1> hs(soa.Self());
668 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass));
669
670 for (uint32_t idx = 0; idx != array_size; ++idx) {
671 art::ObjPtr<art::mirror::Class> inf_klass =
672 art::mirror::Class::ResolveDirectInterface(soa.Self(), h_klass, idx);
673 if (inf_klass == nullptr) {
674 soa.Self()->ClearException();
675 env->Deallocate(out_ptr);
676 // TODO: What is the right error code here?
677 return ERR(INTERNAL);
678 }
679 interface_array[idx] = soa.AddLocalReference<jclass>(inf_klass);
680 }
681
682 *interface_count_ptr = static_cast<jint>(array_size);
683 *interfaces_ptr = interface_array;
684
685 return ERR(NONE);
686}
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800687
Andreas Gampee492ae32016-10-28 19:34:57 -0700688jvmtiError ClassUtil::GetClassSignature(jvmtiEnv* env,
689 jclass jklass,
690 char** signature_ptr,
691 char** generic_ptr) {
692 art::ScopedObjectAccess soa(art::Thread::Current());
693 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
694 if (klass == nullptr) {
695 return ERR(INVALID_CLASS);
696 }
697
Andreas Gampe54711412017-02-21 12:41:43 -0800698 JvmtiUniquePtr<char[]> sig_copy;
Andreas Gampee492ae32016-10-28 19:34:57 -0700699 if (signature_ptr != nullptr) {
700 std::string storage;
701 const char* descriptor = klass->GetDescriptor(&storage);
702
Andreas Gampe54711412017-02-21 12:41:43 -0800703 jvmtiError ret;
704 sig_copy = CopyString(env, descriptor, &ret);
705 if (sig_copy == nullptr) {
Andreas Gampee492ae32016-10-28 19:34:57 -0700706 return ret;
707 }
Andreas Gampe54711412017-02-21 12:41:43 -0800708 *signature_ptr = sig_copy.get();
Andreas Gampee492ae32016-10-28 19:34:57 -0700709 }
710
Andreas Gampee6377462017-01-20 17:37:50 -0800711 if (generic_ptr != nullptr) {
712 *generic_ptr = nullptr;
Andreas Gampe0eb36432017-02-15 18:36:14 -0800713 if (!klass->IsProxyClass() && klass->GetDexCache() != nullptr) {
714 art::StackHandleScope<1> hs(soa.Self());
715 art::Handle<art::mirror::Class> h_klass = hs.NewHandle(klass);
716 art::mirror::ObjectArray<art::mirror::String>* str_array =
717 art::annotations::GetSignatureAnnotationForClass(h_klass);
718 if (str_array != nullptr) {
719 std::ostringstream oss;
720 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
721 oss << str_array->Get(i)->ToModifiedUtf8();
722 }
723 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800724 jvmtiError ret;
725 JvmtiUniquePtr<char[]> copy = CopyString(env, output_string.c_str(), &ret);
726 if (copy == nullptr) {
Andreas Gampe0eb36432017-02-15 18:36:14 -0800727 return ret;
728 }
Andreas Gampe54711412017-02-21 12:41:43 -0800729 *generic_ptr = copy.release();
Andreas Gampe0eb36432017-02-15 18:36:14 -0800730 } else if (soa.Self()->IsExceptionPending()) {
731 // TODO: Should we report an error here?
732 soa.Self()->ClearException();
733 }
734 }
Andreas Gampee6377462017-01-20 17:37:50 -0800735 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700736
737 // Everything is fine, release the buffers.
738 sig_copy.release();
739
740 return ERR(NONE);
741}
742
Andreas Gampeff9d2092017-01-06 09:12:49 -0800743jvmtiError ClassUtil::GetClassStatus(jvmtiEnv* env ATTRIBUTE_UNUSED,
744 jclass jklass,
745 jint* status_ptr) {
746 art::ScopedObjectAccess soa(art::Thread::Current());
747 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
748 if (klass == nullptr) {
749 return ERR(INVALID_CLASS);
750 }
751
752 if (status_ptr == nullptr) {
753 return ERR(NULL_POINTER);
754 }
755
756 if (klass->IsArrayClass()) {
757 *status_ptr = JVMTI_CLASS_STATUS_ARRAY;
758 } else if (klass->IsPrimitive()) {
759 *status_ptr = JVMTI_CLASS_STATUS_PRIMITIVE;
760 } else {
761 *status_ptr = JVMTI_CLASS_STATUS_VERIFIED; // All loaded classes are structurally verified.
762 // This is finicky. If there's an error, we'll say it wasn't prepared.
763 if (klass->IsResolved()) {
764 *status_ptr |= JVMTI_CLASS_STATUS_PREPARED;
765 }
766 if (klass->IsInitialized()) {
767 *status_ptr |= JVMTI_CLASS_STATUS_INITIALIZED;
768 }
769 // Technically the class may be erroneous for other reasons, but we do not have enough info.
770 if (klass->IsErroneous()) {
771 *status_ptr |= JVMTI_CLASS_STATUS_ERROR;
772 }
773 }
774
775 return ERR(NONE);
776}
777
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800778template <typename T>
779static jvmtiError ClassIsT(jclass jklass, T test, jboolean* is_t_ptr) {
780 art::ScopedObjectAccess soa(art::Thread::Current());
781 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
782 if (klass == nullptr) {
783 return ERR(INVALID_CLASS);
784 }
785
786 if (is_t_ptr == nullptr) {
787 return ERR(NULL_POINTER);
788 }
789
790 *is_t_ptr = test(klass) ? JNI_TRUE : JNI_FALSE;
791 return ERR(NONE);
792}
793
794jvmtiError ClassUtil::IsInterface(jvmtiEnv* env ATTRIBUTE_UNUSED,
795 jclass jklass,
796 jboolean* is_interface_ptr) {
797 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
798 return klass->IsInterface();
799 };
800 return ClassIsT(jklass, test, is_interface_ptr);
801}
802
803jvmtiError ClassUtil::IsArrayClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
804 jclass jklass,
805 jboolean* is_array_class_ptr) {
806 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
807 return klass->IsArrayClass();
808 };
809 return ClassIsT(jklass, test, is_array_class_ptr);
810}
811
Andreas Gampe64013e52017-01-06 13:07:19 -0800812// Keep this in sync with Class.getModifiers().
813static uint32_t ClassGetModifiers(art::Thread* self, art::ObjPtr<art::mirror::Class> klass)
814 REQUIRES_SHARED(art::Locks::mutator_lock_) {
815 if (klass->IsArrayClass()) {
816 uint32_t component_modifiers = ClassGetModifiers(self, klass->GetComponentType());
817 if ((component_modifiers & art::kAccInterface) != 0) {
818 component_modifiers &= ~(art::kAccInterface | art::kAccStatic);
819 }
820 return art::kAccAbstract | art::kAccFinal | component_modifiers;
821 }
822
823 uint32_t modifiers = klass->GetAccessFlags() & art::kAccJavaFlagsMask;
824
825 art::StackHandleScope<1> hs(self);
826 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass));
827 return art::mirror::Class::GetInnerClassFlags(h_klass, modifiers);
828}
829
830jvmtiError ClassUtil::GetClassModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
831 jclass jklass,
832 jint* modifiers_ptr) {
833 art::ScopedObjectAccess soa(art::Thread::Current());
834 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
835 if (klass == nullptr) {
836 return ERR(INVALID_CLASS);
837 }
838
839 if (modifiers_ptr == nullptr) {
840 return ERR(NULL_POINTER);
841 }
842
843 *modifiers_ptr = ClassGetModifiers(soa.Self(), klass);
844
845 return ERR(NONE);
846}
847
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800848jvmtiError ClassUtil::GetClassLoader(jvmtiEnv* env ATTRIBUTE_UNUSED,
849 jclass jklass,
850 jobject* classloader_ptr) {
851 art::ScopedObjectAccess soa(art::Thread::Current());
852 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
853 if (klass == nullptr) {
854 return ERR(INVALID_CLASS);
855 }
856
857 if (classloader_ptr == nullptr) {
858 return ERR(NULL_POINTER);
859 }
860
861 *classloader_ptr = soa.AddLocalReference<jobject>(klass->GetClassLoader());
862
863 return ERR(NONE);
864}
865
Alex Lightc7588752018-02-20 11:15:54 -0800866// Copies unique class descriptors into the classes list from dex_files.
867static jvmtiError CopyClassDescriptors(jvmtiEnv* env,
868 const std::vector<const art::DexFile*>& dex_files,
869 /*out*/jint* count_ptr,
870 /*out*/char*** classes) {
871 jvmtiError res = OK;
872 std::set<art::StringPiece> unique_descriptors;
873 std::vector<const char*> descriptors;
874 auto add_descriptor = [&](const char* desc) {
875 // Don't add duplicates.
876 if (res == OK && unique_descriptors.find(desc) == unique_descriptors.end()) {
877 // The desc will remain valid since we hold a ref to the class_loader.
878 unique_descriptors.insert(desc);
879 descriptors.push_back(CopyString(env, desc, &res).release());
880 }
881 };
882 for (const art::DexFile* dex_file : dex_files) {
883 uint32_t num_defs = dex_file->NumClassDefs();
884 for (uint32_t i = 0; i < num_defs; i++) {
885 add_descriptor(dex_file->GetClassDescriptor(dex_file->GetClassDef(i)));
886 }
887 }
888 char** out_data = nullptr;
889 if (res == OK) {
890 res = env->Allocate(sizeof(char*) * descriptors.size(),
891 reinterpret_cast<unsigned char**>(&out_data));
892 }
893 if (res != OK) {
894 env->Deallocate(reinterpret_cast<unsigned char*>(out_data));
895 // Failed to allocate. Cleanup everything.
896 for (const char* data : descriptors) {
897 env->Deallocate(reinterpret_cast<unsigned char*>(const_cast<char*>(data)));
898 }
899 descriptors.clear();
900 return res;
901 }
902 // Everything is good.
903 memcpy(out_data, descriptors.data(), sizeof(char*) * descriptors.size());
904 *count_ptr = static_cast<jint>(descriptors.size());
905 *classes = out_data;
906 return OK;
907}
908
909jvmtiError ClassUtil::GetClassLoaderClassDescriptors(jvmtiEnv* env,
910 jobject loader,
911 /*out*/jint* count_ptr,
912 /*out*/char*** classes) {
913 art::Thread* self = art::Thread::Current();
914 if (env == nullptr) {
915 return ERR(INVALID_ENVIRONMENT);
916 } else if (self == nullptr) {
917 return ERR(UNATTACHED_THREAD);
918 } else if (count_ptr == nullptr || classes == nullptr) {
919 return ERR(NULL_POINTER);
920 }
921 art::JNIEnvExt* jnienv = self->GetJniEnv();
922 if (loader == nullptr ||
923 jnienv->IsInstanceOf(loader, art::WellKnownClasses::java_lang_BootClassLoader)) {
924 // We can just get the dex files directly for the boot class path.
925 return CopyClassDescriptors(env,
926 art::Runtime::Current()->GetClassLinker()->GetBootClassPath(),
927 count_ptr,
928 classes);
929 }
930 if (!jnienv->IsInstanceOf(loader, art::WellKnownClasses::java_lang_ClassLoader)) {
931 return ERR(ILLEGAL_ARGUMENT);
932 } else if (!jnienv->IsInstanceOf(loader,
933 art::WellKnownClasses::dalvik_system_BaseDexClassLoader)) {
934 LOG(ERROR) << "GetClassLoaderClassDescriptors is only implemented for BootClassPath and "
935 << "dalvik.system.BaseDexClassLoader class loaders";
936 // TODO Possibly return OK With no classes would be better since these ones cannot have any
937 // real classes associated with them.
938 return ERR(NOT_IMPLEMENTED);
939 }
940
941 art::ScopedObjectAccess soa(self);
942 art::StackHandleScope<1> hs(self);
943 art::Handle<art::mirror::ClassLoader> class_loader(
944 hs.NewHandle(soa.Decode<art::mirror::ClassLoader>(loader)));
945 std::vector<const art::DexFile*> dex_files;
Andreas Gampee8320ea2018-02-20 18:24:55 -0800946 art::VisitClassLoaderDexFiles(
947 soa,
Alex Lightc7588752018-02-20 11:15:54 -0800948 class_loader,
Andreas Gampee8320ea2018-02-20 18:24:55 -0800949 [&](const art::DexFile* dex_file) {
950 dex_files.push_back(dex_file);
951 return true; // Continue with other dex files.
Alex Lightc7588752018-02-20 11:15:54 -0800952 });
953 // We hold the loader so the dex files won't go away until after this call at worst.
954 return CopyClassDescriptors(env, dex_files, count_ptr, classes);
955}
956
Andreas Gampe70f16392017-01-16 14:20:10 -0800957jvmtiError ClassUtil::GetClassLoaderClasses(jvmtiEnv* env,
958 jobject initiating_loader,
959 jint* class_count_ptr,
960 jclass** classes_ptr) {
961 UNUSED(env, initiating_loader, class_count_ptr, classes_ptr);
962
963 if (class_count_ptr == nullptr || classes_ptr == nullptr) {
964 return ERR(NULL_POINTER);
965 }
966 art::Thread* self = art::Thread::Current();
967 if (!self->GetJniEnv()->IsInstanceOf(initiating_loader,
968 art::WellKnownClasses::java_lang_ClassLoader)) {
969 return ERR(ILLEGAL_ARGUMENT);
970 }
971 if (self->GetJniEnv()->IsInstanceOf(initiating_loader,
972 art::WellKnownClasses::java_lang_BootClassLoader)) {
973 // Need to use null for the BootClassLoader.
974 initiating_loader = nullptr;
975 }
976
977 art::ScopedObjectAccess soa(self);
978 art::ObjPtr<art::mirror::ClassLoader> class_loader =
979 soa.Decode<art::mirror::ClassLoader>(initiating_loader);
980
981 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
982
983 art::ReaderMutexLock mu(self, *art::Locks::classlinker_classes_lock_);
984
985 art::ClassTable* class_table = class_linker->ClassTableForClassLoader(class_loader);
986 if (class_table == nullptr) {
987 // Nothing loaded.
988 *class_count_ptr = 0;
989 *classes_ptr = nullptr;
990 return ERR(NONE);
991 }
992
993 struct ClassTableCount {
994 bool operator()(art::ObjPtr<art::mirror::Class> klass) {
995 DCHECK(klass != nullptr);
996 ++count;
997 return true;
998 }
999
1000 size_t count = 0;
1001 };
1002 ClassTableCount ctc;
1003 class_table->Visit(ctc);
1004
1005 if (ctc.count == 0) {
1006 // Nothing loaded.
1007 *class_count_ptr = 0;
1008 *classes_ptr = nullptr;
1009 return ERR(NONE);
1010 }
1011
1012 unsigned char* data;
1013 jvmtiError data_result = env->Allocate(ctc.count * sizeof(jclass), &data);
1014 if (data_result != ERR(NONE)) {
1015 return data_result;
1016 }
1017 jclass* class_array = reinterpret_cast<jclass*>(data);
1018
1019 struct ClassTableFill {
1020 bool operator()(art::ObjPtr<art::mirror::Class> klass)
1021 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1022 DCHECK(klass != nullptr);
1023 DCHECK_LT(count, ctc_ref.count);
1024 local_class_array[count++] = soa_ptr->AddLocalReference<jclass>(klass);
1025 return true;
1026 }
1027
1028 jclass* local_class_array;
1029 const ClassTableCount& ctc_ref;
1030 art::ScopedObjectAccess* soa_ptr;
1031 size_t count;
1032 };
1033 ClassTableFill ctf = { class_array, ctc, &soa, 0 };
1034 class_table->Visit(ctf);
1035 DCHECK_EQ(ctc.count, ctf.count);
1036
1037 *class_count_ptr = ctc.count;
1038 *classes_ptr = class_array;
1039
1040 return ERR(NONE);
1041}
1042
Andreas Gampe812a2442017-01-19 22:04:46 -08001043jvmtiError ClassUtil::GetClassVersionNumbers(jvmtiEnv* env ATTRIBUTE_UNUSED,
1044 jclass jklass,
1045 jint* minor_version_ptr,
1046 jint* major_version_ptr) {
1047 art::ScopedObjectAccess soa(art::Thread::Current());
1048 if (jklass == nullptr) {
1049 return ERR(INVALID_CLASS);
1050 }
1051 art::ObjPtr<art::mirror::Object> jklass_obj = soa.Decode<art::mirror::Object>(jklass);
1052 if (!jklass_obj->IsClass()) {
1053 return ERR(INVALID_CLASS);
1054 }
1055 art::ObjPtr<art::mirror::Class> klass = jklass_obj->AsClass();
1056 if (klass->IsPrimitive() || klass->IsArrayClass()) {
1057 return ERR(INVALID_CLASS);
1058 }
1059
1060 if (minor_version_ptr == nullptr || major_version_ptr == nullptr) {
1061 return ERR(NULL_POINTER);
1062 }
1063
1064 // Note: proxies will show the dex file version of java.lang.reflect.Proxy, as that is
1065 // what their dex cache copies from.
1066 uint32_t version = klass->GetDexFile().GetHeader().GetVersion();
1067
1068 *major_version_ptr = static_cast<jint>(version);
1069 *minor_version_ptr = 0;
1070
1071 return ERR(NONE);
1072}
1073
Alex Light6fa7b812017-06-16 09:04:29 -07001074jvmtiError ClassUtil::GetSourceFileName(jvmtiEnv* env, jclass jklass, char** source_name_ptr) {
1075 art::ScopedObjectAccess soa(art::Thread::Current());
1076 if (jklass == nullptr) {
1077 return ERR(INVALID_CLASS);
1078 }
1079 art::ObjPtr<art::mirror::Object> jklass_obj = soa.Decode<art::mirror::Object>(jklass);
1080 if (!jklass_obj->IsClass()) {
1081 return ERR(INVALID_CLASS);
1082 }
1083 art::ObjPtr<art::mirror::Class> klass = jklass_obj->AsClass();
1084 if (klass->IsPrimitive() || klass->IsArrayClass()) {
1085 return ERR(ABSENT_INFORMATION);
1086 }
1087 JvmtiUniquePtr<char[]> source_copy;
1088 const char* file_name = klass->GetSourceFile();
1089 if (file_name == nullptr) {
1090 return ERR(ABSENT_INFORMATION);
1091 }
1092 jvmtiError ret;
1093 source_copy = CopyString(env, file_name, &ret);
1094 if (source_copy == nullptr) {
1095 return ret;
1096 }
1097 *source_name_ptr = source_copy.release();
1098 return OK;
1099}
1100
1101jvmtiError ClassUtil::GetSourceDebugExtension(jvmtiEnv* env,
1102 jclass jklass,
1103 char** source_debug_extension_ptr) {
1104 art::ScopedObjectAccess soa(art::Thread::Current());
1105 if (jklass == nullptr) {
1106 return ERR(INVALID_CLASS);
1107 }
1108 art::ObjPtr<art::mirror::Object> jklass_obj = soa.Decode<art::mirror::Object>(jklass);
1109 if (!jklass_obj->IsClass()) {
1110 return ERR(INVALID_CLASS);
1111 }
1112 art::StackHandleScope<1> hs(art::Thread::Current());
1113 art::Handle<art::mirror::Class> klass(hs.NewHandle(jklass_obj->AsClass()));
1114 if (klass->IsPrimitive() || klass->IsArrayClass()) {
1115 return ERR(ABSENT_INFORMATION);
1116 }
1117 JvmtiUniquePtr<char[]> ext_copy;
1118 const char* data = art::annotations::GetSourceDebugExtension(klass);
1119 if (data == nullptr) {
1120 return ERR(ABSENT_INFORMATION);
1121 }
1122 jvmtiError ret;
1123 ext_copy = CopyString(env, data, &ret);
1124 if (ext_copy == nullptr) {
1125 return ret;
1126 }
1127 *source_debug_extension_ptr = ext_copy.release();
1128 return OK;
1129}
1130
Andreas Gampee492ae32016-10-28 19:34:57 -07001131} // namespace openjdkjvmti