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