blob: ed54cd13c3eee615ebf9fc2e39737540448d842d [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
Andreas Gampee6377462017-01-20 17:37:50 -080036#include <mutex>
37#include <unordered_set>
38
Andreas Gampee492ae32016-10-28 19:34:57 -070039#include "art_jvmti.h"
Andreas Gampee6377462017-01-20 17:37:50 -080040#include "base/macros.h"
Andreas Gampe70f16392017-01-16 14:20:10 -080041#include "class_table-inl.h"
42#include "class_linker.h"
Alex Light440b5d92017-01-24 15:32:25 -080043#include "common_throws.h"
Andreas Gampe0eb36432017-02-15 18:36:14 -080044#include "dex_file_annotations.h"
Andreas Gampee6377462017-01-20 17:37:50 -080045#include "events-inl.h"
Alex Light40528472017-03-28 09:07:36 -070046#include "fixed_up_dex_file.h"
Andreas Gampe691051b2017-02-09 09:15:24 -080047#include "gc/heap.h"
48#include "gc_root.h"
Andreas Gampee6377462017-01-20 17:37:50 -080049#include "handle.h"
50#include "jni_env_ext-inl.h"
Andreas Gampeac587272017-01-05 15:21:34 -080051#include "jni_internal.h"
Alex Light440b5d92017-01-24 15:32:25 -080052#include "mirror/array-inl.h"
53#include "mirror/class-inl.h"
54#include "mirror/class_ext.h"
Andreas Gampe0eb36432017-02-15 18:36:14 -080055#include "mirror/object_array-inl.h"
Andreas Gampea67354b2017-02-10 16:18:30 -080056#include "mirror/object_reference.h"
57#include "mirror/object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080058#include "mirror/object-refvisitor-inl.h"
Andreas Gampe52784ac2017-02-13 18:10:09 -080059#include "mirror/reference.h"
Alex Light6a656312017-03-29 17:18:00 -070060#include "primitive.h"
61#include "reflection.h"
Andreas Gampe70f16392017-01-16 14:20:10 -080062#include "runtime.h"
Andreas Gampee6377462017-01-20 17:37:50 -080063#include "runtime_callbacks.h"
64#include "ScopedLocalRef.h"
Andreas Gampee492ae32016-10-28 19:34:57 -070065#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070066#include "thread-current-inl.h"
Andreas Gampee6377462017-01-20 17:37:50 -080067#include "thread_list.h"
Alex Lighteb98b082017-01-25 13:02:32 -080068#include "ti_class_loader.h"
Alex Lightd8ce4e72017-02-27 10:52:29 -080069#include "ti_phase.h"
Alex Light440b5d92017-01-24 15:32:25 -080070#include "ti_redefine.h"
71#include "utils.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070072#include "well_known_classes.h"
Andreas Gampee492ae32016-10-28 19:34:57 -070073
74namespace openjdkjvmti {
75
Alex Light440b5d92017-01-24 15:32:25 -080076using android::base::StringPrintf;
77
78static std::unique_ptr<const art::DexFile> MakeSingleDexFile(art::Thread* self,
79 const char* descriptor,
80 const std::string& orig_location,
81 jint final_len,
82 const unsigned char* final_dex_data)
83 REQUIRES_SHARED(art::Locks::mutator_lock_) {
84 // Make the mmap
85 std::string error_msg;
Alex Lightb7354d52017-03-30 15:17:01 -070086 art::ArraySlice<const unsigned char> final_data(final_dex_data, final_len);
Alex Light440b5d92017-01-24 15:32:25 -080087 std::unique_ptr<art::MemMap> map(Redefiner::MoveDataToMemMap(orig_location,
Alex Lightb7354d52017-03-30 15:17:01 -070088 final_data,
Alex Light440b5d92017-01-24 15:32:25 -080089 &error_msg));
90 if (map.get() == nullptr) {
91 LOG(WARNING) << "Unable to allocate mmap for redefined dex file! Error was: " << error_msg;
92 self->ThrowOutOfMemoryError(StringPrintf(
93 "Unable to allocate dex file for transformation of %s", descriptor).c_str());
94 return nullptr;
95 }
96
97 // Make a dex-file
98 if (map->Size() < sizeof(art::DexFile::Header)) {
99 LOG(WARNING) << "Could not read dex file header because dex_data was too short";
100 art::ThrowClassFormatError(nullptr,
101 "Unable to read transformed dex file of %s",
102 descriptor);
103 return nullptr;
104 }
105 uint32_t checksum = reinterpret_cast<const art::DexFile::Header*>(map->Begin())->checksum_;
Andreas Gampef45d61c2017-06-07 10:29:33 -0700106 std::string map_name = map->GetName();
107 std::unique_ptr<const art::DexFile> dex_file(art::DexFile::Open(map_name,
Alex Light440b5d92017-01-24 15:32:25 -0800108 checksum,
109 std::move(map),
110 /*verify*/true,
111 /*verify_checksum*/true,
112 &error_msg));
113 if (dex_file.get() == nullptr) {
114 LOG(WARNING) << "Unable to load modified dex file for " << descriptor << ": " << error_msg;
115 art::ThrowClassFormatError(nullptr,
116 "Unable to read transformed dex file of %s because %s",
117 descriptor,
118 error_msg.c_str());
119 return nullptr;
120 }
121 if (dex_file->NumClassDefs() != 1) {
122 LOG(WARNING) << "Dex file contains more than 1 class_def. Ignoring.";
123 // TODO Throw some other sort of error here maybe?
124 art::ThrowClassFormatError(
125 nullptr,
126 "Unable to use transformed dex file of %s because it contained too many classes",
127 descriptor);
128 return nullptr;
129 }
130 return dex_file;
131}
132
Alex Light28b6e7e2017-05-22 16:05:59 -0700133// A deleter that acts like the jvmtiEnv->Deallocate so that asan does not get tripped up.
134// TODO We should everything use the actual jvmtiEnv->Allocate/Deallocate functions once we can
135// figure out which env to use.
136template <typename T>
137class FakeJvmtiDeleter {
138 public:
139 FakeJvmtiDeleter() {}
140
141 FakeJvmtiDeleter(FakeJvmtiDeleter&) = default;
142 FakeJvmtiDeleter(FakeJvmtiDeleter&&) = default;
143 FakeJvmtiDeleter& operator=(const FakeJvmtiDeleter&) = default;
144
145 template <typename U> void operator()(const U* ptr) const {
146 if (ptr != nullptr) {
147 free(const_cast<U*>(ptr));
148 }
149 }
150};
151
Andreas Gampee6377462017-01-20 17:37:50 -0800152struct ClassCallback : public art::ClassLoadCallback {
Alex Light440b5d92017-01-24 15:32:25 -0800153 void ClassPreDefine(const char* descriptor,
154 art::Handle<art::mirror::Class> klass,
155 art::Handle<art::mirror::ClassLoader> class_loader,
156 const art::DexFile& initial_dex_file,
157 const art::DexFile::ClassDef& initial_class_def ATTRIBUTE_UNUSED,
158 /*out*/art::DexFile const** final_dex_file,
159 /*out*/art::DexFile::ClassDef const** final_class_def)
160 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
161 bool is_enabled =
162 event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassFileLoadHookRetransformable) ||
163 event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassFileLoadHookNonRetransformable);
164 if (!is_enabled) {
165 return;
166 }
167 if (descriptor[0] != 'L') {
168 // It is a primitive or array. Just return
169 return;
170 }
Alex Lightd8ce4e72017-02-27 10:52:29 -0800171 jvmtiPhase phase = PhaseUtil::GetPhaseUnchecked();
172 if (UNLIKELY(phase != JVMTI_PHASE_START && phase != JVMTI_PHASE_LIVE)) {
173 // We want to wait until we are at least in the START phase so that all WellKnownClasses and
174 // mirror classes have been initialized and loaded. The runtime relies on these classes having
175 // specific fields and methods present. Since PreDefine hooks don't need to abide by this
176 // restriction we will simply not send the event for these classes.
177 LOG(WARNING) << "Ignoring load of class <" << descriptor << "> as it is being loaded during "
178 << "runtime initialization.";
179 return;
180 }
181
182 // Strip the 'L' and ';' from the descriptor
Alex Light28027122017-01-26 17:21:51 -0800183 std::string name(std::string(descriptor).substr(1, strlen(descriptor) - 2));
Alex Light440b5d92017-01-24 15:32:25 -0800184
185 art::Thread* self = art::Thread::Current();
186 art::JNIEnvExt* env = self->GetJniEnv();
187 ScopedLocalRef<jobject> loader(
188 env, class_loader.IsNull() ? nullptr : env->AddLocalReference<jobject>(class_loader.Get()));
Alex Light40528472017-03-28 09:07:36 -0700189 std::unique_ptr<FixedUpDexFile> dex_file_copy(FixedUpDexFile::Create(initial_dex_file));
190
Alex Light440b5d92017-01-24 15:32:25 -0800191 // Go back to native.
192 art::ScopedThreadSuspension sts(self, art::ThreadState::kNative);
193 // Call all Non-retransformable agents.
194 jint post_no_redefine_len = 0;
195 unsigned char* post_no_redefine_dex_data = nullptr;
Alex Light28b6e7e2017-05-22 16:05:59 -0700196 std::unique_ptr<const unsigned char, FakeJvmtiDeleter<const unsigned char>>
197 post_no_redefine_unique_ptr(nullptr, FakeJvmtiDeleter<const unsigned char>());
Alex Light440b5d92017-01-24 15:32:25 -0800198 event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
199 self,
200 static_cast<JNIEnv*>(env),
201 static_cast<jclass>(nullptr), // The class doesn't really exist yet so send null.
202 loader.get(),
203 name.c_str(),
204 static_cast<jobject>(nullptr), // Android doesn't seem to have protection domains
Alex Light40528472017-03-28 09:07:36 -0700205 static_cast<jint>(dex_file_copy->Size()),
206 static_cast<const unsigned char*>(dex_file_copy->Begin()),
Alex Light440b5d92017-01-24 15:32:25 -0800207 static_cast<jint*>(&post_no_redefine_len),
208 static_cast<unsigned char**>(&post_no_redefine_dex_data));
209 if (post_no_redefine_dex_data == nullptr) {
210 DCHECK_EQ(post_no_redefine_len, 0);
Alex Light40528472017-03-28 09:07:36 -0700211 post_no_redefine_dex_data = const_cast<unsigned char*>(dex_file_copy->Begin());
212 post_no_redefine_len = dex_file_copy->Size();
Alex Light440b5d92017-01-24 15:32:25 -0800213 } else {
Alex Light28b6e7e2017-05-22 16:05:59 -0700214 post_no_redefine_unique_ptr =
215 std::unique_ptr<const unsigned char, FakeJvmtiDeleter<const unsigned char>>(
216 post_no_redefine_dex_data, FakeJvmtiDeleter<const unsigned char>());
Alex Light440b5d92017-01-24 15:32:25 -0800217 DCHECK_GT(post_no_redefine_len, 0);
218 }
219 // Call all retransformable agents.
220 jint final_len = 0;
221 unsigned char* final_dex_data = nullptr;
Alex Light28b6e7e2017-05-22 16:05:59 -0700222 std::unique_ptr<const unsigned char, FakeJvmtiDeleter<const unsigned char>>
223 final_dex_unique_ptr(nullptr, FakeJvmtiDeleter<const unsigned char>());
Alex Light440b5d92017-01-24 15:32:25 -0800224 event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
225 self,
226 static_cast<JNIEnv*>(env),
227 static_cast<jclass>(nullptr), // The class doesn't really exist yet so send null.
228 loader.get(),
229 name.c_str(),
230 static_cast<jobject>(nullptr), // Android doesn't seem to have protection domains
231 static_cast<jint>(post_no_redefine_len),
232 static_cast<const unsigned char*>(post_no_redefine_dex_data),
233 static_cast<jint*>(&final_len),
234 static_cast<unsigned char**>(&final_dex_data));
235 if (final_dex_data == nullptr) {
236 DCHECK_EQ(final_len, 0);
237 final_dex_data = post_no_redefine_dex_data;
238 final_len = post_no_redefine_len;
239 } else {
Alex Light28b6e7e2017-05-22 16:05:59 -0700240 final_dex_unique_ptr =
241 std::unique_ptr<const unsigned char, FakeJvmtiDeleter<const unsigned char>>(
242 final_dex_data, FakeJvmtiDeleter<const unsigned char>());
Alex Light440b5d92017-01-24 15:32:25 -0800243 DCHECK_GT(final_len, 0);
244 }
245
Alex Light40528472017-03-28 09:07:36 -0700246 if (final_dex_data != dex_file_copy->Begin()) {
Alex Light440b5d92017-01-24 15:32:25 -0800247 LOG(WARNING) << "Changing class " << descriptor;
248 art::ScopedObjectAccess soa(self);
249 art::StackHandleScope<2> hs(self);
250 // Save the results of all the non-retransformable agents.
251 // First allocate the ClassExt
252 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->EnsureExtDataPresent(self)));
253 // Make sure we have a ClassExt. This is fine even though we are a temporary since it will
254 // get copied.
255 if (ext.IsNull()) {
256 // We will just return failure if we fail to allocate
257 LOG(WARNING) << "Could not allocate ext-data for class '" << descriptor << "'. "
258 << "Aborting transformation since we will be unable to store it.";
259 self->AssertPendingOOMException();
260 return;
261 }
262
263 // Allocate the byte array to store the dex file bytes in.
Alex Light6a656312017-03-29 17:18:00 -0700264 art::MutableHandle<art::mirror::Object> arr(hs.NewHandle<art::mirror::Object>(nullptr));
265 if (post_no_redefine_dex_data == dex_file_copy->Begin() && name != "java/lang/Long") {
266 // we didn't have any non-retransformable agents. We can just cache a pointer to the
267 // initial_dex_file. It will be kept live by the class_loader.
268 jlong dex_ptr = reinterpret_cast<uintptr_t>(&initial_dex_file);
269 art::JValue val;
270 val.SetJ(dex_ptr);
271 arr.Assign(art::BoxPrimitive(art::Primitive::kPrimLong, val));
272 } else {
273 arr.Assign(art::mirror::ByteArray::AllocateAndFill(
274 self,
275 reinterpret_cast<const signed char*>(post_no_redefine_dex_data),
276 post_no_redefine_len));
277 }
Alex Light440b5d92017-01-24 15:32:25 -0800278 if (arr.IsNull()) {
Alex Light6a656312017-03-29 17:18:00 -0700279 LOG(WARNING) << "Unable to allocate memory for initial dex-file. Aborting transformation";
Alex Light440b5d92017-01-24 15:32:25 -0800280 self->AssertPendingOOMException();
281 return;
282 }
283
284 std::unique_ptr<const art::DexFile> dex_file(MakeSingleDexFile(self,
285 descriptor,
286 initial_dex_file.GetLocation(),
287 final_len,
288 final_dex_data));
289 if (dex_file.get() == nullptr) {
290 return;
291 }
292
Alex Lighteb98b082017-01-25 13:02:32 -0800293 // TODO Check Redefined dex file for all invariants.
Alex Light440b5d92017-01-24 15:32:25 -0800294 LOG(WARNING) << "Dex file created by class-definition time transformation of "
295 << descriptor << " is not checked for all retransformation invariants.";
Alex Lighteb98b082017-01-25 13:02:32 -0800296
297 if (!ClassLoaderHelper::AddToClassLoader(self, class_loader, dex_file.get())) {
298 LOG(ERROR) << "Unable to add " << descriptor << " to class loader!";
299 return;
300 }
301
Alex Light440b5d92017-01-24 15:32:25 -0800302 // Actually set the ClassExt's original bytes once we have actually succeeded.
Alex Light2f814aa2017-03-24 15:21:34 +0000303 ext->SetOriginalDexFile(arr.Get());
Alex Light440b5d92017-01-24 15:32:25 -0800304 // Set the return values
305 *final_class_def = &dex_file->GetClassDef(0);
306 *final_dex_file = dex_file.release();
307 }
308 }
309
Andreas Gampee6377462017-01-20 17:37:50 -0800310 void ClassLoad(art::Handle<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
311 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassLoad)) {
312 art::Thread* thread = art::Thread::Current();
313 ScopedLocalRef<jclass> jklass(thread->GetJniEnv(),
314 thread->GetJniEnv()->AddLocalReference<jclass>(klass.Get()));
Andreas Gampe983c1752017-01-23 19:46:56 -0800315 ScopedLocalRef<jthread> thread_jni(
316 thread->GetJniEnv(), thread->GetJniEnv()->AddLocalReference<jthread>(thread->GetPeer()));
Andreas Gampee6377462017-01-20 17:37:50 -0800317 {
318 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -0800319 event_handler->DispatchEvent<ArtJvmtiEvent::kClassLoad>(
320 thread,
321 static_cast<JNIEnv*>(thread->GetJniEnv()),
322 thread_jni.get(),
323 jklass.get());
Andreas Gampee6377462017-01-20 17:37:50 -0800324 }
Andreas Gampe691051b2017-02-09 09:15:24 -0800325 if (klass->IsTemp()) {
326 AddTempClass(thread, jklass.get());
327 }
Andreas Gampee6377462017-01-20 17:37:50 -0800328 }
329 }
330
Andreas Gampe691051b2017-02-09 09:15:24 -0800331 void ClassPrepare(art::Handle<art::mirror::Class> temp_klass,
Andreas Gampee6377462017-01-20 17:37:50 -0800332 art::Handle<art::mirror::Class> klass)
333 REQUIRES_SHARED(art::Locks::mutator_lock_) {
334 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassPrepare)) {
335 art::Thread* thread = art::Thread::Current();
Andreas Gampe691051b2017-02-09 09:15:24 -0800336 if (temp_klass.Get() != klass.Get()) {
337 DCHECK(temp_klass->IsTemp());
338 DCHECK(temp_klass->IsRetired());
339 HandleTempClass(thread, temp_klass, klass);
340 }
Andreas Gampee6377462017-01-20 17:37:50 -0800341 ScopedLocalRef<jclass> jklass(thread->GetJniEnv(),
342 thread->GetJniEnv()->AddLocalReference<jclass>(klass.Get()));
Andreas Gampe983c1752017-01-23 19:46:56 -0800343 ScopedLocalRef<jthread> thread_jni(
344 thread->GetJniEnv(), thread->GetJniEnv()->AddLocalReference<jthread>(thread->GetPeer()));
Andreas Gampee6377462017-01-20 17:37:50 -0800345 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -0800346 event_handler->DispatchEvent<ArtJvmtiEvent::kClassPrepare>(
347 thread,
348 static_cast<JNIEnv*>(thread->GetJniEnv()),
349 thread_jni.get(),
350 jklass.get());
Andreas Gampee6377462017-01-20 17:37:50 -0800351 }
352 }
353
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800354 // To support parallel class-loading, we need to perform some locking dances here. Namely,
355 // the fixup stage must not be holding the temp_classes lock when it fixes up the system
356 // (as that requires suspending all mutators).
357
Andreas Gampee6377462017-01-20 17:37:50 -0800358 void AddTempClass(art::Thread* self, jclass klass) {
359 std::unique_lock<std::mutex> mu(temp_classes_lock);
Andreas Gampe691051b2017-02-09 09:15:24 -0800360 jclass global_klass = reinterpret_cast<jclass>(self->GetJniEnv()->NewGlobalRef(klass));
361 temp_classes.push_back(global_klass);
Andreas Gampee6377462017-01-20 17:37:50 -0800362 }
363
Andreas Gampe691051b2017-02-09 09:15:24 -0800364 void HandleTempClass(art::Thread* self,
365 art::Handle<art::mirror::Class> temp_klass,
Andreas Gampee6377462017-01-20 17:37:50 -0800366 art::Handle<art::mirror::Class> klass)
367 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800368 bool requires_fixup = false;
369 {
370 std::unique_lock<std::mutex> mu(temp_classes_lock);
371 if (temp_classes.empty()) {
372 return;
Andreas Gampee6377462017-01-20 17:37:50 -0800373 }
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800374
375 for (auto it = temp_classes.begin(); it != temp_classes.end(); ++it) {
376 if (temp_klass.Get() == art::ObjPtr<art::mirror::Class>::DownCast(self->DecodeJObject(*it))) {
377 self->GetJniEnv()->DeleteGlobalRef(*it);
378 temp_classes.erase(it);
379 requires_fixup = true;
380 break;
381 }
382 }
383 }
384 if (requires_fixup) {
385 FixupTempClass(self, temp_klass, klass);
Andreas Gampee6377462017-01-20 17:37:50 -0800386 }
387 }
388
Andreas Gampe691051b2017-02-09 09:15:24 -0800389 void FixupTempClass(art::Thread* self,
390 art::Handle<art::mirror::Class> temp_klass,
391 art::Handle<art::mirror::Class> klass)
Andreas Gampee6377462017-01-20 17:37:50 -0800392 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe691051b2017-02-09 09:15:24 -0800393 // Suspend everything.
394 art::gc::Heap* heap = art::Runtime::Current()->GetHeap();
395 if (heap->IsGcConcurrentAndMoving()) {
396 // Need to take a heap dump while GC isn't running. See the
397 // comment in Heap::VisitObjects().
398 heap->IncrementDisableMovingGC(self);
399 }
400 {
401 art::ScopedThreadSuspension sts(self, art::kWaitingForVisitObjects);
402 art::ScopedSuspendAll ssa("FixupTempClass");
403
404 art::mirror::Class* input = temp_klass.Get();
405 art::mirror::Class* output = klass.Get();
406
407 FixupGlobalReferenceTables(input, output);
Andreas Gampe94dda932017-02-09 18:19:21 -0800408 FixupLocalReferenceTables(self, input, output);
Andreas Gampea67354b2017-02-10 16:18:30 -0800409 FixupHeap(input, output);
Andreas Gampe691051b2017-02-09 09:15:24 -0800410 }
411 if (heap->IsGcConcurrentAndMoving()) {
412 heap->DecrementDisableMovingGC(self);
413 }
414 }
415
Andreas Gampe94dda932017-02-09 18:19:21 -0800416 class RootUpdater : public art::RootVisitor {
417 public:
418 RootUpdater(const art::mirror::Class* input, art::mirror::Class* output)
419 : input_(input), output_(output) {}
420
421 void VisitRoots(art::mirror::Object*** roots,
422 size_t count,
423 const art::RootInfo& info ATTRIBUTE_UNUSED)
424 OVERRIDE {
425 for (size_t i = 0; i != count; ++i) {
426 if (*roots[i] == input_) {
427 *roots[i] = output_;
428 }
429 }
430 }
431
432 void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
433 size_t count,
434 const art::RootInfo& info ATTRIBUTE_UNUSED)
435 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
436 for (size_t i = 0; i != count; ++i) {
437 if (roots[i]->AsMirrorPtr() == input_) {
438 roots[i]->Assign(output_);
439 }
440 }
441 }
442
443 private:
444 const art::mirror::Class* input_;
445 art::mirror::Class* output_;
446 };
447
Andreas Gampea67354b2017-02-10 16:18:30 -0800448 void FixupGlobalReferenceTables(art::mirror::Class* input, art::mirror::Class* output)
Andreas Gampe691051b2017-02-09 09:15:24 -0800449 REQUIRES(art::Locks::mutator_lock_) {
450 art::JavaVMExt* java_vm = art::Runtime::Current()->GetJavaVM();
451
452 // Fix up the global table with a root visitor.
Andreas Gampe94dda932017-02-09 18:19:21 -0800453 RootUpdater global_update(input, output);
Andreas Gampe691051b2017-02-09 09:15:24 -0800454 java_vm->VisitRoots(&global_update);
455
456 class WeakGlobalUpdate : public art::IsMarkedVisitor {
457 public:
458 WeakGlobalUpdate(art::mirror::Class* root_input, art::mirror::Class* root_output)
459 : input_(root_input), output_(root_output) {}
460
461 art::mirror::Object* IsMarked(art::mirror::Object* obj) OVERRIDE {
462 if (obj == input_) {
463 return output_;
464 }
465 return obj;
466 }
467
468 private:
469 const art::mirror::Class* input_;
470 art::mirror::Class* output_;
471 };
472 WeakGlobalUpdate weak_global_update(input, output);
473 java_vm->SweepJniWeakGlobals(&weak_global_update);
Andreas Gampee6377462017-01-20 17:37:50 -0800474 }
475
Andreas Gampe94dda932017-02-09 18:19:21 -0800476 void FixupLocalReferenceTables(art::Thread* self,
477 art::mirror::Class* input,
478 art::mirror::Class* output)
479 REQUIRES(art::Locks::mutator_lock_) {
480 class LocalUpdate {
481 public:
482 LocalUpdate(const art::mirror::Class* root_input, art::mirror::Class* root_output)
483 : input_(root_input), output_(root_output) {}
484
485 static void Callback(art::Thread* t, void* arg) REQUIRES(art::Locks::mutator_lock_) {
486 LocalUpdate* local = reinterpret_cast<LocalUpdate*>(arg);
487
488 // Fix up the local table with a root visitor.
489 RootUpdater local_update(local->input_, local->output_);
490 t->GetJniEnv()->locals.VisitRoots(
491 &local_update, art::RootInfo(art::kRootJNILocal, t->GetThreadId()));
492 }
493
494 private:
495 const art::mirror::Class* input_;
496 art::mirror::Class* output_;
497 };
498 LocalUpdate local_upd(input, output);
499 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
500 art::Runtime::Current()->GetThreadList()->ForEach(LocalUpdate::Callback, &local_upd);
501 }
502
Andreas Gampea67354b2017-02-10 16:18:30 -0800503 void FixupHeap(art::mirror::Class* input, art::mirror::Class* output)
504 REQUIRES(art::Locks::mutator_lock_) {
505 class HeapFixupVisitor {
506 public:
507 HeapFixupVisitor(const art::mirror::Class* root_input, art::mirror::Class* root_output)
508 : input_(root_input), output_(root_output) {}
509
510 void operator()(art::mirror::Object* src,
511 art::MemberOffset field_offset,
512 bool is_static ATTRIBUTE_UNUSED) const
513 REQUIRES_SHARED(art::Locks::mutator_lock_) {
514 art::mirror::HeapReference<art::mirror::Object>* trg =
515 src->GetFieldObjectReferenceAddr(field_offset);
516 if (trg->AsMirrorPtr() == input_) {
517 DCHECK_NE(field_offset.Uint32Value(), 0u); // This shouldn't be the class field of
518 // an object.
519 trg->Assign(output_);
520 }
521 }
522
Andreas Gampe52784ac2017-02-13 18:10:09 -0800523 void operator()(art::ObjPtr<art::mirror::Class> klass ATTRIBUTE_UNUSED,
524 art::ObjPtr<art::mirror::Reference> reference) const
525 REQUIRES_SHARED(art::Locks::mutator_lock_) {
526 art::mirror::Object* val = reference->GetReferent();
527 if (val == input_) {
528 reference->SetReferent<false>(output_);
529 }
530 }
531
Andreas Gampea67354b2017-02-10 16:18:30 -0800532 void VisitRoot(art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED)
Andreas Gampe52784ac2017-02-13 18:10:09 -0800533 const {
Andreas Gampea67354b2017-02-10 16:18:30 -0800534 LOG(FATAL) << "Unreachable";
535 }
536
537 void VisitRootIfNonNull(
538 art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED) const {
539 LOG(FATAL) << "Unreachable";
540 }
541
542 static void AllObjectsCallback(art::mirror::Object* obj, void* arg)
543 REQUIRES_SHARED(art::Locks::mutator_lock_) {
544 HeapFixupVisitor* hfv = reinterpret_cast<HeapFixupVisitor*>(arg);
545
546 // Visit references, not native roots.
Andreas Gampe52784ac2017-02-13 18:10:09 -0800547 obj->VisitReferences<false>(*hfv, *hfv);
Andreas Gampea67354b2017-02-10 16:18:30 -0800548 }
549
550 private:
551 const art::mirror::Class* input_;
552 art::mirror::Class* output_;
553 };
554 HeapFixupVisitor hfv(input, output);
555 art::Runtime::Current()->GetHeap()->VisitObjectsPaused(HeapFixupVisitor::AllObjectsCallback,
556 &hfv);
557 }
558
Andreas Gampee6377462017-01-20 17:37:50 -0800559 // A set of all the temp classes we have handed out. We have to fix up references to these.
560 // For simplicity, we store the temp classes as JNI global references in a vector. Normally a
561 // Prepare event will closely follow, so the vector should be small.
562 std::mutex temp_classes_lock;
563 std::vector<jclass> temp_classes;
564
565 EventHandler* event_handler = nullptr;
566};
567
568ClassCallback gClassCallback;
569
570void ClassUtil::Register(EventHandler* handler) {
571 gClassCallback.event_handler = handler;
572 art::ScopedThreadStateChange stsc(art::Thread::Current(),
573 art::ThreadState::kWaitingForDebuggerToAttach);
574 art::ScopedSuspendAll ssa("Add load callback");
575 art::Runtime::Current()->GetRuntimeCallbacks()->AddClassLoadCallback(&gClassCallback);
576}
577
578void ClassUtil::Unregister() {
579 art::ScopedThreadStateChange stsc(art::Thread::Current(),
580 art::ThreadState::kWaitingForDebuggerToAttach);
581 art::ScopedSuspendAll ssa("Remove thread callback");
582 art::Runtime* runtime = art::Runtime::Current();
583 runtime->GetRuntimeCallbacks()->RemoveClassLoadCallback(&gClassCallback);
584}
585
Andreas Gampeac587272017-01-05 15:21:34 -0800586jvmtiError ClassUtil::GetClassFields(jvmtiEnv* env,
587 jclass jklass,
588 jint* field_count_ptr,
589 jfieldID** fields_ptr) {
590 art::ScopedObjectAccess soa(art::Thread::Current());
591 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
592 if (klass == nullptr) {
593 return ERR(INVALID_CLASS);
594 }
595
596 if (field_count_ptr == nullptr || fields_ptr == nullptr) {
597 return ERR(NULL_POINTER);
598 }
599
Andreas Gampeac587272017-01-05 15:21:34 -0800600 art::IterationRange<art::StrideIterator<art::ArtField>> ifields = klass->GetIFields();
601 art::IterationRange<art::StrideIterator<art::ArtField>> sfields = klass->GetSFields();
602 size_t array_size = klass->NumInstanceFields() + klass->NumStaticFields();
603
604 unsigned char* out_ptr;
605 jvmtiError allocError = env->Allocate(array_size * sizeof(jfieldID), &out_ptr);
606 if (allocError != ERR(NONE)) {
607 return allocError;
608 }
609 jfieldID* field_array = reinterpret_cast<jfieldID*>(out_ptr);
610
611 size_t array_idx = 0;
612 for (art::ArtField& field : sfields) {
613 field_array[array_idx] = art::jni::EncodeArtField(&field);
614 ++array_idx;
615 }
616 for (art::ArtField& field : ifields) {
617 field_array[array_idx] = art::jni::EncodeArtField(&field);
618 ++array_idx;
619 }
620
621 *field_count_ptr = static_cast<jint>(array_size);
622 *fields_ptr = field_array;
623
624 return ERR(NONE);
625}
626
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800627jvmtiError ClassUtil::GetClassMethods(jvmtiEnv* env,
628 jclass jklass,
629 jint* method_count_ptr,
630 jmethodID** methods_ptr) {
631 art::ScopedObjectAccess soa(art::Thread::Current());
632 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
633 if (klass == nullptr) {
634 return ERR(INVALID_CLASS);
635 }
636
637 if (method_count_ptr == nullptr || methods_ptr == nullptr) {
638 return ERR(NULL_POINTER);
639 }
640
641 size_t array_size = klass->NumDeclaredVirtualMethods() + klass->NumDirectMethods();
642 unsigned char* out_ptr;
643 jvmtiError allocError = env->Allocate(array_size * sizeof(jmethodID), &out_ptr);
644 if (allocError != ERR(NONE)) {
645 return allocError;
646 }
647 jmethodID* method_array = reinterpret_cast<jmethodID*>(out_ptr);
648
649 if (art::kIsDebugBuild) {
650 size_t count = 0;
651 for (auto& m ATTRIBUTE_UNUSED : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
652 count++;
653 }
654 CHECK_EQ(count, klass->NumDirectMethods() + klass->NumDeclaredVirtualMethods());
655 }
656
657 size_t array_idx = 0;
658 for (auto& m : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
659 method_array[array_idx] = art::jni::EncodeArtMethod(&m);
660 ++array_idx;
661 }
662
663 *method_count_ptr = static_cast<jint>(array_size);
664 *methods_ptr = method_array;
665
666 return ERR(NONE);
667}
668
Andreas Gampe8b07e472017-01-06 14:20:39 -0800669jvmtiError ClassUtil::GetImplementedInterfaces(jvmtiEnv* env,
670 jclass jklass,
671 jint* interface_count_ptr,
672 jclass** interfaces_ptr) {
673 art::ScopedObjectAccess soa(art::Thread::Current());
674 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
675 if (klass == nullptr) {
676 return ERR(INVALID_CLASS);
677 }
678
679 if (interface_count_ptr == nullptr || interfaces_ptr == nullptr) {
680 return ERR(NULL_POINTER);
681 }
682
683 // Need to handle array specifically. Arrays implement Serializable and Cloneable, but the
684 // spec says these should not be reported.
685 if (klass->IsArrayClass()) {
686 *interface_count_ptr = 0;
687 *interfaces_ptr = nullptr; // TODO: Should we allocate a dummy here?
688 return ERR(NONE);
689 }
690
691 size_t array_size = klass->NumDirectInterfaces();
692 unsigned char* out_ptr;
693 jvmtiError allocError = env->Allocate(array_size * sizeof(jclass), &out_ptr);
694 if (allocError != ERR(NONE)) {
695 return allocError;
696 }
697 jclass* interface_array = reinterpret_cast<jclass*>(out_ptr);
698
699 art::StackHandleScope<1> hs(soa.Self());
700 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass));
701
702 for (uint32_t idx = 0; idx != array_size; ++idx) {
703 art::ObjPtr<art::mirror::Class> inf_klass =
704 art::mirror::Class::ResolveDirectInterface(soa.Self(), h_klass, idx);
705 if (inf_klass == nullptr) {
706 soa.Self()->ClearException();
707 env->Deallocate(out_ptr);
708 // TODO: What is the right error code here?
709 return ERR(INTERNAL);
710 }
711 interface_array[idx] = soa.AddLocalReference<jclass>(inf_klass);
712 }
713
714 *interface_count_ptr = static_cast<jint>(array_size);
715 *interfaces_ptr = interface_array;
716
717 return ERR(NONE);
718}
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800719
Andreas Gampee492ae32016-10-28 19:34:57 -0700720jvmtiError ClassUtil::GetClassSignature(jvmtiEnv* env,
721 jclass jklass,
722 char** signature_ptr,
723 char** generic_ptr) {
724 art::ScopedObjectAccess soa(art::Thread::Current());
725 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
726 if (klass == nullptr) {
727 return ERR(INVALID_CLASS);
728 }
729
Andreas Gampe54711412017-02-21 12:41:43 -0800730 JvmtiUniquePtr<char[]> sig_copy;
Andreas Gampee492ae32016-10-28 19:34:57 -0700731 if (signature_ptr != nullptr) {
732 std::string storage;
733 const char* descriptor = klass->GetDescriptor(&storage);
734
Andreas Gampe54711412017-02-21 12:41:43 -0800735 jvmtiError ret;
736 sig_copy = CopyString(env, descriptor, &ret);
737 if (sig_copy == nullptr) {
Andreas Gampee492ae32016-10-28 19:34:57 -0700738 return ret;
739 }
Andreas Gampe54711412017-02-21 12:41:43 -0800740 *signature_ptr = sig_copy.get();
Andreas Gampee492ae32016-10-28 19:34:57 -0700741 }
742
Andreas Gampee6377462017-01-20 17:37:50 -0800743 if (generic_ptr != nullptr) {
744 *generic_ptr = nullptr;
Andreas Gampe0eb36432017-02-15 18:36:14 -0800745 if (!klass->IsProxyClass() && klass->GetDexCache() != nullptr) {
746 art::StackHandleScope<1> hs(soa.Self());
747 art::Handle<art::mirror::Class> h_klass = hs.NewHandle(klass);
748 art::mirror::ObjectArray<art::mirror::String>* str_array =
749 art::annotations::GetSignatureAnnotationForClass(h_klass);
750 if (str_array != nullptr) {
751 std::ostringstream oss;
752 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
753 oss << str_array->Get(i)->ToModifiedUtf8();
754 }
755 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800756 jvmtiError ret;
757 JvmtiUniquePtr<char[]> copy = CopyString(env, output_string.c_str(), &ret);
758 if (copy == nullptr) {
Andreas Gampe0eb36432017-02-15 18:36:14 -0800759 return ret;
760 }
Andreas Gampe54711412017-02-21 12:41:43 -0800761 *generic_ptr = copy.release();
Andreas Gampe0eb36432017-02-15 18:36:14 -0800762 } else if (soa.Self()->IsExceptionPending()) {
763 // TODO: Should we report an error here?
764 soa.Self()->ClearException();
765 }
766 }
Andreas Gampee6377462017-01-20 17:37:50 -0800767 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700768
769 // Everything is fine, release the buffers.
770 sig_copy.release();
771
772 return ERR(NONE);
773}
774
Andreas Gampeff9d2092017-01-06 09:12:49 -0800775jvmtiError ClassUtil::GetClassStatus(jvmtiEnv* env ATTRIBUTE_UNUSED,
776 jclass jklass,
777 jint* status_ptr) {
778 art::ScopedObjectAccess soa(art::Thread::Current());
779 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
780 if (klass == nullptr) {
781 return ERR(INVALID_CLASS);
782 }
783
784 if (status_ptr == nullptr) {
785 return ERR(NULL_POINTER);
786 }
787
788 if (klass->IsArrayClass()) {
789 *status_ptr = JVMTI_CLASS_STATUS_ARRAY;
790 } else if (klass->IsPrimitive()) {
791 *status_ptr = JVMTI_CLASS_STATUS_PRIMITIVE;
792 } else {
793 *status_ptr = JVMTI_CLASS_STATUS_VERIFIED; // All loaded classes are structurally verified.
794 // This is finicky. If there's an error, we'll say it wasn't prepared.
795 if (klass->IsResolved()) {
796 *status_ptr |= JVMTI_CLASS_STATUS_PREPARED;
797 }
798 if (klass->IsInitialized()) {
799 *status_ptr |= JVMTI_CLASS_STATUS_INITIALIZED;
800 }
801 // Technically the class may be erroneous for other reasons, but we do not have enough info.
802 if (klass->IsErroneous()) {
803 *status_ptr |= JVMTI_CLASS_STATUS_ERROR;
804 }
805 }
806
807 return ERR(NONE);
808}
809
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800810template <typename T>
811static jvmtiError ClassIsT(jclass jklass, T test, jboolean* is_t_ptr) {
812 art::ScopedObjectAccess soa(art::Thread::Current());
813 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
814 if (klass == nullptr) {
815 return ERR(INVALID_CLASS);
816 }
817
818 if (is_t_ptr == nullptr) {
819 return ERR(NULL_POINTER);
820 }
821
822 *is_t_ptr = test(klass) ? JNI_TRUE : JNI_FALSE;
823 return ERR(NONE);
824}
825
826jvmtiError ClassUtil::IsInterface(jvmtiEnv* env ATTRIBUTE_UNUSED,
827 jclass jklass,
828 jboolean* is_interface_ptr) {
829 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
830 return klass->IsInterface();
831 };
832 return ClassIsT(jklass, test, is_interface_ptr);
833}
834
835jvmtiError ClassUtil::IsArrayClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
836 jclass jklass,
837 jboolean* is_array_class_ptr) {
838 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
839 return klass->IsArrayClass();
840 };
841 return ClassIsT(jklass, test, is_array_class_ptr);
842}
843
Andreas Gampe64013e52017-01-06 13:07:19 -0800844// Keep this in sync with Class.getModifiers().
845static uint32_t ClassGetModifiers(art::Thread* self, art::ObjPtr<art::mirror::Class> klass)
846 REQUIRES_SHARED(art::Locks::mutator_lock_) {
847 if (klass->IsArrayClass()) {
848 uint32_t component_modifiers = ClassGetModifiers(self, klass->GetComponentType());
849 if ((component_modifiers & art::kAccInterface) != 0) {
850 component_modifiers &= ~(art::kAccInterface | art::kAccStatic);
851 }
852 return art::kAccAbstract | art::kAccFinal | component_modifiers;
853 }
854
855 uint32_t modifiers = klass->GetAccessFlags() & art::kAccJavaFlagsMask;
856
857 art::StackHandleScope<1> hs(self);
858 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass));
859 return art::mirror::Class::GetInnerClassFlags(h_klass, modifiers);
860}
861
862jvmtiError ClassUtil::GetClassModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
863 jclass jklass,
864 jint* modifiers_ptr) {
865 art::ScopedObjectAccess soa(art::Thread::Current());
866 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
867 if (klass == nullptr) {
868 return ERR(INVALID_CLASS);
869 }
870
871 if (modifiers_ptr == nullptr) {
872 return ERR(NULL_POINTER);
873 }
874
875 *modifiers_ptr = ClassGetModifiers(soa.Self(), klass);
876
877 return ERR(NONE);
878}
879
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800880jvmtiError ClassUtil::GetClassLoader(jvmtiEnv* env ATTRIBUTE_UNUSED,
881 jclass jklass,
882 jobject* classloader_ptr) {
883 art::ScopedObjectAccess soa(art::Thread::Current());
884 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
885 if (klass == nullptr) {
886 return ERR(INVALID_CLASS);
887 }
888
889 if (classloader_ptr == nullptr) {
890 return ERR(NULL_POINTER);
891 }
892
893 *classloader_ptr = soa.AddLocalReference<jobject>(klass->GetClassLoader());
894
895 return ERR(NONE);
896}
897
Andreas Gampe70f16392017-01-16 14:20:10 -0800898jvmtiError ClassUtil::GetClassLoaderClasses(jvmtiEnv* env,
899 jobject initiating_loader,
900 jint* class_count_ptr,
901 jclass** classes_ptr) {
902 UNUSED(env, initiating_loader, class_count_ptr, classes_ptr);
903
904 if (class_count_ptr == nullptr || classes_ptr == nullptr) {
905 return ERR(NULL_POINTER);
906 }
907 art::Thread* self = art::Thread::Current();
908 if (!self->GetJniEnv()->IsInstanceOf(initiating_loader,
909 art::WellKnownClasses::java_lang_ClassLoader)) {
910 return ERR(ILLEGAL_ARGUMENT);
911 }
912 if (self->GetJniEnv()->IsInstanceOf(initiating_loader,
913 art::WellKnownClasses::java_lang_BootClassLoader)) {
914 // Need to use null for the BootClassLoader.
915 initiating_loader = nullptr;
916 }
917
918 art::ScopedObjectAccess soa(self);
919 art::ObjPtr<art::mirror::ClassLoader> class_loader =
920 soa.Decode<art::mirror::ClassLoader>(initiating_loader);
921
922 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
923
924 art::ReaderMutexLock mu(self, *art::Locks::classlinker_classes_lock_);
925
926 art::ClassTable* class_table = class_linker->ClassTableForClassLoader(class_loader);
927 if (class_table == nullptr) {
928 // Nothing loaded.
929 *class_count_ptr = 0;
930 *classes_ptr = nullptr;
931 return ERR(NONE);
932 }
933
934 struct ClassTableCount {
935 bool operator()(art::ObjPtr<art::mirror::Class> klass) {
936 DCHECK(klass != nullptr);
937 ++count;
938 return true;
939 }
940
941 size_t count = 0;
942 };
943 ClassTableCount ctc;
944 class_table->Visit(ctc);
945
946 if (ctc.count == 0) {
947 // Nothing loaded.
948 *class_count_ptr = 0;
949 *classes_ptr = nullptr;
950 return ERR(NONE);
951 }
952
953 unsigned char* data;
954 jvmtiError data_result = env->Allocate(ctc.count * sizeof(jclass), &data);
955 if (data_result != ERR(NONE)) {
956 return data_result;
957 }
958 jclass* class_array = reinterpret_cast<jclass*>(data);
959
960 struct ClassTableFill {
961 bool operator()(art::ObjPtr<art::mirror::Class> klass)
962 REQUIRES_SHARED(art::Locks::mutator_lock_) {
963 DCHECK(klass != nullptr);
964 DCHECK_LT(count, ctc_ref.count);
965 local_class_array[count++] = soa_ptr->AddLocalReference<jclass>(klass);
966 return true;
967 }
968
969 jclass* local_class_array;
970 const ClassTableCount& ctc_ref;
971 art::ScopedObjectAccess* soa_ptr;
972 size_t count;
973 };
974 ClassTableFill ctf = { class_array, ctc, &soa, 0 };
975 class_table->Visit(ctf);
976 DCHECK_EQ(ctc.count, ctf.count);
977
978 *class_count_ptr = ctc.count;
979 *classes_ptr = class_array;
980
981 return ERR(NONE);
982}
983
Andreas Gampe812a2442017-01-19 22:04:46 -0800984jvmtiError ClassUtil::GetClassVersionNumbers(jvmtiEnv* env ATTRIBUTE_UNUSED,
985 jclass jklass,
986 jint* minor_version_ptr,
987 jint* major_version_ptr) {
988 art::ScopedObjectAccess soa(art::Thread::Current());
989 if (jklass == nullptr) {
990 return ERR(INVALID_CLASS);
991 }
992 art::ObjPtr<art::mirror::Object> jklass_obj = soa.Decode<art::mirror::Object>(jklass);
993 if (!jklass_obj->IsClass()) {
994 return ERR(INVALID_CLASS);
995 }
996 art::ObjPtr<art::mirror::Class> klass = jklass_obj->AsClass();
997 if (klass->IsPrimitive() || klass->IsArrayClass()) {
998 return ERR(INVALID_CLASS);
999 }
1000
1001 if (minor_version_ptr == nullptr || major_version_ptr == nullptr) {
1002 return ERR(NULL_POINTER);
1003 }
1004
1005 // Note: proxies will show the dex file version of java.lang.reflect.Proxy, as that is
1006 // what their dex cache copies from.
1007 uint32_t version = klass->GetDexFile().GetHeader().GetVersion();
1008
1009 *major_version_ptr = static_cast<jint>(version);
1010 *minor_version_ptr = 0;
1011
1012 return ERR(NONE);
1013}
1014
Andreas Gampee492ae32016-10-28 19:34:57 -07001015} // namespace openjdkjvmti