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