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