blob: dd90a71240c4da5b860c9ca03437300e042d0096 [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"
66#include "thread-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_;
106 std::unique_ptr<const art::DexFile> dex_file(art::DexFile::Open(map->GetName(),
107 checksum,
108 std::move(map),
109 /*verify*/true,
110 /*verify_checksum*/true,
111 &error_msg));
112 if (dex_file.get() == nullptr) {
113 LOG(WARNING) << "Unable to load modified dex file for " << descriptor << ": " << error_msg;
114 art::ThrowClassFormatError(nullptr,
115 "Unable to read transformed dex file of %s because %s",
116 descriptor,
117 error_msg.c_str());
118 return nullptr;
119 }
120 if (dex_file->NumClassDefs() != 1) {
121 LOG(WARNING) << "Dex file contains more than 1 class_def. Ignoring.";
122 // TODO Throw some other sort of error here maybe?
123 art::ThrowClassFormatError(
124 nullptr,
125 "Unable to use transformed dex file of %s because it contained too many classes",
126 descriptor);
127 return nullptr;
128 }
129 return dex_file;
130}
131
Alex Light28b6e7e2017-05-22 16:05:59 -0700132// A deleter that acts like the jvmtiEnv->Deallocate so that asan does not get tripped up.
133// TODO We should everything use the actual jvmtiEnv->Allocate/Deallocate functions once we can
134// figure out which env to use.
135template <typename T>
136class FakeJvmtiDeleter {
137 public:
138 FakeJvmtiDeleter() {}
139
140 FakeJvmtiDeleter(FakeJvmtiDeleter&) = default;
141 FakeJvmtiDeleter(FakeJvmtiDeleter&&) = default;
142 FakeJvmtiDeleter& operator=(const FakeJvmtiDeleter&) = default;
143
144 template <typename U> void operator()(const U* ptr) const {
145 if (ptr != nullptr) {
146 free(const_cast<U*>(ptr));
147 }
148 }
149};
150
Andreas Gampee6377462017-01-20 17:37:50 -0800151struct ClassCallback : public art::ClassLoadCallback {
Alex Light440b5d92017-01-24 15:32:25 -0800152 void ClassPreDefine(const char* descriptor,
153 art::Handle<art::mirror::Class> klass,
154 art::Handle<art::mirror::ClassLoader> class_loader,
155 const art::DexFile& initial_dex_file,
156 const art::DexFile::ClassDef& initial_class_def ATTRIBUTE_UNUSED,
157 /*out*/art::DexFile const** final_dex_file,
158 /*out*/art::DexFile::ClassDef const** final_class_def)
159 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
160 bool is_enabled =
161 event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassFileLoadHookRetransformable) ||
162 event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassFileLoadHookNonRetransformable);
163 if (!is_enabled) {
164 return;
165 }
166 if (descriptor[0] != 'L') {
167 // It is a primitive or array. Just return
168 return;
169 }
Alex Lightd8ce4e72017-02-27 10:52:29 -0800170 jvmtiPhase phase = PhaseUtil::GetPhaseUnchecked();
171 if (UNLIKELY(phase != JVMTI_PHASE_START && phase != JVMTI_PHASE_LIVE)) {
172 // We want to wait until we are at least in the START phase so that all WellKnownClasses and
173 // mirror classes have been initialized and loaded. The runtime relies on these classes having
174 // specific fields and methods present. Since PreDefine hooks don't need to abide by this
175 // restriction we will simply not send the event for these classes.
176 LOG(WARNING) << "Ignoring load of class <" << descriptor << "> as it is being loaded during "
177 << "runtime initialization.";
178 return;
179 }
180
181 // Strip the 'L' and ';' from the descriptor
Alex Light28027122017-01-26 17:21:51 -0800182 std::string name(std::string(descriptor).substr(1, strlen(descriptor) - 2));
Alex Light440b5d92017-01-24 15:32:25 -0800183
184 art::Thread* self = art::Thread::Current();
185 art::JNIEnvExt* env = self->GetJniEnv();
186 ScopedLocalRef<jobject> loader(
187 env, class_loader.IsNull() ? nullptr : env->AddLocalReference<jobject>(class_loader.Get()));
Alex Light40528472017-03-28 09:07:36 -0700188 std::unique_ptr<FixedUpDexFile> dex_file_copy(FixedUpDexFile::Create(initial_dex_file));
189
Alex Light440b5d92017-01-24 15:32:25 -0800190 // Go back to native.
191 art::ScopedThreadSuspension sts(self, art::ThreadState::kNative);
192 // Call all Non-retransformable agents.
193 jint post_no_redefine_len = 0;
194 unsigned char* post_no_redefine_dex_data = nullptr;
Alex Light28b6e7e2017-05-22 16:05:59 -0700195 std::unique_ptr<const unsigned char, FakeJvmtiDeleter<const unsigned char>>
196 post_no_redefine_unique_ptr(nullptr, FakeJvmtiDeleter<const unsigned char>());
Alex Light440b5d92017-01-24 15:32:25 -0800197 event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
198 self,
199 static_cast<JNIEnv*>(env),
200 static_cast<jclass>(nullptr), // The class doesn't really exist yet so send null.
201 loader.get(),
202 name.c_str(),
203 static_cast<jobject>(nullptr), // Android doesn't seem to have protection domains
Alex Light40528472017-03-28 09:07:36 -0700204 static_cast<jint>(dex_file_copy->Size()),
205 static_cast<const unsigned char*>(dex_file_copy->Begin()),
Alex Light440b5d92017-01-24 15:32:25 -0800206 static_cast<jint*>(&post_no_redefine_len),
207 static_cast<unsigned char**>(&post_no_redefine_dex_data));
208 if (post_no_redefine_dex_data == nullptr) {
209 DCHECK_EQ(post_no_redefine_len, 0);
Alex Light40528472017-03-28 09:07:36 -0700210 post_no_redefine_dex_data = const_cast<unsigned char*>(dex_file_copy->Begin());
211 post_no_redefine_len = dex_file_copy->Size();
Alex Light440b5d92017-01-24 15:32:25 -0800212 } else {
Alex Light28b6e7e2017-05-22 16:05:59 -0700213 post_no_redefine_unique_ptr =
214 std::unique_ptr<const unsigned char, FakeJvmtiDeleter<const unsigned char>>(
215 post_no_redefine_dex_data, FakeJvmtiDeleter<const unsigned char>());
Alex Light440b5d92017-01-24 15:32:25 -0800216 DCHECK_GT(post_no_redefine_len, 0);
217 }
218 // Call all retransformable agents.
219 jint final_len = 0;
220 unsigned char* final_dex_data = nullptr;
Alex Light28b6e7e2017-05-22 16:05:59 -0700221 std::unique_ptr<const unsigned char, FakeJvmtiDeleter<const unsigned char>>
222 final_dex_unique_ptr(nullptr, FakeJvmtiDeleter<const unsigned char>());
Alex Light440b5d92017-01-24 15:32:25 -0800223 event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
224 self,
225 static_cast<JNIEnv*>(env),
226 static_cast<jclass>(nullptr), // The class doesn't really exist yet so send null.
227 loader.get(),
228 name.c_str(),
229 static_cast<jobject>(nullptr), // Android doesn't seem to have protection domains
230 static_cast<jint>(post_no_redefine_len),
231 static_cast<const unsigned char*>(post_no_redefine_dex_data),
232 static_cast<jint*>(&final_len),
233 static_cast<unsigned char**>(&final_dex_data));
234 if (final_dex_data == nullptr) {
235 DCHECK_EQ(final_len, 0);
236 final_dex_data = post_no_redefine_dex_data;
237 final_len = post_no_redefine_len;
238 } else {
Alex Light28b6e7e2017-05-22 16:05:59 -0700239 final_dex_unique_ptr =
240 std::unique_ptr<const unsigned char, FakeJvmtiDeleter<const unsigned char>>(
241 final_dex_data, FakeJvmtiDeleter<const unsigned char>());
Alex Light440b5d92017-01-24 15:32:25 -0800242 DCHECK_GT(final_len, 0);
243 }
244
Alex Light40528472017-03-28 09:07:36 -0700245 if (final_dex_data != dex_file_copy->Begin()) {
Alex Light440b5d92017-01-24 15:32:25 -0800246 LOG(WARNING) << "Changing class " << descriptor;
247 art::ScopedObjectAccess soa(self);
248 art::StackHandleScope<2> hs(self);
249 // Save the results of all the non-retransformable agents.
250 // First allocate the ClassExt
251 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->EnsureExtDataPresent(self)));
252 // Make sure we have a ClassExt. This is fine even though we are a temporary since it will
253 // get copied.
254 if (ext.IsNull()) {
255 // We will just return failure if we fail to allocate
256 LOG(WARNING) << "Could not allocate ext-data for class '" << descriptor << "'. "
257 << "Aborting transformation since we will be unable to store it.";
258 self->AssertPendingOOMException();
259 return;
260 }
261
262 // Allocate the byte array to store the dex file bytes in.
Alex Light6a656312017-03-29 17:18:00 -0700263 art::MutableHandle<art::mirror::Object> arr(hs.NewHandle<art::mirror::Object>(nullptr));
264 if (post_no_redefine_dex_data == dex_file_copy->Begin() && name != "java/lang/Long") {
265 // we didn't have any non-retransformable agents. We can just cache a pointer to the
266 // initial_dex_file. It will be kept live by the class_loader.
267 jlong dex_ptr = reinterpret_cast<uintptr_t>(&initial_dex_file);
268 art::JValue val;
269 val.SetJ(dex_ptr);
270 arr.Assign(art::BoxPrimitive(art::Primitive::kPrimLong, val));
271 } else {
272 arr.Assign(art::mirror::ByteArray::AllocateAndFill(
273 self,
274 reinterpret_cast<const signed char*>(post_no_redefine_dex_data),
275 post_no_redefine_len));
276 }
Alex Light440b5d92017-01-24 15:32:25 -0800277 if (arr.IsNull()) {
Alex Light6a656312017-03-29 17:18:00 -0700278 LOG(WARNING) << "Unable to allocate memory for initial dex-file. Aborting transformation";
Alex Light440b5d92017-01-24 15:32:25 -0800279 self->AssertPendingOOMException();
280 return;
281 }
282
283 std::unique_ptr<const art::DexFile> dex_file(MakeSingleDexFile(self,
284 descriptor,
285 initial_dex_file.GetLocation(),
286 final_len,
287 final_dex_data));
288 if (dex_file.get() == nullptr) {
289 return;
290 }
291
Alex Lighteb98b082017-01-25 13:02:32 -0800292 // TODO Check Redefined dex file for all invariants.
Alex Light440b5d92017-01-24 15:32:25 -0800293 LOG(WARNING) << "Dex file created by class-definition time transformation of "
294 << descriptor << " is not checked for all retransformation invariants.";
Alex Lighteb98b082017-01-25 13:02:32 -0800295
296 if (!ClassLoaderHelper::AddToClassLoader(self, class_loader, dex_file.get())) {
297 LOG(ERROR) << "Unable to add " << descriptor << " to class loader!";
298 return;
299 }
300
Alex Light440b5d92017-01-24 15:32:25 -0800301 // Actually set the ClassExt's original bytes once we have actually succeeded.
Alex Light2f814aa2017-03-24 15:21:34 +0000302 ext->SetOriginalDexFile(arr.Get());
Alex Light440b5d92017-01-24 15:32:25 -0800303 // Set the return values
304 *final_class_def = &dex_file->GetClassDef(0);
305 *final_dex_file = dex_file.release();
306 }
307 }
308
Andreas Gampee6377462017-01-20 17:37:50 -0800309 void ClassLoad(art::Handle<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
310 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassLoad)) {
311 art::Thread* thread = art::Thread::Current();
312 ScopedLocalRef<jclass> jklass(thread->GetJniEnv(),
313 thread->GetJniEnv()->AddLocalReference<jclass>(klass.Get()));
Andreas Gampe983c1752017-01-23 19:46:56 -0800314 ScopedLocalRef<jthread> thread_jni(
315 thread->GetJniEnv(), thread->GetJniEnv()->AddLocalReference<jthread>(thread->GetPeer()));
Andreas Gampee6377462017-01-20 17:37:50 -0800316 {
317 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -0800318 event_handler->DispatchEvent<ArtJvmtiEvent::kClassLoad>(
319 thread,
320 static_cast<JNIEnv*>(thread->GetJniEnv()),
321 thread_jni.get(),
322 jklass.get());
Andreas Gampee6377462017-01-20 17:37:50 -0800323 }
Andreas Gampe691051b2017-02-09 09:15:24 -0800324 if (klass->IsTemp()) {
325 AddTempClass(thread, jklass.get());
326 }
Andreas Gampee6377462017-01-20 17:37:50 -0800327 }
328 }
329
Andreas Gampe691051b2017-02-09 09:15:24 -0800330 void ClassPrepare(art::Handle<art::mirror::Class> temp_klass,
Andreas Gampee6377462017-01-20 17:37:50 -0800331 art::Handle<art::mirror::Class> klass)
332 REQUIRES_SHARED(art::Locks::mutator_lock_) {
333 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassPrepare)) {
334 art::Thread* thread = art::Thread::Current();
Andreas Gampe691051b2017-02-09 09:15:24 -0800335 if (temp_klass.Get() != klass.Get()) {
336 DCHECK(temp_klass->IsTemp());
337 DCHECK(temp_klass->IsRetired());
338 HandleTempClass(thread, temp_klass, klass);
339 }
Andreas Gampee6377462017-01-20 17:37:50 -0800340 ScopedLocalRef<jclass> jklass(thread->GetJniEnv(),
341 thread->GetJniEnv()->AddLocalReference<jclass>(klass.Get()));
Andreas Gampe983c1752017-01-23 19:46:56 -0800342 ScopedLocalRef<jthread> thread_jni(
343 thread->GetJniEnv(), thread->GetJniEnv()->AddLocalReference<jthread>(thread->GetPeer()));
Andreas Gampee6377462017-01-20 17:37:50 -0800344 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -0800345 event_handler->DispatchEvent<ArtJvmtiEvent::kClassPrepare>(
346 thread,
347 static_cast<JNIEnv*>(thread->GetJniEnv()),
348 thread_jni.get(),
349 jklass.get());
Andreas Gampee6377462017-01-20 17:37:50 -0800350 }
351 }
352
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800353 // To support parallel class-loading, we need to perform some locking dances here. Namely,
354 // the fixup stage must not be holding the temp_classes lock when it fixes up the system
355 // (as that requires suspending all mutators).
356
Andreas Gampee6377462017-01-20 17:37:50 -0800357 void AddTempClass(art::Thread* self, jclass klass) {
358 std::unique_lock<std::mutex> mu(temp_classes_lock);
Andreas Gampe691051b2017-02-09 09:15:24 -0800359 jclass global_klass = reinterpret_cast<jclass>(self->GetJniEnv()->NewGlobalRef(klass));
360 temp_classes.push_back(global_klass);
Andreas Gampee6377462017-01-20 17:37:50 -0800361 }
362
Andreas Gampe691051b2017-02-09 09:15:24 -0800363 void HandleTempClass(art::Thread* self,
364 art::Handle<art::mirror::Class> temp_klass,
Andreas Gampee6377462017-01-20 17:37:50 -0800365 art::Handle<art::mirror::Class> klass)
366 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800367 bool requires_fixup = false;
368 {
369 std::unique_lock<std::mutex> mu(temp_classes_lock);
370 if (temp_classes.empty()) {
371 return;
Andreas Gampee6377462017-01-20 17:37:50 -0800372 }
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800373
374 for (auto it = temp_classes.begin(); it != temp_classes.end(); ++it) {
375 if (temp_klass.Get() == art::ObjPtr<art::mirror::Class>::DownCast(self->DecodeJObject(*it))) {
376 self->GetJniEnv()->DeleteGlobalRef(*it);
377 temp_classes.erase(it);
378 requires_fixup = true;
379 break;
380 }
381 }
382 }
383 if (requires_fixup) {
384 FixupTempClass(self, temp_klass, klass);
Andreas Gampee6377462017-01-20 17:37:50 -0800385 }
386 }
387
Andreas Gampe691051b2017-02-09 09:15:24 -0800388 void FixupTempClass(art::Thread* self,
389 art::Handle<art::mirror::Class> temp_klass,
390 art::Handle<art::mirror::Class> klass)
Andreas Gampee6377462017-01-20 17:37:50 -0800391 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe691051b2017-02-09 09:15:24 -0800392 // Suspend everything.
393 art::gc::Heap* heap = art::Runtime::Current()->GetHeap();
394 if (heap->IsGcConcurrentAndMoving()) {
395 // Need to take a heap dump while GC isn't running. See the
396 // comment in Heap::VisitObjects().
397 heap->IncrementDisableMovingGC(self);
398 }
399 {
400 art::ScopedThreadSuspension sts(self, art::kWaitingForVisitObjects);
401 art::ScopedSuspendAll ssa("FixupTempClass");
402
403 art::mirror::Class* input = temp_klass.Get();
404 art::mirror::Class* output = klass.Get();
405
406 FixupGlobalReferenceTables(input, output);
Andreas Gampe94dda932017-02-09 18:19:21 -0800407 FixupLocalReferenceTables(self, input, output);
Andreas Gampea67354b2017-02-10 16:18:30 -0800408 FixupHeap(input, output);
Andreas Gampe691051b2017-02-09 09:15:24 -0800409 }
410 if (heap->IsGcConcurrentAndMoving()) {
411 heap->DecrementDisableMovingGC(self);
412 }
413 }
414
Andreas Gampe94dda932017-02-09 18:19:21 -0800415 class RootUpdater : public art::RootVisitor {
416 public:
417 RootUpdater(const art::mirror::Class* input, art::mirror::Class* output)
418 : input_(input), output_(output) {}
419
420 void VisitRoots(art::mirror::Object*** roots,
421 size_t count,
422 const art::RootInfo& info ATTRIBUTE_UNUSED)
423 OVERRIDE {
424 for (size_t i = 0; i != count; ++i) {
425 if (*roots[i] == input_) {
426 *roots[i] = output_;
427 }
428 }
429 }
430
431 void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
432 size_t count,
433 const art::RootInfo& info ATTRIBUTE_UNUSED)
434 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
435 for (size_t i = 0; i != count; ++i) {
436 if (roots[i]->AsMirrorPtr() == input_) {
437 roots[i]->Assign(output_);
438 }
439 }
440 }
441
442 private:
443 const art::mirror::Class* input_;
444 art::mirror::Class* output_;
445 };
446
Andreas Gampea67354b2017-02-10 16:18:30 -0800447 void FixupGlobalReferenceTables(art::mirror::Class* input, art::mirror::Class* output)
Andreas Gampe691051b2017-02-09 09:15:24 -0800448 REQUIRES(art::Locks::mutator_lock_) {
449 art::JavaVMExt* java_vm = art::Runtime::Current()->GetJavaVM();
450
451 // Fix up the global table with a root visitor.
Andreas Gampe94dda932017-02-09 18:19:21 -0800452 RootUpdater global_update(input, output);
Andreas Gampe691051b2017-02-09 09:15:24 -0800453 java_vm->VisitRoots(&global_update);
454
455 class WeakGlobalUpdate : public art::IsMarkedVisitor {
456 public:
457 WeakGlobalUpdate(art::mirror::Class* root_input, art::mirror::Class* root_output)
458 : input_(root_input), output_(root_output) {}
459
460 art::mirror::Object* IsMarked(art::mirror::Object* obj) OVERRIDE {
461 if (obj == input_) {
462 return output_;
463 }
464 return obj;
465 }
466
467 private:
468 const art::mirror::Class* input_;
469 art::mirror::Class* output_;
470 };
471 WeakGlobalUpdate weak_global_update(input, output);
472 java_vm->SweepJniWeakGlobals(&weak_global_update);
Andreas Gampee6377462017-01-20 17:37:50 -0800473 }
474
Andreas Gampe94dda932017-02-09 18:19:21 -0800475 void FixupLocalReferenceTables(art::Thread* self,
476 art::mirror::Class* input,
477 art::mirror::Class* output)
478 REQUIRES(art::Locks::mutator_lock_) {
479 class LocalUpdate {
480 public:
481 LocalUpdate(const art::mirror::Class* root_input, art::mirror::Class* root_output)
482 : input_(root_input), output_(root_output) {}
483
484 static void Callback(art::Thread* t, void* arg) REQUIRES(art::Locks::mutator_lock_) {
485 LocalUpdate* local = reinterpret_cast<LocalUpdate*>(arg);
486
487 // Fix up the local table with a root visitor.
488 RootUpdater local_update(local->input_, local->output_);
489 t->GetJniEnv()->locals.VisitRoots(
490 &local_update, art::RootInfo(art::kRootJNILocal, t->GetThreadId()));
491 }
492
493 private:
494 const art::mirror::Class* input_;
495 art::mirror::Class* output_;
496 };
497 LocalUpdate local_upd(input, output);
498 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
499 art::Runtime::Current()->GetThreadList()->ForEach(LocalUpdate::Callback, &local_upd);
500 }
501
Andreas Gampea67354b2017-02-10 16:18:30 -0800502 void FixupHeap(art::mirror::Class* input, art::mirror::Class* output)
503 REQUIRES(art::Locks::mutator_lock_) {
504 class HeapFixupVisitor {
505 public:
506 HeapFixupVisitor(const art::mirror::Class* root_input, art::mirror::Class* root_output)
507 : input_(root_input), output_(root_output) {}
508
509 void operator()(art::mirror::Object* src,
510 art::MemberOffset field_offset,
511 bool is_static ATTRIBUTE_UNUSED) const
512 REQUIRES_SHARED(art::Locks::mutator_lock_) {
513 art::mirror::HeapReference<art::mirror::Object>* trg =
514 src->GetFieldObjectReferenceAddr(field_offset);
515 if (trg->AsMirrorPtr() == input_) {
516 DCHECK_NE(field_offset.Uint32Value(), 0u); // This shouldn't be the class field of
517 // an object.
518 trg->Assign(output_);
519 }
520 }
521
Andreas Gampe52784ac2017-02-13 18:10:09 -0800522 void operator()(art::ObjPtr<art::mirror::Class> klass ATTRIBUTE_UNUSED,
523 art::ObjPtr<art::mirror::Reference> reference) const
524 REQUIRES_SHARED(art::Locks::mutator_lock_) {
525 art::mirror::Object* val = reference->GetReferent();
526 if (val == input_) {
527 reference->SetReferent<false>(output_);
528 }
529 }
530
Andreas Gampea67354b2017-02-10 16:18:30 -0800531 void VisitRoot(art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED)
Andreas Gampe52784ac2017-02-13 18:10:09 -0800532 const {
Andreas Gampea67354b2017-02-10 16:18:30 -0800533 LOG(FATAL) << "Unreachable";
534 }
535
536 void VisitRootIfNonNull(
537 art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED) const {
538 LOG(FATAL) << "Unreachable";
539 }
540
541 static void AllObjectsCallback(art::mirror::Object* obj, void* arg)
542 REQUIRES_SHARED(art::Locks::mutator_lock_) {
543 HeapFixupVisitor* hfv = reinterpret_cast<HeapFixupVisitor*>(arg);
544
545 // Visit references, not native roots.
Andreas Gampe52784ac2017-02-13 18:10:09 -0800546 obj->VisitReferences<false>(*hfv, *hfv);
Andreas Gampea67354b2017-02-10 16:18:30 -0800547 }
548
549 private:
550 const art::mirror::Class* input_;
551 art::mirror::Class* output_;
552 };
553 HeapFixupVisitor hfv(input, output);
554 art::Runtime::Current()->GetHeap()->VisitObjectsPaused(HeapFixupVisitor::AllObjectsCallback,
555 &hfv);
556 }
557
Andreas Gampee6377462017-01-20 17:37:50 -0800558 // A set of all the temp classes we have handed out. We have to fix up references to these.
559 // For simplicity, we store the temp classes as JNI global references in a vector. Normally a
560 // Prepare event will closely follow, so the vector should be small.
561 std::mutex temp_classes_lock;
562 std::vector<jclass> temp_classes;
563
564 EventHandler* event_handler = nullptr;
565};
566
567ClassCallback gClassCallback;
568
569void ClassUtil::Register(EventHandler* handler) {
570 gClassCallback.event_handler = handler;
571 art::ScopedThreadStateChange stsc(art::Thread::Current(),
572 art::ThreadState::kWaitingForDebuggerToAttach);
573 art::ScopedSuspendAll ssa("Add load callback");
574 art::Runtime::Current()->GetRuntimeCallbacks()->AddClassLoadCallback(&gClassCallback);
575}
576
577void ClassUtil::Unregister() {
578 art::ScopedThreadStateChange stsc(art::Thread::Current(),
579 art::ThreadState::kWaitingForDebuggerToAttach);
580 art::ScopedSuspendAll ssa("Remove thread callback");
581 art::Runtime* runtime = art::Runtime::Current();
582 runtime->GetRuntimeCallbacks()->RemoveClassLoadCallback(&gClassCallback);
583}
584
Andreas Gampeac587272017-01-05 15:21:34 -0800585jvmtiError ClassUtil::GetClassFields(jvmtiEnv* env,
586 jclass jklass,
587 jint* field_count_ptr,
588 jfieldID** fields_ptr) {
589 art::ScopedObjectAccess soa(art::Thread::Current());
590 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
591 if (klass == nullptr) {
592 return ERR(INVALID_CLASS);
593 }
594
595 if (field_count_ptr == nullptr || fields_ptr == nullptr) {
596 return ERR(NULL_POINTER);
597 }
598
Andreas Gampeac587272017-01-05 15:21:34 -0800599 art::IterationRange<art::StrideIterator<art::ArtField>> ifields = klass->GetIFields();
600 art::IterationRange<art::StrideIterator<art::ArtField>> sfields = klass->GetSFields();
601 size_t array_size = klass->NumInstanceFields() + klass->NumStaticFields();
602
603 unsigned char* out_ptr;
604 jvmtiError allocError = env->Allocate(array_size * sizeof(jfieldID), &out_ptr);
605 if (allocError != ERR(NONE)) {
606 return allocError;
607 }
608 jfieldID* field_array = reinterpret_cast<jfieldID*>(out_ptr);
609
610 size_t array_idx = 0;
611 for (art::ArtField& field : sfields) {
612 field_array[array_idx] = art::jni::EncodeArtField(&field);
613 ++array_idx;
614 }
615 for (art::ArtField& field : ifields) {
616 field_array[array_idx] = art::jni::EncodeArtField(&field);
617 ++array_idx;
618 }
619
620 *field_count_ptr = static_cast<jint>(array_size);
621 *fields_ptr = field_array;
622
623 return ERR(NONE);
624}
625
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800626jvmtiError ClassUtil::GetClassMethods(jvmtiEnv* env,
627 jclass jklass,
628 jint* method_count_ptr,
629 jmethodID** methods_ptr) {
630 art::ScopedObjectAccess soa(art::Thread::Current());
631 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
632 if (klass == nullptr) {
633 return ERR(INVALID_CLASS);
634 }
635
636 if (method_count_ptr == nullptr || methods_ptr == nullptr) {
637 return ERR(NULL_POINTER);
638 }
639
640 size_t array_size = klass->NumDeclaredVirtualMethods() + klass->NumDirectMethods();
641 unsigned char* out_ptr;
642 jvmtiError allocError = env->Allocate(array_size * sizeof(jmethodID), &out_ptr);
643 if (allocError != ERR(NONE)) {
644 return allocError;
645 }
646 jmethodID* method_array = reinterpret_cast<jmethodID*>(out_ptr);
647
648 if (art::kIsDebugBuild) {
649 size_t count = 0;
650 for (auto& m ATTRIBUTE_UNUSED : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
651 count++;
652 }
653 CHECK_EQ(count, klass->NumDirectMethods() + klass->NumDeclaredVirtualMethods());
654 }
655
656 size_t array_idx = 0;
657 for (auto& m : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
658 method_array[array_idx] = art::jni::EncodeArtMethod(&m);
659 ++array_idx;
660 }
661
662 *method_count_ptr = static_cast<jint>(array_size);
663 *methods_ptr = method_array;
664
665 return ERR(NONE);
666}
667
Andreas Gampe8b07e472017-01-06 14:20:39 -0800668jvmtiError ClassUtil::GetImplementedInterfaces(jvmtiEnv* env,
669 jclass jklass,
670 jint* interface_count_ptr,
671 jclass** interfaces_ptr) {
672 art::ScopedObjectAccess soa(art::Thread::Current());
673 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
674 if (klass == nullptr) {
675 return ERR(INVALID_CLASS);
676 }
677
678 if (interface_count_ptr == nullptr || interfaces_ptr == nullptr) {
679 return ERR(NULL_POINTER);
680 }
681
682 // Need to handle array specifically. Arrays implement Serializable and Cloneable, but the
683 // spec says these should not be reported.
684 if (klass->IsArrayClass()) {
685 *interface_count_ptr = 0;
686 *interfaces_ptr = nullptr; // TODO: Should we allocate a dummy here?
687 return ERR(NONE);
688 }
689
690 size_t array_size = klass->NumDirectInterfaces();
691 unsigned char* out_ptr;
692 jvmtiError allocError = env->Allocate(array_size * sizeof(jclass), &out_ptr);
693 if (allocError != ERR(NONE)) {
694 return allocError;
695 }
696 jclass* interface_array = reinterpret_cast<jclass*>(out_ptr);
697
698 art::StackHandleScope<1> hs(soa.Self());
699 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass));
700
701 for (uint32_t idx = 0; idx != array_size; ++idx) {
702 art::ObjPtr<art::mirror::Class> inf_klass =
703 art::mirror::Class::ResolveDirectInterface(soa.Self(), h_klass, idx);
704 if (inf_klass == nullptr) {
705 soa.Self()->ClearException();
706 env->Deallocate(out_ptr);
707 // TODO: What is the right error code here?
708 return ERR(INTERNAL);
709 }
710 interface_array[idx] = soa.AddLocalReference<jclass>(inf_klass);
711 }
712
713 *interface_count_ptr = static_cast<jint>(array_size);
714 *interfaces_ptr = interface_array;
715
716 return ERR(NONE);
717}
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800718
Andreas Gampee492ae32016-10-28 19:34:57 -0700719jvmtiError ClassUtil::GetClassSignature(jvmtiEnv* env,
720 jclass jklass,
721 char** signature_ptr,
722 char** generic_ptr) {
723 art::ScopedObjectAccess soa(art::Thread::Current());
724 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
725 if (klass == nullptr) {
726 return ERR(INVALID_CLASS);
727 }
728
Andreas Gampe54711412017-02-21 12:41:43 -0800729 JvmtiUniquePtr<char[]> sig_copy;
Andreas Gampee492ae32016-10-28 19:34:57 -0700730 if (signature_ptr != nullptr) {
731 std::string storage;
732 const char* descriptor = klass->GetDescriptor(&storage);
733
Andreas Gampe54711412017-02-21 12:41:43 -0800734 jvmtiError ret;
735 sig_copy = CopyString(env, descriptor, &ret);
736 if (sig_copy == nullptr) {
Andreas Gampee492ae32016-10-28 19:34:57 -0700737 return ret;
738 }
Andreas Gampe54711412017-02-21 12:41:43 -0800739 *signature_ptr = sig_copy.get();
Andreas Gampee492ae32016-10-28 19:34:57 -0700740 }
741
Andreas Gampee6377462017-01-20 17:37:50 -0800742 if (generic_ptr != nullptr) {
743 *generic_ptr = nullptr;
Andreas Gampe0eb36432017-02-15 18:36:14 -0800744 if (!klass->IsProxyClass() && klass->GetDexCache() != nullptr) {
745 art::StackHandleScope<1> hs(soa.Self());
746 art::Handle<art::mirror::Class> h_klass = hs.NewHandle(klass);
747 art::mirror::ObjectArray<art::mirror::String>* str_array =
748 art::annotations::GetSignatureAnnotationForClass(h_klass);
749 if (str_array != nullptr) {
750 std::ostringstream oss;
751 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
752 oss << str_array->Get(i)->ToModifiedUtf8();
753 }
754 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800755 jvmtiError ret;
756 JvmtiUniquePtr<char[]> copy = CopyString(env, output_string.c_str(), &ret);
757 if (copy == nullptr) {
Andreas Gampe0eb36432017-02-15 18:36:14 -0800758 return ret;
759 }
Andreas Gampe54711412017-02-21 12:41:43 -0800760 *generic_ptr = copy.release();
Andreas Gampe0eb36432017-02-15 18:36:14 -0800761 } else if (soa.Self()->IsExceptionPending()) {
762 // TODO: Should we report an error here?
763 soa.Self()->ClearException();
764 }
765 }
Andreas Gampee6377462017-01-20 17:37:50 -0800766 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700767
768 // Everything is fine, release the buffers.
769 sig_copy.release();
770
771 return ERR(NONE);
772}
773
Andreas Gampeff9d2092017-01-06 09:12:49 -0800774jvmtiError ClassUtil::GetClassStatus(jvmtiEnv* env ATTRIBUTE_UNUSED,
775 jclass jklass,
776 jint* status_ptr) {
777 art::ScopedObjectAccess soa(art::Thread::Current());
778 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
779 if (klass == nullptr) {
780 return ERR(INVALID_CLASS);
781 }
782
783 if (status_ptr == nullptr) {
784 return ERR(NULL_POINTER);
785 }
786
787 if (klass->IsArrayClass()) {
788 *status_ptr = JVMTI_CLASS_STATUS_ARRAY;
789 } else if (klass->IsPrimitive()) {
790 *status_ptr = JVMTI_CLASS_STATUS_PRIMITIVE;
791 } else {
792 *status_ptr = JVMTI_CLASS_STATUS_VERIFIED; // All loaded classes are structurally verified.
793 // This is finicky. If there's an error, we'll say it wasn't prepared.
794 if (klass->IsResolved()) {
795 *status_ptr |= JVMTI_CLASS_STATUS_PREPARED;
796 }
797 if (klass->IsInitialized()) {
798 *status_ptr |= JVMTI_CLASS_STATUS_INITIALIZED;
799 }
800 // Technically the class may be erroneous for other reasons, but we do not have enough info.
801 if (klass->IsErroneous()) {
802 *status_ptr |= JVMTI_CLASS_STATUS_ERROR;
803 }
804 }
805
806 return ERR(NONE);
807}
808
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800809template <typename T>
810static jvmtiError ClassIsT(jclass jklass, T test, jboolean* is_t_ptr) {
811 art::ScopedObjectAccess soa(art::Thread::Current());
812 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
813 if (klass == nullptr) {
814 return ERR(INVALID_CLASS);
815 }
816
817 if (is_t_ptr == nullptr) {
818 return ERR(NULL_POINTER);
819 }
820
821 *is_t_ptr = test(klass) ? JNI_TRUE : JNI_FALSE;
822 return ERR(NONE);
823}
824
825jvmtiError ClassUtil::IsInterface(jvmtiEnv* env ATTRIBUTE_UNUSED,
826 jclass jklass,
827 jboolean* is_interface_ptr) {
828 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
829 return klass->IsInterface();
830 };
831 return ClassIsT(jklass, test, is_interface_ptr);
832}
833
834jvmtiError ClassUtil::IsArrayClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
835 jclass jklass,
836 jboolean* is_array_class_ptr) {
837 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
838 return klass->IsArrayClass();
839 };
840 return ClassIsT(jklass, test, is_array_class_ptr);
841}
842
Andreas Gampe64013e52017-01-06 13:07:19 -0800843// Keep this in sync with Class.getModifiers().
844static uint32_t ClassGetModifiers(art::Thread* self, art::ObjPtr<art::mirror::Class> klass)
845 REQUIRES_SHARED(art::Locks::mutator_lock_) {
846 if (klass->IsArrayClass()) {
847 uint32_t component_modifiers = ClassGetModifiers(self, klass->GetComponentType());
848 if ((component_modifiers & art::kAccInterface) != 0) {
849 component_modifiers &= ~(art::kAccInterface | art::kAccStatic);
850 }
851 return art::kAccAbstract | art::kAccFinal | component_modifiers;
852 }
853
854 uint32_t modifiers = klass->GetAccessFlags() & art::kAccJavaFlagsMask;
855
856 art::StackHandleScope<1> hs(self);
857 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass));
858 return art::mirror::Class::GetInnerClassFlags(h_klass, modifiers);
859}
860
861jvmtiError ClassUtil::GetClassModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
862 jclass jklass,
863 jint* modifiers_ptr) {
864 art::ScopedObjectAccess soa(art::Thread::Current());
865 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
866 if (klass == nullptr) {
867 return ERR(INVALID_CLASS);
868 }
869
870 if (modifiers_ptr == nullptr) {
871 return ERR(NULL_POINTER);
872 }
873
874 *modifiers_ptr = ClassGetModifiers(soa.Self(), klass);
875
876 return ERR(NONE);
877}
878
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800879jvmtiError ClassUtil::GetClassLoader(jvmtiEnv* env ATTRIBUTE_UNUSED,
880 jclass jklass,
881 jobject* classloader_ptr) {
882 art::ScopedObjectAccess soa(art::Thread::Current());
883 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
884 if (klass == nullptr) {
885 return ERR(INVALID_CLASS);
886 }
887
888 if (classloader_ptr == nullptr) {
889 return ERR(NULL_POINTER);
890 }
891
892 *classloader_ptr = soa.AddLocalReference<jobject>(klass->GetClassLoader());
893
894 return ERR(NONE);
895}
896
Andreas Gampe70f16392017-01-16 14:20:10 -0800897jvmtiError ClassUtil::GetClassLoaderClasses(jvmtiEnv* env,
898 jobject initiating_loader,
899 jint* class_count_ptr,
900 jclass** classes_ptr) {
901 UNUSED(env, initiating_loader, class_count_ptr, classes_ptr);
902
903 if (class_count_ptr == nullptr || classes_ptr == nullptr) {
904 return ERR(NULL_POINTER);
905 }
906 art::Thread* self = art::Thread::Current();
907 if (!self->GetJniEnv()->IsInstanceOf(initiating_loader,
908 art::WellKnownClasses::java_lang_ClassLoader)) {
909 return ERR(ILLEGAL_ARGUMENT);
910 }
911 if (self->GetJniEnv()->IsInstanceOf(initiating_loader,
912 art::WellKnownClasses::java_lang_BootClassLoader)) {
913 // Need to use null for the BootClassLoader.
914 initiating_loader = nullptr;
915 }
916
917 art::ScopedObjectAccess soa(self);
918 art::ObjPtr<art::mirror::ClassLoader> class_loader =
919 soa.Decode<art::mirror::ClassLoader>(initiating_loader);
920
921 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
922
923 art::ReaderMutexLock mu(self, *art::Locks::classlinker_classes_lock_);
924
925 art::ClassTable* class_table = class_linker->ClassTableForClassLoader(class_loader);
926 if (class_table == nullptr) {
927 // Nothing loaded.
928 *class_count_ptr = 0;
929 *classes_ptr = nullptr;
930 return ERR(NONE);
931 }
932
933 struct ClassTableCount {
934 bool operator()(art::ObjPtr<art::mirror::Class> klass) {
935 DCHECK(klass != nullptr);
936 ++count;
937 return true;
938 }
939
940 size_t count = 0;
941 };
942 ClassTableCount ctc;
943 class_table->Visit(ctc);
944
945 if (ctc.count == 0) {
946 // Nothing loaded.
947 *class_count_ptr = 0;
948 *classes_ptr = nullptr;
949 return ERR(NONE);
950 }
951
952 unsigned char* data;
953 jvmtiError data_result = env->Allocate(ctc.count * sizeof(jclass), &data);
954 if (data_result != ERR(NONE)) {
955 return data_result;
956 }
957 jclass* class_array = reinterpret_cast<jclass*>(data);
958
959 struct ClassTableFill {
960 bool operator()(art::ObjPtr<art::mirror::Class> klass)
961 REQUIRES_SHARED(art::Locks::mutator_lock_) {
962 DCHECK(klass != nullptr);
963 DCHECK_LT(count, ctc_ref.count);
964 local_class_array[count++] = soa_ptr->AddLocalReference<jclass>(klass);
965 return true;
966 }
967
968 jclass* local_class_array;
969 const ClassTableCount& ctc_ref;
970 art::ScopedObjectAccess* soa_ptr;
971 size_t count;
972 };
973 ClassTableFill ctf = { class_array, ctc, &soa, 0 };
974 class_table->Visit(ctf);
975 DCHECK_EQ(ctc.count, ctf.count);
976
977 *class_count_ptr = ctc.count;
978 *classes_ptr = class_array;
979
980 return ERR(NONE);
981}
982
Andreas Gampe812a2442017-01-19 22:04:46 -0800983jvmtiError ClassUtil::GetClassVersionNumbers(jvmtiEnv* env ATTRIBUTE_UNUSED,
984 jclass jklass,
985 jint* minor_version_ptr,
986 jint* major_version_ptr) {
987 art::ScopedObjectAccess soa(art::Thread::Current());
988 if (jklass == nullptr) {
989 return ERR(INVALID_CLASS);
990 }
991 art::ObjPtr<art::mirror::Object> jklass_obj = soa.Decode<art::mirror::Object>(jklass);
992 if (!jklass_obj->IsClass()) {
993 return ERR(INVALID_CLASS);
994 }
995 art::ObjPtr<art::mirror::Class> klass = jklass_obj->AsClass();
996 if (klass->IsPrimitive() || klass->IsArrayClass()) {
997 return ERR(INVALID_CLASS);
998 }
999
1000 if (minor_version_ptr == nullptr || major_version_ptr == nullptr) {
1001 return ERR(NULL_POINTER);
1002 }
1003
1004 // Note: proxies will show the dex file version of java.lang.reflect.Proxy, as that is
1005 // what their dex cache copies from.
1006 uint32_t version = klass->GetDexFile().GetHeader().GetVersion();
1007
1008 *major_version_ptr = static_cast<jint>(version);
1009 *minor_version_ptr = 0;
1010
1011 return ERR(NONE);
1012}
1013
Andreas Gampee492ae32016-10-28 19:34:57 -07001014} // namespace openjdkjvmti