blob: abcc84925ba42832f2f7792c0c195cfa8925eec7 [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
34#include "art_jvmti.h"
Andreas Gampe70f16392017-01-16 14:20:10 -080035#include "class_table-inl.h"
36#include "class_linker.h"
Andreas Gampeac587272017-01-05 15:21:34 -080037#include "jni_internal.h"
Andreas Gampe70f16392017-01-16 14:20:10 -080038#include "runtime.h"
Andreas Gampee492ae32016-10-28 19:34:57 -070039#include "scoped_thread_state_change-inl.h"
40#include "thread-inl.h"
41
42namespace openjdkjvmti {
43
Andreas Gampeac587272017-01-05 15:21:34 -080044jvmtiError ClassUtil::GetClassFields(jvmtiEnv* env,
45 jclass jklass,
46 jint* field_count_ptr,
47 jfieldID** fields_ptr) {
48 art::ScopedObjectAccess soa(art::Thread::Current());
49 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
50 if (klass == nullptr) {
51 return ERR(INVALID_CLASS);
52 }
53
54 if (field_count_ptr == nullptr || fields_ptr == nullptr) {
55 return ERR(NULL_POINTER);
56 }
57
Andreas Gampeac587272017-01-05 15:21:34 -080058 art::IterationRange<art::StrideIterator<art::ArtField>> ifields = klass->GetIFields();
59 art::IterationRange<art::StrideIterator<art::ArtField>> sfields = klass->GetSFields();
60 size_t array_size = klass->NumInstanceFields() + klass->NumStaticFields();
61
62 unsigned char* out_ptr;
63 jvmtiError allocError = env->Allocate(array_size * sizeof(jfieldID), &out_ptr);
64 if (allocError != ERR(NONE)) {
65 return allocError;
66 }
67 jfieldID* field_array = reinterpret_cast<jfieldID*>(out_ptr);
68
69 size_t array_idx = 0;
70 for (art::ArtField& field : sfields) {
71 field_array[array_idx] = art::jni::EncodeArtField(&field);
72 ++array_idx;
73 }
74 for (art::ArtField& field : ifields) {
75 field_array[array_idx] = art::jni::EncodeArtField(&field);
76 ++array_idx;
77 }
78
79 *field_count_ptr = static_cast<jint>(array_size);
80 *fields_ptr = field_array;
81
82 return ERR(NONE);
83}
84
Andreas Gampe18fee4d2017-01-06 11:36:35 -080085jvmtiError ClassUtil::GetClassMethods(jvmtiEnv* env,
86 jclass jklass,
87 jint* method_count_ptr,
88 jmethodID** methods_ptr) {
89 art::ScopedObjectAccess soa(art::Thread::Current());
90 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
91 if (klass == nullptr) {
92 return ERR(INVALID_CLASS);
93 }
94
95 if (method_count_ptr == nullptr || methods_ptr == nullptr) {
96 return ERR(NULL_POINTER);
97 }
98
99 size_t array_size = klass->NumDeclaredVirtualMethods() + klass->NumDirectMethods();
100 unsigned char* out_ptr;
101 jvmtiError allocError = env->Allocate(array_size * sizeof(jmethodID), &out_ptr);
102 if (allocError != ERR(NONE)) {
103 return allocError;
104 }
105 jmethodID* method_array = reinterpret_cast<jmethodID*>(out_ptr);
106
107 if (art::kIsDebugBuild) {
108 size_t count = 0;
109 for (auto& m ATTRIBUTE_UNUSED : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
110 count++;
111 }
112 CHECK_EQ(count, klass->NumDirectMethods() + klass->NumDeclaredVirtualMethods());
113 }
114
115 size_t array_idx = 0;
116 for (auto& m : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
117 method_array[array_idx] = art::jni::EncodeArtMethod(&m);
118 ++array_idx;
119 }
120
121 *method_count_ptr = static_cast<jint>(array_size);
122 *methods_ptr = method_array;
123
124 return ERR(NONE);
125}
126
Andreas Gampe8b07e472017-01-06 14:20:39 -0800127jvmtiError ClassUtil::GetImplementedInterfaces(jvmtiEnv* env,
128 jclass jklass,
129 jint* interface_count_ptr,
130 jclass** interfaces_ptr) {
131 art::ScopedObjectAccess soa(art::Thread::Current());
132 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
133 if (klass == nullptr) {
134 return ERR(INVALID_CLASS);
135 }
136
137 if (interface_count_ptr == nullptr || interfaces_ptr == nullptr) {
138 return ERR(NULL_POINTER);
139 }
140
141 // Need to handle array specifically. Arrays implement Serializable and Cloneable, but the
142 // spec says these should not be reported.
143 if (klass->IsArrayClass()) {
144 *interface_count_ptr = 0;
145 *interfaces_ptr = nullptr; // TODO: Should we allocate a dummy here?
146 return ERR(NONE);
147 }
148
149 size_t array_size = klass->NumDirectInterfaces();
150 unsigned char* out_ptr;
151 jvmtiError allocError = env->Allocate(array_size * sizeof(jclass), &out_ptr);
152 if (allocError != ERR(NONE)) {
153 return allocError;
154 }
155 jclass* interface_array = reinterpret_cast<jclass*>(out_ptr);
156
157 art::StackHandleScope<1> hs(soa.Self());
158 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass));
159
160 for (uint32_t idx = 0; idx != array_size; ++idx) {
161 art::ObjPtr<art::mirror::Class> inf_klass =
162 art::mirror::Class::ResolveDirectInterface(soa.Self(), h_klass, idx);
163 if (inf_klass == nullptr) {
164 soa.Self()->ClearException();
165 env->Deallocate(out_ptr);
166 // TODO: What is the right error code here?
167 return ERR(INTERNAL);
168 }
169 interface_array[idx] = soa.AddLocalReference<jclass>(inf_klass);
170 }
171
172 *interface_count_ptr = static_cast<jint>(array_size);
173 *interfaces_ptr = interface_array;
174
175 return ERR(NONE);
176}
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800177
Andreas Gampee492ae32016-10-28 19:34:57 -0700178jvmtiError ClassUtil::GetClassSignature(jvmtiEnv* env,
179 jclass jklass,
180 char** signature_ptr,
181 char** generic_ptr) {
182 art::ScopedObjectAccess soa(art::Thread::Current());
183 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
184 if (klass == nullptr) {
185 return ERR(INVALID_CLASS);
186 }
187
188 JvmtiUniquePtr sig_copy;
189 if (signature_ptr != nullptr) {
190 std::string storage;
191 const char* descriptor = klass->GetDescriptor(&storage);
192
193 unsigned char* tmp;
194 jvmtiError ret = CopyString(env, descriptor, &tmp);
195 if (ret != ERR(NONE)) {
196 return ret;
197 }
198 sig_copy = MakeJvmtiUniquePtr(env, tmp);
199 *signature_ptr = reinterpret_cast<char*>(tmp);
200 }
201
202 // TODO: Support generic signature.
203 *generic_ptr = nullptr;
204
205 // Everything is fine, release the buffers.
206 sig_copy.release();
207
208 return ERR(NONE);
209}
210
Andreas Gampeff9d2092017-01-06 09:12:49 -0800211jvmtiError ClassUtil::GetClassStatus(jvmtiEnv* env ATTRIBUTE_UNUSED,
212 jclass jklass,
213 jint* status_ptr) {
214 art::ScopedObjectAccess soa(art::Thread::Current());
215 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
216 if (klass == nullptr) {
217 return ERR(INVALID_CLASS);
218 }
219
220 if (status_ptr == nullptr) {
221 return ERR(NULL_POINTER);
222 }
223
224 if (klass->IsArrayClass()) {
225 *status_ptr = JVMTI_CLASS_STATUS_ARRAY;
226 } else if (klass->IsPrimitive()) {
227 *status_ptr = JVMTI_CLASS_STATUS_PRIMITIVE;
228 } else {
229 *status_ptr = JVMTI_CLASS_STATUS_VERIFIED; // All loaded classes are structurally verified.
230 // This is finicky. If there's an error, we'll say it wasn't prepared.
231 if (klass->IsResolved()) {
232 *status_ptr |= JVMTI_CLASS_STATUS_PREPARED;
233 }
234 if (klass->IsInitialized()) {
235 *status_ptr |= JVMTI_CLASS_STATUS_INITIALIZED;
236 }
237 // Technically the class may be erroneous for other reasons, but we do not have enough info.
238 if (klass->IsErroneous()) {
239 *status_ptr |= JVMTI_CLASS_STATUS_ERROR;
240 }
241 }
242
243 return ERR(NONE);
244}
245
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800246template <typename T>
247static jvmtiError ClassIsT(jclass jklass, T test, jboolean* is_t_ptr) {
248 art::ScopedObjectAccess soa(art::Thread::Current());
249 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
250 if (klass == nullptr) {
251 return ERR(INVALID_CLASS);
252 }
253
254 if (is_t_ptr == nullptr) {
255 return ERR(NULL_POINTER);
256 }
257
258 *is_t_ptr = test(klass) ? JNI_TRUE : JNI_FALSE;
259 return ERR(NONE);
260}
261
262jvmtiError ClassUtil::IsInterface(jvmtiEnv* env ATTRIBUTE_UNUSED,
263 jclass jklass,
264 jboolean* is_interface_ptr) {
265 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
266 return klass->IsInterface();
267 };
268 return ClassIsT(jklass, test, is_interface_ptr);
269}
270
271jvmtiError ClassUtil::IsArrayClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
272 jclass jklass,
273 jboolean* is_array_class_ptr) {
274 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
275 return klass->IsArrayClass();
276 };
277 return ClassIsT(jklass, test, is_array_class_ptr);
278}
279
Andreas Gampe64013e52017-01-06 13:07:19 -0800280// Keep this in sync with Class.getModifiers().
281static uint32_t ClassGetModifiers(art::Thread* self, art::ObjPtr<art::mirror::Class> klass)
282 REQUIRES_SHARED(art::Locks::mutator_lock_) {
283 if (klass->IsArrayClass()) {
284 uint32_t component_modifiers = ClassGetModifiers(self, klass->GetComponentType());
285 if ((component_modifiers & art::kAccInterface) != 0) {
286 component_modifiers &= ~(art::kAccInterface | art::kAccStatic);
287 }
288 return art::kAccAbstract | art::kAccFinal | component_modifiers;
289 }
290
291 uint32_t modifiers = klass->GetAccessFlags() & art::kAccJavaFlagsMask;
292
293 art::StackHandleScope<1> hs(self);
294 art::Handle<art::mirror::Class> h_klass(hs.NewHandle(klass));
295 return art::mirror::Class::GetInnerClassFlags(h_klass, modifiers);
296}
297
298jvmtiError ClassUtil::GetClassModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
299 jclass jklass,
300 jint* modifiers_ptr) {
301 art::ScopedObjectAccess soa(art::Thread::Current());
302 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
303 if (klass == nullptr) {
304 return ERR(INVALID_CLASS);
305 }
306
307 if (modifiers_ptr == nullptr) {
308 return ERR(NULL_POINTER);
309 }
310
311 *modifiers_ptr = ClassGetModifiers(soa.Self(), klass);
312
313 return ERR(NONE);
314}
315
Andreas Gampe8f5b6032017-01-06 15:50:55 -0800316jvmtiError ClassUtil::GetClassLoader(jvmtiEnv* env ATTRIBUTE_UNUSED,
317 jclass jklass,
318 jobject* classloader_ptr) {
319 art::ScopedObjectAccess soa(art::Thread::Current());
320 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
321 if (klass == nullptr) {
322 return ERR(INVALID_CLASS);
323 }
324
325 if (classloader_ptr == nullptr) {
326 return ERR(NULL_POINTER);
327 }
328
329 *classloader_ptr = soa.AddLocalReference<jobject>(klass->GetClassLoader());
330
331 return ERR(NONE);
332}
333
Andreas Gampe70f16392017-01-16 14:20:10 -0800334jvmtiError ClassUtil::GetClassLoaderClasses(jvmtiEnv* env,
335 jobject initiating_loader,
336 jint* class_count_ptr,
337 jclass** classes_ptr) {
338 UNUSED(env, initiating_loader, class_count_ptr, classes_ptr);
339
340 if (class_count_ptr == nullptr || classes_ptr == nullptr) {
341 return ERR(NULL_POINTER);
342 }
343 art::Thread* self = art::Thread::Current();
344 if (!self->GetJniEnv()->IsInstanceOf(initiating_loader,
345 art::WellKnownClasses::java_lang_ClassLoader)) {
346 return ERR(ILLEGAL_ARGUMENT);
347 }
348 if (self->GetJniEnv()->IsInstanceOf(initiating_loader,
349 art::WellKnownClasses::java_lang_BootClassLoader)) {
350 // Need to use null for the BootClassLoader.
351 initiating_loader = nullptr;
352 }
353
354 art::ScopedObjectAccess soa(self);
355 art::ObjPtr<art::mirror::ClassLoader> class_loader =
356 soa.Decode<art::mirror::ClassLoader>(initiating_loader);
357
358 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
359
360 art::ReaderMutexLock mu(self, *art::Locks::classlinker_classes_lock_);
361
362 art::ClassTable* class_table = class_linker->ClassTableForClassLoader(class_loader);
363 if (class_table == nullptr) {
364 // Nothing loaded.
365 *class_count_ptr = 0;
366 *classes_ptr = nullptr;
367 return ERR(NONE);
368 }
369
370 struct ClassTableCount {
371 bool operator()(art::ObjPtr<art::mirror::Class> klass) {
372 DCHECK(klass != nullptr);
373 ++count;
374 return true;
375 }
376
377 size_t count = 0;
378 };
379 ClassTableCount ctc;
380 class_table->Visit(ctc);
381
382 if (ctc.count == 0) {
383 // Nothing loaded.
384 *class_count_ptr = 0;
385 *classes_ptr = nullptr;
386 return ERR(NONE);
387 }
388
389 unsigned char* data;
390 jvmtiError data_result = env->Allocate(ctc.count * sizeof(jclass), &data);
391 if (data_result != ERR(NONE)) {
392 return data_result;
393 }
394 jclass* class_array = reinterpret_cast<jclass*>(data);
395
396 struct ClassTableFill {
397 bool operator()(art::ObjPtr<art::mirror::Class> klass)
398 REQUIRES_SHARED(art::Locks::mutator_lock_) {
399 DCHECK(klass != nullptr);
400 DCHECK_LT(count, ctc_ref.count);
401 local_class_array[count++] = soa_ptr->AddLocalReference<jclass>(klass);
402 return true;
403 }
404
405 jclass* local_class_array;
406 const ClassTableCount& ctc_ref;
407 art::ScopedObjectAccess* soa_ptr;
408 size_t count;
409 };
410 ClassTableFill ctf = { class_array, ctc, &soa, 0 };
411 class_table->Visit(ctf);
412 DCHECK_EQ(ctc.count, ctf.count);
413
414 *class_count_ptr = ctc.count;
415 *classes_ptr = class_array;
416
417 return ERR(NONE);
418}
419
Andreas Gampe812a2442017-01-19 22:04:46 -0800420jvmtiError ClassUtil::GetClassVersionNumbers(jvmtiEnv* env ATTRIBUTE_UNUSED,
421 jclass jklass,
422 jint* minor_version_ptr,
423 jint* major_version_ptr) {
424 art::ScopedObjectAccess soa(art::Thread::Current());
425 if (jklass == nullptr) {
426 return ERR(INVALID_CLASS);
427 }
428 art::ObjPtr<art::mirror::Object> jklass_obj = soa.Decode<art::mirror::Object>(jklass);
429 if (!jklass_obj->IsClass()) {
430 return ERR(INVALID_CLASS);
431 }
432 art::ObjPtr<art::mirror::Class> klass = jklass_obj->AsClass();
433 if (klass->IsPrimitive() || klass->IsArrayClass()) {
434 return ERR(INVALID_CLASS);
435 }
436
437 if (minor_version_ptr == nullptr || major_version_ptr == nullptr) {
438 return ERR(NULL_POINTER);
439 }
440
441 // Note: proxies will show the dex file version of java.lang.reflect.Proxy, as that is
442 // what their dex cache copies from.
443 uint32_t version = klass->GetDexFile().GetHeader().GetVersion();
444
445 *major_version_ptr = static_cast<jint>(version);
446 *minor_version_ptr = 0;
447
448 return ERR(NONE);
449}
450
Andreas Gampee492ae32016-10-28 19:34:57 -0700451} // namespace openjdkjvmti