Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/prims/jvm.cpp
41145 views
1
/*
2
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include "precompiled.hpp"
26
#include "jvm.h"
27
#include "cds/classListParser.hpp"
28
#include "cds/classListWriter.hpp"
29
#include "cds/dynamicArchive.hpp"
30
#include "cds/heapShared.hpp"
31
#include "cds/lambdaFormInvokers.hpp"
32
#include "classfile/classFileStream.hpp"
33
#include "classfile/classLoader.hpp"
34
#include "classfile/classLoaderData.hpp"
35
#include "classfile/classLoaderData.inline.hpp"
36
#include "classfile/classLoadInfo.hpp"
37
#include "classfile/javaAssertions.hpp"
38
#include "classfile/javaClasses.inline.hpp"
39
#include "classfile/moduleEntry.hpp"
40
#include "classfile/modules.hpp"
41
#include "classfile/packageEntry.hpp"
42
#include "classfile/stringTable.hpp"
43
#include "classfile/symbolTable.hpp"
44
#include "classfile/systemDictionary.hpp"
45
#include "classfile/vmClasses.hpp"
46
#include "classfile/vmSymbols.hpp"
47
#include "gc/shared/collectedHeap.inline.hpp"
48
#include "interpreter/bytecode.hpp"
49
#include "interpreter/bytecodeUtils.hpp"
50
#include "jfr/jfrEvents.hpp"
51
#include "logging/log.hpp"
52
#include "memory/oopFactory.hpp"
53
#include "memory/referenceType.hpp"
54
#include "memory/resourceArea.hpp"
55
#include "memory/universe.hpp"
56
#include "oops/access.inline.hpp"
57
#include "oops/constantPool.hpp"
58
#include "oops/fieldStreams.inline.hpp"
59
#include "oops/instanceKlass.hpp"
60
#include "oops/klass.inline.hpp"
61
#include "oops/method.hpp"
62
#include "oops/recordComponent.hpp"
63
#include "oops/objArrayKlass.hpp"
64
#include "oops/objArrayOop.inline.hpp"
65
#include "oops/oop.inline.hpp"
66
#include "prims/jvm_misc.hpp"
67
#include "prims/jvmtiExport.hpp"
68
#include "prims/jvmtiThreadState.hpp"
69
#include "prims/stackwalk.hpp"
70
#include "runtime/arguments.hpp"
71
#include "runtime/atomic.hpp"
72
#include "runtime/globals_extension.hpp"
73
#include "runtime/handles.inline.hpp"
74
#include "runtime/init.hpp"
75
#include "runtime/interfaceSupport.inline.hpp"
76
#include "runtime/deoptimization.hpp"
77
#include "runtime/handshake.hpp"
78
#include "runtime/java.hpp"
79
#include "runtime/javaCalls.hpp"
80
#include "runtime/jfieldIDWorkaround.hpp"
81
#include "runtime/jniHandles.inline.hpp"
82
#include "runtime/os.inline.hpp"
83
#include "runtime/osThread.hpp"
84
#include "runtime/perfData.hpp"
85
#include "runtime/reflection.hpp"
86
#include "runtime/synchronizer.hpp"
87
#include "runtime/thread.inline.hpp"
88
#include "runtime/threadSMR.hpp"
89
#include "runtime/vframe.inline.hpp"
90
#include "runtime/vmOperations.hpp"
91
#include "runtime/vm_version.hpp"
92
#include "services/attachListener.hpp"
93
#include "services/management.hpp"
94
#include "services/threadService.hpp"
95
#include "utilities/copy.hpp"
96
#include "utilities/defaultStream.hpp"
97
#include "utilities/dtrace.hpp"
98
#include "utilities/events.hpp"
99
#include "utilities/macros.hpp"
100
#include "utilities/utf8.hpp"
101
#if INCLUDE_CDS
102
#include "classfile/systemDictionaryShared.hpp"
103
#endif
104
#if INCLUDE_JFR
105
#include "jfr/jfr.hpp"
106
#endif
107
108
#include <errno.h>
109
110
/*
111
NOTE about use of any ctor or function call that can trigger a safepoint/GC:
112
such ctors and calls MUST NOT come between an oop declaration/init and its
113
usage because if objects are move this may cause various memory stomps, bus
114
errors and segfaults. Here is a cookbook for causing so called "naked oop
115
failures":
116
117
JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields<etc> {
118
// Object address to be held directly in mirror & not visible to GC
119
oop mirror = JNIHandles::resolve_non_null(ofClass);
120
121
// If this ctor can hit a safepoint, moving objects around, then
122
ComplexConstructor foo;
123
124
// Boom! mirror may point to JUNK instead of the intended object
125
(some dereference of mirror)
126
127
// Here's another call that may block for GC, making mirror stale
128
MutexLocker ml(some_lock);
129
130
// And here's an initializer that can result in a stale oop
131
// all in one step.
132
oop o = call_that_can_throw_exception(TRAPS);
133
134
135
The solution is to keep the oop declaration BELOW the ctor or function
136
call that might cause a GC, do another resolve to reassign the oop, or
137
consider use of a Handle instead of an oop so there is immunity from object
138
motion. But note that the "QUICK" entries below do not have a handlemark
139
and thus can only support use of handles passed in.
140
*/
141
142
static void trace_class_resolution_impl(Klass* to_class, TRAPS) {
143
ResourceMark rm;
144
int line_number = -1;
145
const char * source_file = NULL;
146
const char * trace = "explicit";
147
InstanceKlass* caller = NULL;
148
JavaThread* jthread = THREAD;
149
if (jthread->has_last_Java_frame()) {
150
vframeStream vfst(jthread);
151
152
// scan up the stack skipping ClassLoader, AccessController and PrivilegedAction frames
153
TempNewSymbol access_controller = SymbolTable::new_symbol("java/security/AccessController");
154
Klass* access_controller_klass = SystemDictionary::resolve_or_fail(access_controller, false, CHECK);
155
TempNewSymbol privileged_action = SymbolTable::new_symbol("java/security/PrivilegedAction");
156
Klass* privileged_action_klass = SystemDictionary::resolve_or_fail(privileged_action, false, CHECK);
157
158
Method* last_caller = NULL;
159
160
while (!vfst.at_end()) {
161
Method* m = vfst.method();
162
if (!vfst.method()->method_holder()->is_subclass_of(vmClasses::ClassLoader_klass())&&
163
!vfst.method()->method_holder()->is_subclass_of(access_controller_klass) &&
164
!vfst.method()->method_holder()->is_subclass_of(privileged_action_klass)) {
165
break;
166
}
167
last_caller = m;
168
vfst.next();
169
}
170
// if this is called from Class.forName0 and that is called from Class.forName,
171
// then print the caller of Class.forName. If this is Class.loadClass, then print
172
// that caller, otherwise keep quiet since this should be picked up elsewhere.
173
bool found_it = false;
174
if (!vfst.at_end() &&
175
vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class() &&
176
vfst.method()->name() == vmSymbols::forName0_name()) {
177
vfst.next();
178
if (!vfst.at_end() &&
179
vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class() &&
180
vfst.method()->name() == vmSymbols::forName_name()) {
181
vfst.next();
182
found_it = true;
183
}
184
} else if (last_caller != NULL &&
185
last_caller->method_holder()->name() ==
186
vmSymbols::java_lang_ClassLoader() &&
187
last_caller->name() == vmSymbols::loadClass_name()) {
188
found_it = true;
189
} else if (!vfst.at_end()) {
190
if (vfst.method()->is_native()) {
191
// JNI call
192
found_it = true;
193
}
194
}
195
if (found_it && !vfst.at_end()) {
196
// found the caller
197
caller = vfst.method()->method_holder();
198
line_number = vfst.method()->line_number_from_bci(vfst.bci());
199
if (line_number == -1) {
200
// show method name if it's a native method
201
trace = vfst.method()->name_and_sig_as_C_string();
202
}
203
Symbol* s = caller->source_file_name();
204
if (s != NULL) {
205
source_file = s->as_C_string();
206
}
207
}
208
}
209
if (caller != NULL) {
210
if (to_class != caller) {
211
const char * from = caller->external_name();
212
const char * to = to_class->external_name();
213
// print in a single call to reduce interleaving between threads
214
if (source_file != NULL) {
215
log_debug(class, resolve)("%s %s %s:%d (%s)", from, to, source_file, line_number, trace);
216
} else {
217
log_debug(class, resolve)("%s %s (%s)", from, to, trace);
218
}
219
}
220
}
221
}
222
223
void trace_class_resolution(Klass* to_class) {
224
EXCEPTION_MARK;
225
trace_class_resolution_impl(to_class, THREAD);
226
if (HAS_PENDING_EXCEPTION) {
227
CLEAR_PENDING_EXCEPTION;
228
}
229
}
230
231
// java.lang.System //////////////////////////////////////////////////////////////////////
232
233
234
JVM_LEAF(jlong, JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored))
235
return os::javaTimeMillis();
236
JVM_END
237
238
JVM_LEAF(jlong, JVM_NanoTime(JNIEnv *env, jclass ignored))
239
return os::javaTimeNanos();
240
JVM_END
241
242
// The function below is actually exposed by jdk.internal.misc.VM and not
243
// java.lang.System, but we choose to keep it here so that it stays next
244
// to JVM_CurrentTimeMillis and JVM_NanoTime
245
246
const jlong MAX_DIFF_SECS = CONST64(0x0100000000); // 2^32
247
const jlong MIN_DIFF_SECS = -MAX_DIFF_SECS; // -2^32
248
249
JVM_LEAF(jlong, JVM_GetNanoTimeAdjustment(JNIEnv *env, jclass ignored, jlong offset_secs))
250
jlong seconds;
251
jlong nanos;
252
253
os::javaTimeSystemUTC(seconds, nanos);
254
255
// We're going to verify that the result can fit in a long.
256
// For that we need the difference in seconds between 'seconds'
257
// and 'offset_secs' to be such that:
258
// |seconds - offset_secs| < (2^63/10^9)
259
// We're going to approximate 10^9 ~< 2^30 (1000^3 ~< 1024^3)
260
// which makes |seconds - offset_secs| < 2^33
261
// and we will prefer +/- 2^32 as the maximum acceptable diff
262
// as 2^32 has a more natural feel than 2^33...
263
//
264
// So if |seconds - offset_secs| >= 2^32 - we return a special
265
// sentinel value (-1) which the caller should take as an
266
// exception value indicating that the offset given to us is
267
// too far from range of the current time - leading to too big
268
// a nano adjustment. The caller is expected to recover by
269
// computing a more accurate offset and calling this method
270
// again. (For the record 2^32 secs is ~136 years, so that
271
// should rarely happen)
272
//
273
jlong diff = seconds - offset_secs;
274
if (diff >= MAX_DIFF_SECS || diff <= MIN_DIFF_SECS) {
275
return -1; // sentinel value: the offset is too far off the target
276
}
277
278
// return the adjustment. If you compute a time by adding
279
// this number of nanoseconds along with the number of seconds
280
// in the offset you should get the current UTC time.
281
return (diff * (jlong)1000000000) + nanos;
282
JVM_END
283
284
JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos,
285
jobject dst, jint dst_pos, jint length))
286
// Check if we have null pointers
287
if (src == NULL || dst == NULL) {
288
THROW(vmSymbols::java_lang_NullPointerException());
289
}
290
arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
291
arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
292
assert(oopDesc::is_oop(s), "JVM_ArrayCopy: src not an oop");
293
assert(oopDesc::is_oop(d), "JVM_ArrayCopy: dst not an oop");
294
// Do copy
295
s->klass()->copy_array(s, src_pos, d, dst_pos, length, thread);
296
JVM_END
297
298
299
static void set_property(Handle props, const char* key, const char* value, TRAPS) {
300
JavaValue r(T_OBJECT);
301
// public synchronized Object put(Object key, Object value);
302
HandleMark hm(THREAD);
303
Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK);
304
Handle value_str = java_lang_String::create_from_platform_dependent_str((value != NULL ? value : ""), CHECK);
305
JavaCalls::call_virtual(&r,
306
props,
307
vmClasses::Properties_klass(),
308
vmSymbols::put_name(),
309
vmSymbols::object_object_object_signature(),
310
key_str,
311
value_str,
312
THREAD);
313
}
314
315
316
#define PUTPROP(props, name, value) set_property((props), (name), (value), CHECK_(properties));
317
318
/*
319
* Return all of the system properties in a Java String array with alternating
320
* names and values from the jvm SystemProperty.
321
* Which includes some internal and all commandline -D defined properties.
322
*/
323
JVM_ENTRY(jobjectArray, JVM_GetProperties(JNIEnv *env))
324
ResourceMark rm(THREAD);
325
HandleMark hm(THREAD);
326
int ndx = 0;
327
int fixedCount = 2;
328
329
SystemProperty* p = Arguments::system_properties();
330
int count = Arguments::PropertyList_count(p);
331
332
// Allocate result String array
333
InstanceKlass* ik = vmClasses::String_klass();
334
objArrayOop r = oopFactory::new_objArray(ik, (count + fixedCount) * 2, CHECK_NULL);
335
objArrayHandle result_h(THREAD, r);
336
337
while (p != NULL) {
338
const char * key = p->key();
339
if (strcmp(key, "sun.nio.MaxDirectMemorySize") != 0) {
340
const char * value = p->value();
341
Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK_NULL);
342
Handle value_str = java_lang_String::create_from_platform_dependent_str((value != NULL ? value : ""), CHECK_NULL);
343
result_h->obj_at_put(ndx * 2, key_str());
344
result_h->obj_at_put(ndx * 2 + 1, value_str());
345
ndx++;
346
}
347
p = p->next();
348
}
349
350
// Convert the -XX:MaxDirectMemorySize= command line flag
351
// to the sun.nio.MaxDirectMemorySize property.
352
// Do this after setting user properties to prevent people
353
// from setting the value with a -D option, as requested.
354
// Leave empty if not supplied
355
if (!FLAG_IS_DEFAULT(MaxDirectMemorySize)) {
356
char as_chars[256];
357
jio_snprintf(as_chars, sizeof(as_chars), JULONG_FORMAT, MaxDirectMemorySize);
358
Handle key_str = java_lang_String::create_from_platform_dependent_str("sun.nio.MaxDirectMemorySize", CHECK_NULL);
359
Handle value_str = java_lang_String::create_from_platform_dependent_str(as_chars, CHECK_NULL);
360
result_h->obj_at_put(ndx * 2, key_str());
361
result_h->obj_at_put(ndx * 2 + 1, value_str());
362
ndx++;
363
}
364
365
// JVM monitoring and management support
366
// Add the sun.management.compiler property for the compiler's name
367
{
368
#undef CSIZE
369
#if defined(_LP64) || defined(_WIN64)
370
#define CSIZE "64-Bit "
371
#else
372
#define CSIZE
373
#endif // 64bit
374
375
#if COMPILER1_AND_COMPILER2
376
const char* compiler_name = "HotSpot " CSIZE "Tiered Compilers";
377
#else
378
#if defined(COMPILER1)
379
const char* compiler_name = "HotSpot " CSIZE "Client Compiler";
380
#elif defined(COMPILER2)
381
const char* compiler_name = "HotSpot " CSIZE "Server Compiler";
382
#elif INCLUDE_JVMCI
383
#error "INCLUDE_JVMCI should imply COMPILER1_OR_COMPILER2"
384
#else
385
const char* compiler_name = "";
386
#endif // compilers
387
#endif // COMPILER1_AND_COMPILER2
388
389
if (*compiler_name != '\0' &&
390
(Arguments::mode() != Arguments::_int)) {
391
Handle key_str = java_lang_String::create_from_platform_dependent_str("sun.management.compiler", CHECK_NULL);
392
Handle value_str = java_lang_String::create_from_platform_dependent_str(compiler_name, CHECK_NULL);
393
result_h->obj_at_put(ndx * 2, key_str());
394
result_h->obj_at_put(ndx * 2 + 1, value_str());
395
ndx++;
396
}
397
}
398
399
return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
400
JVM_END
401
402
403
/*
404
* Return the temporary directory that the VM uses for the attach
405
* and perf data files.
406
*
407
* It is important that this directory is well-known and the
408
* same for all VM instances. It cannot be affected by configuration
409
* variables such as java.io.tmpdir.
410
*/
411
JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
412
HandleMark hm(THREAD);
413
const char* temp_dir = os::get_temp_directory();
414
Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
415
return (jstring) JNIHandles::make_local(THREAD, h());
416
JVM_END
417
418
419
// java.lang.Runtime /////////////////////////////////////////////////////////////////////////
420
421
extern volatile jint vm_created;
422
423
JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
424
#if INCLUDE_CDS
425
// Link all classes for dynamic CDS dumping before vm exit.
426
if (DynamicDumpSharedSpaces) {
427
DynamicArchive::prepare_for_dynamic_dumping_at_exit();
428
}
429
#endif
430
EventShutdown event;
431
if (event.should_commit()) {
432
event.set_reason("Shutdown requested from Java");
433
event.commit();
434
}
435
JVM_END
436
437
438
JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
439
before_exit(thread);
440
vm_exit(code);
441
JVM_END
442
443
444
JVM_ENTRY_NO_ENV(void, JVM_GC(void))
445
if (!DisableExplicitGC) {
446
Universe::heap()->collect(GCCause::_java_lang_system_gc);
447
}
448
JVM_END
449
450
451
JVM_LEAF(jlong, JVM_MaxObjectInspectionAge(void))
452
return Universe::heap()->millis_since_last_whole_heap_examined();
453
JVM_END
454
455
456
static inline jlong convert_size_t_to_jlong(size_t val) {
457
// In the 64-bit vm, a size_t can overflow a jlong (which is signed).
458
NOT_LP64 (return (jlong)val;)
459
LP64_ONLY(return (jlong)MIN2(val, (size_t)max_jlong);)
460
}
461
462
JVM_ENTRY_NO_ENV(jlong, JVM_TotalMemory(void))
463
size_t n = Universe::heap()->capacity();
464
return convert_size_t_to_jlong(n);
465
JVM_END
466
467
468
JVM_ENTRY_NO_ENV(jlong, JVM_FreeMemory(void))
469
size_t n = Universe::heap()->unused();
470
return convert_size_t_to_jlong(n);
471
JVM_END
472
473
474
JVM_ENTRY_NO_ENV(jlong, JVM_MaxMemory(void))
475
size_t n = Universe::heap()->max_capacity();
476
return convert_size_t_to_jlong(n);
477
JVM_END
478
479
480
JVM_ENTRY_NO_ENV(jint, JVM_ActiveProcessorCount(void))
481
return os::active_processor_count();
482
JVM_END
483
484
JVM_ENTRY_NO_ENV(jboolean, JVM_IsUseContainerSupport(void))
485
#ifdef LINUX
486
if (UseContainerSupport) {
487
return JNI_TRUE;
488
}
489
#endif
490
return JNI_FALSE;
491
JVM_END
492
493
// java.lang.Throwable //////////////////////////////////////////////////////
494
495
JVM_ENTRY(void, JVM_FillInStackTrace(JNIEnv *env, jobject receiver))
496
Handle exception(thread, JNIHandles::resolve_non_null(receiver));
497
java_lang_Throwable::fill_in_stack_trace(exception);
498
JVM_END
499
500
// java.lang.NullPointerException ///////////////////////////////////////////
501
502
JVM_ENTRY(jstring, JVM_GetExtendedNPEMessage(JNIEnv *env, jthrowable throwable))
503
if (!ShowCodeDetailsInExceptionMessages) return NULL;
504
505
oop exc = JNIHandles::resolve_non_null(throwable);
506
507
Method* method;
508
int bci;
509
if (!java_lang_Throwable::get_top_method_and_bci(exc, &method, &bci)) {
510
return NULL;
511
}
512
if (method->is_native()) {
513
return NULL;
514
}
515
516
stringStream ss;
517
bool ok = BytecodeUtils::get_NPE_message_at(&ss, method, bci);
518
if (ok) {
519
oop result = java_lang_String::create_oop_from_str(ss.base(), CHECK_NULL);
520
return (jstring) JNIHandles::make_local(THREAD, result);
521
} else {
522
return NULL;
523
}
524
JVM_END
525
526
// java.lang.StackTraceElement //////////////////////////////////////////////
527
528
529
JVM_ENTRY(void, JVM_InitStackTraceElementArray(JNIEnv *env, jobjectArray elements, jobject throwable))
530
Handle exception(THREAD, JNIHandles::resolve(throwable));
531
objArrayOop st = objArrayOop(JNIHandles::resolve(elements));
532
objArrayHandle stack_trace(THREAD, st);
533
// Fill in the allocated stack trace
534
java_lang_Throwable::get_stack_trace_elements(exception, stack_trace, CHECK);
535
JVM_END
536
537
538
JVM_ENTRY(void, JVM_InitStackTraceElement(JNIEnv* env, jobject element, jobject stackFrameInfo))
539
Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(stackFrameInfo));
540
Handle stack_trace_element(THREAD, JNIHandles::resolve_non_null(element));
541
java_lang_StackFrameInfo::to_stack_trace_element(stack_frame_info, stack_trace_element, THREAD);
542
JVM_END
543
544
545
// java.lang.StackWalker //////////////////////////////////////////////////////
546
547
548
JVM_ENTRY(jobject, JVM_CallStackWalk(JNIEnv *env, jobject stackStream, jlong mode,
549
jint skip_frames, jint frame_count, jint start_index,
550
jobjectArray frames))
551
if (!thread->has_last_Java_frame()) {
552
THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: no stack trace", NULL);
553
}
554
555
Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
556
557
// frames array is a Class<?>[] array when only getting caller reference,
558
// and a StackFrameInfo[] array (or derivative) otherwise. It should never
559
// be null.
560
objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
561
objArrayHandle frames_array_h(THREAD, fa);
562
563
int limit = start_index + frame_count;
564
if (frames_array_h->length() < limit) {
565
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers", NULL);
566
}
567
568
oop result = StackWalk::walk(stackStream_h, mode, skip_frames, frame_count,
569
start_index, frames_array_h, CHECK_NULL);
570
return JNIHandles::make_local(THREAD, result);
571
JVM_END
572
573
574
JVM_ENTRY(jint, JVM_MoreStackWalk(JNIEnv *env, jobject stackStream, jlong mode, jlong anchor,
575
jint frame_count, jint start_index,
576
jobjectArray frames))
577
// frames array is a Class<?>[] array when only getting caller reference,
578
// and a StackFrameInfo[] array (or derivative) otherwise. It should never
579
// be null.
580
objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
581
objArrayHandle frames_array_h(THREAD, fa);
582
583
int limit = start_index+frame_count;
584
if (frames_array_h->length() < limit) {
585
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers");
586
}
587
588
Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
589
return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, frame_count,
590
start_index, frames_array_h, THREAD);
591
JVM_END
592
593
// java.lang.Object ///////////////////////////////////////////////
594
595
596
JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
597
// as implemented in the classic virtual machine; return 0 if object is NULL
598
return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;
599
JVM_END
600
601
602
JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
603
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
604
JavaThreadInObjectWaitState jtiows(thread, ms != 0);
605
if (JvmtiExport::should_post_monitor_wait()) {
606
JvmtiExport::post_monitor_wait(thread, obj(), ms);
607
608
// The current thread already owns the monitor and it has not yet
609
// been added to the wait queue so the current thread cannot be
610
// made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
611
// event handler cannot accidentally consume an unpark() meant for
612
// the ParkEvent associated with this ObjectMonitor.
613
}
614
ObjectSynchronizer::wait(obj, ms, CHECK);
615
JVM_END
616
617
618
JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
619
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
620
ObjectSynchronizer::notify(obj, CHECK);
621
JVM_END
622
623
624
JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
625
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
626
ObjectSynchronizer::notifyall(obj, CHECK);
627
JVM_END
628
629
630
JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
631
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
632
Klass* klass = obj->klass();
633
JvmtiVMObjectAllocEventCollector oam;
634
635
#ifdef ASSERT
636
// Just checking that the cloneable flag is set correct
637
if (obj->is_array()) {
638
guarantee(klass->is_cloneable(), "all arrays are cloneable");
639
} else {
640
guarantee(obj->is_instance(), "should be instanceOop");
641
bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
642
guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
643
}
644
#endif
645
646
// Check if class of obj supports the Cloneable interface.
647
// All arrays are considered to be cloneable (See JLS 20.1.5).
648
// All j.l.r.Reference classes are considered non-cloneable.
649
if (!klass->is_cloneable() ||
650
(klass->is_instance_klass() &&
651
InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
652
ResourceMark rm(THREAD);
653
THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
654
}
655
656
// Make shallow object copy
657
const int size = obj->size();
658
oop new_obj_oop = NULL;
659
if (obj->is_array()) {
660
const int length = ((arrayOop)obj())->length();
661
new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
662
/* do_zero */ true, CHECK_NULL);
663
} else {
664
new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
665
}
666
667
HeapAccess<>::clone(obj(), new_obj_oop, size);
668
669
Handle new_obj(THREAD, new_obj_oop);
670
// Caution: this involves a java upcall, so the clone should be
671
// "gc-robust" by this stage.
672
if (klass->has_finalizer()) {
673
assert(obj->is_instance(), "should be instanceOop");
674
new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
675
new_obj = Handle(THREAD, new_obj_oop);
676
}
677
678
return JNIHandles::make_local(THREAD, new_obj());
679
JVM_END
680
681
// java.io.File ///////////////////////////////////////////////////////////////
682
683
JVM_LEAF(char*, JVM_NativePath(char* path))
684
return os::native_path(path);
685
JVM_END
686
687
688
// Misc. class handling ///////////////////////////////////////////////////////////
689
690
691
JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env))
692
// Getting the class of the caller frame.
693
//
694
// The call stack at this point looks something like this:
695
//
696
// [0] [ @CallerSensitive public sun.reflect.Reflection.getCallerClass ]
697
// [1] [ @CallerSensitive API.method ]
698
// [.] [ (skipped intermediate frames) ]
699
// [n] [ caller ]
700
vframeStream vfst(thread);
701
// Cf. LibraryCallKit::inline_native_Reflection_getCallerClass
702
for (int n = 0; !vfst.at_end(); vfst.security_next(), n++) {
703
Method* m = vfst.method();
704
assert(m != NULL, "sanity");
705
switch (n) {
706
case 0:
707
// This must only be called from Reflection.getCallerClass
708
if (m->intrinsic_id() != vmIntrinsics::_getCallerClass) {
709
THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetCallerClass must only be called from Reflection.getCallerClass");
710
}
711
// fall-through
712
case 1:
713
// Frame 0 and 1 must be caller sensitive.
714
if (!m->caller_sensitive()) {
715
THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), err_msg("CallerSensitive annotation expected at frame %d", n));
716
}
717
break;
718
default:
719
if (!m->is_ignored_by_security_stack_walk()) {
720
// We have reached the desired frame; return the holder class.
721
return (jclass) JNIHandles::make_local(THREAD, m->method_holder()->java_mirror());
722
}
723
break;
724
}
725
}
726
return NULL;
727
JVM_END
728
729
730
JVM_ENTRY(jclass, JVM_FindPrimitiveClass(JNIEnv* env, const char* utf))
731
oop mirror = NULL;
732
BasicType t = name2type(utf);
733
if (t != T_ILLEGAL && !is_reference_type(t)) {
734
mirror = Universe::java_mirror(t);
735
}
736
if (mirror == NULL) {
737
THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), (char*) utf);
738
} else {
739
return (jclass) JNIHandles::make_local(THREAD, mirror);
740
}
741
JVM_END
742
743
744
// Returns a class loaded by the bootstrap class loader; or null
745
// if not found. ClassNotFoundException is not thrown.
746
// FindClassFromBootLoader is exported to the launcher for windows.
747
JVM_ENTRY(jclass, JVM_FindClassFromBootLoader(JNIEnv* env,
748
const char* name))
749
// Java libraries should ensure that name is never null or illegal.
750
if (name == NULL || (int)strlen(name) > Symbol::max_length()) {
751
// It's impossible to create this class; the name cannot fit
752
// into the constant pool.
753
return NULL;
754
}
755
assert(UTF8::is_legal_utf8((const unsigned char*)name, (int)strlen(name), false), "illegal UTF name");
756
757
TempNewSymbol h_name = SymbolTable::new_symbol(name);
758
Klass* k = SystemDictionary::resolve_or_null(h_name, CHECK_NULL);
759
if (k == NULL) {
760
return NULL;
761
}
762
763
if (log_is_enabled(Debug, class, resolve)) {
764
trace_class_resolution(k);
765
}
766
return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
767
JVM_END
768
769
// Find a class with this name in this loader, using the caller's protection domain.
770
JVM_ENTRY(jclass, JVM_FindClassFromCaller(JNIEnv* env, const char* name,
771
jboolean init, jobject loader,
772
jclass caller))
773
TempNewSymbol h_name =
774
SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_ClassNotFoundException(),
775
CHECK_NULL);
776
777
oop loader_oop = JNIHandles::resolve(loader);
778
oop from_class = JNIHandles::resolve(caller);
779
oop protection_domain = NULL;
780
// If loader is null, shouldn't call ClassLoader.checkPackageAccess; otherwise get
781
// NPE. Put it in another way, the bootstrap class loader has all permission and
782
// thus no checkPackageAccess equivalence in the VM class loader.
783
// The caller is also passed as NULL by the java code if there is no security
784
// manager to avoid the performance cost of getting the calling class.
785
if (from_class != NULL && loader_oop != NULL) {
786
protection_domain = java_lang_Class::as_Klass(from_class)->protection_domain();
787
}
788
789
Handle h_loader(THREAD, loader_oop);
790
Handle h_prot(THREAD, protection_domain);
791
jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
792
h_prot, false, THREAD);
793
794
if (log_is_enabled(Debug, class, resolve) && result != NULL) {
795
trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
796
}
797
return result;
798
JVM_END
799
800
// Currently only called from the old verifier.
801
JVM_ENTRY(jclass, JVM_FindClassFromClass(JNIEnv *env, const char *name,
802
jboolean init, jclass from))
803
TempNewSymbol h_name =
804
SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_ClassNotFoundException(),
805
CHECK_NULL);
806
oop from_class_oop = JNIHandles::resolve(from);
807
Klass* from_class = (from_class_oop == NULL)
808
? (Klass*)NULL
809
: java_lang_Class::as_Klass(from_class_oop);
810
oop class_loader = NULL;
811
oop protection_domain = NULL;
812
if (from_class != NULL) {
813
class_loader = from_class->class_loader();
814
protection_domain = from_class->protection_domain();
815
}
816
Handle h_loader(THREAD, class_loader);
817
Handle h_prot (THREAD, protection_domain);
818
jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
819
h_prot, true, thread);
820
821
if (log_is_enabled(Debug, class, resolve) && result != NULL) {
822
// this function is generally only used for class loading during verification.
823
ResourceMark rm;
824
oop from_mirror = JNIHandles::resolve_non_null(from);
825
Klass* from_class = java_lang_Class::as_Klass(from_mirror);
826
const char * from_name = from_class->external_name();
827
828
oop mirror = JNIHandles::resolve_non_null(result);
829
Klass* to_class = java_lang_Class::as_Klass(mirror);
830
const char * to = to_class->external_name();
831
log_debug(class, resolve)("%s %s (verification)", from_name, to);
832
}
833
834
return result;
835
JVM_END
836
837
// common code for JVM_DefineClass() and JVM_DefineClassWithSource()
838
static jclass jvm_define_class_common(const char *name,
839
jobject loader, const jbyte *buf,
840
jsize len, jobject pd, const char *source,
841
TRAPS) {
842
if (source == NULL) source = "__JVM_DefineClass__";
843
844
JavaThread* jt = THREAD;
845
846
PerfClassTraceTime vmtimer(ClassLoader::perf_define_appclass_time(),
847
ClassLoader::perf_define_appclass_selftime(),
848
ClassLoader::perf_define_appclasses(),
849
jt->get_thread_stat()->perf_recursion_counts_addr(),
850
jt->get_thread_stat()->perf_timers_addr(),
851
PerfClassTraceTime::DEFINE_CLASS);
852
853
if (UsePerfData) {
854
ClassLoader::perf_app_classfile_bytes_read()->inc(len);
855
}
856
857
// Class resolution will get the class name from the .class stream if the name is null.
858
TempNewSymbol class_name = name == NULL ? NULL :
859
SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_NoClassDefFoundError(),
860
CHECK_NULL);
861
862
ResourceMark rm(THREAD);
863
ClassFileStream st((u1*)buf, len, source, ClassFileStream::verify);
864
Handle class_loader (THREAD, JNIHandles::resolve(loader));
865
Handle protection_domain (THREAD, JNIHandles::resolve(pd));
866
ClassLoadInfo cl_info(protection_domain);
867
Klass* k = SystemDictionary::resolve_from_stream(&st, class_name,
868
class_loader,
869
cl_info,
870
CHECK_NULL);
871
872
if (log_is_enabled(Debug, class, resolve)) {
873
trace_class_resolution(k);
874
}
875
876
return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
877
}
878
879
enum {
880
NESTMATE = java_lang_invoke_MemberName::MN_NESTMATE_CLASS,
881
HIDDEN_CLASS = java_lang_invoke_MemberName::MN_HIDDEN_CLASS,
882
STRONG_LOADER_LINK = java_lang_invoke_MemberName::MN_STRONG_LOADER_LINK,
883
ACCESS_VM_ANNOTATIONS = java_lang_invoke_MemberName::MN_ACCESS_VM_ANNOTATIONS
884
};
885
886
/*
887
* Define a class with the specified flags that indicates if it's a nestmate,
888
* hidden, or strongly referenced from class loader.
889
*/
890
static jclass jvm_lookup_define_class(jclass lookup, const char *name,
891
const jbyte *buf, jsize len, jobject pd,
892
jboolean init, int flags, jobject classData, TRAPS) {
893
ResourceMark rm(THREAD);
894
895
Klass* lookup_k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(lookup));
896
// Lookup class must be a non-null instance
897
if (lookup_k == NULL) {
898
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
899
}
900
assert(lookup_k->is_instance_klass(), "Lookup class must be an instance klass");
901
902
Handle class_loader (THREAD, lookup_k->class_loader());
903
904
bool is_nestmate = (flags & NESTMATE) == NESTMATE;
905
bool is_hidden = (flags & HIDDEN_CLASS) == HIDDEN_CLASS;
906
bool is_strong = (flags & STRONG_LOADER_LINK) == STRONG_LOADER_LINK;
907
bool vm_annotations = (flags & ACCESS_VM_ANNOTATIONS) == ACCESS_VM_ANNOTATIONS;
908
909
InstanceKlass* host_class = NULL;
910
if (is_nestmate) {
911
host_class = InstanceKlass::cast(lookup_k)->nest_host(CHECK_NULL);
912
}
913
914
log_info(class, nestmates)("LookupDefineClass: %s - %s%s, %s, %s, %s",
915
name,
916
is_nestmate ? "with dynamic nest-host " : "non-nestmate",
917
is_nestmate ? host_class->external_name() : "",
918
is_hidden ? "hidden" : "not hidden",
919
is_strong ? "strong" : "weak",
920
vm_annotations ? "with vm annotations" : "without vm annotation");
921
922
if (!is_hidden) {
923
// classData is only applicable for hidden classes
924
if (classData != NULL) {
925
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "classData is only applicable for hidden classes");
926
}
927
if (is_nestmate) {
928
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "dynamic nestmate is only applicable for hidden classes");
929
}
930
if (!is_strong) {
931
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "an ordinary class must be strongly referenced by its defining loader");
932
}
933
if (vm_annotations) {
934
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "vm annotations only allowed for hidden classes");
935
}
936
if (flags != STRONG_LOADER_LINK) {
937
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
938
err_msg("invalid flag 0x%x", flags));
939
}
940
}
941
942
// Class resolution will get the class name from the .class stream if the name is null.
943
TempNewSymbol class_name = name == NULL ? NULL :
944
SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_NoClassDefFoundError(),
945
CHECK_NULL);
946
947
Handle protection_domain (THREAD, JNIHandles::resolve(pd));
948
const char* source = is_nestmate ? host_class->external_name() : "__JVM_LookupDefineClass__";
949
ClassFileStream st((u1*)buf, len, source, ClassFileStream::verify);
950
951
InstanceKlass* ik = NULL;
952
if (!is_hidden) {
953
ClassLoadInfo cl_info(protection_domain);
954
ik = SystemDictionary::resolve_from_stream(&st, class_name,
955
class_loader,
956
cl_info,
957
CHECK_NULL);
958
959
if (log_is_enabled(Debug, class, resolve)) {
960
trace_class_resolution(ik);
961
}
962
} else { // hidden
963
Handle classData_h(THREAD, JNIHandles::resolve(classData));
964
ClassLoadInfo cl_info(protection_domain,
965
host_class,
966
classData_h,
967
is_hidden,
968
is_strong,
969
vm_annotations);
970
ik = SystemDictionary::resolve_from_stream(&st, class_name,
971
class_loader,
972
cl_info,
973
CHECK_NULL);
974
975
// The hidden class loader data has been artificially been kept alive to
976
// this point. The mirror and any instances of this class have to keep
977
// it alive afterwards.
978
ik->class_loader_data()->dec_keep_alive();
979
980
if (is_nestmate && log_is_enabled(Debug, class, nestmates)) {
981
ModuleEntry* module = ik->module();
982
const char * module_name = module->is_named() ? module->name()->as_C_string() : UNNAMED_MODULE;
983
log_debug(class, nestmates)("Dynamic nestmate: %s/%s, nest_host %s, %s",
984
module_name,
985
ik->external_name(),
986
host_class->external_name(),
987
ik->is_hidden() ? "is hidden" : "is not hidden");
988
}
989
}
990
assert(Reflection::is_same_class_package(lookup_k, ik),
991
"lookup class and defined class are in different packages");
992
993
if (init) {
994
ik->initialize(CHECK_NULL);
995
} else {
996
ik->link_class(CHECK_NULL);
997
}
998
999
return (jclass) JNIHandles::make_local(THREAD, ik->java_mirror());
1000
}
1001
1002
JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
1003
return jvm_define_class_common(name, loader, buf, len, pd, NULL, THREAD);
1004
JVM_END
1005
1006
/*
1007
* Define a class with the specified lookup class.
1008
* lookup: Lookup class
1009
* name: the name of the class
1010
* buf: class bytes
1011
* len: length of class bytes
1012
* pd: protection domain
1013
* init: initialize the class
1014
* flags: properties of the class
1015
* classData: private static pre-initialized field
1016
*/
1017
JVM_ENTRY(jclass, JVM_LookupDefineClass(JNIEnv *env, jclass lookup, const char *name, const jbyte *buf,
1018
jsize len, jobject pd, jboolean initialize, int flags, jobject classData))
1019
1020
if (lookup == NULL) {
1021
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
1022
}
1023
1024
assert(buf != NULL, "buf must not be NULL");
1025
1026
return jvm_lookup_define_class(lookup, name, buf, len, pd, initialize, flags, classData, THREAD);
1027
JVM_END
1028
1029
JVM_ENTRY(jclass, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source))
1030
1031
return jvm_define_class_common(name, loader, buf, len, pd, source, THREAD);
1032
JVM_END
1033
1034
JVM_ENTRY(jclass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name))
1035
ResourceMark rm(THREAD);
1036
1037
Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
1038
char* str = java_lang_String::as_utf8_string(h_name());
1039
1040
// Sanity check, don't expect null
1041
if (str == NULL) return NULL;
1042
1043
// Internalize the string, converting '.' to '/' in string.
1044
char* p = (char*)str;
1045
while (*p != '\0') {
1046
if (*p == '.') {
1047
*p = '/';
1048
}
1049
p++;
1050
}
1051
1052
const int str_len = (int)(p - str);
1053
if (str_len > Symbol::max_length()) {
1054
// It's impossible to create this class; the name cannot fit
1055
// into the constant pool.
1056
return NULL;
1057
}
1058
TempNewSymbol klass_name = SymbolTable::new_symbol(str, str_len);
1059
1060
// Security Note:
1061
// The Java level wrapper will perform the necessary security check allowing
1062
// us to pass the NULL as the initiating class loader.
1063
Handle h_loader(THREAD, JNIHandles::resolve(loader));
1064
Klass* k = SystemDictionary::find_instance_or_array_klass(klass_name,
1065
h_loader,
1066
Handle());
1067
#if INCLUDE_CDS
1068
if (k == NULL) {
1069
// If the class is not already loaded, try to see if it's in the shared
1070
// archive for the current classloader (h_loader).
1071
k = SystemDictionaryShared::find_or_load_shared_class(klass_name, h_loader, CHECK_NULL);
1072
}
1073
#endif
1074
return (k == NULL) ? NULL :
1075
(jclass) JNIHandles::make_local(THREAD, k->java_mirror());
1076
JVM_END
1077
1078
// Module support //////////////////////////////////////////////////////////////////////////////
1079
1080
JVM_ENTRY(void, JVM_DefineModule(JNIEnv *env, jobject module, jboolean is_open, jstring version,
1081
jstring location, jobjectArray packages))
1082
Handle h_module (THREAD, JNIHandles::resolve(module));
1083
Modules::define_module(h_module, is_open, version, location, packages, CHECK);
1084
JVM_END
1085
1086
JVM_ENTRY(void, JVM_SetBootLoaderUnnamedModule(JNIEnv *env, jobject module))
1087
Handle h_module (THREAD, JNIHandles::resolve(module));
1088
Modules::set_bootloader_unnamed_module(h_module, CHECK);
1089
JVM_END
1090
1091
JVM_ENTRY(void, JVM_AddModuleExports(JNIEnv *env, jobject from_module, jstring package, jobject to_module))
1092
Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1093
Handle h_to_module (THREAD, JNIHandles::resolve(to_module));
1094
Modules::add_module_exports_qualified(h_from_module, package, h_to_module, CHECK);
1095
JVM_END
1096
1097
JVM_ENTRY(void, JVM_AddModuleExportsToAllUnnamed(JNIEnv *env, jobject from_module, jstring package))
1098
Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1099
Modules::add_module_exports_to_all_unnamed(h_from_module, package, CHECK);
1100
JVM_END
1101
1102
JVM_ENTRY(void, JVM_AddModuleExportsToAll(JNIEnv *env, jobject from_module, jstring package))
1103
Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1104
Modules::add_module_exports(h_from_module, package, Handle(), CHECK);
1105
JVM_END
1106
1107
JVM_ENTRY (void, JVM_AddReadsModule(JNIEnv *env, jobject from_module, jobject source_module))
1108
Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1109
Handle h_source_module (THREAD, JNIHandles::resolve(source_module));
1110
Modules::add_reads_module(h_from_module, h_source_module, CHECK);
1111
JVM_END
1112
1113
JVM_ENTRY(void, JVM_DefineArchivedModules(JNIEnv *env, jobject platform_loader, jobject system_loader))
1114
Handle h_platform_loader (THREAD, JNIHandles::resolve(platform_loader));
1115
Handle h_system_loader (THREAD, JNIHandles::resolve(system_loader));
1116
Modules::define_archived_modules(h_platform_loader, h_system_loader, CHECK);
1117
JVM_END
1118
1119
// Reflection support //////////////////////////////////////////////////////////////////////////////
1120
1121
JVM_ENTRY(jstring, JVM_InitClassName(JNIEnv *env, jclass cls))
1122
assert (cls != NULL, "illegal class");
1123
JvmtiVMObjectAllocEventCollector oam;
1124
ResourceMark rm(THREAD);
1125
HandleMark hm(THREAD);
1126
Handle java_class(THREAD, JNIHandles::resolve(cls));
1127
oop result = java_lang_Class::name(java_class, CHECK_NULL);
1128
return (jstring) JNIHandles::make_local(THREAD, result);
1129
JVM_END
1130
1131
1132
JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1133
JvmtiVMObjectAllocEventCollector oam;
1134
oop mirror = JNIHandles::resolve_non_null(cls);
1135
1136
// Special handling for primitive objects
1137
if (java_lang_Class::is_primitive(mirror)) {
1138
// Primitive objects does not have any interfaces
1139
objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1140
return (jobjectArray) JNIHandles::make_local(THREAD, r);
1141
}
1142
1143
Klass* klass = java_lang_Class::as_Klass(mirror);
1144
// Figure size of result array
1145
int size;
1146
if (klass->is_instance_klass()) {
1147
size = InstanceKlass::cast(klass)->local_interfaces()->length();
1148
} else {
1149
assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1150
size = 2;
1151
}
1152
1153
// Allocate result array
1154
objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1155
objArrayHandle result (THREAD, r);
1156
// Fill in result
1157
if (klass->is_instance_klass()) {
1158
// Regular instance klass, fill in all local interfaces
1159
for (int index = 0; index < size; index++) {
1160
Klass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
1161
result->obj_at_put(index, k->java_mirror());
1162
}
1163
} else {
1164
// All arrays implement java.lang.Cloneable and java.io.Serializable
1165
result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1166
result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1167
}
1168
return (jobjectArray) JNIHandles::make_local(THREAD, result());
1169
JVM_END
1170
1171
1172
JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
1173
oop mirror = JNIHandles::resolve_non_null(cls);
1174
if (java_lang_Class::is_primitive(mirror)) {
1175
return JNI_FALSE;
1176
}
1177
Klass* k = java_lang_Class::as_Klass(mirror);
1178
jboolean result = k->is_interface();
1179
assert(!result || k->is_instance_klass(),
1180
"all interfaces are instance types");
1181
// The compiler intrinsic for isInterface tests the
1182
// Klass::_access_flags bits in the same way.
1183
return result;
1184
JVM_END
1185
1186
JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1187
oop mirror = JNIHandles::resolve_non_null(cls);
1188
if (java_lang_Class::is_primitive(mirror)) {
1189
return JNI_FALSE;
1190
}
1191
Klass* k = java_lang_Class::as_Klass(mirror);
1192
return k->is_hidden();
1193
JVM_END
1194
1195
JVM_ENTRY(jobjectArray, JVM_GetClassSigners(JNIEnv *env, jclass cls))
1196
JvmtiVMObjectAllocEventCollector oam;
1197
oop mirror = JNIHandles::resolve_non_null(cls);
1198
if (java_lang_Class::is_primitive(mirror)) {
1199
// There are no signers for primitive types
1200
return NULL;
1201
}
1202
1203
objArrayHandle signers(THREAD, java_lang_Class::signers(mirror));
1204
1205
// If there are no signers set in the class, or if the class
1206
// is an array, return NULL.
1207
if (signers == NULL) return NULL;
1208
1209
// copy of the signers array
1210
Klass* element = ObjArrayKlass::cast(signers->klass())->element_klass();
1211
objArrayOop signers_copy = oopFactory::new_objArray(element, signers->length(), CHECK_NULL);
1212
for (int index = 0; index < signers->length(); index++) {
1213
signers_copy->obj_at_put(index, signers->obj_at(index));
1214
}
1215
1216
// return the copy
1217
return (jobjectArray) JNIHandles::make_local(THREAD, signers_copy);
1218
JVM_END
1219
1220
1221
JVM_ENTRY(void, JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers))
1222
oop mirror = JNIHandles::resolve_non_null(cls);
1223
if (!java_lang_Class::is_primitive(mirror)) {
1224
// This call is ignored for primitive types and arrays.
1225
// Signers are only set once, ClassLoader.java, and thus shouldn't
1226
// be called with an array. Only the bootstrap loader creates arrays.
1227
Klass* k = java_lang_Class::as_Klass(mirror);
1228
if (k->is_instance_klass()) {
1229
java_lang_Class::set_signers(k->java_mirror(), objArrayOop(JNIHandles::resolve(signers)));
1230
}
1231
}
1232
JVM_END
1233
1234
1235
JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
1236
oop mirror = JNIHandles::resolve_non_null(cls);
1237
if (mirror == NULL) {
1238
THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
1239
}
1240
1241
if (java_lang_Class::is_primitive(mirror)) {
1242
// Primitive types does not have a protection domain.
1243
return NULL;
1244
}
1245
1246
oop pd = java_lang_Class::protection_domain(mirror);
1247
return (jobject) JNIHandles::make_local(THREAD, pd);
1248
JVM_END
1249
1250
1251
// Returns the inherited_access_control_context field of the running thread.
1252
JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls))
1253
oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj());
1254
return JNIHandles::make_local(THREAD, result);
1255
JVM_END
1256
1257
JVM_ENTRY(jobject, JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls))
1258
if (!UsePrivilegedStack) return NULL;
1259
1260
ResourceMark rm(THREAD);
1261
GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1262
JvmtiVMObjectAllocEventCollector oam;
1263
1264
// count the protection domains on the execution stack. We collapse
1265
// duplicate consecutive protection domains into a single one, as
1266
// well as stopping when we hit a privileged frame.
1267
1268
oop previous_protection_domain = NULL;
1269
Handle privileged_context(thread, NULL);
1270
bool is_privileged = false;
1271
oop protection_domain = NULL;
1272
1273
// Iterate through Java frames
1274
vframeStream vfst(thread);
1275
for(; !vfst.at_end(); vfst.next()) {
1276
// get method of frame
1277
Method* method = vfst.method();
1278
1279
// stop at the first privileged frame
1280
if (method->method_holder() == vmClasses::AccessController_klass() &&
1281
method->name() == vmSymbols::executePrivileged_name())
1282
{
1283
// this frame is privileged
1284
is_privileged = true;
1285
1286
javaVFrame *priv = vfst.asJavaVFrame(); // executePrivileged
1287
1288
StackValueCollection* locals = priv->locals();
1289
StackValue* ctx_sv = locals->at(1); // AccessControlContext context
1290
StackValue* clr_sv = locals->at(2); // Class<?> caller
1291
assert(!ctx_sv->obj_is_scalar_replaced(), "found scalar-replaced object");
1292
assert(!clr_sv->obj_is_scalar_replaced(), "found scalar-replaced object");
1293
privileged_context = ctx_sv->get_obj();
1294
Handle caller = clr_sv->get_obj();
1295
1296
Klass *caller_klass = java_lang_Class::as_Klass(caller());
1297
protection_domain = caller_klass->protection_domain();
1298
} else {
1299
protection_domain = method->method_holder()->protection_domain();
1300
}
1301
1302
if ((previous_protection_domain != protection_domain) && (protection_domain != NULL)) {
1303
local_array->push(Handle(thread, protection_domain));
1304
previous_protection_domain = protection_domain;
1305
}
1306
1307
if (is_privileged) break;
1308
}
1309
1310
1311
// either all the domains on the stack were system domains, or
1312
// we had a privileged system domain
1313
if (local_array->is_empty()) {
1314
if (is_privileged && privileged_context.is_null()) return NULL;
1315
1316
oop result = java_security_AccessControlContext::create(objArrayHandle(), is_privileged, privileged_context, CHECK_NULL);
1317
return JNIHandles::make_local(THREAD, result);
1318
}
1319
1320
objArrayOop context = oopFactory::new_objArray(vmClasses::ProtectionDomain_klass(),
1321
local_array->length(), CHECK_NULL);
1322
objArrayHandle h_context(thread, context);
1323
for (int index = 0; index < local_array->length(); index++) {
1324
h_context->obj_at_put(index, local_array->at(index)());
1325
}
1326
1327
oop result = java_security_AccessControlContext::create(h_context, is_privileged, privileged_context, CHECK_NULL);
1328
1329
return JNIHandles::make_local(THREAD, result);
1330
JVM_END
1331
1332
1333
JVM_ENTRY(jboolean, JVM_IsArrayClass(JNIEnv *env, jclass cls))
1334
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1335
return (k != NULL) && k->is_array_klass() ? true : false;
1336
JVM_END
1337
1338
1339
JVM_ENTRY(jboolean, JVM_IsPrimitiveClass(JNIEnv *env, jclass cls))
1340
oop mirror = JNIHandles::resolve_non_null(cls);
1341
return (jboolean) java_lang_Class::is_primitive(mirror);
1342
JVM_END
1343
1344
1345
JVM_ENTRY(jint, JVM_GetClassModifiers(JNIEnv *env, jclass cls))
1346
oop mirror = JNIHandles::resolve_non_null(cls);
1347
if (java_lang_Class::is_primitive(mirror)) {
1348
// Primitive type
1349
return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1350
}
1351
1352
Klass* k = java_lang_Class::as_Klass(mirror);
1353
debug_only(int computed_modifiers = k->compute_modifier_flags());
1354
assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK");
1355
return k->modifier_flags();
1356
JVM_END
1357
1358
1359
// Inner class reflection ///////////////////////////////////////////////////////////////////////////////
1360
1361
JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
1362
JvmtiVMObjectAllocEventCollector oam;
1363
// ofClass is a reference to a java_lang_Class object. The mirror object
1364
// of an InstanceKlass
1365
oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1366
if (java_lang_Class::is_primitive(ofMirror) ||
1367
! java_lang_Class::as_Klass(ofMirror)->is_instance_klass()) {
1368
oop result = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1369
return (jobjectArray)JNIHandles::make_local(THREAD, result);
1370
}
1371
1372
InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1373
InnerClassesIterator iter(k);
1374
1375
if (iter.length() == 0) {
1376
// Neither an inner nor outer class
1377
oop result = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1378
return (jobjectArray)JNIHandles::make_local(THREAD, result);
1379
}
1380
1381
// find inner class info
1382
constantPoolHandle cp(thread, k->constants());
1383
int length = iter.length();
1384
1385
// Allocate temp. result array
1386
objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), length/4, CHECK_NULL);
1387
objArrayHandle result (THREAD, r);
1388
int members = 0;
1389
1390
for (; !iter.done(); iter.next()) {
1391
int ioff = iter.inner_class_info_index();
1392
int ooff = iter.outer_class_info_index();
1393
1394
if (ioff != 0 && ooff != 0) {
1395
// Check to see if the name matches the class we're looking for
1396
// before attempting to find the class.
1397
if (cp->klass_name_at_matches(k, ooff)) {
1398
Klass* outer_klass = cp->klass_at(ooff, CHECK_NULL);
1399
if (outer_klass == k) {
1400
Klass* ik = cp->klass_at(ioff, CHECK_NULL);
1401
InstanceKlass* inner_klass = InstanceKlass::cast(ik);
1402
1403
// Throws an exception if outer klass has not declared k as
1404
// an inner klass
1405
Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL);
1406
1407
result->obj_at_put(members, inner_klass->java_mirror());
1408
members++;
1409
}
1410
}
1411
}
1412
}
1413
1414
if (members != length) {
1415
// Return array of right length
1416
objArrayOop res = oopFactory::new_objArray(vmClasses::Class_klass(), members, CHECK_NULL);
1417
for(int i = 0; i < members; i++) {
1418
res->obj_at_put(i, result->obj_at(i));
1419
}
1420
return (jobjectArray)JNIHandles::make_local(THREAD, res);
1421
}
1422
1423
return (jobjectArray)JNIHandles::make_local(THREAD, result());
1424
JVM_END
1425
1426
1427
JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
1428
{
1429
// ofClass is a reference to a java_lang_Class object.
1430
oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1431
if (java_lang_Class::is_primitive(ofMirror)) {
1432
return NULL;
1433
}
1434
Klass* klass = java_lang_Class::as_Klass(ofMirror);
1435
if (!klass->is_instance_klass()) {
1436
return NULL;
1437
}
1438
1439
bool inner_is_member = false;
1440
Klass* outer_klass
1441
= InstanceKlass::cast(klass)->compute_enclosing_class(&inner_is_member, CHECK_NULL);
1442
if (outer_klass == NULL) return NULL; // already a top-level class
1443
if (!inner_is_member) return NULL; // a hidden class (inside a method)
1444
return (jclass) JNIHandles::make_local(THREAD, outer_klass->java_mirror());
1445
}
1446
JVM_END
1447
1448
JVM_ENTRY(jstring, JVM_GetSimpleBinaryName(JNIEnv *env, jclass cls))
1449
{
1450
oop mirror = JNIHandles::resolve_non_null(cls);
1451
if (java_lang_Class::is_primitive(mirror)) {
1452
return NULL;
1453
}
1454
Klass* klass = java_lang_Class::as_Klass(mirror);
1455
if (!klass->is_instance_klass()) {
1456
return NULL;
1457
}
1458
InstanceKlass* k = InstanceKlass::cast(klass);
1459
int ooff = 0, noff = 0;
1460
if (k->find_inner_classes_attr(&ooff, &noff, THREAD)) {
1461
if (noff != 0) {
1462
constantPoolHandle i_cp(thread, k->constants());
1463
Symbol* name = i_cp->symbol_at(noff);
1464
Handle str = java_lang_String::create_from_symbol(name, CHECK_NULL);
1465
return (jstring) JNIHandles::make_local(THREAD, str());
1466
}
1467
}
1468
return NULL;
1469
}
1470
JVM_END
1471
1472
JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls))
1473
assert (cls != NULL, "illegal class");
1474
JvmtiVMObjectAllocEventCollector oam;
1475
ResourceMark rm(THREAD);
1476
oop mirror = JNIHandles::resolve_non_null(cls);
1477
// Return null for arrays and primatives
1478
if (!java_lang_Class::is_primitive(mirror)) {
1479
Klass* k = java_lang_Class::as_Klass(mirror);
1480
if (k->is_instance_klass()) {
1481
Symbol* sym = InstanceKlass::cast(k)->generic_signature();
1482
if (sym == NULL) return NULL;
1483
Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
1484
return (jstring) JNIHandles::make_local(THREAD, str());
1485
}
1486
}
1487
return NULL;
1488
JVM_END
1489
1490
1491
JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls))
1492
assert (cls != NULL, "illegal class");
1493
oop mirror = JNIHandles::resolve_non_null(cls);
1494
// Return null for arrays and primitives
1495
if (!java_lang_Class::is_primitive(mirror)) {
1496
Klass* k = java_lang_Class::as_Klass(mirror);
1497
if (k->is_instance_klass()) {
1498
typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL);
1499
return (jbyteArray) JNIHandles::make_local(THREAD, a);
1500
}
1501
}
1502
return NULL;
1503
JVM_END
1504
1505
1506
static bool jvm_get_field_common(jobject field, fieldDescriptor& fd) {
1507
// some of this code was adapted from from jni_FromReflectedField
1508
1509
oop reflected = JNIHandles::resolve_non_null(field);
1510
oop mirror = java_lang_reflect_Field::clazz(reflected);
1511
Klass* k = java_lang_Class::as_Klass(mirror);
1512
int slot = java_lang_reflect_Field::slot(reflected);
1513
int modifiers = java_lang_reflect_Field::modifiers(reflected);
1514
1515
InstanceKlass* ik = InstanceKlass::cast(k);
1516
intptr_t offset = ik->field_offset(slot);
1517
1518
if (modifiers & JVM_ACC_STATIC) {
1519
// for static fields we only look in the current class
1520
if (!ik->find_local_field_from_offset(offset, true, &fd)) {
1521
assert(false, "cannot find static field");
1522
return false;
1523
}
1524
} else {
1525
// for instance fields we start with the current class and work
1526
// our way up through the superclass chain
1527
if (!ik->find_field_from_offset(offset, false, &fd)) {
1528
assert(false, "cannot find instance field");
1529
return false;
1530
}
1531
}
1532
return true;
1533
}
1534
1535
static Method* jvm_get_method_common(jobject method) {
1536
// some of this code was adapted from from jni_FromReflectedMethod
1537
1538
oop reflected = JNIHandles::resolve_non_null(method);
1539
oop mirror = NULL;
1540
int slot = 0;
1541
1542
if (reflected->klass() == vmClasses::reflect_Constructor_klass()) {
1543
mirror = java_lang_reflect_Constructor::clazz(reflected);
1544
slot = java_lang_reflect_Constructor::slot(reflected);
1545
} else {
1546
assert(reflected->klass() == vmClasses::reflect_Method_klass(),
1547
"wrong type");
1548
mirror = java_lang_reflect_Method::clazz(reflected);
1549
slot = java_lang_reflect_Method::slot(reflected);
1550
}
1551
Klass* k = java_lang_Class::as_Klass(mirror);
1552
1553
Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
1554
assert(m != NULL, "cannot find method");
1555
return m; // caller has to deal with NULL in product mode
1556
}
1557
1558
/* Type use annotations support (JDK 1.8) */
1559
1560
JVM_ENTRY(jbyteArray, JVM_GetClassTypeAnnotations(JNIEnv *env, jclass cls))
1561
assert (cls != NULL, "illegal class");
1562
ResourceMark rm(THREAD);
1563
// Return null for arrays and primitives
1564
if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1565
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1566
if (k->is_instance_klass()) {
1567
AnnotationArray* type_annotations = InstanceKlass::cast(k)->class_type_annotations();
1568
if (type_annotations != NULL) {
1569
typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
1570
return (jbyteArray) JNIHandles::make_local(THREAD, a);
1571
}
1572
}
1573
}
1574
return NULL;
1575
JVM_END
1576
1577
JVM_ENTRY(jbyteArray, JVM_GetMethodTypeAnnotations(JNIEnv *env, jobject method))
1578
assert (method != NULL, "illegal method");
1579
// method is a handle to a java.lang.reflect.Method object
1580
Method* m = jvm_get_method_common(method);
1581
if (m == NULL) {
1582
return NULL;
1583
}
1584
1585
AnnotationArray* type_annotations = m->type_annotations();
1586
if (type_annotations != NULL) {
1587
typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
1588
return (jbyteArray) JNIHandles::make_local(THREAD, a);
1589
}
1590
1591
return NULL;
1592
JVM_END
1593
1594
JVM_ENTRY(jbyteArray, JVM_GetFieldTypeAnnotations(JNIEnv *env, jobject field))
1595
assert (field != NULL, "illegal field");
1596
fieldDescriptor fd;
1597
bool gotFd = jvm_get_field_common(field, fd);
1598
if (!gotFd) {
1599
return NULL;
1600
}
1601
1602
return (jbyteArray) JNIHandles::make_local(THREAD, Annotations::make_java_array(fd.type_annotations(), THREAD));
1603
JVM_END
1604
1605
static void bounds_check(const constantPoolHandle& cp, jint index, TRAPS) {
1606
if (!cp->is_within_bounds(index)) {
1607
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds");
1608
}
1609
}
1610
1611
JVM_ENTRY(jobjectArray, JVM_GetMethodParameters(JNIEnv *env, jobject method))
1612
{
1613
// method is a handle to a java.lang.reflect.Method object
1614
Method* method_ptr = jvm_get_method_common(method);
1615
methodHandle mh (THREAD, method_ptr);
1616
Handle reflected_method (THREAD, JNIHandles::resolve_non_null(method));
1617
const int num_params = mh->method_parameters_length();
1618
1619
if (num_params < 0) {
1620
// A -1 return value from method_parameters_length means there is no
1621
// parameter data. Return null to indicate this to the reflection
1622
// API.
1623
assert(num_params == -1, "num_params should be -1 if it is less than zero");
1624
return (jobjectArray)NULL;
1625
} else {
1626
// Otherwise, we return something up to reflection, even if it is
1627
// a zero-length array. Why? Because in some cases this can
1628
// trigger a MalformedParametersException.
1629
1630
// make sure all the symbols are properly formatted
1631
for (int i = 0; i < num_params; i++) {
1632
MethodParametersElement* params = mh->method_parameters_start();
1633
int index = params[i].name_cp_index;
1634
constantPoolHandle cp(THREAD, mh->constants());
1635
bounds_check(cp, index, CHECK_NULL);
1636
1637
if (0 != index && !mh->constants()->tag_at(index).is_utf8()) {
1638
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1639
"Wrong type at constant pool index");
1640
}
1641
1642
}
1643
1644
objArrayOop result_oop = oopFactory::new_objArray(vmClasses::reflect_Parameter_klass(), num_params, CHECK_NULL);
1645
objArrayHandle result (THREAD, result_oop);
1646
1647
for (int i = 0; i < num_params; i++) {
1648
MethodParametersElement* params = mh->method_parameters_start();
1649
// For a 0 index, give a NULL symbol
1650
Symbol* sym = 0 != params[i].name_cp_index ?
1651
mh->constants()->symbol_at(params[i].name_cp_index) : NULL;
1652
int flags = params[i].flags;
1653
oop param = Reflection::new_parameter(reflected_method, i, sym,
1654
flags, CHECK_NULL);
1655
result->obj_at_put(i, param);
1656
}
1657
return (jobjectArray)JNIHandles::make_local(THREAD, result());
1658
}
1659
}
1660
JVM_END
1661
1662
// New (JDK 1.4) reflection implementation /////////////////////////////////////
1663
1664
JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1665
{
1666
JvmtiVMObjectAllocEventCollector oam;
1667
1668
oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1669
// Exclude primitive types and array types
1670
if (java_lang_Class::is_primitive(ofMirror) ||
1671
java_lang_Class::as_Klass(ofMirror)->is_array_klass()) {
1672
// Return empty array
1673
oop res = oopFactory::new_objArray(vmClasses::reflect_Field_klass(), 0, CHECK_NULL);
1674
return (jobjectArray) JNIHandles::make_local(THREAD, res);
1675
}
1676
1677
InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1678
constantPoolHandle cp(THREAD, k->constants());
1679
1680
// Ensure class is linked
1681
k->link_class(CHECK_NULL);
1682
1683
// Allocate result
1684
int num_fields;
1685
1686
if (publicOnly) {
1687
num_fields = 0;
1688
for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
1689
if (fs.access_flags().is_public()) ++num_fields;
1690
}
1691
} else {
1692
num_fields = k->java_fields_count();
1693
}
1694
1695
objArrayOop r = oopFactory::new_objArray(vmClasses::reflect_Field_klass(), num_fields, CHECK_NULL);
1696
objArrayHandle result (THREAD, r);
1697
1698
int out_idx = 0;
1699
fieldDescriptor fd;
1700
for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
1701
if (!publicOnly || fs.access_flags().is_public()) {
1702
fd.reinitialize(k, fs.index());
1703
oop field = Reflection::new_field(&fd, CHECK_NULL);
1704
result->obj_at_put(out_idx, field);
1705
++out_idx;
1706
}
1707
}
1708
assert(out_idx == num_fields, "just checking");
1709
return (jobjectArray) JNIHandles::make_local(THREAD, result());
1710
}
1711
JVM_END
1712
1713
// A class is a record if and only if it is final and a direct subclass of
1714
// java.lang.Record and has a Record attribute; otherwise, it is not a record.
1715
JVM_ENTRY(jboolean, JVM_IsRecord(JNIEnv *env, jclass cls))
1716
{
1717
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1718
if (k != NULL && k->is_instance_klass()) {
1719
InstanceKlass* ik = InstanceKlass::cast(k);
1720
return ik->is_record();
1721
} else {
1722
return false;
1723
}
1724
}
1725
JVM_END
1726
1727
// Returns an array containing the components of the Record attribute,
1728
// or NULL if the attribute is not present.
1729
//
1730
// Note that this function returns the components of the Record attribute
1731
// even if the class is not a record.
1732
JVM_ENTRY(jobjectArray, JVM_GetRecordComponents(JNIEnv* env, jclass ofClass))
1733
{
1734
Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass));
1735
assert(c->is_instance_klass(), "must be");
1736
InstanceKlass* ik = InstanceKlass::cast(c);
1737
1738
Array<RecordComponent*>* components = ik->record_components();
1739
if (components != NULL) {
1740
JvmtiVMObjectAllocEventCollector oam;
1741
constantPoolHandle cp(THREAD, ik->constants());
1742
int length = components->length();
1743
assert(length >= 0, "unexpected record_components length");
1744
objArrayOop record_components =
1745
oopFactory::new_objArray(vmClasses::RecordComponent_klass(), length, CHECK_NULL);
1746
objArrayHandle components_h (THREAD, record_components);
1747
1748
for (int x = 0; x < length; x++) {
1749
RecordComponent* component = components->at(x);
1750
assert(component != NULL, "unexpected NULL record component");
1751
oop component_oop = java_lang_reflect_RecordComponent::create(ik, component, CHECK_NULL);
1752
components_h->obj_at_put(x, component_oop);
1753
}
1754
return (jobjectArray)JNIHandles::make_local(THREAD, components_h());
1755
}
1756
1757
return NULL;
1758
}
1759
JVM_END
1760
1761
static bool select_method(const methodHandle& method, bool want_constructor) {
1762
if (want_constructor) {
1763
return (method->is_initializer() && !method->is_static());
1764
} else {
1765
return (!method->is_initializer() && !method->is_overpass());
1766
}
1767
}
1768
1769
static jobjectArray get_class_declared_methods_helper(
1770
JNIEnv *env,
1771
jclass ofClass, jboolean publicOnly,
1772
bool want_constructor,
1773
Klass* klass, TRAPS) {
1774
1775
JvmtiVMObjectAllocEventCollector oam;
1776
1777
oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1778
// Exclude primitive types and array types
1779
if (java_lang_Class::is_primitive(ofMirror)
1780
|| java_lang_Class::as_Klass(ofMirror)->is_array_klass()) {
1781
// Return empty array
1782
oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL);
1783
return (jobjectArray) JNIHandles::make_local(THREAD, res);
1784
}
1785
1786
InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1787
1788
// Ensure class is linked
1789
k->link_class(CHECK_NULL);
1790
1791
Array<Method*>* methods = k->methods();
1792
int methods_length = methods->length();
1793
1794
// Save original method_idnum in case of redefinition, which can change
1795
// the idnum of obsolete methods. The new method will have the same idnum
1796
// but if we refresh the methods array, the counts will be wrong.
1797
ResourceMark rm(THREAD);
1798
GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1799
int num_methods = 0;
1800
1801
for (int i = 0; i < methods_length; i++) {
1802
methodHandle method(THREAD, methods->at(i));
1803
if (select_method(method, want_constructor)) {
1804
if (!publicOnly || method->is_public()) {
1805
idnums->push(method->method_idnum());
1806
++num_methods;
1807
}
1808
}
1809
}
1810
1811
// Allocate result
1812
objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1813
objArrayHandle result (THREAD, r);
1814
1815
// Now just put the methods that we selected above, but go by their idnum
1816
// in case of redefinition. The methods can be redefined at any safepoint,
1817
// so above when allocating the oop array and below when creating reflect
1818
// objects.
1819
for (int i = 0; i < num_methods; i++) {
1820
methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1821
if (method.is_null()) {
1822
// Method may have been deleted and seems this API can handle null
1823
// Otherwise should probably put a method that throws NSME
1824
result->obj_at_put(i, NULL);
1825
} else {
1826
oop m;
1827
if (want_constructor) {
1828
m = Reflection::new_constructor(method, CHECK_NULL);
1829
} else {
1830
m = Reflection::new_method(method, false, CHECK_NULL);
1831
}
1832
result->obj_at_put(i, m);
1833
}
1834
}
1835
1836
return (jobjectArray) JNIHandles::make_local(THREAD, result());
1837
}
1838
1839
JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1840
{
1841
return get_class_declared_methods_helper(env, ofClass, publicOnly,
1842
/*want_constructor*/ false,
1843
vmClasses::reflect_Method_klass(), THREAD);
1844
}
1845
JVM_END
1846
1847
JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1848
{
1849
return get_class_declared_methods_helper(env, ofClass, publicOnly,
1850
/*want_constructor*/ true,
1851
vmClasses::reflect_Constructor_klass(), THREAD);
1852
}
1853
JVM_END
1854
1855
JVM_ENTRY(jint, JVM_GetClassAccessFlags(JNIEnv *env, jclass cls))
1856
{
1857
oop mirror = JNIHandles::resolve_non_null(cls);
1858
if (java_lang_Class::is_primitive(mirror)) {
1859
// Primitive type
1860
return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1861
}
1862
1863
Klass* k = java_lang_Class::as_Klass(mirror);
1864
return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS;
1865
}
1866
JVM_END
1867
1868
JVM_ENTRY(jboolean, JVM_AreNestMates(JNIEnv *env, jclass current, jclass member))
1869
{
1870
Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(current));
1871
assert(c->is_instance_klass(), "must be");
1872
InstanceKlass* ck = InstanceKlass::cast(c);
1873
Klass* m = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(member));
1874
assert(m->is_instance_klass(), "must be");
1875
InstanceKlass* mk = InstanceKlass::cast(m);
1876
return ck->has_nestmate_access_to(mk, THREAD);
1877
}
1878
JVM_END
1879
1880
JVM_ENTRY(jclass, JVM_GetNestHost(JNIEnv* env, jclass current))
1881
{
1882
// current is not a primitive or array class
1883
Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(current));
1884
assert(c->is_instance_klass(), "must be");
1885
InstanceKlass* ck = InstanceKlass::cast(c);
1886
InstanceKlass* host = ck->nest_host(THREAD);
1887
return (jclass) (host == NULL ? NULL :
1888
JNIHandles::make_local(THREAD, host->java_mirror()));
1889
}
1890
JVM_END
1891
1892
JVM_ENTRY(jobjectArray, JVM_GetNestMembers(JNIEnv* env, jclass current))
1893
{
1894
// current is not a primitive or array class
1895
ResourceMark rm(THREAD);
1896
Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(current));
1897
assert(c->is_instance_klass(), "must be");
1898
InstanceKlass* ck = InstanceKlass::cast(c);
1899
InstanceKlass* host = ck->nest_host(THREAD);
1900
1901
log_trace(class, nestmates)("Calling GetNestMembers for type %s with nest-host %s",
1902
ck->external_name(), host->external_name());
1903
{
1904
JvmtiVMObjectAllocEventCollector oam;
1905
Array<u2>* members = host->nest_members();
1906
int length = members == NULL ? 0 : members->length();
1907
1908
log_trace(class, nestmates)(" - host has %d listed nest members", length);
1909
1910
// nest host is first in the array so make it one bigger
1911
objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(),
1912
length + 1, CHECK_NULL);
1913
objArrayHandle result(THREAD, r);
1914
result->obj_at_put(0, host->java_mirror());
1915
if (length != 0) {
1916
int count = 0;
1917
for (int i = 0; i < length; i++) {
1918
int cp_index = members->at(i);
1919
Klass* k = host->constants()->klass_at(cp_index, THREAD);
1920
if (HAS_PENDING_EXCEPTION) {
1921
if (PENDING_EXCEPTION->is_a(vmClasses::VirtualMachineError_klass())) {
1922
return NULL; // propagate VMEs
1923
}
1924
if (log_is_enabled(Trace, class, nestmates)) {
1925
stringStream ss;
1926
char* target_member_class = host->constants()->klass_name_at(cp_index)->as_C_string();
1927
ss.print(" - resolution of nest member %s failed: ", target_member_class);
1928
java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
1929
log_trace(class, nestmates)("%s", ss.as_string());
1930
}
1931
CLEAR_PENDING_EXCEPTION;
1932
continue;
1933
}
1934
if (k->is_instance_klass()) {
1935
InstanceKlass* ik = InstanceKlass::cast(k);
1936
InstanceKlass* nest_host_k = ik->nest_host(CHECK_NULL);
1937
if (nest_host_k == host) {
1938
result->obj_at_put(count+1, k->java_mirror());
1939
count++;
1940
log_trace(class, nestmates)(" - [%d] = %s", count, ik->external_name());
1941
} else {
1942
log_trace(class, nestmates)(" - skipping member %s with different host %s",
1943
ik->external_name(), nest_host_k->external_name());
1944
}
1945
} else {
1946
log_trace(class, nestmates)(" - skipping member %s that is not an instance class",
1947
k->external_name());
1948
}
1949
}
1950
if (count < length) {
1951
// we had invalid entries so we need to compact the array
1952
log_trace(class, nestmates)(" - compacting array from length %d to %d",
1953
length + 1, count + 1);
1954
1955
objArrayOop r2 = oopFactory::new_objArray(vmClasses::Class_klass(),
1956
count + 1, CHECK_NULL);
1957
objArrayHandle result2(THREAD, r2);
1958
for (int i = 0; i < count + 1; i++) {
1959
result2->obj_at_put(i, result->obj_at(i));
1960
}
1961
return (jobjectArray)JNIHandles::make_local(THREAD, result2());
1962
}
1963
}
1964
else {
1965
assert(host == ck || ck->is_hidden(), "must be singleton nest or dynamic nestmate");
1966
}
1967
return (jobjectArray)JNIHandles::make_local(THREAD, result());
1968
}
1969
}
1970
JVM_END
1971
1972
JVM_ENTRY(jobjectArray, JVM_GetPermittedSubclasses(JNIEnv* env, jclass current))
1973
{
1974
oop mirror = JNIHandles::resolve_non_null(current);
1975
assert(!java_lang_Class::is_primitive(mirror), "should not be");
1976
Klass* c = java_lang_Class::as_Klass(mirror);
1977
assert(c->is_instance_klass(), "must be");
1978
InstanceKlass* ik = InstanceKlass::cast(c);
1979
ResourceMark rm(THREAD);
1980
log_trace(class, sealed)("Calling GetPermittedSubclasses for %s type %s",
1981
ik->is_sealed() ? "sealed" : "non-sealed", ik->external_name());
1982
if (ik->is_sealed()) {
1983
JvmtiVMObjectAllocEventCollector oam;
1984
Array<u2>* subclasses = ik->permitted_subclasses();
1985
int length = subclasses->length();
1986
1987
log_trace(class, sealed)(" - sealed class has %d permitted subclasses", length);
1988
1989
objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(),
1990
length, CHECK_NULL);
1991
objArrayHandle result(THREAD, r);
1992
int count = 0;
1993
for (int i = 0; i < length; i++) {
1994
int cp_index = subclasses->at(i);
1995
Klass* k = ik->constants()->klass_at(cp_index, THREAD);
1996
if (HAS_PENDING_EXCEPTION) {
1997
if (PENDING_EXCEPTION->is_a(vmClasses::VirtualMachineError_klass())) {
1998
return NULL; // propagate VMEs
1999
}
2000
if (log_is_enabled(Trace, class, sealed)) {
2001
stringStream ss;
2002
char* permitted_subclass = ik->constants()->klass_name_at(cp_index)->as_C_string();
2003
ss.print(" - resolution of permitted subclass %s failed: ", permitted_subclass);
2004
java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
2005
log_trace(class, sealed)("%s", ss.as_string());
2006
}
2007
2008
CLEAR_PENDING_EXCEPTION;
2009
continue;
2010
}
2011
if (k->is_instance_klass()) {
2012
result->obj_at_put(count++, k->java_mirror());
2013
log_trace(class, sealed)(" - [%d] = %s", count, k->external_name());
2014
}
2015
}
2016
if (count < length) {
2017
// we had invalid entries so we need to compact the array
2018
objArrayOop r2 = oopFactory::new_objArray(vmClasses::Class_klass(),
2019
count, CHECK_NULL);
2020
objArrayHandle result2(THREAD, r2);
2021
for (int i = 0; i < count; i++) {
2022
result2->obj_at_put(i, result->obj_at(i));
2023
}
2024
return (jobjectArray)JNIHandles::make_local(THREAD, result2());
2025
}
2026
return (jobjectArray)JNIHandles::make_local(THREAD, result());
2027
} else {
2028
return NULL;
2029
}
2030
}
2031
JVM_END
2032
2033
// Constant pool access //////////////////////////////////////////////////////////
2034
2035
JVM_ENTRY(jobject, JVM_GetClassConstantPool(JNIEnv *env, jclass cls))
2036
{
2037
JvmtiVMObjectAllocEventCollector oam;
2038
oop mirror = JNIHandles::resolve_non_null(cls);
2039
// Return null for primitives and arrays
2040
if (!java_lang_Class::is_primitive(mirror)) {
2041
Klass* k = java_lang_Class::as_Klass(mirror);
2042
if (k->is_instance_klass()) {
2043
InstanceKlass* k_h = InstanceKlass::cast(k);
2044
Handle jcp = reflect_ConstantPool::create(CHECK_NULL);
2045
reflect_ConstantPool::set_cp(jcp(), k_h->constants());
2046
return JNIHandles::make_local(THREAD, jcp());
2047
}
2048
}
2049
return NULL;
2050
}
2051
JVM_END
2052
2053
2054
JVM_ENTRY(jint, JVM_ConstantPoolGetSize(JNIEnv *env, jobject obj, jobject unused))
2055
{
2056
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2057
return cp->length();
2058
}
2059
JVM_END
2060
2061
2062
JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2063
{
2064
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2065
bounds_check(cp, index, CHECK_NULL);
2066
constantTag tag = cp->tag_at(index);
2067
if (!tag.is_klass() && !tag.is_unresolved_klass()) {
2068
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2069
}
2070
Klass* k = cp->klass_at(index, CHECK_NULL);
2071
return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
2072
}
2073
JVM_END
2074
2075
JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2076
{
2077
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2078
bounds_check(cp, index, CHECK_NULL);
2079
constantTag tag = cp->tag_at(index);
2080
if (!tag.is_klass() && !tag.is_unresolved_klass()) {
2081
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2082
}
2083
Klass* k = ConstantPool::klass_at_if_loaded(cp, index);
2084
if (k == NULL) return NULL;
2085
return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
2086
}
2087
JVM_END
2088
2089
static jobject get_method_at_helper(const constantPoolHandle& cp, jint index, bool force_resolution, TRAPS) {
2090
constantTag tag = cp->tag_at(index);
2091
if (!tag.is_method() && !tag.is_interface_method()) {
2092
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2093
}
2094
int klass_ref = cp->uncached_klass_ref_index_at(index);
2095
Klass* k_o;
2096
if (force_resolution) {
2097
k_o = cp->klass_at(klass_ref, CHECK_NULL);
2098
} else {
2099
k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2100
if (k_o == NULL) return NULL;
2101
}
2102
InstanceKlass* k = InstanceKlass::cast(k_o);
2103
Symbol* name = cp->uncached_name_ref_at(index);
2104
Symbol* sig = cp->uncached_signature_ref_at(index);
2105
methodHandle m (THREAD, k->find_method(name, sig));
2106
if (m.is_null()) {
2107
THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2108
}
2109
oop method;
2110
if (!m->is_initializer() || m->is_static()) {
2111
method = Reflection::new_method(m, true, CHECK_NULL);
2112
} else {
2113
method = Reflection::new_constructor(m, CHECK_NULL);
2114
}
2115
return JNIHandles::make_local(THREAD, method);
2116
}
2117
2118
JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2119
{
2120
JvmtiVMObjectAllocEventCollector oam;
2121
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2122
bounds_check(cp, index, CHECK_NULL);
2123
jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2124
return res;
2125
}
2126
JVM_END
2127
2128
JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2129
{
2130
JvmtiVMObjectAllocEventCollector oam;
2131
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2132
bounds_check(cp, index, CHECK_NULL);
2133
jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
2134
return res;
2135
}
2136
JVM_END
2137
2138
static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
2139
constantTag tag = cp->tag_at(index);
2140
if (!tag.is_field()) {
2141
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2142
}
2143
int klass_ref = cp->uncached_klass_ref_index_at(index);
2144
Klass* k_o;
2145
if (force_resolution) {
2146
k_o = cp->klass_at(klass_ref, CHECK_NULL);
2147
} else {
2148
k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2149
if (k_o == NULL) return NULL;
2150
}
2151
InstanceKlass* k = InstanceKlass::cast(k_o);
2152
Symbol* name = cp->uncached_name_ref_at(index);
2153
Symbol* sig = cp->uncached_signature_ref_at(index);
2154
fieldDescriptor fd;
2155
Klass* target_klass = k->find_field(name, sig, &fd);
2156
if (target_klass == NULL) {
2157
THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class");
2158
}
2159
oop field = Reflection::new_field(&fd, CHECK_NULL);
2160
return JNIHandles::make_local(THREAD, field);
2161
}
2162
2163
JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject obj, jobject unusedl, jint index))
2164
{
2165
JvmtiVMObjectAllocEventCollector oam;
2166
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2167
bounds_check(cp, index, CHECK_NULL);
2168
jobject res = get_field_at_helper(cp, index, true, CHECK_NULL);
2169
return res;
2170
}
2171
JVM_END
2172
2173
JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2174
{
2175
JvmtiVMObjectAllocEventCollector oam;
2176
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2177
bounds_check(cp, index, CHECK_NULL);
2178
jobject res = get_field_at_helper(cp, index, false, CHECK_NULL);
2179
return res;
2180
}
2181
JVM_END
2182
2183
JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2184
{
2185
JvmtiVMObjectAllocEventCollector oam;
2186
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2187
bounds_check(cp, index, CHECK_NULL);
2188
constantTag tag = cp->tag_at(index);
2189
if (!tag.is_field_or_method()) {
2190
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2191
}
2192
int klass_ref = cp->uncached_klass_ref_index_at(index);
2193
Symbol* klass_name = cp->klass_name_at(klass_ref);
2194
Symbol* member_name = cp->uncached_name_ref_at(index);
2195
Symbol* member_sig = cp->uncached_signature_ref_at(index);
2196
objArrayOop dest_o = oopFactory::new_objArray(vmClasses::String_klass(), 3, CHECK_NULL);
2197
objArrayHandle dest(THREAD, dest_o);
2198
Handle str = java_lang_String::create_from_symbol(klass_name, CHECK_NULL);
2199
dest->obj_at_put(0, str());
2200
str = java_lang_String::create_from_symbol(member_name, CHECK_NULL);
2201
dest->obj_at_put(1, str());
2202
str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL);
2203
dest->obj_at_put(2, str());
2204
return (jobjectArray) JNIHandles::make_local(THREAD, dest());
2205
}
2206
JVM_END
2207
2208
JVM_ENTRY(jint, JVM_ConstantPoolGetClassRefIndexAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2209
{
2210
JvmtiVMObjectAllocEventCollector oam;
2211
constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2212
bounds_check(cp, index, CHECK_0);
2213
constantTag tag = cp->tag_at(index);
2214
if (!tag.is_field_or_method()) {
2215
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2216
}
2217
return (jint) cp->uncached_klass_ref_index_at(index);
2218
}
2219
JVM_END
2220
2221
JVM_ENTRY(jint, JVM_ConstantPoolGetNameAndTypeRefIndexAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2222
{
2223
JvmtiVMObjectAllocEventCollector oam;
2224
constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2225
bounds_check(cp, index, CHECK_0);
2226
constantTag tag = cp->tag_at(index);
2227
if (!tag.is_invoke_dynamic() && !tag.is_field_or_method()) {
2228
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2229
}
2230
return (jint) cp->uncached_name_and_type_ref_index_at(index);
2231
}
2232
JVM_END
2233
2234
JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetNameAndTypeRefInfoAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2235
{
2236
JvmtiVMObjectAllocEventCollector oam;
2237
constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2238
bounds_check(cp, index, CHECK_NULL);
2239
constantTag tag = cp->tag_at(index);
2240
if (!tag.is_name_and_type()) {
2241
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2242
}
2243
Symbol* member_name = cp->symbol_at(cp->name_ref_index_at(index));
2244
Symbol* member_sig = cp->symbol_at(cp->signature_ref_index_at(index));
2245
objArrayOop dest_o = oopFactory::new_objArray(vmClasses::String_klass(), 2, CHECK_NULL);
2246
objArrayHandle dest(THREAD, dest_o);
2247
Handle str = java_lang_String::create_from_symbol(member_name, CHECK_NULL);
2248
dest->obj_at_put(0, str());
2249
str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL);
2250
dest->obj_at_put(1, str());
2251
return (jobjectArray) JNIHandles::make_local(THREAD, dest());
2252
}
2253
JVM_END
2254
2255
JVM_ENTRY(jint, JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2256
{
2257
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2258
bounds_check(cp, index, CHECK_0);
2259
constantTag tag = cp->tag_at(index);
2260
if (!tag.is_int()) {
2261
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2262
}
2263
return cp->int_at(index);
2264
}
2265
JVM_END
2266
2267
JVM_ENTRY(jlong, JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2268
{
2269
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2270
bounds_check(cp, index, CHECK_(0L));
2271
constantTag tag = cp->tag_at(index);
2272
if (!tag.is_long()) {
2273
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2274
}
2275
return cp->long_at(index);
2276
}
2277
JVM_END
2278
2279
JVM_ENTRY(jfloat, JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2280
{
2281
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2282
bounds_check(cp, index, CHECK_(0.0f));
2283
constantTag tag = cp->tag_at(index);
2284
if (!tag.is_float()) {
2285
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2286
}
2287
return cp->float_at(index);
2288
}
2289
JVM_END
2290
2291
JVM_ENTRY(jdouble, JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2292
{
2293
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2294
bounds_check(cp, index, CHECK_(0.0));
2295
constantTag tag = cp->tag_at(index);
2296
if (!tag.is_double()) {
2297
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2298
}
2299
return cp->double_at(index);
2300
}
2301
JVM_END
2302
2303
JVM_ENTRY(jstring, JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2304
{
2305
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2306
bounds_check(cp, index, CHECK_NULL);
2307
constantTag tag = cp->tag_at(index);
2308
if (!tag.is_string()) {
2309
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2310
}
2311
oop str = cp->string_at(index, CHECK_NULL);
2312
return (jstring) JNIHandles::make_local(THREAD, str);
2313
}
2314
JVM_END
2315
2316
JVM_ENTRY(jstring, JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject obj, jobject unused, jint index))
2317
{
2318
JvmtiVMObjectAllocEventCollector oam;
2319
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2320
bounds_check(cp, index, CHECK_NULL);
2321
constantTag tag = cp->tag_at(index);
2322
if (!tag.is_symbol()) {
2323
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2324
}
2325
Symbol* sym = cp->symbol_at(index);
2326
Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
2327
return (jstring) JNIHandles::make_local(THREAD, str());
2328
}
2329
JVM_END
2330
2331
JVM_ENTRY(jbyte, JVM_ConstantPoolGetTagAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2332
{
2333
constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2334
bounds_check(cp, index, CHECK_0);
2335
constantTag tag = cp->tag_at(index);
2336
jbyte result = tag.value();
2337
// If returned tag values are not from the JVM spec, e.g. tags from 100 to 105,
2338
// they are changed to the corresponding tags from the JVM spec, so that java code in
2339
// sun.reflect.ConstantPool will return only tags from the JVM spec, not internal ones.
2340
if (tag.is_klass_or_reference()) {
2341
result = JVM_CONSTANT_Class;
2342
} else if (tag.is_string_index()) {
2343
result = JVM_CONSTANT_String;
2344
} else if (tag.is_method_type_in_error()) {
2345
result = JVM_CONSTANT_MethodType;
2346
} else if (tag.is_method_handle_in_error()) {
2347
result = JVM_CONSTANT_MethodHandle;
2348
} else if (tag.is_dynamic_constant_in_error()) {
2349
result = JVM_CONSTANT_Dynamic;
2350
}
2351
return result;
2352
}
2353
JVM_END
2354
2355
// Assertion support. //////////////////////////////////////////////////////////
2356
2357
JVM_ENTRY(jboolean, JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls))
2358
assert(cls != NULL, "bad class");
2359
2360
oop r = JNIHandles::resolve(cls);
2361
assert(! java_lang_Class::is_primitive(r), "primitive classes not allowed");
2362
if (java_lang_Class::is_primitive(r)) return false;
2363
2364
Klass* k = java_lang_Class::as_Klass(r);
2365
assert(k->is_instance_klass(), "must be an instance klass");
2366
if (!k->is_instance_klass()) return false;
2367
2368
ResourceMark rm(THREAD);
2369
const char* name = k->name()->as_C_string();
2370
bool system_class = k->class_loader() == NULL;
2371
return JavaAssertions::enabled(name, system_class);
2372
2373
JVM_END
2374
2375
2376
// Return a new AssertionStatusDirectives object with the fields filled in with
2377
// command-line assertion arguments (i.e., -ea, -da).
2378
JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2379
JvmtiVMObjectAllocEventCollector oam;
2380
oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2381
return JNIHandles::make_local(THREAD, asd);
2382
JVM_END
2383
2384
// Verification ////////////////////////////////////////////////////////////////////////////////
2385
2386
// Reflection for the verifier /////////////////////////////////////////////////////////////////
2387
2388
// RedefineClasses support: bug 6214132 caused verification to fail.
2389
// All functions from this section should call the jvmtiThreadSate function:
2390
// Klass* class_to_verify_considering_redefinition(Klass* klass).
2391
// The function returns a Klass* of the _scratch_class if the verifier
2392
// was invoked in the middle of the class redefinition.
2393
// Otherwise it returns its argument value which is the _the_class Klass*.
2394
// Please, refer to the description in the jvmtiThreadSate.hpp.
2395
2396
JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
2397
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2398
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2399
return k->name()->as_utf8();
2400
JVM_END
2401
2402
2403
JVM_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
2404
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2405
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2406
// types will have length zero if this is not an InstanceKlass
2407
// (length is determined by call to JVM_GetClassCPEntriesCount)
2408
if (k->is_instance_klass()) {
2409
ConstantPool* cp = InstanceKlass::cast(k)->constants();
2410
for (int index = cp->length() - 1; index >= 0; index--) {
2411
constantTag tag = cp->tag_at(index);
2412
types[index] = (tag.is_unresolved_klass()) ? (unsigned char) JVM_CONSTANT_Class : tag.value();
2413
}
2414
}
2415
JVM_END
2416
2417
2418
JVM_ENTRY(jint, JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls))
2419
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2420
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2421
return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->constants()->length();
2422
JVM_END
2423
2424
2425
JVM_ENTRY(jint, JVM_GetClassFieldsCount(JNIEnv *env, jclass cls))
2426
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2427
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2428
return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->java_fields_count();
2429
JVM_END
2430
2431
2432
JVM_ENTRY(jint, JVM_GetClassMethodsCount(JNIEnv *env, jclass cls))
2433
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2434
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2435
return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->methods()->length();
2436
JVM_END
2437
2438
2439
// The following methods, used for the verifier, are never called with
2440
// array klasses, so a direct cast to InstanceKlass is safe.
2441
// Typically, these methods are called in a loop with bounds determined
2442
// by the results of JVM_GetClass{Fields,Methods}Count, which return
2443
// zero for arrays.
2444
JVM_ENTRY(void, JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions))
2445
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2446
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2447
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2448
int length = method->checked_exceptions_length();
2449
if (length > 0) {
2450
CheckedExceptionElement* table= method->checked_exceptions_start();
2451
for (int i = 0; i < length; i++) {
2452
exceptions[i] = table[i].class_cp_index;
2453
}
2454
}
2455
JVM_END
2456
2457
2458
JVM_ENTRY(jint, JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index))
2459
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2460
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2461
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2462
return method->checked_exceptions_length();
2463
JVM_END
2464
2465
2466
JVM_ENTRY(void, JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code))
2467
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2468
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2469
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2470
memcpy(code, method->code_base(), method->code_size());
2471
JVM_END
2472
2473
2474
JVM_ENTRY(jint, JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index))
2475
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2476
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2477
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2478
return method->code_size();
2479
JVM_END
2480
2481
2482
JVM_ENTRY(void, JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry))
2483
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2484
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2485
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2486
ExceptionTable extable(method);
2487
entry->start_pc = extable.start_pc(entry_index);
2488
entry->end_pc = extable.end_pc(entry_index);
2489
entry->handler_pc = extable.handler_pc(entry_index);
2490
entry->catchType = extable.catch_type_index(entry_index);
2491
JVM_END
2492
2493
2494
JVM_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
2495
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2496
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2497
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2498
return method->exception_table_length();
2499
JVM_END
2500
2501
2502
JVM_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
2503
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2504
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2505
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2506
return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2507
JVM_END
2508
2509
2510
JVM_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
2511
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2512
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2513
return InstanceKlass::cast(k)->field_access_flags(field_index) & JVM_RECOGNIZED_FIELD_MODIFIERS;
2514
JVM_END
2515
2516
2517
JVM_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
2518
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2519
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2520
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2521
return method->max_locals();
2522
JVM_END
2523
2524
2525
JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2526
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2527
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2528
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2529
return method->size_of_parameters();
2530
JVM_END
2531
2532
2533
JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2534
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2535
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2536
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2537
return method->verifier_max_stack();
2538
JVM_END
2539
2540
2541
JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2542
ResourceMark rm(THREAD);
2543
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2544
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2545
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2546
return method->name() == vmSymbols::object_initializer_name();
2547
JVM_END
2548
2549
2550
JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2551
ResourceMark rm(THREAD);
2552
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2553
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2554
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2555
return method->is_overpass();
2556
JVM_END
2557
2558
JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2559
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2560
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2561
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2562
return method->name()->as_utf8();
2563
JVM_END
2564
2565
2566
JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2567
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2568
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2569
Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2570
return method->signature()->as_utf8();
2571
JVM_END
2572
2573
/**
2574
* All of these JVM_GetCP-xxx methods are used by the old verifier to
2575
* read entries in the constant pool. Since the old verifier always
2576
* works on a copy of the code, it will not see any rewriting that
2577
* may possibly occur in the middle of verification. So it is important
2578
* that nothing it calls tries to use the cpCache instead of the raw
2579
* constant pool, so we must use cp->uncached_x methods when appropriate.
2580
*/
2581
JVM_ENTRY(const char*, JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2582
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2583
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2584
ConstantPool* cp = InstanceKlass::cast(k)->constants();
2585
switch (cp->tag_at(cp_index).value()) {
2586
case JVM_CONSTANT_Fieldref:
2587
return cp->uncached_name_ref_at(cp_index)->as_utf8();
2588
default:
2589
fatal("JVM_GetCPFieldNameUTF: illegal constant");
2590
}
2591
ShouldNotReachHere();
2592
return NULL;
2593
JVM_END
2594
2595
2596
JVM_ENTRY(const char*, JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2597
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2598
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2599
ConstantPool* cp = InstanceKlass::cast(k)->constants();
2600
switch (cp->tag_at(cp_index).value()) {
2601
case JVM_CONSTANT_InterfaceMethodref:
2602
case JVM_CONSTANT_Methodref:
2603
return cp->uncached_name_ref_at(cp_index)->as_utf8();
2604
default:
2605
fatal("JVM_GetCPMethodNameUTF: illegal constant");
2606
}
2607
ShouldNotReachHere();
2608
return NULL;
2609
JVM_END
2610
2611
2612
JVM_ENTRY(const char*, JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
2613
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2614
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2615
ConstantPool* cp = InstanceKlass::cast(k)->constants();
2616
switch (cp->tag_at(cp_index).value()) {
2617
case JVM_CONSTANT_InterfaceMethodref:
2618
case JVM_CONSTANT_Methodref:
2619
return cp->uncached_signature_ref_at(cp_index)->as_utf8();
2620
default:
2621
fatal("JVM_GetCPMethodSignatureUTF: illegal constant");
2622
}
2623
ShouldNotReachHere();
2624
return NULL;
2625
JVM_END
2626
2627
2628
JVM_ENTRY(const char*, JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
2629
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2630
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2631
ConstantPool* cp = InstanceKlass::cast(k)->constants();
2632
switch (cp->tag_at(cp_index).value()) {
2633
case JVM_CONSTANT_Fieldref:
2634
return cp->uncached_signature_ref_at(cp_index)->as_utf8();
2635
default:
2636
fatal("JVM_GetCPFieldSignatureUTF: illegal constant");
2637
}
2638
ShouldNotReachHere();
2639
return NULL;
2640
JVM_END
2641
2642
2643
JVM_ENTRY(const char*, JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2644
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2645
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2646
ConstantPool* cp = InstanceKlass::cast(k)->constants();
2647
Symbol* classname = cp->klass_name_at(cp_index);
2648
return classname->as_utf8();
2649
JVM_END
2650
2651
2652
JVM_ENTRY(const char*, JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2653
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2654
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2655
ConstantPool* cp = InstanceKlass::cast(k)->constants();
2656
switch (cp->tag_at(cp_index).value()) {
2657
case JVM_CONSTANT_Fieldref: {
2658
int class_index = cp->uncached_klass_ref_index_at(cp_index);
2659
Symbol* classname = cp->klass_name_at(class_index);
2660
return classname->as_utf8();
2661
}
2662
default:
2663
fatal("JVM_GetCPFieldClassNameUTF: illegal constant");
2664
}
2665
ShouldNotReachHere();
2666
return NULL;
2667
JVM_END
2668
2669
2670
JVM_ENTRY(const char*, JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2671
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2672
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2673
ConstantPool* cp = InstanceKlass::cast(k)->constants();
2674
switch (cp->tag_at(cp_index).value()) {
2675
case JVM_CONSTANT_Methodref:
2676
case JVM_CONSTANT_InterfaceMethodref: {
2677
int class_index = cp->uncached_klass_ref_index_at(cp_index);
2678
Symbol* classname = cp->klass_name_at(class_index);
2679
return classname->as_utf8();
2680
}
2681
default:
2682
fatal("JVM_GetCPMethodClassNameUTF: illegal constant");
2683
}
2684
ShouldNotReachHere();
2685
return NULL;
2686
JVM_END
2687
2688
2689
JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2690
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2691
Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2692
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2693
k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2694
ConstantPool* cp = InstanceKlass::cast(k)->constants();
2695
ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants();
2696
switch (cp->tag_at(cp_index).value()) {
2697
case JVM_CONSTANT_Fieldref: {
2698
Symbol* name = cp->uncached_name_ref_at(cp_index);
2699
Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2700
InstanceKlass* ik = InstanceKlass::cast(k_called);
2701
for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
2702
if (fs.name() == name && fs.signature() == signature) {
2703
return fs.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS;
2704
}
2705
}
2706
return -1;
2707
}
2708
default:
2709
fatal("JVM_GetCPFieldModifiers: illegal constant");
2710
}
2711
ShouldNotReachHere();
2712
return 0;
2713
JVM_END
2714
2715
2716
JVM_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2717
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2718
Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2719
k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2720
k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2721
ConstantPool* cp = InstanceKlass::cast(k)->constants();
2722
switch (cp->tag_at(cp_index).value()) {
2723
case JVM_CONSTANT_Methodref:
2724
case JVM_CONSTANT_InterfaceMethodref: {
2725
Symbol* name = cp->uncached_name_ref_at(cp_index);
2726
Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2727
Array<Method*>* methods = InstanceKlass::cast(k_called)->methods();
2728
int methods_count = methods->length();
2729
for (int i = 0; i < methods_count; i++) {
2730
Method* method = methods->at(i);
2731
if (method->name() == name && method->signature() == signature) {
2732
return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2733
}
2734
}
2735
return -1;
2736
}
2737
default:
2738
fatal("JVM_GetCPMethodModifiers: illegal constant");
2739
}
2740
ShouldNotReachHere();
2741
return 0;
2742
JVM_END
2743
2744
2745
// Misc //////////////////////////////////////////////////////////////////////////////////////////////
2746
2747
JVM_LEAF(void, JVM_ReleaseUTF(const char *utf))
2748
// So long as UTF8::convert_to_utf8 returns resource strings, we don't have to do anything
2749
JVM_END
2750
2751
2752
JVM_ENTRY(jboolean, JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2))
2753
oop class1_mirror = JNIHandles::resolve_non_null(class1);
2754
oop class2_mirror = JNIHandles::resolve_non_null(class2);
2755
Klass* klass1 = java_lang_Class::as_Klass(class1_mirror);
2756
Klass* klass2 = java_lang_Class::as_Klass(class2_mirror);
2757
return (jboolean) Reflection::is_same_class_package(klass1, klass2);
2758
JVM_END
2759
2760
// Printing support //////////////////////////////////////////////////
2761
extern "C" {
2762
2763
ATTRIBUTE_PRINTF(3, 0)
2764
int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) {
2765
// Reject count values that are negative signed values converted to
2766
// unsigned; see bug 4399518, 4417214
2767
if ((intptr_t)count <= 0) return -1;
2768
2769
int result = os::vsnprintf(str, count, fmt, args);
2770
if (result > 0 && (size_t)result >= count) {
2771
result = -1;
2772
}
2773
2774
return result;
2775
}
2776
2777
ATTRIBUTE_PRINTF(3, 4)
2778
int jio_snprintf(char *str, size_t count, const char *fmt, ...) {
2779
va_list args;
2780
int len;
2781
va_start(args, fmt);
2782
len = jio_vsnprintf(str, count, fmt, args);
2783
va_end(args);
2784
return len;
2785
}
2786
2787
ATTRIBUTE_PRINTF(2, 3)
2788
int jio_fprintf(FILE* f, const char *fmt, ...) {
2789
int len;
2790
va_list args;
2791
va_start(args, fmt);
2792
len = jio_vfprintf(f, fmt, args);
2793
va_end(args);
2794
return len;
2795
}
2796
2797
ATTRIBUTE_PRINTF(2, 0)
2798
int jio_vfprintf(FILE* f, const char *fmt, va_list args) {
2799
if (Arguments::vfprintf_hook() != NULL) {
2800
return Arguments::vfprintf_hook()(f, fmt, args);
2801
} else {
2802
return vfprintf(f, fmt, args);
2803
}
2804
}
2805
2806
ATTRIBUTE_PRINTF(1, 2)
2807
JNIEXPORT int jio_printf(const char *fmt, ...) {
2808
int len;
2809
va_list args;
2810
va_start(args, fmt);
2811
len = jio_vfprintf(defaultStream::output_stream(), fmt, args);
2812
va_end(args);
2813
return len;
2814
}
2815
2816
// HotSpot specific jio method
2817
void jio_print(const char* s, size_t len) {
2818
// Try to make this function as atomic as possible.
2819
if (Arguments::vfprintf_hook() != NULL) {
2820
jio_fprintf(defaultStream::output_stream(), "%.*s", (int)len, s);
2821
} else {
2822
// Make an unused local variable to avoid warning from gcc compiler.
2823
size_t count = ::write(defaultStream::output_fd(), s, (int)len);
2824
}
2825
}
2826
2827
} // Extern C
2828
2829
// java.lang.Thread //////////////////////////////////////////////////////////////////////////////
2830
2831
// In most of the JVM thread support functions we need to access the
2832
// thread through a ThreadsListHandle to prevent it from exiting and
2833
// being reclaimed while we try to operate on it. The exceptions to this
2834
// rule are when operating on the current thread, or if the monitor of
2835
// the target java.lang.Thread is locked at the Java level - in both
2836
// cases the target cannot exit.
2837
2838
static void thread_entry(JavaThread* thread, TRAPS) {
2839
HandleMark hm(THREAD);
2840
Handle obj(THREAD, thread->threadObj());
2841
JavaValue result(T_VOID);
2842
JavaCalls::call_virtual(&result,
2843
obj,
2844
vmClasses::Thread_klass(),
2845
vmSymbols::run_method_name(),
2846
vmSymbols::void_method_signature(),
2847
THREAD);
2848
}
2849
2850
2851
JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
2852
JavaThread *native_thread = NULL;
2853
2854
// We cannot hold the Threads_lock when we throw an exception,
2855
// due to rank ordering issues. Example: we might need to grab the
2856
// Heap_lock while we construct the exception.
2857
bool throw_illegal_thread_state = false;
2858
2859
// We must release the Threads_lock before we can post a jvmti event
2860
// in Thread::start.
2861
{
2862
// Ensure that the C++ Thread and OSThread structures aren't freed before
2863
// we operate.
2864
MutexLocker mu(Threads_lock);
2865
2866
// Since JDK 5 the java.lang.Thread threadStatus is used to prevent
2867
// re-starting an already started thread, so we should usually find
2868
// that the JavaThread is null. However for a JNI attached thread
2869
// there is a small window between the Thread object being created
2870
// (with its JavaThread set) and the update to its threadStatus, so we
2871
// have to check for this
2872
if (java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) {
2873
throw_illegal_thread_state = true;
2874
} else {
2875
// We could also check the stillborn flag to see if this thread was already stopped, but
2876
// for historical reasons we let the thread detect that itself when it starts running
2877
2878
jlong size =
2879
java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread));
2880
// Allocate the C++ Thread structure and create the native thread. The
2881
// stack size retrieved from java is 64-bit signed, but the constructor takes
2882
// size_t (an unsigned type), which may be 32 or 64-bit depending on the platform.
2883
// - Avoid truncating on 32-bit platforms if size is greater than UINT_MAX.
2884
// - Avoid passing negative values which would result in really large stacks.
2885
NOT_LP64(if (size > SIZE_MAX) size = SIZE_MAX;)
2886
size_t sz = size > 0 ? (size_t) size : 0;
2887
native_thread = new JavaThread(&thread_entry, sz);
2888
2889
// At this point it may be possible that no osthread was created for the
2890
// JavaThread due to lack of memory. Check for this situation and throw
2891
// an exception if necessary. Eventually we may want to change this so
2892
// that we only grab the lock if the thread was created successfully -
2893
// then we can also do this check and throw the exception in the
2894
// JavaThread constructor.
2895
if (native_thread->osthread() != NULL) {
2896
// Note: the current thread is not being used within "prepare".
2897
native_thread->prepare(jthread);
2898
}
2899
}
2900
}
2901
2902
if (throw_illegal_thread_state) {
2903
THROW(vmSymbols::java_lang_IllegalThreadStateException());
2904
}
2905
2906
assert(native_thread != NULL, "Starting null thread?");
2907
2908
if (native_thread->osthread() == NULL) {
2909
// No one should hold a reference to the 'native_thread'.
2910
native_thread->smr_delete();
2911
if (JvmtiExport::should_post_resource_exhausted()) {
2912
JvmtiExport::post_resource_exhausted(
2913
JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
2914
os::native_thread_creation_failed_msg());
2915
}
2916
THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
2917
os::native_thread_creation_failed_msg());
2918
}
2919
2920
#if INCLUDE_JFR
2921
if (Jfr::is_recording() && EventThreadStart::is_enabled() &&
2922
EventThreadStart::is_stacktrace_enabled()) {
2923
JfrThreadLocal* tl = native_thread->jfr_thread_local();
2924
// skip Thread.start() and Thread.start0()
2925
tl->set_cached_stack_trace_id(JfrStackTraceRepository::record(thread, 2));
2926
}
2927
#endif
2928
2929
Thread::start(native_thread);
2930
2931
JVM_END
2932
2933
2934
// JVM_Stop is implemented using a VM_Operation, so threads are forced to safepoints
2935
// before the quasi-asynchronous exception is delivered. This is a little obtrusive,
2936
// but is thought to be reliable and simple. In the case, where the receiver is the
2937
// same thread as the sender, no VM_Operation is needed.
2938
JVM_ENTRY(void, JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable))
2939
ThreadsListHandle tlh(thread);
2940
oop java_throwable = JNIHandles::resolve(throwable);
2941
if (java_throwable == NULL) {
2942
THROW(vmSymbols::java_lang_NullPointerException());
2943
}
2944
oop java_thread = NULL;
2945
JavaThread* receiver = NULL;
2946
bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, &java_thread);
2947
Events::log_exception(thread,
2948
"JVM_StopThread thread JavaThread " INTPTR_FORMAT " as oop " INTPTR_FORMAT " [exception " INTPTR_FORMAT "]",
2949
p2i(receiver), p2i(java_thread), p2i(throwable));
2950
2951
if (is_alive) {
2952
// jthread refers to a live JavaThread.
2953
if (thread == receiver) {
2954
// Exception is getting thrown at self so no VM_Operation needed.
2955
THROW_OOP(java_throwable);
2956
} else {
2957
// Use a VM_Operation to throw the exception.
2958
JavaThread::send_async_exception(java_thread, java_throwable);
2959
}
2960
} else {
2961
// Either:
2962
// - target thread has not been started before being stopped, or
2963
// - target thread already terminated
2964
// We could read the threadStatus to determine which case it is
2965
// but that is overkill as it doesn't matter. We must set the
2966
// stillborn flag for the first case, and if the thread has already
2967
// exited setting this flag has no effect.
2968
java_lang_Thread::set_stillborn(java_thread);
2969
}
2970
JVM_END
2971
2972
2973
JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread))
2974
oop thread_oop = JNIHandles::resolve_non_null(jthread);
2975
return java_lang_Thread::is_alive(thread_oop);
2976
JVM_END
2977
2978
2979
JVM_ENTRY(void, JVM_SuspendThread(JNIEnv* env, jobject jthread))
2980
ThreadsListHandle tlh(thread);
2981
JavaThread* receiver = NULL;
2982
bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL);
2983
if (is_alive) {
2984
// jthread refers to a live JavaThread, but java_suspend() will
2985
// detect a thread that has started to exit and will ignore it.
2986
receiver->java_suspend();
2987
}
2988
JVM_END
2989
2990
2991
JVM_ENTRY(void, JVM_ResumeThread(JNIEnv* env, jobject jthread))
2992
ThreadsListHandle tlh(thread);
2993
JavaThread* receiver = NULL;
2994
bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL);
2995
if (is_alive) {
2996
// jthread refers to a live JavaThread.
2997
receiver->java_resume();
2998
}
2999
JVM_END
3000
3001
3002
JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
3003
ThreadsListHandle tlh(thread);
3004
oop java_thread = NULL;
3005
JavaThread* receiver = NULL;
3006
bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, &java_thread);
3007
java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
3008
3009
if (is_alive) {
3010
// jthread refers to a live JavaThread.
3011
Thread::set_priority(receiver, (ThreadPriority)prio);
3012
}
3013
// Implied else: If the JavaThread hasn't started yet, then the
3014
// priority set in the java.lang.Thread object above will be pushed
3015
// down when it does start.
3016
JVM_END
3017
3018
3019
JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass))
3020
if (os::dont_yield()) return;
3021
HOTSPOT_THREAD_YIELD();
3022
os::naked_yield();
3023
JVM_END
3024
3025
static void post_thread_sleep_event(EventThreadSleep* event, jlong millis) {
3026
assert(event != NULL, "invariant");
3027
assert(event->should_commit(), "invariant");
3028
event->set_time(millis);
3029
event->commit();
3030
}
3031
3032
JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
3033
if (millis < 0) {
3034
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
3035
}
3036
3037
if (thread->is_interrupted(true) && !HAS_PENDING_EXCEPTION) {
3038
THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3039
}
3040
3041
// Save current thread state and restore it at the end of this block.
3042
// And set new thread state to SLEEPING.
3043
JavaThreadSleepState jtss(thread);
3044
3045
HOTSPOT_THREAD_SLEEP_BEGIN(millis);
3046
EventThreadSleep event;
3047
3048
if (millis == 0) {
3049
os::naked_yield();
3050
} else {
3051
ThreadState old_state = thread->osthread()->get_state();
3052
thread->osthread()->set_state(SLEEPING);
3053
if (!thread->sleep(millis)) { // interrupted
3054
// An asynchronous exception (e.g., ThreadDeathException) could have been thrown on
3055
// us while we were sleeping. We do not overwrite those.
3056
if (!HAS_PENDING_EXCEPTION) {
3057
if (event.should_commit()) {
3058
post_thread_sleep_event(&event, millis);
3059
}
3060
HOTSPOT_THREAD_SLEEP_END(1);
3061
3062
// TODO-FIXME: THROW_MSG returns which means we will not call set_state()
3063
// to properly restore the thread state. That's likely wrong.
3064
THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3065
}
3066
}
3067
thread->osthread()->set_state(old_state);
3068
}
3069
if (event.should_commit()) {
3070
post_thread_sleep_event(&event, millis);
3071
}
3072
HOTSPOT_THREAD_SLEEP_END(0);
3073
JVM_END
3074
3075
JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass))
3076
oop jthread = thread->threadObj();
3077
assert(jthread != NULL, "no current thread!");
3078
return JNIHandles::make_local(THREAD, jthread);
3079
JVM_END
3080
3081
JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
3082
ThreadsListHandle tlh(thread);
3083
JavaThread* receiver = NULL;
3084
bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL);
3085
if (is_alive) {
3086
// jthread refers to a live JavaThread.
3087
receiver->interrupt();
3088
}
3089
JVM_END
3090
3091
3092
// Return true iff the current thread has locked the object passed in
3093
3094
JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj))
3095
if (obj == NULL) {
3096
THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
3097
}
3098
Handle h_obj(THREAD, JNIHandles::resolve(obj));
3099
return ObjectSynchronizer::current_thread_holds_lock(thread, h_obj);
3100
JVM_END
3101
3102
3103
JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass))
3104
VM_PrintThreads op;
3105
VMThread::execute(&op);
3106
if (JvmtiExport::should_post_data_dump()) {
3107
JvmtiExport::post_data_dump();
3108
}
3109
JVM_END
3110
3111
JVM_ENTRY(void, JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring name))
3112
// We don't use a ThreadsListHandle here because the current thread
3113
// must be alive.
3114
oop java_thread = JNIHandles::resolve_non_null(jthread);
3115
JavaThread* thr = java_lang_Thread::thread(java_thread);
3116
if (thread == thr && !thr->has_attached_via_jni()) {
3117
// Thread naming is only supported for the current thread and
3118
// we don't set the name of an attached thread to avoid stepping
3119
// on other programs.
3120
ResourceMark rm(thread);
3121
const char *thread_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
3122
os::set_native_thread_name(thread_name);
3123
}
3124
JVM_END
3125
3126
// java.lang.SecurityManager ///////////////////////////////////////////////////////////////////////
3127
3128
JVM_ENTRY(jobjectArray, JVM_GetClassContext(JNIEnv *env))
3129
ResourceMark rm(THREAD);
3130
JvmtiVMObjectAllocEventCollector oam;
3131
vframeStream vfst(thread);
3132
3133
if (vmClasses::reflect_CallerSensitive_klass() != NULL) {
3134
// This must only be called from SecurityManager.getClassContext
3135
Method* m = vfst.method();
3136
if (!(m->method_holder() == vmClasses::SecurityManager_klass() &&
3137
m->name() == vmSymbols::getClassContext_name() &&
3138
m->signature() == vmSymbols::void_class_array_signature())) {
3139
THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetClassContext must only be called from SecurityManager.getClassContext");
3140
}
3141
}
3142
3143
// Collect method holders
3144
GrowableArray<Klass*>* klass_array = new GrowableArray<Klass*>();
3145
for (; !vfst.at_end(); vfst.security_next()) {
3146
Method* m = vfst.method();
3147
// Native frames are not returned
3148
if (!m->is_ignored_by_security_stack_walk() && !m->is_native()) {
3149
Klass* holder = m->method_holder();
3150
assert(holder->is_klass(), "just checking");
3151
klass_array->append(holder);
3152
}
3153
}
3154
3155
// Create result array of type [Ljava/lang/Class;
3156
objArrayOop result = oopFactory::new_objArray(vmClasses::Class_klass(), klass_array->length(), CHECK_NULL);
3157
// Fill in mirrors corresponding to method holders
3158
for (int i = 0; i < klass_array->length(); i++) {
3159
result->obj_at_put(i, klass_array->at(i)->java_mirror());
3160
}
3161
3162
return (jobjectArray) JNIHandles::make_local(THREAD, result);
3163
JVM_END
3164
3165
3166
// java.lang.Package ////////////////////////////////////////////////////////////////
3167
3168
3169
JVM_ENTRY(jstring, JVM_GetSystemPackage(JNIEnv *env, jstring name))
3170
ResourceMark rm(THREAD);
3171
JvmtiVMObjectAllocEventCollector oam;
3172
char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
3173
oop result = ClassLoader::get_system_package(str, CHECK_NULL);
3174
return (jstring) JNIHandles::make_local(THREAD, result);
3175
JVM_END
3176
3177
3178
JVM_ENTRY(jobjectArray, JVM_GetSystemPackages(JNIEnv *env))
3179
JvmtiVMObjectAllocEventCollector oam;
3180
objArrayOop result = ClassLoader::get_system_packages(CHECK_NULL);
3181
return (jobjectArray) JNIHandles::make_local(THREAD, result);
3182
JVM_END
3183
3184
3185
// java.lang.ref.Reference ///////////////////////////////////////////////////////////////
3186
3187
3188
JVM_ENTRY(jobject, JVM_GetAndClearReferencePendingList(JNIEnv* env))
3189
MonitorLocker ml(Heap_lock);
3190
oop ref = Universe::reference_pending_list();
3191
if (ref != NULL) {
3192
Universe::clear_reference_pending_list();
3193
}
3194
return JNIHandles::make_local(THREAD, ref);
3195
JVM_END
3196
3197
JVM_ENTRY(jboolean, JVM_HasReferencePendingList(JNIEnv* env))
3198
MonitorLocker ml(Heap_lock);
3199
return Universe::has_reference_pending_list();
3200
JVM_END
3201
3202
JVM_ENTRY(void, JVM_WaitForReferencePendingList(JNIEnv* env))
3203
MonitorLocker ml(Heap_lock);
3204
while (!Universe::has_reference_pending_list()) {
3205
ml.wait();
3206
}
3207
JVM_END
3208
3209
JVM_ENTRY(jboolean, JVM_ReferenceRefersTo(JNIEnv* env, jobject ref, jobject o))
3210
oop ref_oop = JNIHandles::resolve_non_null(ref);
3211
oop referent = java_lang_ref_Reference::weak_referent_no_keepalive(ref_oop);
3212
return referent == JNIHandles::resolve(o);
3213
JVM_END
3214
3215
JVM_ENTRY(void, JVM_ReferenceClear(JNIEnv* env, jobject ref))
3216
oop ref_oop = JNIHandles::resolve_non_null(ref);
3217
// FinalReference has it's own implementation of clear().
3218
assert(!java_lang_ref_Reference::is_final(ref_oop), "precondition");
3219
if (java_lang_ref_Reference::unknown_referent_no_keepalive(ref_oop) == NULL) {
3220
// If the referent has already been cleared then done.
3221
// However, if the referent is dead but has not yet been cleared by
3222
// concurrent reference processing, it should NOT be cleared here.
3223
// Instead, clearing should be left to the GC. Clearing it here could
3224
// detectably lose an expected notification, which is impossible with
3225
// STW reference processing. The clearing in enqueue() doesn't have
3226
// this problem, since the enqueue covers the notification, but it's not
3227
// worth the effort to handle that case specially.
3228
return;
3229
}
3230
java_lang_ref_Reference::clear_referent(ref_oop);
3231
JVM_END
3232
3233
3234
// java.lang.ref.PhantomReference //////////////////////////////////////////////////
3235
3236
3237
JVM_ENTRY(jboolean, JVM_PhantomReferenceRefersTo(JNIEnv* env, jobject ref, jobject o))
3238
oop ref_oop = JNIHandles::resolve_non_null(ref);
3239
oop referent = java_lang_ref_Reference::phantom_referent_no_keepalive(ref_oop);
3240
return referent == JNIHandles::resolve(o);
3241
JVM_END
3242
3243
3244
// ObjectInputStream ///////////////////////////////////////////////////////////////
3245
3246
// Return the first user-defined class loader up the execution stack, or null
3247
// if only code from the bootstrap or platform class loader is on the stack.
3248
3249
JVM_ENTRY(jobject, JVM_LatestUserDefinedLoader(JNIEnv *env))
3250
for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
3251
InstanceKlass* ik = vfst.method()->method_holder();
3252
oop loader = ik->class_loader();
3253
if (loader != NULL && !SystemDictionary::is_platform_class_loader(loader)) {
3254
// Skip reflection related frames
3255
if (!ik->is_subclass_of(vmClasses::reflect_MethodAccessorImpl_klass()) &&
3256
!ik->is_subclass_of(vmClasses::reflect_ConstructorAccessorImpl_klass())) {
3257
return JNIHandles::make_local(THREAD, loader);
3258
}
3259
}
3260
}
3261
return NULL;
3262
JVM_END
3263
3264
3265
// Array ///////////////////////////////////////////////////////////////////////////////////////////
3266
3267
3268
// resolve array handle and check arguments
3269
static inline arrayOop check_array(JNIEnv *env, jobject arr, bool type_array_only, TRAPS) {
3270
if (arr == NULL) {
3271
THROW_0(vmSymbols::java_lang_NullPointerException());
3272
}
3273
oop a = JNIHandles::resolve_non_null(arr);
3274
if (!a->is_array()) {
3275
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array");
3276
} else if (type_array_only && !a->is_typeArray()) {
3277
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array of primitive type");
3278
}
3279
return arrayOop(a);
3280
}
3281
3282
3283
JVM_ENTRY(jint, JVM_GetArrayLength(JNIEnv *env, jobject arr))
3284
arrayOop a = check_array(env, arr, false, CHECK_0);
3285
return a->length();
3286
JVM_END
3287
3288
3289
JVM_ENTRY(jobject, JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index))
3290
JvmtiVMObjectAllocEventCollector oam;
3291
arrayOop a = check_array(env, arr, false, CHECK_NULL);
3292
jvalue value;
3293
BasicType type = Reflection::array_get(&value, a, index, CHECK_NULL);
3294
oop box = Reflection::box(&value, type, CHECK_NULL);
3295
return JNIHandles::make_local(THREAD, box);
3296
JVM_END
3297
3298
3299
JVM_ENTRY(jvalue, JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode))
3300
jvalue value;
3301
value.i = 0; // to initialize value before getting used in CHECK
3302
arrayOop a = check_array(env, arr, true, CHECK_(value));
3303
assert(a->is_typeArray(), "just checking");
3304
BasicType type = Reflection::array_get(&value, a, index, CHECK_(value));
3305
BasicType wide_type = (BasicType) wCode;
3306
if (type != wide_type) {
3307
Reflection::widen(&value, type, wide_type, CHECK_(value));
3308
}
3309
return value;
3310
JVM_END
3311
3312
3313
JVM_ENTRY(void, JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val))
3314
arrayOop a = check_array(env, arr, false, CHECK);
3315
oop box = JNIHandles::resolve(val);
3316
jvalue value;
3317
value.i = 0; // to initialize value before getting used in CHECK
3318
BasicType value_type;
3319
if (a->is_objArray()) {
3320
// Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array
3321
value_type = Reflection::unbox_for_regular_object(box, &value);
3322
} else {
3323
value_type = Reflection::unbox_for_primitive(box, &value, CHECK);
3324
}
3325
Reflection::array_set(&value, a, index, value_type, CHECK);
3326
JVM_END
3327
3328
3329
JVM_ENTRY(void, JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode))
3330
arrayOop a = check_array(env, arr, true, CHECK);
3331
assert(a->is_typeArray(), "just checking");
3332
BasicType value_type = (BasicType) vCode;
3333
Reflection::array_set(&v, a, index, value_type, CHECK);
3334
JVM_END
3335
3336
3337
JVM_ENTRY(jobject, JVM_NewArray(JNIEnv *env, jclass eltClass, jint length))
3338
JvmtiVMObjectAllocEventCollector oam;
3339
oop element_mirror = JNIHandles::resolve(eltClass);
3340
oop result = Reflection::reflect_new_array(element_mirror, length, CHECK_NULL);
3341
return JNIHandles::make_local(THREAD, result);
3342
JVM_END
3343
3344
3345
JVM_ENTRY(jobject, JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim))
3346
JvmtiVMObjectAllocEventCollector oam;
3347
arrayOop dim_array = check_array(env, dim, true, CHECK_NULL);
3348
oop element_mirror = JNIHandles::resolve(eltClass);
3349
assert(dim_array->is_typeArray(), "just checking");
3350
oop result = Reflection::reflect_new_multi_array(element_mirror, typeArrayOop(dim_array), CHECK_NULL);
3351
return JNIHandles::make_local(THREAD, result);
3352
JVM_END
3353
3354
3355
// Library support ///////////////////////////////////////////////////////////////////////////
3356
3357
JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name))
3358
//%note jvm_ct
3359
char ebuf[1024];
3360
void *load_result;
3361
{
3362
ThreadToNativeFromVM ttnfvm(thread);
3363
load_result = os::dll_load(name, ebuf, sizeof ebuf);
3364
}
3365
if (load_result == NULL) {
3366
char msg[1024];
3367
jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf);
3368
// Since 'ebuf' may contain a string encoded using
3369
// platform encoding scheme, we need to pass
3370
// Exceptions::unsafe_to_utf8 to the new_exception method
3371
// as the last argument. See bug 6367357.
3372
Handle h_exception =
3373
Exceptions::new_exception(thread,
3374
vmSymbols::java_lang_UnsatisfiedLinkError(),
3375
msg, Exceptions::unsafe_to_utf8);
3376
3377
THROW_HANDLE_0(h_exception);
3378
}
3379
log_info(library)("Loaded library %s, handle " INTPTR_FORMAT, name, p2i(load_result));
3380
return load_result;
3381
JVM_END
3382
3383
3384
JVM_LEAF(void, JVM_UnloadLibrary(void* handle))
3385
os::dll_unload(handle);
3386
log_info(library)("Unloaded library with handle " INTPTR_FORMAT, p2i(handle));
3387
JVM_END
3388
3389
3390
JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3391
void* find_result = os::dll_lookup(handle, name);
3392
log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3393
find_result != NULL ? "Found" : "Failed to find",
3394
name, p2i(handle));
3395
return find_result;
3396
JVM_END
3397
3398
3399
// JNI version ///////////////////////////////////////////////////////////////////////////////
3400
3401
JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3402
return Threads::is_supported_jni_version_including_1_1(version);
3403
JVM_END
3404
3405
3406
// String support ///////////////////////////////////////////////////////////////////////////
3407
3408
JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3409
JvmtiVMObjectAllocEventCollector oam;
3410
if (str == NULL) return NULL;
3411
oop string = JNIHandles::resolve_non_null(str);
3412
oop result = StringTable::intern(string, CHECK_NULL);
3413
return (jstring) JNIHandles::make_local(THREAD, result);
3414
JVM_END
3415
3416
3417
// VM Raw monitor support //////////////////////////////////////////////////////////////////////
3418
3419
// VM Raw monitors (not to be confused with JvmtiRawMonitors) are a simple mutual exclusion
3420
// lock (not actually monitors: no wait/notify) that is exported by the VM for use by JDK
3421
// library code. They may be used by JavaThreads and non-JavaThreads and do not participate
3422
// in the safepoint protocol, thread suspension, thread interruption, or anything of that
3423
// nature. JavaThreads will be "in native" when using this API from JDK code.
3424
3425
3426
JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void) {
3427
VM_Exit::block_if_vm_exited();
3428
return new os::PlatformMutex();
3429
}
3430
3431
3432
JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void *mon) {
3433
VM_Exit::block_if_vm_exited();
3434
delete ((os::PlatformMutex*) mon);
3435
}
3436
3437
3438
JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon) {
3439
VM_Exit::block_if_vm_exited();
3440
((os::PlatformMutex*) mon)->lock();
3441
return 0;
3442
}
3443
3444
3445
JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon) {
3446
VM_Exit::block_if_vm_exited();
3447
((os::PlatformMutex*) mon)->unlock();
3448
}
3449
3450
3451
// Shared JNI/JVM entry points //////////////////////////////////////////////////////////////
3452
3453
jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3454
Handle loader, Handle protection_domain,
3455
jboolean throwError, TRAPS) {
3456
// Security Note:
3457
// The Java level wrapper will perform the necessary security check allowing
3458
// us to pass the NULL as the initiating class loader. The VM is responsible for
3459
// the checkPackageAccess relative to the initiating class loader via the
3460
// protection_domain. The protection_domain is passed as NULL by the java code
3461
// if there is no security manager in 3-arg Class.forName().
3462
Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3463
3464
// Check if we should initialize the class
3465
if (init && klass->is_instance_klass()) {
3466
klass->initialize(CHECK_NULL);
3467
}
3468
return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3469
}
3470
3471
3472
// Method ///////////////////////////////////////////////////////////////////////////////////////////
3473
3474
JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3475
Handle method_handle;
3476
if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3477
method_handle = Handle(THREAD, JNIHandles::resolve(method));
3478
Handle receiver(THREAD, JNIHandles::resolve(obj));
3479
objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3480
oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3481
jobject res = JNIHandles::make_local(THREAD, result);
3482
if (JvmtiExport::should_post_vm_object_alloc()) {
3483
oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3484
assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!");
3485
if (java_lang_Class::is_primitive(ret_type)) {
3486
// Only for primitive type vm allocates memory for java object.
3487
// See box() method.
3488
JvmtiExport::post_vm_object_alloc(thread, result);
3489
}
3490
}
3491
return res;
3492
} else {
3493
THROW_0(vmSymbols::java_lang_StackOverflowError());
3494
}
3495
JVM_END
3496
3497
3498
JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3499
oop constructor_mirror = JNIHandles::resolve(c);
3500
objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3501
oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3502
jobject res = JNIHandles::make_local(THREAD, result);
3503
if (JvmtiExport::should_post_vm_object_alloc()) {
3504
JvmtiExport::post_vm_object_alloc(thread, result);
3505
}
3506
return res;
3507
JVM_END
3508
3509
// Atomic ///////////////////////////////////////////////////////////////////////////////////////////
3510
3511
JVM_LEAF(jboolean, JVM_SupportsCX8())
3512
return VM_Version::supports_cx8();
3513
JVM_END
3514
3515
JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3516
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3517
assert(k->is_klass(), "just checking");
3518
HeapShared::initialize_from_archived_subgraph(k, THREAD);
3519
JVM_END
3520
3521
JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3522
jclass caller,
3523
jstring invokedName,
3524
jobject invokedType,
3525
jobject methodType,
3526
jobject implMethodMember,
3527
jobject instantiatedMethodType,
3528
jclass lambdaProxyClass))
3529
#if INCLUDE_CDS
3530
if (!Arguments::is_dumping_archive()) {
3531
return;
3532
}
3533
3534
Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller));
3535
InstanceKlass* caller_ik = InstanceKlass::cast(caller_k);
3536
if (caller_ik->is_hidden()) {
3537
// Hidden classes not of type lambda proxy classes are currently not being archived.
3538
// If the caller_ik is of one of the above types, the corresponding lambda proxy class won't be
3539
// registered for archiving.
3540
return;
3541
}
3542
Klass* lambda_k = java_lang_Class::as_Klass(JNIHandles::resolve(lambdaProxyClass));
3543
InstanceKlass* lambda_ik = InstanceKlass::cast(lambda_k);
3544
assert(lambda_ik->is_hidden(), "must be a hidden class");
3545
assert(!lambda_ik->is_non_strong_hidden(), "expected a strong hidden class");
3546
3547
Symbol* invoked_name = NULL;
3548
if (invokedName != NULL) {
3549
invoked_name = java_lang_String::as_symbol(JNIHandles::resolve_non_null(invokedName));
3550
}
3551
Handle invoked_type_oop(THREAD, JNIHandles::resolve_non_null(invokedType));
3552
Symbol* invoked_type = java_lang_invoke_MethodType::as_signature(invoked_type_oop(), true);
3553
3554
Handle method_type_oop(THREAD, JNIHandles::resolve_non_null(methodType));
3555
Symbol* method_type = java_lang_invoke_MethodType::as_signature(method_type_oop(), true);
3556
3557
Handle impl_method_member_oop(THREAD, JNIHandles::resolve_non_null(implMethodMember));
3558
assert(java_lang_invoke_MemberName::is_method(impl_method_member_oop()), "must be");
3559
Method* m = java_lang_invoke_MemberName::vmtarget(impl_method_member_oop());
3560
3561
Handle instantiated_method_type_oop(THREAD, JNIHandles::resolve_non_null(instantiatedMethodType));
3562
Symbol* instantiated_method_type = java_lang_invoke_MethodType::as_signature(instantiated_method_type_oop(), true);
3563
3564
SystemDictionaryShared::add_lambda_proxy_class(caller_ik, lambda_ik, invoked_name, invoked_type,
3565
method_type, m, instantiated_method_type, THREAD);
3566
#endif // INCLUDE_CDS
3567
JVM_END
3568
3569
JVM_ENTRY(jclass, JVM_LookupLambdaProxyClassFromArchive(JNIEnv* env,
3570
jclass caller,
3571
jstring invokedName,
3572
jobject invokedType,
3573
jobject methodType,
3574
jobject implMethodMember,
3575
jobject instantiatedMethodType))
3576
#if INCLUDE_CDS
3577
3578
if (invokedName == NULL || invokedType == NULL || methodType == NULL ||
3579
implMethodMember == NULL || instantiatedMethodType == NULL) {
3580
THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
3581
}
3582
3583
Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller));
3584
InstanceKlass* caller_ik = InstanceKlass::cast(caller_k);
3585
if (!caller_ik->is_shared()) {
3586
// there won't be a shared lambda class if the caller_ik is not in the shared archive.
3587
return NULL;
3588
}
3589
3590
Symbol* invoked_name = java_lang_String::as_symbol(JNIHandles::resolve_non_null(invokedName));
3591
Handle invoked_type_oop(THREAD, JNIHandles::resolve_non_null(invokedType));
3592
Symbol* invoked_type = java_lang_invoke_MethodType::as_signature(invoked_type_oop(), true);
3593
3594
Handle method_type_oop(THREAD, JNIHandles::resolve_non_null(methodType));
3595
Symbol* method_type = java_lang_invoke_MethodType::as_signature(method_type_oop(), true);
3596
3597
Handle impl_method_member_oop(THREAD, JNIHandles::resolve_non_null(implMethodMember));
3598
assert(java_lang_invoke_MemberName::is_method(impl_method_member_oop()), "must be");
3599
Method* m = java_lang_invoke_MemberName::vmtarget(impl_method_member_oop());
3600
3601
Handle instantiated_method_type_oop(THREAD, JNIHandles::resolve_non_null(instantiatedMethodType));
3602
Symbol* instantiated_method_type = java_lang_invoke_MethodType::as_signature(instantiated_method_type_oop(), true);
3603
3604
InstanceKlass* lambda_ik = SystemDictionaryShared::get_shared_lambda_proxy_class(caller_ik, invoked_name, invoked_type,
3605
method_type, m, instantiated_method_type);
3606
jclass jcls = NULL;
3607
if (lambda_ik != NULL) {
3608
InstanceKlass* loaded_lambda = SystemDictionaryShared::prepare_shared_lambda_proxy_class(lambda_ik, caller_ik, THREAD);
3609
jcls = loaded_lambda == NULL ? NULL : (jclass) JNIHandles::make_local(THREAD, loaded_lambda->java_mirror());
3610
}
3611
return jcls;
3612
#else
3613
return NULL;
3614
#endif // INCLUDE_CDS
3615
JVM_END
3616
3617
JVM_ENTRY(jboolean, JVM_IsCDSDumpingEnabled(JNIEnv* env))
3618
return Arguments::is_dumping_archive();
3619
JVM_END
3620
3621
JVM_ENTRY(jboolean, JVM_IsSharingEnabled(JNIEnv* env))
3622
return UseSharedSpaces;
3623
JVM_END
3624
3625
JVM_ENTRY_NO_ENV(jlong, JVM_GetRandomSeedForDumping())
3626
if (DumpSharedSpaces) {
3627
const char* release = Abstract_VM_Version::vm_release();
3628
const char* dbg_level = Abstract_VM_Version::jdk_debug_level();
3629
const char* version = VM_Version::internal_vm_info_string();
3630
jlong seed = (jlong)(java_lang_String::hash_code((const jbyte*)release, (int)strlen(release)) ^
3631
java_lang_String::hash_code((const jbyte*)dbg_level, (int)strlen(dbg_level)) ^
3632
java_lang_String::hash_code((const jbyte*)version, (int)strlen(version)));
3633
seed += (jlong)Abstract_VM_Version::vm_major_version();
3634
seed += (jlong)Abstract_VM_Version::vm_minor_version();
3635
seed += (jlong)Abstract_VM_Version::vm_security_version();
3636
seed += (jlong)Abstract_VM_Version::vm_patch_version();
3637
if (seed == 0) { // don't let this ever be zero.
3638
seed = 0x87654321;
3639
}
3640
log_debug(cds)("JVM_GetRandomSeedForDumping() = " JLONG_FORMAT, seed);
3641
return seed;
3642
} else {
3643
return 0;
3644
}
3645
JVM_END
3646
3647
JVM_ENTRY(jboolean, JVM_IsDumpingClassList(JNIEnv *env))
3648
#if INCLUDE_CDS
3649
return ClassListWriter::is_enabled() || DynamicDumpSharedSpaces;
3650
#else
3651
return false;
3652
#endif // INCLUDE_CDS
3653
JVM_END
3654
3655
JVM_ENTRY(void, JVM_LogLambdaFormInvoker(JNIEnv *env, jstring line))
3656
#if INCLUDE_CDS
3657
assert(ClassListWriter::is_enabled() || DynamicDumpSharedSpaces, "Should be set and open or do dynamic dump");
3658
if (line != NULL) {
3659
ResourceMark rm(THREAD);
3660
Handle h_line (THREAD, JNIHandles::resolve_non_null(line));
3661
char* c_line = java_lang_String::as_utf8_string(h_line());
3662
if (DynamicDumpSharedSpaces) {
3663
// Note: LambdaFormInvokers::append_filtered and LambdaFormInvokers::append take same format which is not
3664
// same as below the print format. The line does not include LAMBDA_FORM_TAG.
3665
LambdaFormInvokers::append_filtered(os::strdup((const char*)c_line, mtInternal));
3666
}
3667
if (ClassListWriter::is_enabled()) {
3668
ClassListWriter w;
3669
w.stream()->print_cr("%s %s", LAMBDA_FORM_TAG, c_line);
3670
}
3671
}
3672
#endif // INCLUDE_CDS
3673
JVM_END
3674
3675
JVM_ENTRY(void, JVM_DumpClassListToFile(JNIEnv *env, jstring listFileName))
3676
#if INCLUDE_CDS
3677
ResourceMark rm(THREAD);
3678
Handle file_handle(THREAD, JNIHandles::resolve_non_null(listFileName));
3679
char* file_name = java_lang_String::as_utf8_string(file_handle());
3680
MetaspaceShared::dump_loaded_classes(file_name, THREAD);
3681
#endif // INCLUDE_CDS
3682
JVM_END
3683
3684
JVM_ENTRY(void, JVM_DumpDynamicArchive(JNIEnv *env, jstring archiveName))
3685
#if INCLUDE_CDS
3686
ResourceMark rm(THREAD);
3687
Handle file_handle(THREAD, JNIHandles::resolve_non_null(archiveName));
3688
char* archive_name = java_lang_String::as_utf8_string(file_handle());
3689
DynamicArchive::dump(archive_name, CHECK);
3690
#endif // INCLUDE_CDS
3691
JVM_END
3692
3693
// Returns an array of all live Thread objects (VM internal JavaThreads,
3694
// jvmti agent threads, and JNI attaching threads are skipped)
3695
// See CR 6404306 regarding JNI attaching threads
3696
JVM_ENTRY(jobjectArray, JVM_GetAllThreads(JNIEnv *env, jclass dummy))
3697
ResourceMark rm(THREAD);
3698
ThreadsListEnumerator tle(THREAD, false, false);
3699
JvmtiVMObjectAllocEventCollector oam;
3700
3701
int num_threads = tle.num_threads();
3702
objArrayOop r = oopFactory::new_objArray(vmClasses::Thread_klass(), num_threads, CHECK_NULL);
3703
objArrayHandle threads_ah(THREAD, r);
3704
3705
for (int i = 0; i < num_threads; i++) {
3706
Handle h = tle.get_threadObj(i);
3707
threads_ah->obj_at_put(i, h());
3708
}
3709
3710
return (jobjectArray) JNIHandles::make_local(THREAD, threads_ah());
3711
JVM_END
3712
3713
3714
// Support for java.lang.Thread.getStackTrace() and getAllStackTraces() methods
3715
// Return StackTraceElement[][], each element is the stack trace of a thread in
3716
// the corresponding entry in the given threads array
3717
JVM_ENTRY(jobjectArray, JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads))
3718
JvmtiVMObjectAllocEventCollector oam;
3719
3720
// Check if threads is null
3721
if (threads == NULL) {
3722
THROW_(vmSymbols::java_lang_NullPointerException(), 0);
3723
}
3724
3725
objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
3726
objArrayHandle ah(THREAD, a);
3727
int num_threads = ah->length();
3728
// check if threads is non-empty array
3729
if (num_threads == 0) {
3730
THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
3731
}
3732
3733
// check if threads is not an array of objects of Thread class
3734
Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3735
if (k != vmClasses::Thread_klass()) {
3736
THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
3737
}
3738
3739
ResourceMark rm(THREAD);
3740
3741
GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3742
for (int i = 0; i < num_threads; i++) {
3743
oop thread_obj = ah->obj_at(i);
3744
instanceHandle h(THREAD, (instanceOop) thread_obj);
3745
thread_handle_array->append(h);
3746
}
3747
3748
// The JavaThread references in thread_handle_array are validated
3749
// in VM_ThreadDump::doit().
3750
Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3751
return (jobjectArray)JNIHandles::make_local(THREAD, stacktraces());
3752
3753
JVM_END
3754
3755
// JVM monitoring and management support
3756
JVM_ENTRY_NO_ENV(void*, JVM_GetManagement(jint version))
3757
return Management::get_jmm_interface(version);
3758
JVM_END
3759
3760
// com.sun.tools.attach.VirtualMachine agent properties support
3761
//
3762
// Initialize the agent properties with the properties maintained in the VM
3763
JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
3764
ResourceMark rm;
3765
3766
Handle props(THREAD, JNIHandles::resolve_non_null(properties));
3767
3768
PUTPROP(props, "sun.java.command", Arguments::java_command());
3769
PUTPROP(props, "sun.jvm.flags", Arguments::jvm_flags());
3770
PUTPROP(props, "sun.jvm.args", Arguments::jvm_args());
3771
return properties;
3772
JVM_END
3773
3774
JVM_ENTRY(jobjectArray, JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass))
3775
{
3776
JvmtiVMObjectAllocEventCollector oam;
3777
3778
if (ofClass == NULL) {
3779
return NULL;
3780
}
3781
Handle mirror(THREAD, JNIHandles::resolve_non_null(ofClass));
3782
// Special handling for primitive objects
3783
if (java_lang_Class::is_primitive(mirror())) {
3784
return NULL;
3785
}
3786
Klass* k = java_lang_Class::as_Klass(mirror());
3787
if (!k->is_instance_klass()) {
3788
return NULL;
3789
}
3790
InstanceKlass* ik = InstanceKlass::cast(k);
3791
int encl_method_class_idx = ik->enclosing_method_class_index();
3792
if (encl_method_class_idx == 0) {
3793
return NULL;
3794
}
3795
objArrayOop dest_o = oopFactory::new_objArray(vmClasses::Object_klass(), 3, CHECK_NULL);
3796
objArrayHandle dest(THREAD, dest_o);
3797
Klass* enc_k = ik->constants()->klass_at(encl_method_class_idx, CHECK_NULL);
3798
dest->obj_at_put(0, enc_k->java_mirror());
3799
int encl_method_method_idx = ik->enclosing_method_method_index();
3800
if (encl_method_method_idx != 0) {
3801
Symbol* sym = ik->constants()->symbol_at(
3802
extract_low_short_from_int(
3803
ik->constants()->name_and_type_at(encl_method_method_idx)));
3804
Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
3805
dest->obj_at_put(1, str());
3806
sym = ik->constants()->symbol_at(
3807
extract_high_short_from_int(
3808
ik->constants()->name_and_type_at(encl_method_method_idx)));
3809
str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
3810
dest->obj_at_put(2, str());
3811
}
3812
return (jobjectArray) JNIHandles::make_local(THREAD, dest());
3813
}
3814
JVM_END
3815
3816
// Returns an array of java.lang.String objects containing the input arguments to the VM.
3817
JVM_ENTRY(jobjectArray, JVM_GetVmArguments(JNIEnv *env))
3818
ResourceMark rm(THREAD);
3819
3820
if (Arguments::num_jvm_args() == 0 && Arguments::num_jvm_flags() == 0) {
3821
return NULL;
3822
}
3823
3824
char** vm_flags = Arguments::jvm_flags_array();
3825
char** vm_args = Arguments::jvm_args_array();
3826
int num_flags = Arguments::num_jvm_flags();
3827
int num_args = Arguments::num_jvm_args();
3828
3829
InstanceKlass* ik = vmClasses::String_klass();
3830
objArrayOop r = oopFactory::new_objArray(ik, num_args + num_flags, CHECK_NULL);
3831
objArrayHandle result_h(THREAD, r);
3832
3833
int index = 0;
3834
for (int j = 0; j < num_flags; j++, index++) {
3835
Handle h = java_lang_String::create_from_platform_dependent_str(vm_flags[j], CHECK_NULL);
3836
result_h->obj_at_put(index, h());
3837
}
3838
for (int i = 0; i < num_args; i++, index++) {
3839
Handle h = java_lang_String::create_from_platform_dependent_str(vm_args[i], CHECK_NULL);
3840
result_h->obj_at_put(index, h());
3841
}
3842
return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
3843
JVM_END
3844
3845
JVM_ENTRY_NO_ENV(jint, JVM_FindSignal(const char *name))
3846
return os::get_signal_number(name);
3847
JVM_END
3848
3849