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