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