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