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