blob: c3a1acf32b268fe412d754311285c999ef601172 [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 Gampee492ae32016-10-28 19:34:57 -070072
73namespace openjdkjvmti {
74
Alex Light440b5d92017-01-24 15:32:25 -080075using android::base::StringPrintf;
76
77static std::unique_ptr<const art::DexFile> MakeSingleDexFile(art::Thread* self,
78 const char* descriptor,
79 const std::string& orig_location,
80 jint final_len,
81 const unsigned char* final_dex_data)
82 REQUIRES_SHARED(art::Locks::mutator_lock_) {
83 // Make the mmap
84 std::string error_msg;
Alex Lightb7354d52017-03-30 15:17:01 -070085 art::ArraySlice<const unsigned char> final_data(final_dex_data, final_len);
Alex Light440b5d92017-01-24 15:32:25 -080086 std::unique_ptr<art::MemMap> map(Redefiner::MoveDataToMemMap(orig_location,
Alex Lightb7354d52017-03-30 15:17:01 -070087 final_data,
Alex Light440b5d92017-01-24 15:32:25 -080088 &error_msg));
89 if (map.get() == nullptr) {
90 LOG(WARNING) << "Unable to allocate mmap for redefined dex file! Error was: " << error_msg;
91 self->ThrowOutOfMemoryError(StringPrintf(
92 "Unable to allocate dex file for transformation of %s", descriptor).c_str());
93 return nullptr;
94 }
95
96 // Make a dex-file
97 if (map->Size() < sizeof(art::DexFile::Header)) {
98 LOG(WARNING) << "Could not read dex file header because dex_data was too short";
99 art::ThrowClassFormatError(nullptr,
100 "Unable to read transformed dex file of %s",
101 descriptor);
102 return nullptr;
103 }
104 uint32_t checksum = reinterpret_cast<const art::DexFile::Header*>(map->Begin())->checksum_;
105 std::unique_ptr<const art::DexFile> dex_file(art::DexFile::Open(map->GetName(),
106 checksum,
107 std::move(map),
108 /*verify*/true,
109 /*verify_checksum*/true,
110 &error_msg));
111 if (dex_file.get() == nullptr) {
112 LOG(WARNING) << "Unable to load modified dex file for " << descriptor << ": " << error_msg;
113 art::ThrowClassFormatError(nullptr,
114 "Unable to read transformed dex file of %s because %s",
115 descriptor,
116 error_msg.c_str());
117 return nullptr;
118 }
119 if (dex_file->NumClassDefs() != 1) {
120 LOG(WARNING) << "Dex file contains more than 1 class_def. Ignoring.";
121 // TODO Throw some other sort of error here maybe?
122 art::ThrowClassFormatError(
123 nullptr,
124 "Unable to use transformed dex file of %s because it contained too many classes",
125 descriptor);
126 return nullptr;
127 }
128 return dex_file;
129}
130
Andreas Gampee6377462017-01-20 17:37:50 -0800131struct ClassCallback : public art::ClassLoadCallback {
Alex Light440b5d92017-01-24 15:32:25 -0800132 void ClassPreDefine(const char* descriptor,
133 art::Handle<art::mirror::Class> klass,
134 art::Handle<art::mirror::ClassLoader> class_loader,
135 const art::DexFile& initial_dex_file,
136 const art::DexFile::ClassDef& initial_class_def ATTRIBUTE_UNUSED,
137 /*out*/art::DexFile const** final_dex_file,
138 /*out*/art::DexFile::ClassDef const** final_class_def)
139 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
140 bool is_enabled =
141 event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassFileLoadHookRetransformable) ||
142 event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassFileLoadHookNonRetransformable);
143 if (!is_enabled) {
144 return;
145 }
146 if (descriptor[0] != 'L') {
147 // It is a primitive or array. Just return
148 return;
149 }
Alex Lightd8ce4e72017-02-27 10:52:29 -0800150 jvmtiPhase phase = PhaseUtil::GetPhaseUnchecked();
151 if (UNLIKELY(phase != JVMTI_PHASE_START && phase != JVMTI_PHASE_LIVE)) {
152 // We want to wait until we are at least in the START phase so that all WellKnownClasses and
153 // mirror classes have been initialized and loaded. The runtime relies on these classes having
154 // specific fields and methods present. Since PreDefine hooks don't need to abide by this
155 // restriction we will simply not send the event for these classes.
156 LOG(WARNING) << "Ignoring load of class <" << descriptor << "> as it is being loaded during "
157 << "runtime initialization.";
158 return;
159 }
160
161 // Strip the 'L' and ';' from the descriptor
Alex Light28027122017-01-26 17:21:51 -0800162 std::string name(std::string(descriptor).substr(1, strlen(descriptor) - 2));
Alex Light440b5d92017-01-24 15:32:25 -0800163
164 art::Thread* self = art::Thread::Current();
165 art::JNIEnvExt* env = self->GetJniEnv();
166 ScopedLocalRef<jobject> loader(
167 env, class_loader.IsNull() ? nullptr : env->AddLocalReference<jobject>(class_loader.Get()));
Alex Light40528472017-03-28 09:07:36 -0700168 std::unique_ptr<FixedUpDexFile> dex_file_copy(FixedUpDexFile::Create(initial_dex_file));
169
Alex Light440b5d92017-01-24 15:32:25 -0800170 // Go back to native.
171 art::ScopedThreadSuspension sts(self, art::ThreadState::kNative);
172 // Call all Non-retransformable agents.
173 jint post_no_redefine_len = 0;
174 unsigned char* post_no_redefine_dex_data = nullptr;
175 std::unique_ptr<const unsigned char> post_no_redefine_unique_ptr(nullptr);
176 event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
177 self,
178 static_cast<JNIEnv*>(env),
179 static_cast<jclass>(nullptr), // The class doesn't really exist yet so send null.
180 loader.get(),
181 name.c_str(),
182 static_cast<jobject>(nullptr), // Android doesn't seem to have protection domains
Alex Light40528472017-03-28 09:07:36 -0700183 static_cast<jint>(dex_file_copy->Size()),
184 static_cast<const unsigned char*>(dex_file_copy->Begin()),
Alex Light440b5d92017-01-24 15:32:25 -0800185 static_cast<jint*>(&post_no_redefine_len),
186 static_cast<unsigned char**>(&post_no_redefine_dex_data));
187 if (post_no_redefine_dex_data == nullptr) {
188 DCHECK_EQ(post_no_redefine_len, 0);
Alex Light40528472017-03-28 09:07:36 -0700189 post_no_redefine_dex_data = const_cast<unsigned char*>(dex_file_copy->Begin());
190 post_no_redefine_len = dex_file_copy->Size();
Alex Light440b5d92017-01-24 15:32:25 -0800191 } else {
192 post_no_redefine_unique_ptr = std::unique_ptr<const unsigned char>(post_no_redefine_dex_data);
193 DCHECK_GT(post_no_redefine_len, 0);
194 }
195 // Call all retransformable agents.
196 jint final_len = 0;
197 unsigned char* final_dex_data = nullptr;
198 std::unique_ptr<const unsigned char> final_dex_unique_ptr(nullptr);
199 event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
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
206 static_cast<jint>(post_no_redefine_len),
207 static_cast<const unsigned char*>(post_no_redefine_dex_data),
208 static_cast<jint*>(&final_len),
209 static_cast<unsigned char**>(&final_dex_data));
210 if (final_dex_data == nullptr) {
211 DCHECK_EQ(final_len, 0);
212 final_dex_data = post_no_redefine_dex_data;
213 final_len = post_no_redefine_len;
214 } else {
215 final_dex_unique_ptr = std::unique_ptr<const unsigned char>(final_dex_data);
216 DCHECK_GT(final_len, 0);
217 }
218
Alex Light40528472017-03-28 09:07:36 -0700219 if (final_dex_data != dex_file_copy->Begin()) {
Alex Light440b5d92017-01-24 15:32:25 -0800220 LOG(WARNING) << "Changing class " << descriptor;
221 art::ScopedObjectAccess soa(self);
222 art::StackHandleScope<2> hs(self);
223 // Save the results of all the non-retransformable agents.
224 // First allocate the ClassExt
225 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->EnsureExtDataPresent(self)));
226 // Make sure we have a ClassExt. This is fine even though we are a temporary since it will
227 // get copied.
228 if (ext.IsNull()) {
229 // We will just return failure if we fail to allocate
230 LOG(WARNING) << "Could not allocate ext-data for class '" << descriptor << "'. "
231 << "Aborting transformation since we will be unable to store it.";
232 self->AssertPendingOOMException();
233 return;
234 }
235
236 // Allocate the byte array to store the dex file bytes in.
Alex Light6a656312017-03-29 17:18:00 -0700237 art::MutableHandle<art::mirror::Object> arr(hs.NewHandle<art::mirror::Object>(nullptr));
238 if (post_no_redefine_dex_data == dex_file_copy->Begin() && name != "java/lang/Long") {
239 // we didn't have any non-retransformable agents. We can just cache a pointer to the
240 // initial_dex_file. It will be kept live by the class_loader.
241 jlong dex_ptr = reinterpret_cast<uintptr_t>(&initial_dex_file);
242 art::JValue val;
243 val.SetJ(dex_ptr);
244 arr.Assign(art::BoxPrimitive(art::Primitive::kPrimLong, val));
245 } else {
246 arr.Assign(art::mirror::ByteArray::AllocateAndFill(
247 self,
248 reinterpret_cast<const signed char*>(post_no_redefine_dex_data),
249 post_no_redefine_len));
250 }
Alex Light440b5d92017-01-24 15:32:25 -0800251 if (arr.IsNull()) {
Alex Light6a656312017-03-29 17:18:00 -0700252 LOG(WARNING) << "Unable to allocate memory for initial dex-file. Aborting transformation";
Alex Light440b5d92017-01-24 15:32:25 -0800253 self->AssertPendingOOMException();
254 return;
255 }
256
257 std::unique_ptr<const art::DexFile> dex_file(MakeSingleDexFile(self,
258 descriptor,
259 initial_dex_file.GetLocation(),
260 final_len,
261 final_dex_data));
262 if (dex_file.get() == nullptr) {
263 return;
264 }
265
Alex Lighteb98b082017-01-25 13:02:32 -0800266 // TODO Check Redefined dex file for all invariants.
Alex Light440b5d92017-01-24 15:32:25 -0800267 LOG(WARNING) << "Dex file created by class-definition time transformation of "
268 << descriptor << " is not checked for all retransformation invariants.";
Alex Lighteb98b082017-01-25 13:02:32 -0800269
270 if (!ClassLoaderHelper::AddToClassLoader(self, class_loader, dex_file.get())) {
271 LOG(ERROR) << "Unable to add " << descriptor << " to class loader!";
272 return;
273 }
274
Alex Light440b5d92017-01-24 15:32:25 -0800275 // Actually set the ClassExt's original bytes once we have actually succeeded.
Alex Light2f814aa2017-03-24 15:21:34 +0000276 ext->SetOriginalDexFile(arr.Get());
Alex Light440b5d92017-01-24 15:32:25 -0800277 // Set the return values
278 *final_class_def = &dex_file->GetClassDef(0);
279 *final_dex_file = dex_file.release();
280 }
281 }
282
Andreas Gampee6377462017-01-20 17:37:50 -0800283 void ClassLoad(art::Handle<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
284 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassLoad)) {
285 art::Thread* thread = art::Thread::Current();
286 ScopedLocalRef<jclass> jklass(thread->GetJniEnv(),
287 thread->GetJniEnv()->AddLocalReference<jclass>(klass.Get()));
Andreas Gampe983c1752017-01-23 19:46:56 -0800288 ScopedLocalRef<jthread> thread_jni(
289 thread->GetJniEnv(), thread->GetJniEnv()->AddLocalReference<jthread>(thread->GetPeer()));
Andreas Gampee6377462017-01-20 17:37:50 -0800290 {
291 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -0800292 event_handler->DispatchEvent<ArtJvmtiEvent::kClassLoad>(
293 thread,
294 static_cast<JNIEnv*>(thread->GetJniEnv()),
295 thread_jni.get(),
296 jklass.get());
Andreas Gampee6377462017-01-20 17:37:50 -0800297 }
Andreas Gampe691051b2017-02-09 09:15:24 -0800298 if (klass->IsTemp()) {
299 AddTempClass(thread, jklass.get());
300 }
Andreas Gampee6377462017-01-20 17:37:50 -0800301 }
302 }
303
Andreas Gampe691051b2017-02-09 09:15:24 -0800304 void ClassPrepare(art::Handle<art::mirror::Class> temp_klass,
Andreas Gampee6377462017-01-20 17:37:50 -0800305 art::Handle<art::mirror::Class> klass)
306 REQUIRES_SHARED(art::Locks::mutator_lock_) {
307 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kClassPrepare)) {
308 art::Thread* thread = art::Thread::Current();
Andreas Gampe691051b2017-02-09 09:15:24 -0800309 if (temp_klass.Get() != klass.Get()) {
310 DCHECK(temp_klass->IsTemp());
311 DCHECK(temp_klass->IsRetired());
312 HandleTempClass(thread, temp_klass, klass);
313 }
Andreas Gampee6377462017-01-20 17:37:50 -0800314 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 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -0800319 event_handler->DispatchEvent<ArtJvmtiEvent::kClassPrepare>(
320 thread,
321 static_cast<JNIEnv*>(thread->GetJniEnv()),
322 thread_jni.get(),
323 jklass.get());
Andreas Gampee6377462017-01-20 17:37:50 -0800324 }
325 }
326
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800327 // To support parallel class-loading, we need to perform some locking dances here. Namely,
328 // the fixup stage must not be holding the temp_classes lock when it fixes up the system
329 // (as that requires suspending all mutators).
330
Andreas Gampee6377462017-01-20 17:37:50 -0800331 void AddTempClass(art::Thread* self, jclass klass) {
332 std::unique_lock<std::mutex> mu(temp_classes_lock);
Andreas Gampe691051b2017-02-09 09:15:24 -0800333 jclass global_klass = reinterpret_cast<jclass>(self->GetJniEnv()->NewGlobalRef(klass));
334 temp_classes.push_back(global_klass);
Andreas Gampee6377462017-01-20 17:37:50 -0800335 }
336
Andreas Gampe691051b2017-02-09 09:15:24 -0800337 void HandleTempClass(art::Thread* self,
338 art::Handle<art::mirror::Class> temp_klass,
Andreas Gampee6377462017-01-20 17:37:50 -0800339 art::Handle<art::mirror::Class> klass)
340 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800341 bool requires_fixup = false;
342 {
343 std::unique_lock<std::mutex> mu(temp_classes_lock);
344 if (temp_classes.empty()) {
345 return;
Andreas Gampee6377462017-01-20 17:37:50 -0800346 }
Andreas Gampe7619b5b2017-02-10 11:49:12 -0800347
348 for (auto it = temp_classes.begin(); it != temp_classes.end(); ++it) {
349 if (temp_klass.Get() == art::ObjPtr<art::mirror::Class>::DownCast(self->DecodeJObject(*it))) {
350 self->GetJniEnv()->DeleteGlobalRef(*it);
351 temp_classes.erase(it);
352 requires_fixup = true;
353 break;
354 }
355 }
356 }
357 if (requires_fixup) {
358 FixupTempClass(self, temp_klass, klass);
Andreas Gampee6377462017-01-20 17:37:50 -0800359 }
360 }
361
Andreas Gampe691051b2017-02-09 09:15:24 -0800362 void FixupTempClass(art::Thread* self,
363 art::Handle<art::mirror::Class> temp_klass,
364 art::Handle<art::mirror::Class> klass)
Andreas Gampee6377462017-01-20 17:37:50 -0800365 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe691051b2017-02-09 09:15:24 -0800366 // Suspend everything.
367 art::gc::Heap* heap = art::Runtime::Current()->GetHeap();
368 if (heap->IsGcConcurrentAndMoving()) {
369 // Need to take a heap dump while GC isn't running. See the
370 // comment in Heap::VisitObjects().
371 heap->IncrementDisableMovingGC(self);
372 }
373 {
374 art::ScopedThreadSuspension sts(self, art::kWaitingForVisitObjects);
375 art::ScopedSuspendAll ssa("FixupTempClass");
376
377 art::mirror::Class* input = temp_klass.Get();
378 art::mirror::Class* output = klass.Get();
379
380 FixupGlobalReferenceTables(input, output);
Andreas Gampe94dda932017-02-09 18:19:21 -0800381 FixupLocalReferenceTables(self, input, output);
Andreas Gampea67354b2017-02-10 16:18:30 -0800382 FixupHeap(input, output);
Andreas Gampe691051b2017-02-09 09:15:24 -0800383 }
384 if (heap->IsGcConcurrentAndMoving()) {
385 heap->DecrementDisableMovingGC(self);
386 }
387 }
388
Andreas Gampe94dda932017-02-09 18:19:21 -0800389 class RootUpdater : public art::RootVisitor {
390 public:
391 RootUpdater(const art::mirror::Class* input, art::mirror::Class* output)
392 : input_(input), output_(output) {}
393
394 void VisitRoots(art::mirror::Object*** roots,
395 size_t count,
396 const art::RootInfo& info ATTRIBUTE_UNUSED)
397 OVERRIDE {
398 for (size_t i = 0; i != count; ++i) {
399 if (*roots[i] == input_) {
400 *roots[i] = output_;
401 }
402 }
403 }
404
405 void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
406 size_t count,
407 const art::RootInfo& info ATTRIBUTE_UNUSED)
408 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
409 for (size_t i = 0; i != count; ++i) {
410 if (roots[i]->AsMirrorPtr() == input_) {
411 roots[i]->Assign(output_);
412 }
413 }
414 }
415
416 private:
417 const art::mirror::Class* input_;
418 art::mirror::Class* output_;
419 };
420
Andreas Gampea67354b2017-02-10 16:18:30 -0800421 void FixupGlobalReferenceTables(art::mirror::Class* input, art::mirror::Class* output)
Andreas Gampe691051b2017-02-09 09:15:24 -0800422 REQUIRES(art::Locks::mutator_lock_) {
423 art::JavaVMExt* java_vm = art::Runtime::Current()->GetJavaVM();
424
425 // Fix up the global table with a root visitor.
Andreas Gampe94dda932017-02-09 18:19:21 -0800426 RootUpdater global_update(input, output);
Andreas Gampe691051b2017-02-09 09:15:24 -0800427 java_vm->VisitRoots(&global_update);
428
429 class WeakGlobalUpdate : public art::IsMarkedVisitor {
430 public:
431 WeakGlobalUpdate(art::mirror::Class* root_input, art::mirror::Class* root_output)
432 : input_(root_input), output_(root_output) {}
433
434 art::mirror::Object* IsMarked(art::mirror::Object* obj) OVERRIDE {
435 if (obj == input_) {
436 return output_;
437 }
438 return obj;
439 }
440
441 private:
442 const art::mirror::Class* input_;
443 art::mirror::Class* output_;
444 };
445 WeakGlobalUpdate weak_global_update(input, output);
446 java_vm->SweepJniWeakGlobals(&weak_global_update);
Andreas Gampee6377462017-01-20 17:37:50 -0800447 }
448
Andreas Gampe94dda932017-02-09 18:19:21 -0800449 void FixupLocalReferenceTables(art::Thread* self,
450 art::mirror::Class* input,
451 art::mirror::Class* output)
452 REQUIRES(art::Locks::mutator_lock_) {
453 class LocalUpdate {
454 public:
455 LocalUpdate(const art::mirror::Class* root_input, art::mirror::Class* root_output)
456 : input_(root_input), output_(root_output) {}
457
458 static void Callback(art::Thread* t, void* arg) REQUIRES(art::Locks::mutator_lock_) {
459 LocalUpdate* local = reinterpret_cast<LocalUpdate*>(arg);
460
461 // Fix up the local table with a root visitor.
462 RootUpdater local_update(local->input_, local->output_);
463 t->GetJniEnv()->locals.VisitRoots(
464 &local_update, art::RootInfo(art::kRootJNILocal, t->GetThreadId()));
465 }
466
467 private:
468 const art::mirror::Class* input_;
469 art::mirror::Class* output_;
470 };
471 LocalUpdate local_upd(input, output);
472 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
473 art::Runtime::Current()->GetThreadList()->ForEach(LocalUpdate::Callback, &local_upd);
474 }
475
Andreas Gampea67354b2017-02-10 16:18:30 -0800476 void FixupHeap(art::mirror::Class* input, art::mirror::Class* output)
477 REQUIRES(art::Locks::mutator_lock_) {
478 class HeapFixupVisitor {
479 public:
480 HeapFixupVisitor(const art::mirror::Class* root_input, art::mirror::Class* root_output)
481 : input_(root_input), output_(root_output) {}
482
483 void operator()(art::mirror::Object* src,
484 art::MemberOffset field_offset,
485 bool is_static ATTRIBUTE_UNUSED) const
486 REQUIRES_SHARED(art::Locks::mutator_lock_) {
487 art::mirror::HeapReference<art::mirror::Object>* trg =
488 src->GetFieldObjectReferenceAddr(field_offset);
489 if (trg->AsMirrorPtr() == input_) {
490 DCHECK_NE(field_offset.Uint32Value(), 0u); // This shouldn't be the class field of
491 // an object.
492 trg->Assign(output_);
493 }
494 }
495
Andreas Gampe52784ac2017-02-13 18:10:09 -0800496 void operator()(art::ObjPtr<art::mirror::Class> klass ATTRIBUTE_UNUSED,
497 art::ObjPtr<art::mirror::Reference> reference) const
498 REQUIRES_SHARED(art::Locks::mutator_lock_) {
499 art::mirror::Object* val = reference->GetReferent();
500 if (val == input_) {
501 reference->SetReferent<false>(output_);
502 }
503 }
504
Andreas Gampea67354b2017-02-10 16:18:30 -0800505 void VisitRoot(art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED)
Andreas Gampe52784ac2017-02-13 18:10:09 -0800506 const {
Andreas Gampea67354b2017-02-10 16:18:30 -0800507 LOG(FATAL) << "Unreachable";
508 }
509
510 void VisitRootIfNonNull(
511 art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED) const {
512 LOG(FATAL) << "Unreachable";
513 }
514
515 static void AllObjectsCallback(art::mirror::Object* obj, void* arg)
516 REQUIRES_SHARED(art::Locks::mutator_lock_) {
517 HeapFixupVisitor* hfv = reinterpret_cast<HeapFixupVisitor*>(arg);
518
519 // Visit references, not native roots.
Andreas Gampe52784ac2017-02-13 18:10:09 -0800520 obj->VisitReferences<false>(*hfv, *hfv);
Andreas Gampea67354b2017-02-10 16:18:30 -0800521 }
522
523 private:
524 const art::mirror::Class* input_;
525 art::mirror::Class* output_;
526 };
527 HeapFixupVisitor hfv(input, output);
528 art::Runtime::Current()->GetHeap()->VisitObjectsPaused(HeapFixupVisitor::AllObjectsCallback,
529 &hfv);
530 }
531
Andreas Gampee6377462017-01-20 17:37:50 -0800532 // A set of all the temp classes we have handed out. We have to fix up references to these.
533 // For simplicity, we store the temp classes as JNI global references in a vector. Normally a
534 // Prepare event will closely follow, so the vector should be small.
535 std::mutex temp_classes_lock;
536 std::vector<jclass> temp_classes;
537
538 EventHandler* event_handler = nullptr;
539};
540
541ClassCallback gClassCallback;
542
543void ClassUtil::Register(EventHandler* handler) {
544 gClassCallback.event_handler = handler;
545 art::ScopedThreadStateChange stsc(art::Thread::Current(),
546 art::ThreadState::kWaitingForDebuggerToAttach);
547 art::ScopedSuspendAll ssa("Add load callback");
548 art::Runtime::Current()->GetRuntimeCallbacks()->AddClassLoadCallback(&gClassCallback);
549}
550
551void ClassUtil::Unregister() {
552 art::ScopedThreadStateChange stsc(art::Thread::Current(),
553 art::ThreadState::kWaitingForDebuggerToAttach);
554 art::ScopedSuspendAll ssa("Remove thread callback");
555 art::Runtime* runtime = art::Runtime::Current();
556 runtime->GetRuntimeCallbacks()->RemoveClassLoadCallback(&gClassCallback);
557}
558
Andreas Gampeac587272017-01-05 15:21:34 -0800559jvmtiError ClassUtil::GetClassFields(jvmtiEnv* env,
560 jclass jklass,
561 jint* field_count_ptr,
562 jfieldID** fields_ptr) {
563 art::ScopedObjectAccess soa(art::Thread::Current());
564 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
565 if (klass == nullptr) {
566 return ERR(INVALID_CLASS);
567 }
568
569 if (field_count_ptr == nullptr || fields_ptr == nullptr) {
570 return ERR(NULL_POINTER);
571 }
572
Andreas Gampeac587272017-01-05 15:21:34 -0800573 art::IterationRange<art::StrideIterator<art::ArtField>> ifields = klass->GetIFields();
574 art::IterationRange<art::StrideIterator<art::ArtField>> sfields = klass->GetSFields();
575 size_t array_size = klass->NumInstanceFields() + klass->NumStaticFields();
576
577 unsigned char* out_ptr;
578 jvmtiError allocError = env->Allocate(array_size * sizeof(jfieldID), &out_ptr);
579 if (allocError != ERR(NONE)) {
580 return allocError;
581 }
582 jfieldID* field_array = reinterpret_cast<jfieldID*>(out_ptr);
583
584 size_t array_idx = 0;
585 for (art::ArtField& field : sfields) {
586 field_array[array_idx] = art::jni::EncodeArtField(&field);
587 ++array_idx;
588 }
589 for (art::ArtField& field : ifields) {
590 field_array[array_idx] = art::jni::EncodeArtField(&field);
591 ++array_idx;
592 }
593
594 *field_count_ptr = static_cast<jint>(array_size);
595 *fields_ptr = field_array;
596
597 return ERR(NONE);
598}
599
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800600jvmtiError ClassUtil::GetClassMethods(jvmtiEnv* env,
601 jclass jklass,
602 jint* method_count_ptr,
603 jmethodID** methods_ptr) {
604 art::ScopedObjectAccess soa(art::Thread::Current());
605 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
606 if (klass == nullptr) {
607 return ERR(INVALID_CLASS);
608 }
609
610 if (method_count_ptr == nullptr || methods_ptr == nullptr) {
611 return ERR(NULL_POINTER);
612 }
613
614 size_t array_size = klass->NumDeclaredVirtualMethods() + klass->NumDirectMethods();
615 unsigned char* out_ptr;
616 jvmtiError allocError = env->Allocate(array_size * sizeof(jmethodID), &out_ptr);
617 if (allocError != ERR(NONE)) {
618 return allocError;
619 }
620 jmethodID* method_array = reinterpret_cast<jmethodID*>(out_ptr);
621
622 if (art::kIsDebugBuild) {
623 size_t count = 0;
624 for (auto& m ATTRIBUTE_UNUSED : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
625 count++;
626 }
627 CHECK_EQ(count, klass->NumDirectMethods() + klass->NumDeclaredVirtualMethods());
628 }
629
630 size_t array_idx = 0;
631 for (auto& m : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
632 method_array[array_idx] = art::jni::EncodeArtMethod(&m);
633 ++array_idx;
634 }
635
636 *method_count_ptr = static_cast<jint>(array_size);
637 *methods_ptr = method_array;
638
639 return ERR(NONE);
640}
641
Andreas Gampe8b07e472017-01-06 14:20:39 -0800642jvmtiError ClassUtil::GetImplementedInterfaces(jvmtiEnv* env,
643 jclass jklass,
644 jint* interface_count_ptr,
645 jclass** interfaces_ptr) {
646 art::ScopedObjectAccess soa(art::Thread::Current());
647 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
648 if (klass == nullptr) {
649 return ERR(INVALID_CLASS);
650 }
651
652 if (interface_count_ptr == nullptr || interfaces_ptr == nullptr) {
653 return ERR(NULL_POINTER);
654 }
655
656 // Need to handle array specifically. Arrays implement Serializable and Cloneable, but the
657 // spec says these should not be reported.
658 if (klass->IsArrayClass()) {
659 *interface_count_ptr = 0;
660 *interfaces_ptr = nullptr; // TODO: Should we allocate a dummy here?
661 return ERR(NONE);
662 }
663
664 size_t array_size = klass->NumDirectInterfaces();
665 unsigned char* out_ptr;
666 jvmtiError allocError = env->Allocate(array_size * sizeof(jclass), &out_ptr);
667 if (allocError != ERR(NONE)) {
668 return allocError;
669 }
670 jclass* interface_array = reinterpret_cast<jclass*>(out_ptr);
671
672 art::StackHandleScope<1> hs(soa.Self());
673 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass));
674
675 for (uint32_t idx = 0; idx != array_size; ++idx) {
676 art::ObjPtr<art::mirror::Class> inf_klass =
677 art::mirror::Class::ResolveDirectInterface(soa.Self(), h_klass, idx);
678 if (inf_klass == nullptr) {
679 soa.Self()->ClearException();
680 env->Deallocate(out_ptr);
681 // TODO: What is the right error code here?
682 return ERR(INTERNAL);
683 }
684 interface_array[idx] = soa.AddLocalReference<jclass>(inf_klass);
685 }
686
687 *interface_count_ptr = static_cast<jint>(array_size);
688 *interfaces_ptr = interface_array;
689
690 return ERR(NONE);
691}
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800692
Andreas Gampee492ae32016-10-28 19:34:57 -0700693jvmtiError ClassUtil::GetClassSignature(jvmtiEnv* env,
694 jclass jklass,
695 char** signature_ptr,
696 char** generic_ptr) {
697 art::ScopedObjectAccess soa(art::Thread::Current());
698 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
699 if (klass == nullptr) {
700 return ERR(INVALID_CLASS);
701 }
702
Andreas Gampe54711412017-02-21 12:41:43 -0800703 JvmtiUniquePtr<char[]> sig_copy;
Andreas Gampee492ae32016-10-28 19:34:57 -0700704 if (signature_ptr != nullptr) {
705 std::string storage;
706 const char* descriptor = klass->GetDescriptor(&storage);
707
Andreas Gampe54711412017-02-21 12:41:43 -0800708 jvmtiError ret;
709 sig_copy = CopyString(env, descriptor, &ret);
710 if (sig_copy == nullptr) {
Andreas Gampee492ae32016-10-28 19:34:57 -0700711 return ret;
712 }
Andreas Gampe54711412017-02-21 12:41:43 -0800713 *signature_ptr = sig_copy.get();
Andreas Gampee492ae32016-10-28 19:34:57 -0700714 }
715
Andreas Gampee6377462017-01-20 17:37:50 -0800716 if (generic_ptr != nullptr) {
717 *generic_ptr = nullptr;
Andreas Gampe0eb36432017-02-15 18:36:14 -0800718 if (!klass->IsProxyClass() && klass->GetDexCache() != nullptr) {
719 art::StackHandleScope<1> hs(soa.Self());
720 art::Handle<art::mirror::Class> h_klass = hs.NewHandle(klass);
721 art::mirror::ObjectArray<art::mirror::String>* str_array =
722 art::annotations::GetSignatureAnnotationForClass(h_klass);
723 if (str_array != nullptr) {
724 std::ostringstream oss;
725 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
726 oss << str_array->Get(i)->ToModifiedUtf8();
727 }
728 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800729 jvmtiError ret;
730 JvmtiUniquePtr<char[]> copy = CopyString(env, output_string.c_str(), &ret);
731 if (copy == nullptr) {
Andreas Gampe0eb36432017-02-15 18:36:14 -0800732 return ret;
733 }
Andreas Gampe54711412017-02-21 12:41:43 -0800734 *generic_ptr = copy.release();
Andreas Gampe0eb36432017-02-15 18:36:14 -0800735 } else if (soa.Self()->IsExceptionPending()) {
736 // TODO: Should we report an error here?
737 soa.Self()->ClearException();
738 }
739 }
Andreas Gampee6377462017-01-20 17:37:50 -0800740 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700741
742 // Everything is fine, release the buffers.
743 sig_copy.release();
744
745 return ERR(NONE);
746}
747
Andreas Gampeff9d2092017-01-06 09:12:49 -0800748jvmtiError ClassUtil::GetClassStatus(jvmtiEnv* env ATTRIBUTE_UNUSED,
749 jclass jklass,
750 jint* status_ptr) {
751 art::ScopedObjectAccess soa(art::Thread::Current());
752 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
753 if (klass == nullptr) {
754 return ERR(INVALID_CLASS);
755 }
756
757 if (status_ptr == nullptr) {
758 return ERR(NULL_POINTER);
759 }
760
761 if (klass->IsArrayClass()) {
762 *status_ptr = JVMTI_CLASS_STATUS_ARRAY;
763 } else if (klass->IsPrimitive()) {
764 *status_ptr = JVMTI_CLASS_STATUS_PRIMITIVE;
765 } else {
766 *status_ptr = JVMTI_CLASS_STATUS_VERIFIED; // All loaded classes are structurally verified.
767 // This is finicky. If there's an error, we'll say it wasn't prepared.
768 if (klass->IsResolved()) {
769 *status_ptr |= JVMTI_CLASS_STATUS_PREPARED;
770 }
771 if (klass->IsInitialized()) {
772 *status_ptr |= JVMTI_CLASS_STATUS_INITIALIZED;
773 }
774 // Technically the class may be erroneous for other reasons, but we do not have enough info.
775 if (klass->IsErroneous()) {
776 *status_ptr |= JVMTI_CLASS_STATUS_ERROR;
777 }
778 }
779
780 return ERR(NONE);
781}
782
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800783template <typename T>
784static jvmtiError ClassIsT(jclass jklass, T test, jboolean* is_t_ptr) {
785 art::ScopedObjectAccess soa(art::Thread::Current());
786 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
787 if (klass == nullptr) {
788 return ERR(INVALID_CLASS);
789 }
790
791 if (is_t_ptr == nullptr) {
792 return ERR(NULL_POINTER);
793 }
794
795 *is_t_ptr = test(klass) ? JNI_TRUE : JNI_FALSE;
796 return ERR(NONE);
797}
798
799jvmtiError ClassUtil::IsInterface(jvmtiEnv* env ATTRIBUTE_UNUSED,
800 jclass jklass,
801 jboolean* is_interface_ptr) {
802 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
803 return klass->IsInterface();
804 };
805 return ClassIsT(jklass, test, is_interface_ptr);
806}
807
808jvmtiError ClassUtil::IsArrayClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
809 jclass jklass,
810 jboolean* is_array_class_ptr) {
811 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
812 return klass->IsArrayClass();
813 };
814 return ClassIsT(jklass, test, is_array_class_ptr);
815}
816
Andreas Gampe64013e52017-01-06 13:07:19 -0800817// Keep this in sync with Class.getModifiers().
818static uint32_t ClassGetModifiers(art::Thread* self, art::ObjPtr<art::mirror::Class> klass)
819 REQUIRES_SHARED(art::Locks::mutator_lock_) {
820 if (klass->IsArrayClass()) {
821 uint32_t component_modifiers = ClassGetModifiers(self, klass->GetComponentType());
822 if ((component_modifiers & art::kAccInterface) != 0) {
823 component_modifiers &= ~(art::kAccInterface | art::kAccStatic);
824 }
825 return art::kAccAbstract | art::kAccFinal | component_modifiers;
826 }
827
828 uint32_t modifiers = klass->GetAccessFlags() & art::kAccJavaFlagsMask;
829
830 art::StackHandleScope<1> hs(self);
831 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass));
832 return art::mirror::Class::GetInnerClassFlags(h_klass, modifiers);
833}
834
835jvmtiError ClassUtil::GetClassModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
836 jclass jklass,
837 jint* modifiers_ptr) {
838 art::ScopedObjectAccess soa(art::Thread::Current());
839 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
840 if (klass == nullptr) {
841 return ERR(INVALID_CLASS);
842 }
843
844 if (modifiers_ptr == nullptr) {
845 return ERR(NULL_POINTER);
846 }
847
848 *modifiers_ptr = ClassGetModifiers(soa.Self(), klass);
849
850 return ERR(NONE);
851}
852
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800853jvmtiError ClassUtil::GetClassLoader(jvmtiEnv* env ATTRIBUTE_UNUSED,
854 jclass jklass,
855 jobject* classloader_ptr) {
856 art::ScopedObjectAccess soa(art::Thread::Current());
857 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
858 if (klass == nullptr) {
859 return ERR(INVALID_CLASS);
860 }
861
862 if (classloader_ptr == nullptr) {
863 return ERR(NULL_POINTER);
864 }
865
866 *classloader_ptr = soa.AddLocalReference<jobject>(klass->GetClassLoader());
867
868 return ERR(NONE);
869}
870
Andreas Gampe70f16392017-01-16 14:20:10 -0800871jvmtiError ClassUtil::GetClassLoaderClasses(jvmtiEnv* env,
872 jobject initiating_loader,
873 jint* class_count_ptr,
874 jclass** classes_ptr) {
875 UNUSED(env, initiating_loader, class_count_ptr, classes_ptr);
876
877 if (class_count_ptr == nullptr || classes_ptr == nullptr) {
878 return ERR(NULL_POINTER);
879 }
880 art::Thread* self = art::Thread::Current();
881 if (!self->GetJniEnv()->IsInstanceOf(initiating_loader,
882 art::WellKnownClasses::java_lang_ClassLoader)) {
883 return ERR(ILLEGAL_ARGUMENT);
884 }
885 if (self->GetJniEnv()->IsInstanceOf(initiating_loader,
886 art::WellKnownClasses::java_lang_BootClassLoader)) {
887 // Need to use null for the BootClassLoader.
888 initiating_loader = nullptr;
889 }
890
891 art::ScopedObjectAccess soa(self);
892 art::ObjPtr<art::mirror::ClassLoader> class_loader =
893 soa.Decode<art::mirror::ClassLoader>(initiating_loader);
894
895 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
896
897 art::ReaderMutexLock mu(self, *art::Locks::classlinker_classes_lock_);
898
899 art::ClassTable* class_table = class_linker->ClassTableForClassLoader(class_loader);
900 if (class_table == nullptr) {
901 // Nothing loaded.
902 *class_count_ptr = 0;
903 *classes_ptr = nullptr;
904 return ERR(NONE);
905 }
906
907 struct ClassTableCount {
908 bool operator()(art::ObjPtr<art::mirror::Class> klass) {
909 DCHECK(klass != nullptr);
910 ++count;
911 return true;
912 }
913
914 size_t count = 0;
915 };
916 ClassTableCount ctc;
917 class_table->Visit(ctc);
918
919 if (ctc.count == 0) {
920 // Nothing loaded.
921 *class_count_ptr = 0;
922 *classes_ptr = nullptr;
923 return ERR(NONE);
924 }
925
926 unsigned char* data;
927 jvmtiError data_result = env->Allocate(ctc.count * sizeof(jclass), &data);
928 if (data_result != ERR(NONE)) {
929 return data_result;
930 }
931 jclass* class_array = reinterpret_cast<jclass*>(data);
932
933 struct ClassTableFill {
934 bool operator()(art::ObjPtr<art::mirror::Class> klass)
935 REQUIRES_SHARED(art::Locks::mutator_lock_) {
936 DCHECK(klass != nullptr);
937 DCHECK_LT(count, ctc_ref.count);
938 local_class_array[count++] = soa_ptr->AddLocalReference<jclass>(klass);
939 return true;
940 }
941
942 jclass* local_class_array;
943 const ClassTableCount& ctc_ref;
944 art::ScopedObjectAccess* soa_ptr;
945 size_t count;
946 };
947 ClassTableFill ctf = { class_array, ctc, &soa, 0 };
948 class_table->Visit(ctf);
949 DCHECK_EQ(ctc.count, ctf.count);
950
951 *class_count_ptr = ctc.count;
952 *classes_ptr = class_array;
953
954 return ERR(NONE);
955}
956
Andreas Gampe812a2442017-01-19 22:04:46 -0800957jvmtiError ClassUtil::GetClassVersionNumbers(jvmtiEnv* env ATTRIBUTE_UNUSED,
958 jclass jklass,
959 jint* minor_version_ptr,
960 jint* major_version_ptr) {
961 art::ScopedObjectAccess soa(art::Thread::Current());
962 if (jklass == nullptr) {
963 return ERR(INVALID_CLASS);
964 }
965 art::ObjPtr<art::mirror::Object> jklass_obj = soa.Decode<art::mirror::Object>(jklass);
966 if (!jklass_obj->IsClass()) {
967 return ERR(INVALID_CLASS);
968 }
969 art::ObjPtr<art::mirror::Class> klass = jklass_obj->AsClass();
970 if (klass->IsPrimitive() || klass->IsArrayClass()) {
971 return ERR(INVALID_CLASS);
972 }
973
974 if (minor_version_ptr == nullptr || major_version_ptr == nullptr) {
975 return ERR(NULL_POINTER);
976 }
977
978 // Note: proxies will show the dex file version of java.lang.reflect.Proxy, as that is
979 // what their dex cache copies from.
980 uint32_t version = klass->GetDexFile().GetHeader().GetVersion();
981
982 *major_version_ptr = static_cast<jint>(version);
983 *minor_version_ptr = 0;
984
985 return ERR(NONE);
986}
987
Andreas Gampee492ae32016-10-28 19:34:57 -0700988} // namespace openjdkjvmti