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 oop mirror = JNIHandles::resolve_non_null(cls); 1308 if (java_lang_Class::is_primitive(mirror)) { 1309 // There are no signers for primitive types 1310 return NULL; 1311 } 1312 1313 objArrayHandle signers(THREAD, java_lang_Class::signers(mirror)); 1314 1315 // If there are no signers set in the class, or if the class 1316 // is an array, return NULL. 1317 if (signers == NULL) return NULL; 1318 1319 // copy of the signers array 1320 Klass* element = ObjArrayKlass::cast(signers->klass())->element_klass(); 1321 objArrayOop signers_copy = oopFactory::new_objArray(element, signers->length(), CHECK_NULL); 1322 for (int index = 0; index < signers->length(); index++) { 1323 signers_copy->obj_at_put(index, signers->obj_at(index)); 1324 } 1325 1326 // return the copy 1327 return (jobjectArray) JNIHandles::make_local(THREAD, signers_copy); 1328 JVM_END 1329 1330 1331 JVM_ENTRY(void, JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers)) 1332 JVMWrapper("JVM_SetClassSigners"); 1333 oop mirror = JNIHandles::resolve_non_null(cls); 1334 if (!java_lang_Class::is_primitive(mirror)) { 1335 // This call is ignored for primitive types and arrays. 1336 // Signers are only set once, ClassLoader.java, and thus shouldn't 1337 // be called with an array. Only the bootstrap loader creates arrays. 1338 Klass* k = java_lang_Class::as_Klass(mirror); 1339 if (k->is_instance_klass()) { 1340 java_lang_Class::set_signers(k->java_mirror(), objArrayOop(JNIHandles::resolve(signers))); 1341 } 1342 } 1343 JVM_END 1344 1345 1346 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls)) 1347 JVMWrapper("JVM_GetProtectionDomain"); 1348 oop mirror = JNIHandles::resolve_non_null(cls); 1349 if (mirror == NULL) { 1350 THROW_(vmSymbols::java_lang_NullPointerException(), NULL); 1351 } 1352 1353 if (java_lang_Class::is_primitive(mirror)) { 1354 // Primitive types does not have a protection domain. 1355 return NULL; 1356 } 1357 1358 oop pd = java_lang_Class::protection_domain(mirror); 1359 return (jobject) JNIHandles::make_local(THREAD, pd); 1360 JVM_END 1361 1362 1363 // Returns the inherited_access_control_context field of the running thread. 1364 JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls)) 1365 JVMWrapper("JVM_GetInheritedAccessControlContext"); 1366 oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj()); 1367 return JNIHandles::make_local(THREAD, result); 1368 JVM_END 1369 1370 class RegisterArrayForGC { 1371 private: 1372 JavaThread *_thread; 1373 public: 1374 RegisterArrayForGC(JavaThread *thread, GrowableArray<oop>* array) { 1375 _thread = thread; 1376 _thread->register_array_for_gc(array); 1377 } 1378 1379 ~RegisterArrayForGC() { 1380 _thread->register_array_for_gc(NULL); 1381 } 1382 }; 1383 1384 1385 JVM_ENTRY(jobject, JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls)) 1386 JVMWrapper("JVM_GetStackAccessControlContext"); 1387 if (!UsePrivilegedStack) return NULL; 1388 1389 ResourceMark rm(THREAD); 1390 GrowableArray<oop>* local_array = new GrowableArray<oop>(12); 1391 JvmtiVMObjectAllocEventCollector oam; 1392 1393 // count the protection domains on the execution stack. We collapse 1394 // duplicate consecutive protection domains into a single one, as 1395 // well as stopping when we hit a privileged frame. 1396 1397 oop previous_protection_domain = NULL; 1398 Handle privileged_context(thread, NULL); 1399 bool is_privileged = false; 1400 oop protection_domain = NULL; 1401 1402 // Iterate through Java frames 1403 vframeStream vfst(thread); 1404 for(; !vfst.at_end(); vfst.next()) { 1405 // get method of frame 1406 Method* method = vfst.method(); 1407 1408 // stop at the first privileged frame 1409 if (method->method_holder() == SystemDictionary::AccessController_klass() && 1410 method->name() == vmSymbols::executePrivileged_name()) 1411 { 1412 // this frame is privileged 1413 is_privileged = true; 1414 1415 javaVFrame *priv = vfst.asJavaVFrame(); // executePrivileged 1416 1417 StackValueCollection* locals = priv->locals(); 1418 StackValue* ctx_sv = locals->at(1); // AccessControlContext context 1419 StackValue* clr_sv = locals->at(2); // Class<?> caller 1420 assert(!ctx_sv->obj_is_scalar_replaced(), "found scalar-replaced object"); 1421 assert(!clr_sv->obj_is_scalar_replaced(), "found scalar-replaced object"); 1422 privileged_context = ctx_sv->get_obj(); 1423 Handle caller = clr_sv->get_obj(); 1424 1425 Klass *caller_klass = java_lang_Class::as_Klass(caller()); 1426 protection_domain = caller_klass->protection_domain(); 1427 } else { 1428 protection_domain = method->method_holder()->protection_domain(); 1429 } 1430 1431 if ((previous_protection_domain != protection_domain) && (protection_domain != NULL)) { 1432 local_array->push(protection_domain); 1433 previous_protection_domain = protection_domain; 1434 } 1435 1436 if (is_privileged) break; 1437 } 1438 1439 1440 // either all the domains on the stack were system domains, or 1441 // we had a privileged system domain 1442 if (local_array->is_empty()) { 1443 if (is_privileged && privileged_context.is_null()) return NULL; 1444 1445 oop result = java_security_AccessControlContext::create(objArrayHandle(), is_privileged, privileged_context, CHECK_NULL); 1446 return JNIHandles::make_local(THREAD, result); 1447 } 1448 1449 // the resource area must be registered in case of a gc 1450 RegisterArrayForGC ragc(thread, local_array); 1451 objArrayOop context = oopFactory::new_objArray(SystemDictionary::ProtectionDomain_klass(), 1452 local_array->length(), CHECK_NULL); 1453 objArrayHandle h_context(thread, context); 1454 for (int index = 0; index < local_array->length(); index++) { 1455 h_context->obj_at_put(index, local_array->at(index)); 1456 } 1457 1458 oop result = java_security_AccessControlContext::create(h_context, is_privileged, privileged_context, CHECK_NULL); 1459 1460 return JNIHandles::make_local(THREAD, result); 1461 JVM_END 1462 1463 1464 JVM_ENTRY(jboolean, JVM_IsArrayClass(JNIEnv *env, jclass cls)) 1465 JVMWrapper("JVM_IsArrayClass"); 1466 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 1467 return (k != NULL) && k->is_array_klass() ? true : false; 1468 JVM_END 1469 1470 1471 JVM_ENTRY(jboolean, JVM_IsPrimitiveClass(JNIEnv *env, jclass cls)) 1472 JVMWrapper("JVM_IsPrimitiveClass"); 1473 oop mirror = JNIHandles::resolve_non_null(cls); 1474 return (jboolean) java_lang_Class::is_primitive(mirror); 1475 JVM_END 1476 1477 1478 JVM_ENTRY(jint, JVM_GetClassModifiers(JNIEnv *env, jclass cls)) 1479 JVMWrapper("JVM_GetClassModifiers"); 1480 oop mirror = JNIHandles::resolve_non_null(cls); 1481 if (java_lang_Class::is_primitive(mirror)) { 1482 // Primitive type 1483 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC; 1484 } 1485 1486 Klass* k = java_lang_Class::as_Klass(mirror); 1487 debug_only(int computed_modifiers = k->compute_modifier_flags(CHECK_0)); 1488 assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK"); 1489 return k->modifier_flags(); 1490 JVM_END 1491 1492 1493 // Inner class reflection /////////////////////////////////////////////////////////////////////////////// 1494 1495 JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass)) 1496 JvmtiVMObjectAllocEventCollector oam; 1497 // ofClass is a reference to a java_lang_Class object. The mirror object 1498 // of an InstanceKlass 1499 oop ofMirror = JNIHandles::resolve_non_null(ofClass); 1500 if (java_lang_Class::is_primitive(ofMirror) || 1501 ! java_lang_Class::as_Klass(ofMirror)->is_instance_klass()) { 1502 oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL); 1503 return (jobjectArray)JNIHandles::make_local(THREAD, result); 1504 } 1505 1506 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror)); 1507 InnerClassesIterator iter(k); 1508 1509 if (iter.length() == 0) { 1510 // Neither an inner nor outer class 1511 oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL); 1512 return (jobjectArray)JNIHandles::make_local(THREAD, result); 1513 } 1514 1515 // find inner class info 1516 constantPoolHandle cp(thread, k->constants()); 1517 int length = iter.length(); 1518 1519 // Allocate temp. result array 1520 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), length/4, CHECK_NULL); 1521 objArrayHandle result (THREAD, r); 1522 int members = 0; 1523 1524 for (; !iter.done(); iter.next()) { 1525 int ioff = iter.inner_class_info_index(); 1526 int ooff = iter.outer_class_info_index(); 1527 1528 if (ioff != 0 && ooff != 0) { 1529 // Check to see if the name matches the class we're looking for 1530 // before attempting to find the class. 1531 if (cp->klass_name_at_matches(k, ooff)) { 1532 Klass* outer_klass = cp->klass_at(ooff, CHECK_NULL); 1533 if (outer_klass == k) { 1534 Klass* ik = cp->klass_at(ioff, CHECK_NULL); 1535 InstanceKlass* inner_klass = InstanceKlass::cast(ik); 1536 1537 // Throws an exception if outer klass has not declared k as 1538 // an inner klass 1539 Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL); 1540 1541 result->obj_at_put(members, inner_klass->java_mirror()); 1542 members++; 1543 } 1544 } 1545 } 1546 } 1547 1548 if (members != length) { 1549 // Return array of right length 1550 objArrayOop res = oopFactory::new_objArray(SystemDictionary::Class_klass(), members, CHECK_NULL); 1551 for(int i = 0; i < members; i++) { 1552 res->obj_at_put(i, result->obj_at(i)); 1553 } 1554 return (jobjectArray)JNIHandles::make_local(THREAD, res); 1555 } 1556 1557 return (jobjectArray)JNIHandles::make_local(THREAD, result()); 1558 JVM_END 1559 1560 1561 JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass)) 1562 { 1563 // ofClass is a reference to a java_lang_Class object. 1564 oop ofMirror = JNIHandles::resolve_non_null(ofClass); 1565 if (java_lang_Class::is_primitive(ofMirror)) { 1566 return NULL; 1567 } 1568 Klass* klass = java_lang_Class::as_Klass(ofMirror); 1569 if (!klass->is_instance_klass()) { 1570 return NULL; 1571 } 1572 1573 bool inner_is_member = false; 1574 Klass* outer_klass 1575 = InstanceKlass::cast(klass)->compute_enclosing_class(&inner_is_member, CHECK_NULL); 1576 if (outer_klass == NULL) return NULL; // already a top-level class 1577 if (!inner_is_member) return NULL; // a hidden or unsafe anonymous class (inside a method) 1578 return (jclass) JNIHandles::make_local(THREAD, outer_klass->java_mirror()); 1579 } 1580 JVM_END 1581 1582 JVM_ENTRY(jstring, JVM_GetSimpleBinaryName(JNIEnv *env, jclass cls)) 1583 { 1584 oop mirror = JNIHandles::resolve_non_null(cls); 1585 if (java_lang_Class::is_primitive(mirror)) { 1586 return NULL; 1587 } 1588 Klass* klass = java_lang_Class::as_Klass(mirror); 1589 if (!klass->is_instance_klass()) { 1590 return NULL; 1591 } 1592 InstanceKlass* k = InstanceKlass::cast(klass); 1593 int ooff = 0, noff = 0; 1594 if (k->find_inner_classes_attr(&ooff, &noff, THREAD)) { 1595 if (noff != 0) { 1596 constantPoolHandle i_cp(thread, k->constants()); 1597 Symbol* name = i_cp->symbol_at(noff); 1598 Handle str = java_lang_String::create_from_symbol(name, CHECK_NULL); 1599 return (jstring) JNIHandles::make_local(THREAD, str()); 1600 } 1601 } 1602 return NULL; 1603 } 1604 JVM_END 1605 1606 JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls)) 1607 assert (cls != NULL, "illegal class"); 1608 JVMWrapper("JVM_GetClassSignature"); 1609 JvmtiVMObjectAllocEventCollector oam; 1610 ResourceMark rm(THREAD); 1611 oop mirror = JNIHandles::resolve_non_null(cls); 1612 // Return null for arrays and primatives 1613 if (!java_lang_Class::is_primitive(mirror)) { 1614 Klass* k = java_lang_Class::as_Klass(mirror); 1615 if (k->is_instance_klass()) { 1616 Symbol* sym = InstanceKlass::cast(k)->generic_signature(); 1617 if (sym == NULL) return NULL; 1618 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL); 1619 return (jstring) JNIHandles::make_local(THREAD, str()); 1620 } 1621 } 1622 return NULL; 1623 JVM_END 1624 1625 1626 JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls)) 1627 assert (cls != NULL, "illegal class"); 1628 JVMWrapper("JVM_GetClassAnnotations"); 1629 oop mirror = JNIHandles::resolve_non_null(cls); 1630 // Return null for arrays and primitives 1631 if (!java_lang_Class::is_primitive(mirror)) { 1632 Klass* k = java_lang_Class::as_Klass(mirror); 1633 if (k->is_instance_klass()) { 1634 typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL); 1635 return (jbyteArray) JNIHandles::make_local(THREAD, a); 1636 } 1637 } 1638 return NULL; 1639 JVM_END 1640 1641 1642 static bool jvm_get_field_common(jobject field, fieldDescriptor& fd, TRAPS) { 1643 // some of this code was adapted from from jni_FromReflectedField 1644 1645 oop reflected = JNIHandles::resolve_non_null(field); 1646 oop mirror = java_lang_reflect_Field::clazz(reflected); 1647 Klass* k = java_lang_Class::as_Klass(mirror); 1648 int slot = java_lang_reflect_Field::slot(reflected); 1649 int modifiers = java_lang_reflect_Field::modifiers(reflected); 1650 1651 InstanceKlass* ik = InstanceKlass::cast(k); 1652 intptr_t offset = ik->field_offset(slot); 1653 1654 if (modifiers & JVM_ACC_STATIC) { 1655 // for static fields we only look in the current class 1656 if (!ik->find_local_field_from_offset(offset, true, &fd)) { 1657 assert(false, "cannot find static field"); 1658 return false; 1659 } 1660 } else { 1661 // for instance fields we start with the current class and work 1662 // our way up through the superclass chain 1663 if (!ik->find_field_from_offset(offset, false, &fd)) { 1664 assert(false, "cannot find instance field"); 1665 return false; 1666 } 1667 } 1668 return true; 1669 } 1670 1671 static Method* jvm_get_method_common(jobject method) { 1672 // some of this code was adapted from from jni_FromReflectedMethod 1673 1674 oop reflected = JNIHandles::resolve_non_null(method); 1675 oop mirror = NULL; 1676 int slot = 0; 1677 1678 if (reflected->klass() == SystemDictionary::reflect_Constructor_klass()) { 1679 mirror = java_lang_reflect_Constructor::clazz(reflected); 1680 slot = java_lang_reflect_Constructor::slot(reflected); 1681 } else { 1682 assert(reflected->klass() == SystemDictionary::reflect_Method_klass(), 1683 "wrong type"); 1684 mirror = java_lang_reflect_Method::clazz(reflected); 1685 slot = java_lang_reflect_Method::slot(reflected); 1686 } 1687 Klass* k = java_lang_Class::as_Klass(mirror); 1688 1689 Method* m = InstanceKlass::cast(k)->method_with_idnum(slot); 1690 assert(m != NULL, "cannot find method"); 1691 return m; // caller has to deal with NULL in product mode 1692 } 1693 1694 /* Type use annotations support (JDK 1.8) */ 1695 1696 JVM_ENTRY(jbyteArray, JVM_GetClassTypeAnnotations(JNIEnv *env, jclass cls)) 1697 assert (cls != NULL, "illegal class"); 1698 JVMWrapper("JVM_GetClassTypeAnnotations"); 1699 ResourceMark rm(THREAD); 1700 // Return null for arrays and primitives 1701 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { 1702 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); 1703 if (k->is_instance_klass()) { 1704 AnnotationArray* type_annotations = InstanceKlass::cast(k)->class_type_annotations(); 1705 if (type_annotations != NULL) { 1706 typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL); 1707 return (jbyteArray) JNIHandles::make_local(THREAD, a); 1708 } 1709 } 1710 } 1711 return NULL; 1712 JVM_END 1713 1714 JVM_ENTRY(jbyteArray, JVM_GetMethodTypeAnnotations(JNIEnv *env, jobject method)) 1715 assert (method != NULL, "illegal method"); 1716 JVMWrapper("JVM_GetMethodTypeAnnotations"); 1717 1718 // method is a handle to a java.lang.reflect.Method object 1719 Method* m = jvm_get_method_common(method); 1720 if (m == NULL) { 1721 return NULL; 1722 } 1723 1724 AnnotationArray* type_annotations = m->type_annotations(); 1725 if (type_annotations != NULL) { 1726 typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL); 1727 return (jbyteArray) JNIHandles::make_local(THREAD, a); 1728 } 1729 1730 return NULL; 1731 JVM_END 1732 1733 JVM_ENTRY(jbyteArray, JVM_GetFieldTypeAnnotations(JNIEnv *env, jobject field)) 1734 assert (field != NULL, "illegal field"); 1735 JVMWrapper("JVM_GetFieldTypeAnnotations"); 1736 1737 fieldDescriptor fd; 1738 bool gotFd = jvm_get_field_common(field, fd, CHECK_NULL); 1739 if (!gotFd) { 1740 return NULL; 1741 } 1742 1743 return (jbyteArray) JNIHandles::make_local(THREAD, Annotations::make_java_array(fd.type_annotations(), THREAD)); 1744 JVM_END 1745 1746 static void bounds_check(const constantPoolHandle& cp, jint index, TRAPS) { 1747 if (!cp->is_within_bounds(index)) { 1748 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds"); 1749 } 1750 } 1751 1752 JVM_ENTRY(jobjectArray, JVM_GetMethodParameters(JNIEnv *env, jobject method)) 1753 { 1754 JVMWrapper("JVM_GetMethodParameters"); 1755 // method is a handle to a java.lang.reflect.Method object 1756 Method* method_ptr = jvm_get_method_common(method); 1757 methodHandle mh (THREAD, method_ptr); 1758 Handle reflected_method (THREAD, JNIHandles::resolve_non_null(method)); 1759 const int num_params = mh->method_parameters_length(); 1760 1761 if (num_params < 0) { 1762 // A -1 return value from method_parameters_length means there is no 1763 // parameter data. Return null to indicate this to the reflection 1764 // API. 1765 assert(num_params == -1, "num_params should be -1 if it is less than zero"); 1766 return (jobjectArray)NULL; 1767 } else { 1768 // Otherwise, we return something up to reflection, even if it is 1769 // a zero-length array. Why? Because in some cases this can 1770 // trigger a MalformedParametersException. 1771 1772 // make sure all the symbols are properly formatted 1773 for (int i = 0; i < num_params; i++) { 1774 MethodParametersElement* params = mh->method_parameters_start(); 1775 int index = params[i].name_cp_index; 1776 constantPoolHandle cp(THREAD, mh->constants()); 1777 bounds_check(cp, index, CHECK_NULL); 1778 1779 if (0 != index && !mh->constants()->tag_at(index).is_utf8()) { 1780 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), 1781 "Wrong type at constant pool index"); 1782 } 1783 1784 } 1785 1786 objArrayOop result_oop = oopFactory::new_objArray(SystemDictionary::reflect_Parameter_klass(), num_params, CHECK_NULL); 1787 objArrayHandle result (THREAD, result_oop); 1788 1789 for (int i = 0; i < num_params; i++) { 1790 MethodParametersElement* params = mh->method_parameters_start(); 1791 // For a 0 index, give a NULL symbol 1792 Symbol* sym = 0 != params[i].name_cp_index ? 1793 mh->constants()->symbol_at(params[i].name_cp_index) : NULL; 1794 int flags = params[i].flags; 1795 oop param = Reflection::new_parameter(reflected_method, i, sym, 1796 flags, CHECK_NULL); 1797 result->obj_at_put(i, param); 1798 } 1799 return (jobjectArray)JNIHandles::make_local(THREAD, result()); 1800 } 1801 } 1802 JVM_END 1803 1804 // New (JDK 1.4) reflection implementation ///////////////////////////////////// 1805 1806 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly)) 1807 { 1808 JVMWrapper("JVM_GetClassDeclaredFields"); 1809 JvmtiVMObjectAllocEventCollector oam; 1810 1811 oop ofMirror = JNIHandles::resolve_non_null(ofClass); 1812 // Exclude primitive types and array types 1813 if (java_lang_Class::is_primitive(ofMirror) || 1814 java_lang_Class::as_Klass(ofMirror)->is_array_klass()) { 1815 // Return empty array 1816 oop res = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), 0, CHECK_NULL); 1817 return (jobjectArray) JNIHandles::make_local(THREAD, res); 1818 } 1819 1820 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror)); 1821 constantPoolHandle cp(THREAD, k->constants()); 1822 1823 // Ensure class is linked 1824 k->link_class(CHECK_NULL); 1825 1826 // Allocate result 1827 int num_fields; 1828 1829 if (publicOnly) { 1830 num_fields = 0; 1831 for (JavaFieldStream fs(k); !fs.done(); fs.next()) { 1832 if (fs.access_flags().is_public()) ++num_fields; 1833 } 1834 } else { 1835 num_fields = k->java_fields_count(); 1836 } 1837 1838 objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), num_fields, CHECK_NULL); 1839 objArrayHandle result (THREAD, r); 1840 1841 int out_idx = 0; 1842 fieldDescriptor fd; 1843 for (JavaFieldStream fs(k); !fs.done(); fs.next()) { 1844 if (!publicOnly || fs.access_flags().is_public()) { 1845 fd.reinitialize(k, fs.index()); 1846 oop field = Reflection::new_field(&fd, CHECK_NULL); 1847 result->obj_at_put(out_idx, field); 1848 ++out_idx; 1849 } 1850 } 1851 assert(out_idx == num_fields, "just checking"); 1852 return (jobjectArray) JNIHandles::make_local(THREAD, result()); 1853 } 1854 JVM_END 1855 1856 JVM_ENTRY(jboolean, JVM_IsRecord(JNIEnv *env, jclass cls)) 1857 { 1858 JVMWrapper("JVM_IsRecord"); 1859 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 1860 if (k != NULL && k->is_instance_klass()) { 1861 InstanceKlass* ik = InstanceKlass::cast(k); 1862 return ik->is_record(); 1863 } else { 1864 return false; 1865 } 1866 } 1867 JVM_END 1868 1869 JVM_ENTRY(jobjectArray, JVM_GetRecordComponents(JNIEnv* env, jclass ofClass)) 1870 { 1871 JVMWrapper("JVM_GetRecordComponents"); 1872 Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)); 1873 assert(c->is_instance_klass(), "must be"); 1874 InstanceKlass* ik = InstanceKlass::cast(c); 1875 1876 if (ik->is_record()) { 1877 Array<RecordComponent*>* components = ik->record_components(); 1878 assert(components != NULL, "components should not be NULL"); 1879 { 1880 JvmtiVMObjectAllocEventCollector oam; 1881 constantPoolHandle cp(THREAD, ik->constants()); 1882 int length = components->length(); 1883 assert(length >= 0, "unexpected record_components length"); 1884 objArrayOop record_components = 1885 oopFactory::new_objArray(SystemDictionary::RecordComponent_klass(), length, CHECK_NULL); 1886 objArrayHandle components_h (THREAD, record_components); 1887 1888 for (int x = 0; x < length; x++) { 1889 RecordComponent* component = components->at(x); 1890 assert(component != NULL, "unexpected NULL record component"); 1891 oop component_oop = java_lang_reflect_RecordComponent::create(ik, component, CHECK_NULL); 1892 components_h->obj_at_put(x, component_oop); 1893 } 1894 return (jobjectArray)JNIHandles::make_local(THREAD, components_h()); 1895 } 1896 } 1897 1898 // Return empty array if ofClass is not a record. 1899 objArrayOop result = oopFactory::new_objArray(SystemDictionary::RecordComponent_klass(), 0, CHECK_NULL); 1900 return (jobjectArray)JNIHandles::make_local(THREAD, result); 1901 } 1902 JVM_END 1903 1904 static bool select_method(const methodHandle& method, bool want_constructor) { 1905 if (want_constructor) { 1906 return (method->is_initializer() && !method->is_static()); 1907 } else { 1908 return (!method->is_initializer() && !method->is_overpass()); 1909 } 1910 } 1911 1912 static jobjectArray get_class_declared_methods_helper( 1913 JNIEnv *env, 1914 jclass ofClass, jboolean publicOnly, 1915 bool want_constructor, 1916 Klass* klass, TRAPS) { 1917 1918 JvmtiVMObjectAllocEventCollector oam; 1919 1920 oop ofMirror = JNIHandles::resolve_non_null(ofClass); 1921 // Exclude primitive types and array types 1922 if (java_lang_Class::is_primitive(ofMirror) 1923 || java_lang_Class::as_Klass(ofMirror)->is_array_klass()) { 1924 // Return empty array 1925 oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL); 1926 return (jobjectArray) JNIHandles::make_local(THREAD, res); 1927 } 1928 1929 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror)); 1930 1931 // Ensure class is linked 1932 k->link_class(CHECK_NULL); 1933 1934 Array<Method*>* methods = k->methods(); 1935 int methods_length = methods->length(); 1936 1937 // Save original method_idnum in case of redefinition, which can change 1938 // the idnum of obsolete methods. The new method will have the same idnum 1939 // but if we refresh the methods array, the counts will be wrong. 1940 ResourceMark rm(THREAD); 1941 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length); 1942 int num_methods = 0; 1943 1944 for (int i = 0; i < methods_length; i++) { 1945 methodHandle method(THREAD, methods->at(i)); 1946 if (select_method(method, want_constructor)) { 1947 if (!publicOnly || method->is_public()) { 1948 idnums->push(method->method_idnum()); 1949 ++num_methods; 1950 } 1951 } 1952 } 1953 1954 // Allocate result 1955 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL); 1956 objArrayHandle result (THREAD, r); 1957 1958 // Now just put the methods that we selected above, but go by their idnum 1959 // in case of redefinition. The methods can be redefined at any safepoint, 1960 // so above when allocating the oop array and below when creating reflect 1961 // objects. 1962 for (int i = 0; i < num_methods; i++) { 1963 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i))); 1964 if (method.is_null()) { 1965 // Method may have been deleted and seems this API can handle null 1966 // Otherwise should probably put a method that throws NSME 1967 result->obj_at_put(i, NULL); 1968 } else { 1969 oop m; 1970 if (want_constructor) { 1971 m = Reflection::new_constructor(method, CHECK_NULL); 1972 } else { 1973 m = Reflection::new_method(method, false, CHECK_NULL); 1974 } 1975 result->obj_at_put(i, m); 1976 } 1977 } 1978 1979 return (jobjectArray) JNIHandles::make_local(THREAD, result()); 1980 } 1981 1982 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly)) 1983 { 1984 JVMWrapper("JVM_GetClassDeclaredMethods"); 1985 return get_class_declared_methods_helper(env, ofClass, publicOnly, 1986 /*want_constructor*/ false, 1987 SystemDictionary::reflect_Method_klass(), THREAD); 1988 } 1989 JVM_END 1990 1991 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly)) 1992 { 1993 JVMWrapper("JVM_GetClassDeclaredConstructors"); 1994 return get_class_declared_methods_helper(env, ofClass, publicOnly, 1995 /*want_constructor*/ true, 1996 SystemDictionary::reflect_Constructor_klass(), THREAD); 1997 } 1998 JVM_END 1999 2000 JVM_ENTRY(jint, JVM_GetClassAccessFlags(JNIEnv *env, jclass cls)) 2001 { 2002 JVMWrapper("JVM_GetClassAccessFlags"); 2003 oop mirror = JNIHandles::resolve_non_null(cls); 2004 if (java_lang_Class::is_primitive(mirror)) { 2005 // Primitive type 2006 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC; 2007 } 2008 2009 Klass* k = java_lang_Class::as_Klass(mirror); 2010 return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS; 2011 } 2012 JVM_END 2013 2014 JVM_ENTRY(jboolean, JVM_AreNestMates(JNIEnv *env, jclass current, jclass member)) 2015 { 2016 JVMWrapper("JVM_AreNestMates"); 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 Klass* m = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(member)); 2021 assert(m->is_instance_klass(), "must be"); 2022 InstanceKlass* mk = InstanceKlass::cast(m); 2023 return ck->has_nestmate_access_to(mk, THREAD); 2024 } 2025 JVM_END 2026 2027 JVM_ENTRY(jclass, JVM_GetNestHost(JNIEnv* env, jclass current)) 2028 { 2029 // current is not a primitive or array class 2030 JVMWrapper("JVM_GetNestHost"); 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 return (jclass) (host == NULL ? NULL : 2036 JNIHandles::make_local(THREAD, host->java_mirror())); 2037 } 2038 JVM_END 2039 2040 JVM_ENTRY(jobjectArray, JVM_GetNestMembers(JNIEnv* env, jclass current)) 2041 { 2042 // current is not a primitive or array class 2043 JVMWrapper("JVM_GetNestMembers"); 2044 ResourceMark rm(THREAD); 2045 Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(current)); 2046 assert(c->is_instance_klass(), "must be"); 2047 InstanceKlass* ck = InstanceKlass::cast(c); 2048 InstanceKlass* host = ck->nest_host(THREAD); 2049 2050 log_trace(class, nestmates)("Calling GetNestMembers for type %s with nest-host %s", 2051 ck->external_name(), host->external_name()); 2052 { 2053 JvmtiVMObjectAllocEventCollector oam; 2054 Array<u2>* members = host->nest_members(); 2055 int length = members == NULL ? 0 : members->length(); 2056 2057 log_trace(class, nestmates)(" - host has %d listed nest members", length); 2058 2059 // nest host is first in the array so make it one bigger 2060 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), 2061 length + 1, CHECK_NULL); 2062 objArrayHandle result(THREAD, r); 2063 result->obj_at_put(0, host->java_mirror()); 2064 if (length != 0) { 2065 int count = 0; 2066 for (int i = 0; i < length; i++) { 2067 int cp_index = members->at(i); 2068 Klass* k = host->constants()->klass_at(cp_index, THREAD); 2069 if (HAS_PENDING_EXCEPTION) { 2070 if (PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass())) { 2071 return NULL; // propagate VMEs 2072 } 2073 if (log_is_enabled(Trace, class, nestmates)) { 2074 stringStream ss; 2075 char* target_member_class = host->constants()->klass_name_at(cp_index)->as_C_string(); 2076 ss.print(" - resolution of nest member %s failed: ", target_member_class); 2077 java_lang_Throwable::print(PENDING_EXCEPTION, &ss); 2078 log_trace(class, nestmates)("%s", ss.as_string()); 2079 } 2080 CLEAR_PENDING_EXCEPTION; 2081 continue; 2082 } 2083 if (k->is_instance_klass()) { 2084 InstanceKlass* ik = InstanceKlass::cast(k); 2085 InstanceKlass* nest_host_k = ik->nest_host(CHECK_NULL); 2086 if (nest_host_k == host) { 2087 result->obj_at_put(count+1, k->java_mirror()); 2088 count++; 2089 log_trace(class, nestmates)(" - [%d] = %s", count, ik->external_name()); 2090 } else { 2091 log_trace(class, nestmates)(" - skipping member %s with different host %s", 2092 ik->external_name(), nest_host_k->external_name()); 2093 } 2094 } else { 2095 log_trace(class, nestmates)(" - skipping member %s that is not an instance class", 2096 k->external_name()); 2097 } 2098 } 2099 if (count < length) { 2100 // we had invalid entries so we need to compact the array 2101 log_trace(class, nestmates)(" - compacting array from length %d to %d", 2102 length + 1, count + 1); 2103 2104 objArrayOop r2 = oopFactory::new_objArray(SystemDictionary::Class_klass(), 2105 count + 1, CHECK_NULL); 2106 objArrayHandle result2(THREAD, r2); 2107 for (int i = 0; i < count + 1; i++) { 2108 result2->obj_at_put(i, result->obj_at(i)); 2109 } 2110 return (jobjectArray)JNIHandles::make_local(THREAD, result2()); 2111 } 2112 } 2113 else { 2114 assert(host == ck || ck->is_hidden(), "must be singleton nest or dynamic nestmate"); 2115 } 2116 return (jobjectArray)JNIHandles::make_local(THREAD, result()); 2117 } 2118 } 2119 JVM_END 2120 2121 JVM_ENTRY(jobjectArray, JVM_GetPermittedSubclasses(JNIEnv* env, jclass current)) 2122 { 2123 JVMWrapper("JVM_GetPermittedSubclasses"); 2124 oop mirror = JNIHandles::resolve_non_null(current); 2125 assert(!java_lang_Class::is_primitive(mirror), "should not be"); 2126 Klass* c = java_lang_Class::as_Klass(mirror); 2127 assert(c->is_instance_klass(), "must be"); 2128 InstanceKlass* ik = InstanceKlass::cast(c); 2129 { 2130 JvmtiVMObjectAllocEventCollector oam; 2131 Array<u2>* subclasses = ik->permitted_subclasses(); 2132 int length = subclasses == NULL ? 0 : subclasses->length(); 2133 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), 2134 length, CHECK_NULL); 2135 objArrayHandle result(THREAD, r); 2136 for (int i = 0; i < length; i++) { 2137 int cp_index = subclasses->at(i); 2138 // This returns <package-name>/<class-name>. 2139 Symbol* klass_name = ik->constants()->klass_name_at(cp_index); 2140 assert(klass_name != NULL, "Unexpected null klass_name"); 2141 Handle perm_subtype_h = java_lang_String::create_from_symbol(klass_name, CHECK_NULL); 2142 result->obj_at_put(i, perm_subtype_h()); 2143 } 2144 return (jobjectArray)JNIHandles::make_local(THREAD, result()); 2145 } 2146 } 2147 JVM_END 2148 2149 // Constant pool access ////////////////////////////////////////////////////////// 2150 2151 JVM_ENTRY(jobject, JVM_GetClassConstantPool(JNIEnv *env, jclass cls)) 2152 { 2153 JVMWrapper("JVM_GetClassConstantPool"); 2154 JvmtiVMObjectAllocEventCollector oam; 2155 oop mirror = JNIHandles::resolve_non_null(cls); 2156 // Return null for primitives and arrays 2157 if (!java_lang_Class::is_primitive(mirror)) { 2158 Klass* k = java_lang_Class::as_Klass(mirror); 2159 if (k->is_instance_klass()) { 2160 InstanceKlass* k_h = InstanceKlass::cast(k); 2161 Handle jcp = reflect_ConstantPool::create(CHECK_NULL); 2162 reflect_ConstantPool::set_cp(jcp(), k_h->constants()); 2163 return JNIHandles::make_local(THREAD, jcp()); 2164 } 2165 } 2166 return NULL; 2167 } 2168 JVM_END 2169 2170 2171 JVM_ENTRY(jint, JVM_ConstantPoolGetSize(JNIEnv *env, jobject obj, jobject unused)) 2172 { 2173 JVMWrapper("JVM_ConstantPoolGetSize"); 2174 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2175 return cp->length(); 2176 } 2177 JVM_END 2178 2179 2180 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2181 { 2182 JVMWrapper("JVM_ConstantPoolGetClassAt"); 2183 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2184 bounds_check(cp, index, CHECK_NULL); 2185 constantTag tag = cp->tag_at(index); 2186 if (!tag.is_klass() && !tag.is_unresolved_klass()) { 2187 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2188 } 2189 Klass* k = cp->klass_at(index, CHECK_NULL); 2190 return (jclass) JNIHandles::make_local(THREAD, k->java_mirror()); 2191 } 2192 JVM_END 2193 2194 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index)) 2195 { 2196 JVMWrapper("JVM_ConstantPoolGetClassAtIfLoaded"); 2197 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2198 bounds_check(cp, index, CHECK_NULL); 2199 constantTag tag = cp->tag_at(index); 2200 if (!tag.is_klass() && !tag.is_unresolved_klass()) { 2201 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2202 } 2203 Klass* k = ConstantPool::klass_at_if_loaded(cp, index); 2204 if (k == NULL) return NULL; 2205 return (jclass) JNIHandles::make_local(THREAD, k->java_mirror()); 2206 } 2207 JVM_END 2208 2209 static jobject get_method_at_helper(const constantPoolHandle& cp, jint index, bool force_resolution, TRAPS) { 2210 constantTag tag = cp->tag_at(index); 2211 if (!tag.is_method() && !tag.is_interface_method()) { 2212 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2213 } 2214 int klass_ref = cp->uncached_klass_ref_index_at(index); 2215 Klass* k_o; 2216 if (force_resolution) { 2217 k_o = cp->klass_at(klass_ref, CHECK_NULL); 2218 } else { 2219 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref); 2220 if (k_o == NULL) return NULL; 2221 } 2222 InstanceKlass* k = InstanceKlass::cast(k_o); 2223 Symbol* name = cp->uncached_name_ref_at(index); 2224 Symbol* sig = cp->uncached_signature_ref_at(index); 2225 methodHandle m (THREAD, k->find_method(name, sig)); 2226 if (m.is_null()) { 2227 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class"); 2228 } 2229 oop method; 2230 if (!m->is_initializer() || m->is_static()) { 2231 method = Reflection::new_method(m, true, CHECK_NULL); 2232 } else { 2233 method = Reflection::new_constructor(m, CHECK_NULL); 2234 } 2235 return JNIHandles::make_local(THREAD, method); 2236 } 2237 2238 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2239 { 2240 JVMWrapper("JVM_ConstantPoolGetMethodAt"); 2241 JvmtiVMObjectAllocEventCollector oam; 2242 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2243 bounds_check(cp, index, CHECK_NULL); 2244 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL); 2245 return res; 2246 } 2247 JVM_END 2248 2249 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index)) 2250 { 2251 JVMWrapper("JVM_ConstantPoolGetMethodAtIfLoaded"); 2252 JvmtiVMObjectAllocEventCollector oam; 2253 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2254 bounds_check(cp, index, CHECK_NULL); 2255 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL); 2256 return res; 2257 } 2258 JVM_END 2259 2260 static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) { 2261 constantTag tag = cp->tag_at(index); 2262 if (!tag.is_field()) { 2263 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2264 } 2265 int klass_ref = cp->uncached_klass_ref_index_at(index); 2266 Klass* k_o; 2267 if (force_resolution) { 2268 k_o = cp->klass_at(klass_ref, CHECK_NULL); 2269 } else { 2270 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref); 2271 if (k_o == NULL) return NULL; 2272 } 2273 InstanceKlass* k = InstanceKlass::cast(k_o); 2274 Symbol* name = cp->uncached_name_ref_at(index); 2275 Symbol* sig = cp->uncached_signature_ref_at(index); 2276 fieldDescriptor fd; 2277 Klass* target_klass = k->find_field(name, sig, &fd); 2278 if (target_klass == NULL) { 2279 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class"); 2280 } 2281 oop field = Reflection::new_field(&fd, CHECK_NULL); 2282 return JNIHandles::make_local(THREAD, field); 2283 } 2284 2285 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject obj, jobject unusedl, jint index)) 2286 { 2287 JVMWrapper("JVM_ConstantPoolGetFieldAt"); 2288 JvmtiVMObjectAllocEventCollector oam; 2289 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2290 bounds_check(cp, index, CHECK_NULL); 2291 jobject res = get_field_at_helper(cp, index, true, CHECK_NULL); 2292 return res; 2293 } 2294 JVM_END 2295 2296 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index)) 2297 { 2298 JVMWrapper("JVM_ConstantPoolGetFieldAtIfLoaded"); 2299 JvmtiVMObjectAllocEventCollector oam; 2300 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2301 bounds_check(cp, index, CHECK_NULL); 2302 jobject res = get_field_at_helper(cp, index, false, CHECK_NULL); 2303 return res; 2304 } 2305 JVM_END 2306 2307 JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2308 { 2309 JVMWrapper("JVM_ConstantPoolGetMemberRefInfoAt"); 2310 JvmtiVMObjectAllocEventCollector oam; 2311 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2312 bounds_check(cp, index, CHECK_NULL); 2313 constantTag tag = cp->tag_at(index); 2314 if (!tag.is_field_or_method()) { 2315 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2316 } 2317 int klass_ref = cp->uncached_klass_ref_index_at(index); 2318 Symbol* klass_name = cp->klass_name_at(klass_ref); 2319 Symbol* member_name = cp->uncached_name_ref_at(index); 2320 Symbol* member_sig = cp->uncached_signature_ref_at(index); 2321 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::String_klass(), 3, CHECK_NULL); 2322 objArrayHandle dest(THREAD, dest_o); 2323 Handle str = java_lang_String::create_from_symbol(klass_name, CHECK_NULL); 2324 dest->obj_at_put(0, str()); 2325 str = java_lang_String::create_from_symbol(member_name, CHECK_NULL); 2326 dest->obj_at_put(1, str()); 2327 str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL); 2328 dest->obj_at_put(2, str()); 2329 return (jobjectArray) JNIHandles::make_local(THREAD, dest()); 2330 } 2331 JVM_END 2332 2333 JVM_ENTRY(jint, JVM_ConstantPoolGetClassRefIndexAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2334 { 2335 JVMWrapper("JVM_ConstantPoolGetClassRefIndexAt"); 2336 JvmtiVMObjectAllocEventCollector oam; 2337 constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2338 bounds_check(cp, index, CHECK_0); 2339 constantTag tag = cp->tag_at(index); 2340 if (!tag.is_field_or_method()) { 2341 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2342 } 2343 return (jint) cp->uncached_klass_ref_index_at(index); 2344 } 2345 JVM_END 2346 2347 JVM_ENTRY(jint, JVM_ConstantPoolGetNameAndTypeRefIndexAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2348 { 2349 JVMWrapper("JVM_ConstantPoolGetNameAndTypeRefIndexAt"); 2350 JvmtiVMObjectAllocEventCollector oam; 2351 constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2352 bounds_check(cp, index, CHECK_0); 2353 constantTag tag = cp->tag_at(index); 2354 if (!tag.is_invoke_dynamic() && !tag.is_field_or_method()) { 2355 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2356 } 2357 return (jint) cp->uncached_name_and_type_ref_index_at(index); 2358 } 2359 JVM_END 2360 2361 JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetNameAndTypeRefInfoAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2362 { 2363 JVMWrapper("JVM_ConstantPoolGetNameAndTypeRefInfoAt"); 2364 JvmtiVMObjectAllocEventCollector oam; 2365 constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2366 bounds_check(cp, index, CHECK_NULL); 2367 constantTag tag = cp->tag_at(index); 2368 if (!tag.is_name_and_type()) { 2369 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2370 } 2371 Symbol* member_name = cp->symbol_at(cp->name_ref_index_at(index)); 2372 Symbol* member_sig = cp->symbol_at(cp->signature_ref_index_at(index)); 2373 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::String_klass(), 2, CHECK_NULL); 2374 objArrayHandle dest(THREAD, dest_o); 2375 Handle str = java_lang_String::create_from_symbol(member_name, CHECK_NULL); 2376 dest->obj_at_put(0, str()); 2377 str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL); 2378 dest->obj_at_put(1, str()); 2379 return (jobjectArray) JNIHandles::make_local(THREAD, dest()); 2380 } 2381 JVM_END 2382 2383 JVM_ENTRY(jint, JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2384 { 2385 JVMWrapper("JVM_ConstantPoolGetIntAt"); 2386 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2387 bounds_check(cp, index, CHECK_0); 2388 constantTag tag = cp->tag_at(index); 2389 if (!tag.is_int()) { 2390 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2391 } 2392 return cp->int_at(index); 2393 } 2394 JVM_END 2395 2396 JVM_ENTRY(jlong, JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2397 { 2398 JVMWrapper("JVM_ConstantPoolGetLongAt"); 2399 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2400 bounds_check(cp, index, CHECK_(0L)); 2401 constantTag tag = cp->tag_at(index); 2402 if (!tag.is_long()) { 2403 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2404 } 2405 return cp->long_at(index); 2406 } 2407 JVM_END 2408 2409 JVM_ENTRY(jfloat, JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2410 { 2411 JVMWrapper("JVM_ConstantPoolGetFloatAt"); 2412 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2413 bounds_check(cp, index, CHECK_(0.0f)); 2414 constantTag tag = cp->tag_at(index); 2415 if (!tag.is_float()) { 2416 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2417 } 2418 return cp->float_at(index); 2419 } 2420 JVM_END 2421 2422 JVM_ENTRY(jdouble, JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2423 { 2424 JVMWrapper("JVM_ConstantPoolGetDoubleAt"); 2425 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2426 bounds_check(cp, index, CHECK_(0.0)); 2427 constantTag tag = cp->tag_at(index); 2428 if (!tag.is_double()) { 2429 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2430 } 2431 return cp->double_at(index); 2432 } 2433 JVM_END 2434 2435 JVM_ENTRY(jstring, JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2436 { 2437 JVMWrapper("JVM_ConstantPoolGetStringAt"); 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_string()) { 2442 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2443 } 2444 oop str = cp->string_at(index, CHECK_NULL); 2445 return (jstring) JNIHandles::make_local(THREAD, str); 2446 } 2447 JVM_END 2448 2449 JVM_ENTRY(jstring, JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject obj, jobject unused, jint index)) 2450 { 2451 JVMWrapper("JVM_ConstantPoolGetUTF8At"); 2452 JvmtiVMObjectAllocEventCollector oam; 2453 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2454 bounds_check(cp, index, CHECK_NULL); 2455 constantTag tag = cp->tag_at(index); 2456 if (!tag.is_symbol()) { 2457 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2458 } 2459 Symbol* sym = cp->symbol_at(index); 2460 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL); 2461 return (jstring) JNIHandles::make_local(THREAD, str()); 2462 } 2463 JVM_END 2464 2465 JVM_ENTRY(jbyte, JVM_ConstantPoolGetTagAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2466 { 2467 JVMWrapper("JVM_ConstantPoolGetTagAt"); 2468 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2469 bounds_check(cp, index, CHECK_0); 2470 constantTag tag = cp->tag_at(index); 2471 jbyte result = tag.value(); 2472 // If returned tag values are not from the JVM spec, e.g. tags from 100 to 105, 2473 // they are changed to the corresponding tags from the JVM spec, so that java code in 2474 // sun.reflect.ConstantPool will return only tags from the JVM spec, not internal ones. 2475 if (tag.is_klass_or_reference()) { 2476 result = JVM_CONSTANT_Class; 2477 } else if (tag.is_string_index()) { 2478 result = JVM_CONSTANT_String; 2479 } else if (tag.is_method_type_in_error()) { 2480 result = JVM_CONSTANT_MethodType; 2481 } else if (tag.is_method_handle_in_error()) { 2482 result = JVM_CONSTANT_MethodHandle; 2483 } else if (tag.is_dynamic_constant_in_error()) { 2484 result = JVM_CONSTANT_Dynamic; 2485 } 2486 return result; 2487 } 2488 JVM_END 2489 2490 // Assertion support. ////////////////////////////////////////////////////////// 2491 2492 JVM_ENTRY(jboolean, JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls)) 2493 JVMWrapper("JVM_DesiredAssertionStatus"); 2494 assert(cls != NULL, "bad class"); 2495 2496 oop r = JNIHandles::resolve(cls); 2497 assert(! java_lang_Class::is_primitive(r), "primitive classes not allowed"); 2498 if (java_lang_Class::is_primitive(r)) return false; 2499 2500 Klass* k = java_lang_Class::as_Klass(r); 2501 assert(k->is_instance_klass(), "must be an instance klass"); 2502 if (!k->is_instance_klass()) return false; 2503 2504 ResourceMark rm(THREAD); 2505 const char* name = k->name()->as_C_string(); 2506 bool system_class = k->class_loader() == NULL; 2507 return JavaAssertions::enabled(name, system_class); 2508 2509 JVM_END 2510 2511 2512 // Return a new AssertionStatusDirectives object with the fields filled in with 2513 // command-line assertion arguments (i.e., -ea, -da). 2514 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused)) 2515 JVMWrapper("JVM_AssertionStatusDirectives"); 2516 JvmtiVMObjectAllocEventCollector oam; 2517 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL); 2518 return JNIHandles::make_local(THREAD, asd); 2519 JVM_END 2520 2521 // Verification //////////////////////////////////////////////////////////////////////////////// 2522 2523 // Reflection for the verifier ///////////////////////////////////////////////////////////////// 2524 2525 // RedefineClasses support: bug 6214132 caused verification to fail. 2526 // All functions from this section should call the jvmtiThreadSate function: 2527 // Klass* class_to_verify_considering_redefinition(Klass* klass). 2528 // The function returns a Klass* of the _scratch_class if the verifier 2529 // was invoked in the middle of the class redefinition. 2530 // Otherwise it returns its argument value which is the _the_class Klass*. 2531 // Please, refer to the description in the jvmtiThreadSate.hpp. 2532 2533 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls)) 2534 JVMWrapper("JVM_GetClassNameUTF"); 2535 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2536 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2537 return k->name()->as_utf8(); 2538 JVM_END 2539 2540 2541 JVM_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types)) 2542 JVMWrapper("JVM_GetClassCPTypes"); 2543 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2544 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2545 // types will have length zero if this is not an InstanceKlass 2546 // (length is determined by call to JVM_GetClassCPEntriesCount) 2547 if (k->is_instance_klass()) { 2548 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2549 for (int index = cp->length() - 1; index >= 0; index--) { 2550 constantTag tag = cp->tag_at(index); 2551 types[index] = (tag.is_unresolved_klass()) ? (unsigned char) JVM_CONSTANT_Class : tag.value(); 2552 } 2553 } 2554 JVM_END 2555 2556 2557 JVM_ENTRY(jint, JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls)) 2558 JVMWrapper("JVM_GetClassCPEntriesCount"); 2559 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2560 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2561 return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->constants()->length(); 2562 JVM_END 2563 2564 2565 JVM_ENTRY(jint, JVM_GetClassFieldsCount(JNIEnv *env, jclass cls)) 2566 JVMWrapper("JVM_GetClassFieldsCount"); 2567 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2568 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2569 return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->java_fields_count(); 2570 JVM_END 2571 2572 2573 JVM_ENTRY(jint, JVM_GetClassMethodsCount(JNIEnv *env, jclass cls)) 2574 JVMWrapper("JVM_GetClassMethodsCount"); 2575 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2576 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2577 return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->methods()->length(); 2578 JVM_END 2579 2580 2581 // The following methods, used for the verifier, are never called with 2582 // array klasses, so a direct cast to InstanceKlass is safe. 2583 // Typically, these methods are called in a loop with bounds determined 2584 // by the results of JVM_GetClass{Fields,Methods}Count, which return 2585 // zero for arrays. 2586 JVM_ENTRY(void, JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions)) 2587 JVMWrapper("JVM_GetMethodIxExceptionIndexes"); 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 int length = method->checked_exceptions_length(); 2592 if (length > 0) { 2593 CheckedExceptionElement* table= method->checked_exceptions_start(); 2594 for (int i = 0; i < length; i++) { 2595 exceptions[i] = table[i].class_cp_index; 2596 } 2597 } 2598 JVM_END 2599 2600 2601 JVM_ENTRY(jint, JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index)) 2602 JVMWrapper("JVM_GetMethodIxExceptionsCount"); 2603 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2604 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2605 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2606 return method->checked_exceptions_length(); 2607 JVM_END 2608 2609 2610 JVM_ENTRY(void, JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code)) 2611 JVMWrapper("JVM_GetMethodIxByteCode"); 2612 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2613 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2614 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2615 memcpy(code, method->code_base(), method->code_size()); 2616 JVM_END 2617 2618 2619 JVM_ENTRY(jint, JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index)) 2620 JVMWrapper("JVM_GetMethodIxByteCodeLength"); 2621 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2622 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2623 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2624 return method->code_size(); 2625 JVM_END 2626 2627 2628 JVM_ENTRY(void, JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry)) 2629 JVMWrapper("JVM_GetMethodIxExceptionTableEntry"); 2630 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2631 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2632 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2633 ExceptionTable extable(method); 2634 entry->start_pc = extable.start_pc(entry_index); 2635 entry->end_pc = extable.end_pc(entry_index); 2636 entry->handler_pc = extable.handler_pc(entry_index); 2637 entry->catchType = extable.catch_type_index(entry_index); 2638 JVM_END 2639 2640 2641 JVM_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index)) 2642 JVMWrapper("JVM_GetMethodIxExceptionTableLength"); 2643 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2644 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2645 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2646 return method->exception_table_length(); 2647 JVM_END 2648 2649 2650 JVM_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index)) 2651 JVMWrapper("JVM_GetMethodIxModifiers"); 2652 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2653 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2654 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2655 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS; 2656 JVM_END 2657 2658 2659 JVM_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index)) 2660 JVMWrapper("JVM_GetFieldIxModifiers"); 2661 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2662 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2663 return InstanceKlass::cast(k)->field_access_flags(field_index) & JVM_RECOGNIZED_FIELD_MODIFIERS; 2664 JVM_END 2665 2666 2667 JVM_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index)) 2668 JVMWrapper("JVM_GetMethodIxLocalsCount"); 2669 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2670 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2671 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2672 return method->max_locals(); 2673 JVM_END 2674 2675 2676 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index)) 2677 JVMWrapper("JVM_GetMethodIxArgsSize"); 2678 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2679 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2680 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2681 return method->size_of_parameters(); 2682 JVM_END 2683 2684 2685 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index)) 2686 JVMWrapper("JVM_GetMethodIxMaxStack"); 2687 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2688 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2689 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2690 return method->verifier_max_stack(); 2691 JVM_END 2692 2693 2694 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index)) 2695 JVMWrapper("JVM_IsConstructorIx"); 2696 ResourceMark rm(THREAD); 2697 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2698 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2699 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2700 return method->name() == vmSymbols::object_initializer_name(); 2701 JVM_END 2702 2703 2704 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index)) 2705 JVMWrapper("JVM_IsVMGeneratedMethodIx"); 2706 ResourceMark rm(THREAD); 2707 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2708 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2709 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2710 return method->is_overpass(); 2711 JVM_END 2712 2713 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index)) 2714 JVMWrapper("JVM_GetMethodIxIxUTF"); 2715 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2716 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2717 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2718 return method->name()->as_utf8(); 2719 JVM_END 2720 2721 2722 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index)) 2723 JVMWrapper("JVM_GetMethodIxSignatureUTF"); 2724 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2725 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2726 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2727 return method->signature()->as_utf8(); 2728 JVM_END 2729 2730 /** 2731 * All of these JVM_GetCP-xxx methods are used by the old verifier to 2732 * read entries in the constant pool. Since the old verifier always 2733 * works on a copy of the code, it will not see any rewriting that 2734 * may possibly occur in the middle of verification. So it is important 2735 * that nothing it calls tries to use the cpCache instead of the raw 2736 * constant pool, so we must use cp->uncached_x methods when appropriate. 2737 */ 2738 JVM_ENTRY(const char*, JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index)) 2739 JVMWrapper("JVM_GetCPFieldNameUTF"); 2740 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2741 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2742 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2743 switch (cp->tag_at(cp_index).value()) { 2744 case JVM_CONSTANT_Fieldref: 2745 return cp->uncached_name_ref_at(cp_index)->as_utf8(); 2746 default: 2747 fatal("JVM_GetCPFieldNameUTF: illegal constant"); 2748 } 2749 ShouldNotReachHere(); 2750 return NULL; 2751 JVM_END 2752 2753 2754 JVM_ENTRY(const char*, JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index)) 2755 JVMWrapper("JVM_GetCPMethodNameUTF"); 2756 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2757 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2758 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2759 switch (cp->tag_at(cp_index).value()) { 2760 case JVM_CONSTANT_InterfaceMethodref: 2761 case JVM_CONSTANT_Methodref: 2762 return cp->uncached_name_ref_at(cp_index)->as_utf8(); 2763 default: 2764 fatal("JVM_GetCPMethodNameUTF: illegal constant"); 2765 } 2766 ShouldNotReachHere(); 2767 return NULL; 2768 JVM_END 2769 2770 2771 JVM_ENTRY(const char*, JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)) 2772 JVMWrapper("JVM_GetCPMethodSignatureUTF"); 2773 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2774 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2775 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2776 switch (cp->tag_at(cp_index).value()) { 2777 case JVM_CONSTANT_InterfaceMethodref: 2778 case JVM_CONSTANT_Methodref: 2779 return cp->uncached_signature_ref_at(cp_index)->as_utf8(); 2780 default: 2781 fatal("JVM_GetCPMethodSignatureUTF: illegal constant"); 2782 } 2783 ShouldNotReachHere(); 2784 return NULL; 2785 JVM_END 2786 2787 2788 JVM_ENTRY(const char*, JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)) 2789 JVMWrapper("JVM_GetCPFieldSignatureUTF"); 2790 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2791 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2792 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2793 switch (cp->tag_at(cp_index).value()) { 2794 case JVM_CONSTANT_Fieldref: 2795 return cp->uncached_signature_ref_at(cp_index)->as_utf8(); 2796 default: 2797 fatal("JVM_GetCPFieldSignatureUTF: illegal constant"); 2798 } 2799 ShouldNotReachHere(); 2800 return NULL; 2801 JVM_END 2802 2803 2804 JVM_ENTRY(const char*, JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)) 2805 JVMWrapper("JVM_GetCPClassNameUTF"); 2806 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2807 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2808 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2809 Symbol* classname = cp->klass_name_at(cp_index); 2810 return classname->as_utf8(); 2811 JVM_END 2812 2813 2814 JVM_ENTRY(const char*, JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)) 2815 JVMWrapper("JVM_GetCPFieldClassNameUTF"); 2816 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2817 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2818 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2819 switch (cp->tag_at(cp_index).value()) { 2820 case JVM_CONSTANT_Fieldref: { 2821 int class_index = cp->uncached_klass_ref_index_at(cp_index); 2822 Symbol* classname = cp->klass_name_at(class_index); 2823 return classname->as_utf8(); 2824 } 2825 default: 2826 fatal("JVM_GetCPFieldClassNameUTF: illegal constant"); 2827 } 2828 ShouldNotReachHere(); 2829 return NULL; 2830 JVM_END 2831 2832 2833 JVM_ENTRY(const char*, JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)) 2834 JVMWrapper("JVM_GetCPMethodClassNameUTF"); 2835 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2836 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2837 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2838 switch (cp->tag_at(cp_index).value()) { 2839 case JVM_CONSTANT_Methodref: 2840 case JVM_CONSTANT_InterfaceMethodref: { 2841 int class_index = cp->uncached_klass_ref_index_at(cp_index); 2842 Symbol* classname = cp->klass_name_at(class_index); 2843 return classname->as_utf8(); 2844 } 2845 default: 2846 fatal("JVM_GetCPMethodClassNameUTF: illegal constant"); 2847 } 2848 ShouldNotReachHere(); 2849 return NULL; 2850 JVM_END 2851 2852 2853 JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)) 2854 JVMWrapper("JVM_GetCPFieldModifiers"); 2855 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2856 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls)); 2857 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2858 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread); 2859 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2860 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants(); 2861 switch (cp->tag_at(cp_index).value()) { 2862 case JVM_CONSTANT_Fieldref: { 2863 Symbol* name = cp->uncached_name_ref_at(cp_index); 2864 Symbol* signature = cp->uncached_signature_ref_at(cp_index); 2865 InstanceKlass* ik = InstanceKlass::cast(k_called); 2866 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) { 2867 if (fs.name() == name && fs.signature() == signature) { 2868 return fs.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS; 2869 } 2870 } 2871 return -1; 2872 } 2873 default: 2874 fatal("JVM_GetCPFieldModifiers: illegal constant"); 2875 } 2876 ShouldNotReachHere(); 2877 return 0; 2878 JVM_END 2879 2880 2881 JVM_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)) 2882 JVMWrapper("JVM_GetCPMethodModifiers"); 2883 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2884 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls)); 2885 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2886 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread); 2887 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2888 switch (cp->tag_at(cp_index).value()) { 2889 case JVM_CONSTANT_Methodref: 2890 case JVM_CONSTANT_InterfaceMethodref: { 2891 Symbol* name = cp->uncached_name_ref_at(cp_index); 2892 Symbol* signature = cp->uncached_signature_ref_at(cp_index); 2893 Array<Method*>* methods = InstanceKlass::cast(k_called)->methods(); 2894 int methods_count = methods->length(); 2895 for (int i = 0; i < methods_count; i++) { 2896 Method* method = methods->at(i); 2897 if (method->name() == name && method->signature() == signature) { 2898 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS; 2899 } 2900 } 2901 return -1; 2902 } 2903 default: 2904 fatal("JVM_GetCPMethodModifiers: illegal constant"); 2905 } 2906 ShouldNotReachHere(); 2907 return 0; 2908 JVM_END 2909 2910 2911 // Misc ////////////////////////////////////////////////////////////////////////////////////////////// 2912 2913 JVM_LEAF(void, JVM_ReleaseUTF(const char *utf)) 2914 // So long as UTF8::convert_to_utf8 returns resource strings, we don't have to do anything 2915 JVM_END 2916 2917 2918 JVM_ENTRY(jboolean, JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2)) 2919 JVMWrapper("JVM_IsSameClassPackage"); 2920 oop class1_mirror = JNIHandles::resolve_non_null(class1); 2921 oop class2_mirror = JNIHandles::resolve_non_null(class2); 2922 Klass* klass1 = java_lang_Class::as_Klass(class1_mirror); 2923 Klass* klass2 = java_lang_Class::as_Klass(class2_mirror); 2924 return (jboolean) Reflection::is_same_class_package(klass1, klass2); 2925 JVM_END 2926 2927 // Printing support ////////////////////////////////////////////////// 2928 extern "C" { 2929 2930 ATTRIBUTE_PRINTF(3, 0) 2931 int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) { 2932 // Reject count values that are negative signed values converted to 2933 // unsigned; see bug 4399518, 4417214 2934 if ((intptr_t)count <= 0) return -1; 2935 2936 int result = os::vsnprintf(str, count, fmt, args); 2937 if (result > 0 && (size_t)result >= count) { 2938 result = -1; 2939 } 2940 2941 return result; 2942 } 2943 2944 ATTRIBUTE_PRINTF(3, 4) 2945 int jio_snprintf(char *str, size_t count, const char *fmt, ...) { 2946 va_list args; 2947 int len; 2948 va_start(args, fmt); 2949 len = jio_vsnprintf(str, count, fmt, args); 2950 va_end(args); 2951 return len; 2952 } 2953 2954 ATTRIBUTE_PRINTF(2, 3) 2955 int jio_fprintf(FILE* f, const char *fmt, ...) { 2956 int len; 2957 va_list args; 2958 va_start(args, fmt); 2959 len = jio_vfprintf(f, fmt, args); 2960 va_end(args); 2961 return len; 2962 } 2963 2964 ATTRIBUTE_PRINTF(2, 0) 2965 int jio_vfprintf(FILE* f, const char *fmt, va_list args) { 2966 if (Arguments::vfprintf_hook() != NULL) { 2967 return Arguments::vfprintf_hook()(f, fmt, args); 2968 } else { 2969 return vfprintf(f, fmt, args); 2970 } 2971 } 2972 2973 ATTRIBUTE_PRINTF(1, 2) 2974 JNIEXPORT int jio_printf(const char *fmt, ...) { 2975 int len; 2976 va_list args; 2977 va_start(args, fmt); 2978 len = jio_vfprintf(defaultStream::output_stream(), fmt, args); 2979 va_end(args); 2980 return len; 2981 } 2982 2983 // HotSpot specific jio method 2984 void jio_print(const char* s, size_t len) { 2985 // Try to make this function as atomic as possible. 2986 if (Arguments::vfprintf_hook() != NULL) { 2987 jio_fprintf(defaultStream::output_stream(), "%.*s", (int)len, s); 2988 } else { 2989 // Make an unused local variable to avoid warning from gcc compiler. 2990 size_t count = ::write(defaultStream::output_fd(), s, (int)len); 2991 } 2992 } 2993 2994 } // Extern C 2995 2996 // java.lang.Thread ////////////////////////////////////////////////////////////////////////////// 2997 2998 // In most of the JVM thread support functions we need to access the 2999 // thread through a ThreadsListHandle to prevent it from exiting and 3000 // being reclaimed while we try to operate on it. The exceptions to this 3001 // rule are when operating on the current thread, or if the monitor of 3002 // the target java.lang.Thread is locked at the Java level - in both 3003 // cases the target cannot exit. 3004 3005 static void thread_entry(JavaThread* thread, TRAPS) { 3006 HandleMark hm(THREAD); 3007 Handle obj(THREAD, thread->threadObj()); 3008 JavaValue result(T_VOID); 3009 JavaCalls::call_virtual(&result, 3010 obj, 3011 SystemDictionary::Thread_klass(), 3012 vmSymbols::run_method_name(), 3013 vmSymbols::void_method_signature(), 3014 THREAD); 3015 } 3016 3017 3018 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread)) 3019 JVMWrapper("JVM_StartThread"); 3020 JavaThread *native_thread = NULL; 3021 3022 // We cannot hold the Threads_lock when we throw an exception, 3023 // due to rank ordering issues. Example: we might need to grab the 3024 // Heap_lock while we construct the exception. 3025 bool throw_illegal_thread_state = false; 3026 3027 // We must release the Threads_lock before we can post a jvmti event 3028 // in Thread::start. 3029 { 3030 // Ensure that the C++ Thread and OSThread structures aren't freed before 3031 // we operate. 3032 MutexLocker mu(Threads_lock); 3033 3034 // Since JDK 5 the java.lang.Thread threadStatus is used to prevent 3035 // re-starting an already started thread, so we should usually find 3036 // that the JavaThread is null. However for a JNI attached thread 3037 // there is a small window between the Thread object being created 3038 // (with its JavaThread set) and the update to its threadStatus, so we 3039 // have to check for this 3040 if (java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) { 3041 throw_illegal_thread_state = true; 3042 } else { 3043 // We could also check the stillborn flag to see if this thread was already stopped, but 3044 // for historical reasons we let the thread detect that itself when it starts running 3045 3046 jlong size = 3047 java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread)); 3048 // Allocate the C++ Thread structure and create the native thread. The 3049 // stack size retrieved from java is 64-bit signed, but the constructor takes 3050 // size_t (an unsigned type), which may be 32 or 64-bit depending on the platform. 3051 // - Avoid truncating on 32-bit platforms if size is greater than UINT_MAX. 3052 // - Avoid passing negative values which would result in really large stacks. 3053 NOT_LP64(if (size > SIZE_MAX) size = SIZE_MAX;) 3054 size_t sz = size > 0 ? (size_t) size : 0; 3055 native_thread = new JavaThread(&thread_entry, sz); 3056 3057 // At this point it may be possible that no osthread was created for the 3058 // JavaThread due to lack of memory. Check for this situation and throw 3059 // an exception if necessary. Eventually we may want to change this so 3060 // that we only grab the lock if the thread was created successfully - 3061 // then we can also do this check and throw the exception in the 3062 // JavaThread constructor. 3063 if (native_thread->osthread() != NULL) { 3064 // Note: the current thread is not being used within "prepare". 3065 native_thread->prepare(jthread); 3066 } 3067 } 3068 } 3069 3070 if (throw_illegal_thread_state) { 3071 THROW(vmSymbols::java_lang_IllegalThreadStateException()); 3072 } 3073 3074 assert(native_thread != NULL, "Starting null thread?"); 3075 3076 if (native_thread->osthread() == NULL) { 3077 // No one should hold a reference to the 'native_thread'. 3078 native_thread->smr_delete(); 3079 if (JvmtiExport::should_post_resource_exhausted()) { 3080 JvmtiExport::post_resource_exhausted( 3081 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS, 3082 os::native_thread_creation_failed_msg()); 3083 } 3084 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), 3085 os::native_thread_creation_failed_msg()); 3086 } 3087 3088 #if INCLUDE_JFR 3089 if (Jfr::is_recording() && EventThreadStart::is_enabled() && 3090 EventThreadStart::is_stacktrace_enabled()) { 3091 JfrThreadLocal* tl = native_thread->jfr_thread_local(); 3092 // skip Thread.start() and Thread.start0() 3093 tl->set_cached_stack_trace_id(JfrStackTraceRepository::record(thread, 2)); 3094 } 3095 #endif 3096 3097 Thread::start(native_thread); 3098 3099 JVM_END 3100 3101 3102 // JVM_Stop is implemented using a VM_Operation, so threads are forced to safepoints 3103 // before the quasi-asynchronous exception is delivered. This is a little obtrusive, 3104 // but is thought to be reliable and simple. In the case, where the receiver is the 3105 // same thread as the sender, no VM_Operation is needed. 3106 JVM_ENTRY(void, JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable)) 3107 JVMWrapper("JVM_StopThread"); 3108 3109 // A nested ThreadsListHandle will grab the Threads_lock so create 3110 // tlh before we resolve throwable. 3111 ThreadsListHandle tlh(thread); 3112 oop java_throwable = JNIHandles::resolve(throwable); 3113 if (java_throwable == NULL) { 3114 THROW(vmSymbols::java_lang_NullPointerException()); 3115 } 3116 oop java_thread = NULL; 3117 JavaThread* receiver = NULL; 3118 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, &java_thread); 3119 Events::log_exception(thread, 3120 "JVM_StopThread thread JavaThread " INTPTR_FORMAT " as oop " INTPTR_FORMAT " [exception " INTPTR_FORMAT "]", 3121 p2i(receiver), p2i(java_thread), p2i(throwable)); 3122 3123 if (is_alive) { 3124 // jthread refers to a live JavaThread. 3125 if (thread == receiver) { 3126 // Exception is getting thrown at self so no VM_Operation needed. 3127 THROW_OOP(java_throwable); 3128 } else { 3129 // Use a VM_Operation to throw the exception. 3130 Thread::send_async_exception(java_thread, java_throwable); 3131 } 3132 } else { 3133 // Either: 3134 // - target thread has not been started before being stopped, or 3135 // - target thread already terminated 3136 // We could read the threadStatus to determine which case it is 3137 // but that is overkill as it doesn't matter. We must set the 3138 // stillborn flag for the first case, and if the thread has already 3139 // exited setting this flag has no effect. 3140 java_lang_Thread::set_stillborn(java_thread); 3141 } 3142 JVM_END 3143 3144 3145 JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread)) 3146 JVMWrapper("JVM_IsThreadAlive"); 3147 3148 oop thread_oop = JNIHandles::resolve_non_null(jthread); 3149 return java_lang_Thread::is_alive(thread_oop); 3150 JVM_END 3151 3152 3153 JVM_ENTRY(void, JVM_SuspendThread(JNIEnv* env, jobject jthread)) 3154 JVMWrapper("JVM_SuspendThread"); 3155 3156 ThreadsListHandle tlh(thread); 3157 JavaThread* receiver = NULL; 3158 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL); 3159 if (is_alive) { 3160 // jthread refers to a live JavaThread. 3161 { 3162 MutexLocker ml(receiver->SR_lock(), Mutex::_no_safepoint_check_flag); 3163 if (receiver->is_external_suspend()) { 3164 // Don't allow nested external suspend requests. We can't return 3165 // an error from this interface so just ignore the problem. 3166 return; 3167 } 3168 if (receiver->is_exiting()) { // thread is in the process of exiting 3169 return; 3170 } 3171 receiver->set_external_suspend(); 3172 } 3173 3174 // java_suspend() will catch threads in the process of exiting 3175 // and will ignore them. 3176 receiver->java_suspend(); 3177 3178 // It would be nice to have the following assertion in all the 3179 // time, but it is possible for a racing resume request to have 3180 // resumed this thread right after we suspended it. Temporarily 3181 // enable this assertion if you are chasing a different kind of 3182 // bug. 3183 // 3184 // assert(java_lang_Thread::thread(receiver->threadObj()) == NULL || 3185 // receiver->is_being_ext_suspended(), "thread is not suspended"); 3186 } 3187 JVM_END 3188 3189 3190 JVM_ENTRY(void, JVM_ResumeThread(JNIEnv* env, jobject jthread)) 3191 JVMWrapper("JVM_ResumeThread"); 3192 3193 ThreadsListHandle tlh(thread); 3194 JavaThread* receiver = NULL; 3195 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL); 3196 if (is_alive) { 3197 // jthread refers to a live JavaThread. 3198 3199 // This is the original comment for this Threads_lock grab: 3200 // We need to *always* get the threads lock here, since this operation cannot be allowed during 3201 // a safepoint. The safepoint code relies on suspending a thread to examine its state. If other 3202 // threads randomly resumes threads, then a thread might not be suspended when the safepoint code 3203 // looks at it. 3204 // 3205 // The above comment dates back to when we had both internal and 3206 // external suspend APIs that shared a common underlying mechanism. 3207 // External suspend is now entirely cooperative and doesn't share 3208 // anything with internal suspend. That said, there are some 3209 // assumptions in the VM that an external resume grabs the 3210 // Threads_lock. We can't drop the Threads_lock grab here until we 3211 // resolve the assumptions that exist elsewhere. 3212 // 3213 MutexLocker ml(Threads_lock); 3214 receiver->java_resume(); 3215 } 3216 JVM_END 3217 3218 3219 JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio)) 3220 JVMWrapper("JVM_SetThreadPriority"); 3221 3222 ThreadsListHandle tlh(thread); 3223 oop java_thread = NULL; 3224 JavaThread* receiver = NULL; 3225 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, &java_thread); 3226 java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio); 3227 3228 if (is_alive) { 3229 // jthread refers to a live JavaThread. 3230 Thread::set_priority(receiver, (ThreadPriority)prio); 3231 } 3232 // Implied else: If the JavaThread hasn't started yet, then the 3233 // priority set in the java.lang.Thread object above will be pushed 3234 // down when it does start. 3235 JVM_END 3236 3237 3238 JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass)) 3239 JVMWrapper("JVM_Yield"); 3240 if (os::dont_yield()) return; 3241 HOTSPOT_THREAD_YIELD(); 3242 os::naked_yield(); 3243 JVM_END 3244 3245 static void post_thread_sleep_event(EventThreadSleep* event, jlong millis) { 3246 assert(event != NULL, "invariant"); 3247 assert(event->should_commit(), "invariant"); 3248 event->set_time(millis); 3249 event->commit(); 3250 } 3251 3252 JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis)) 3253 JVMWrapper("JVM_Sleep"); 3254 3255 if (millis < 0) { 3256 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative"); 3257 } 3258 3259 if (thread->is_interrupted(true) && !HAS_PENDING_EXCEPTION) { 3260 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted"); 3261 } 3262 3263 // Save current thread state and restore it at the end of this block. 3264 // And set new thread state to SLEEPING. 3265 JavaThreadSleepState jtss(thread); 3266 3267 HOTSPOT_THREAD_SLEEP_BEGIN(millis); 3268 EventThreadSleep event; 3269 3270 if (millis == 0) { 3271 os::naked_yield(); 3272 } else { 3273 ThreadState old_state = thread->osthread()->get_state(); 3274 thread->osthread()->set_state(SLEEPING); 3275 if (!thread->sleep(millis)) { // interrupted 3276 // An asynchronous exception (e.g., ThreadDeathException) could have been thrown on 3277 // us while we were sleeping. We do not overwrite those. 3278 if (!HAS_PENDING_EXCEPTION) { 3279 if (event.should_commit()) { 3280 post_thread_sleep_event(&event, millis); 3281 } 3282 HOTSPOT_THREAD_SLEEP_END(1); 3283 3284 // TODO-FIXME: THROW_MSG returns which means we will not call set_state() 3285 // to properly restore the thread state. That's likely wrong. 3286 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted"); 3287 } 3288 } 3289 thread->osthread()->set_state(old_state); 3290 } 3291 if (event.should_commit()) { 3292 post_thread_sleep_event(&event, millis); 3293 } 3294 HOTSPOT_THREAD_SLEEP_END(0); 3295 JVM_END 3296 3297 JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass)) 3298 JVMWrapper("JVM_CurrentThread"); 3299 oop jthread = thread->threadObj(); 3300 assert(jthread != NULL, "no current thread!"); 3301 return JNIHandles::make_local(THREAD, jthread); 3302 JVM_END 3303 3304 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread)) 3305 JVMWrapper("JVM_Interrupt"); 3306 3307 ThreadsListHandle tlh(thread); 3308 JavaThread* receiver = NULL; 3309 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, NULL); 3310 if (is_alive) { 3311 // jthread refers to a live JavaThread. 3312 receiver->interrupt(); 3313 } 3314 JVM_END 3315 3316 3317 // Return true iff the current thread has locked the object passed in 3318 3319 JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj)) 3320 JVMWrapper("JVM_HoldsLock"); 3321 assert(THREAD->is_Java_thread(), "sanity check"); 3322 if (obj == NULL) { 3323 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE); 3324 } 3325 Handle h_obj(THREAD, JNIHandles::resolve(obj)); 3326 return ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, h_obj); 3327 JVM_END 3328 3329 3330 JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass)) 3331 JVMWrapper("JVM_DumpAllStacks"); 3332 VM_PrintThreads op; 3333 VMThread::execute(&op); 3334 if (JvmtiExport::should_post_data_dump()) { 3335 JvmtiExport::post_data_dump(); 3336 } 3337 JVM_END 3338 3339 JVM_ENTRY(void, JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring name)) 3340 JVMWrapper("JVM_SetNativeThreadName"); 3341 3342 // We don't use a ThreadsListHandle here because the current thread 3343 // must be alive. 3344 oop java_thread = JNIHandles::resolve_non_null(jthread); 3345 JavaThread* thr = java_lang_Thread::thread(java_thread); 3346 if (thread == thr && !thr->has_attached_via_jni()) { 3347 // Thread naming is only supported for the current thread and 3348 // we don't set the name of an attached thread to avoid stepping 3349 // on other programs. 3350 ResourceMark rm(thread); 3351 const char *thread_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name)); 3352 os::set_native_thread_name(thread_name); 3353 } 3354 JVM_END 3355 3356 // java.lang.SecurityManager /////////////////////////////////////////////////////////////////////// 3357 3358 JVM_ENTRY(jobjectArray, JVM_GetClassContext(JNIEnv *env)) 3359 JVMWrapper("JVM_GetClassContext"); 3360 ResourceMark rm(THREAD); 3361 JvmtiVMObjectAllocEventCollector oam; 3362 vframeStream vfst(thread); 3363 3364 if (SystemDictionary::reflect_CallerSensitive_klass() != NULL) { 3365 // This must only be called from SecurityManager.getClassContext 3366 Method* m = vfst.method(); 3367 if (!(m->method_holder() == SystemDictionary::SecurityManager_klass() && 3368 m->name() == vmSymbols::getClassContext_name() && 3369 m->signature() == vmSymbols::void_class_array_signature())) { 3370 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetClassContext must only be called from SecurityManager.getClassContext"); 3371 } 3372 } 3373 3374 // Collect method holders 3375 GrowableArray<Klass*>* klass_array = new GrowableArray<Klass*>(); 3376 for (; !vfst.at_end(); vfst.security_next()) { 3377 Method* m = vfst.method(); 3378 // Native frames are not returned 3379 if (!m->is_ignored_by_security_stack_walk() && !m->is_native()) { 3380 Klass* holder = m->method_holder(); 3381 assert(holder->is_klass(), "just checking"); 3382 klass_array->append(holder); 3383 } 3384 } 3385 3386 // Create result array of type [Ljava/lang/Class; 3387 objArrayOop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), klass_array->length(), CHECK_NULL); 3388 // Fill in mirrors corresponding to method holders 3389 for (int i = 0; i < klass_array->length(); i++) { 3390 result->obj_at_put(i, klass_array->at(i)->java_mirror()); 3391 } 3392 3393 return (jobjectArray) JNIHandles::make_local(THREAD, result); 3394 JVM_END 3395 3396 3397 // java.lang.Package //////////////////////////////////////////////////////////////// 3398 3399 3400 JVM_ENTRY(jstring, JVM_GetSystemPackage(JNIEnv *env, jstring name)) 3401 JVMWrapper("JVM_GetSystemPackage"); 3402 ResourceMark rm(THREAD); 3403 JvmtiVMObjectAllocEventCollector oam; 3404 char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name)); 3405 oop result = ClassLoader::get_system_package(str, CHECK_NULL); 3406 return (jstring) JNIHandles::make_local(THREAD, result); 3407 JVM_END 3408 3409 3410 JVM_ENTRY(jobjectArray, JVM_GetSystemPackages(JNIEnv *env)) 3411 JVMWrapper("JVM_GetSystemPackages"); 3412 JvmtiVMObjectAllocEventCollector oam; 3413 objArrayOop result = ClassLoader::get_system_packages(CHECK_NULL); 3414 return (jobjectArray) JNIHandles::make_local(THREAD, result); 3415 JVM_END 3416 3417 3418 // java.lang.ref.Reference /////////////////////////////////////////////////////////////// 3419 3420 3421 JVM_ENTRY(jobject, JVM_GetAndClearReferencePendingList(JNIEnv* env)) 3422 JVMWrapper("JVM_GetAndClearReferencePendingList"); 3423 3424 MonitorLocker ml(Heap_lock); 3425 oop ref = Universe::reference_pending_list(); 3426 if (ref != NULL) { 3427 Universe::clear_reference_pending_list(); 3428 } 3429 return JNIHandles::make_local(THREAD, ref); 3430 JVM_END 3431 3432 JVM_ENTRY(jboolean, JVM_HasReferencePendingList(JNIEnv* env)) 3433 JVMWrapper("JVM_HasReferencePendingList"); 3434 MonitorLocker ml(Heap_lock); 3435 return Universe::has_reference_pending_list(); 3436 JVM_END 3437 3438 JVM_ENTRY(void, JVM_WaitForReferencePendingList(JNIEnv* env)) 3439 JVMWrapper("JVM_WaitForReferencePendingList"); 3440 MonitorLocker ml(Heap_lock); 3441 while (!Universe::has_reference_pending_list()) { 3442 ml.wait(); 3443 } 3444 JVM_END 3445 3446 3447 // ObjectInputStream /////////////////////////////////////////////////////////////// 3448 3449 // Return the first user-defined class loader up the execution stack, or null 3450 // if only code from the bootstrap or platform class loader is on the stack. 3451 3452 JVM_ENTRY(jobject, JVM_LatestUserDefinedLoader(JNIEnv *env)) 3453 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) { 3454 vfst.skip_reflection_related_frames(); // Only needed for 1.4 reflection 3455 oop loader = vfst.method()->method_holder()->class_loader(); 3456 if (loader != NULL && !SystemDictionary::is_platform_class_loader(loader)) { 3457 return JNIHandles::make_local(THREAD, loader); 3458 } 3459 } 3460 return NULL; 3461 JVM_END 3462 3463 3464 // Array /////////////////////////////////////////////////////////////////////////////////////////// 3465 3466 3467 // resolve array handle and check arguments 3468 static inline arrayOop check_array(JNIEnv *env, jobject arr, bool type_array_only, TRAPS) { 3469 if (arr == NULL) { 3470 THROW_0(vmSymbols::java_lang_NullPointerException()); 3471 } 3472 oop a = JNIHandles::resolve_non_null(arr); 3473 if (!a->is_array()) { 3474 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array"); 3475 } else if (type_array_only && !a->is_typeArray()) { 3476 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array of primitive type"); 3477 } 3478 return arrayOop(a); 3479 } 3480 3481 3482 JVM_ENTRY(jint, JVM_GetArrayLength(JNIEnv *env, jobject arr)) 3483 JVMWrapper("JVM_GetArrayLength"); 3484 arrayOop a = check_array(env, arr, false, CHECK_0); 3485 return a->length(); 3486 JVM_END 3487 3488 3489 JVM_ENTRY(jobject, JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index)) 3490 JVMWrapper("JVM_Array_Get"); 3491 JvmtiVMObjectAllocEventCollector oam; 3492 arrayOop a = check_array(env, arr, false, CHECK_NULL); 3493 jvalue value; 3494 BasicType type = Reflection::array_get(&value, a, index, CHECK_NULL); 3495 oop box = Reflection::box(&value, type, CHECK_NULL); 3496 return JNIHandles::make_local(THREAD, box); 3497 JVM_END 3498 3499 3500 JVM_ENTRY(jvalue, JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode)) 3501 JVMWrapper("JVM_GetPrimitiveArrayElement"); 3502 jvalue value; 3503 value.i = 0; // to initialize value before getting used in CHECK 3504 arrayOop a = check_array(env, arr, true, CHECK_(value)); 3505 assert(a->is_typeArray(), "just checking"); 3506 BasicType type = Reflection::array_get(&value, a, index, CHECK_(value)); 3507 BasicType wide_type = (BasicType) wCode; 3508 if (type != wide_type) { 3509 Reflection::widen(&value, type, wide_type, CHECK_(value)); 3510 } 3511 return value; 3512 JVM_END 3513 3514 3515 JVM_ENTRY(void, JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val)) 3516 JVMWrapper("JVM_SetArrayElement"); 3517 arrayOop a = check_array(env, arr, false, CHECK); 3518 oop box = JNIHandles::resolve(val); 3519 jvalue value; 3520 value.i = 0; // to initialize value before getting used in CHECK 3521 BasicType value_type; 3522 if (a->is_objArray()) { 3523 // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array 3524 value_type = Reflection::unbox_for_regular_object(box, &value); 3525 } else { 3526 value_type = Reflection::unbox_for_primitive(box, &value, CHECK); 3527 } 3528 Reflection::array_set(&value, a, index, value_type, CHECK); 3529 JVM_END 3530 3531 3532 JVM_ENTRY(void, JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode)) 3533 JVMWrapper("JVM_SetPrimitiveArrayElement"); 3534 arrayOop a = check_array(env, arr, true, CHECK); 3535 assert(a->is_typeArray(), "just checking"); 3536 BasicType value_type = (BasicType) vCode; 3537 Reflection::array_set(&v, a, index, value_type, CHECK); 3538 JVM_END 3539 3540 3541 JVM_ENTRY(jobject, JVM_NewArray(JNIEnv *env, jclass eltClass, jint length)) 3542 JVMWrapper("JVM_NewArray"); 3543 JvmtiVMObjectAllocEventCollector oam; 3544 oop element_mirror = JNIHandles::resolve(eltClass); 3545 oop result = Reflection::reflect_new_array(element_mirror, length, CHECK_NULL); 3546 return JNIHandles::make_local(THREAD, result); 3547 JVM_END 3548 3549 3550 JVM_ENTRY(jobject, JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim)) 3551 JVMWrapper("JVM_NewMultiArray"); 3552 JvmtiVMObjectAllocEventCollector oam; 3553 arrayOop dim_array = check_array(env, dim, true, CHECK_NULL); 3554 oop element_mirror = JNIHandles::resolve(eltClass); 3555 assert(dim_array->is_typeArray(), "just checking"); 3556 oop result = Reflection::reflect_new_multi_array(element_mirror, typeArrayOop(dim_array), CHECK_NULL); 3557 return JNIHandles::make_local(THREAD, result); 3558 JVM_END 3559 3560 3561 // Library support /////////////////////////////////////////////////////////////////////////// 3562 3563 JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name)) 3564 //%note jvm_ct 3565 JVMWrapper("JVM_LoadLibrary"); 3566 char ebuf[1024]; 3567 void *load_result; 3568 { 3569 ThreadToNativeFromVM ttnfvm(thread); 3570 load_result = os::dll_load(name, ebuf, sizeof ebuf); 3571 } 3572 if (load_result == NULL) { 3573 char msg[1024]; 3574 jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf); 3575 // Since 'ebuf' may contain a string encoded using 3576 // platform encoding scheme, we need to pass 3577 // Exceptions::unsafe_to_utf8 to the new_exception method 3578 // as the last argument. See bug 6367357. 3579 Handle h_exception = 3580 Exceptions::new_exception(thread, 3581 vmSymbols::java_lang_UnsatisfiedLinkError(), 3582 msg, Exceptions::unsafe_to_utf8); 3583 3584 THROW_HANDLE_0(h_exception); 3585 } 3586 log_info(library)("Loaded library %s, handle " INTPTR_FORMAT, name, p2i(load_result)); 3587 return load_result; 3588 JVM_END 3589 3590 3591 JVM_LEAF(void, JVM_UnloadLibrary(void* handle)) 3592 JVMWrapper("JVM_UnloadLibrary"); 3593 os::dll_unload(handle); 3594 log_info(library)("Unloaded library with handle " INTPTR_FORMAT, p2i(handle)); 3595 JVM_END 3596 3597 3598 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name)) 3599 JVMWrapper("JVM_FindLibraryEntry"); 3600 void* find_result = os::dll_lookup(handle, name); 3601 log_info(library)("%s %s in library with handle " INTPTR_FORMAT, 3602 find_result != NULL ? "Found" : "Failed to find", 3603 name, p2i(handle)); 3604 return find_result; 3605 JVM_END 3606 3607 3608 // JNI version /////////////////////////////////////////////////////////////////////////////// 3609 3610 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version)) 3611 JVMWrapper("JVM_IsSupportedJNIVersion"); 3612 return Threads::is_supported_jni_version_including_1_1(version); 3613 JVM_END 3614 3615 3616 // String support /////////////////////////////////////////////////////////////////////////// 3617 3618 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str)) 3619 JVMWrapper("JVM_InternString"); 3620 JvmtiVMObjectAllocEventCollector oam; 3621 if (str == NULL) return NULL; 3622 oop string = JNIHandles::resolve_non_null(str); 3623 oop result = StringTable::intern(string, CHECK_NULL); 3624 return (jstring) JNIHandles::make_local(THREAD, result); 3625 JVM_END 3626 3627 3628 // VM Raw monitor support ////////////////////////////////////////////////////////////////////// 3629 3630 // VM Raw monitors (not to be confused with JvmtiRawMonitors) are a simple mutual exclusion 3631 // lock (not actually monitors: no wait/notify) that is exported by the VM for use by JDK 3632 // library code. They may be used by JavaThreads and non-JavaThreads and do not participate 3633 // in the safepoint protocol, thread suspension, thread interruption, or anything of that 3634 // nature. JavaThreads will be "in native" when using this API from JDK code. 3635 3636 3637 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void) { 3638 VM_Exit::block_if_vm_exited(); 3639 JVMWrapper("JVM_RawMonitorCreate"); 3640 return new os::PlatformMutex(); 3641 } 3642 3643 3644 JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void *mon) { 3645 VM_Exit::block_if_vm_exited(); 3646 JVMWrapper("JVM_RawMonitorDestroy"); 3647 delete ((os::PlatformMutex*) mon); 3648 } 3649 3650 3651 JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon) { 3652 VM_Exit::block_if_vm_exited(); 3653 JVMWrapper("JVM_RawMonitorEnter"); 3654 ((os::PlatformMutex*) mon)->lock(); 3655 return 0; 3656 } 3657 3658 3659 JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon) { 3660 VM_Exit::block_if_vm_exited(); 3661 JVMWrapper("JVM_RawMonitorExit"); 3662 ((os::PlatformMutex*) mon)->unlock(); 3663 } 3664 3665 3666 // Shared JNI/JVM entry points ////////////////////////////////////////////////////////////// 3667 3668 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init, 3669 Handle loader, Handle protection_domain, 3670 jboolean throwError, TRAPS) { 3671 // Security Note: 3672 // The Java level wrapper will perform the necessary security check allowing 3673 // us to pass the NULL as the initiating class loader. The VM is responsible for 3674 // the checkPackageAccess relative to the initiating class loader via the 3675 // protection_domain. The protection_domain is passed as NULL by the java code 3676 // if there is no security manager in 3-arg Class.forName(). 3677 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL); 3678 3679 // Check if we should initialize the class 3680 if (init && klass->is_instance_klass()) { 3681 klass->initialize(CHECK_NULL); 3682 } 3683 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror()); 3684 } 3685 3686 3687 // Method /////////////////////////////////////////////////////////////////////////////////////////// 3688 3689 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0)) 3690 JVMWrapper("JVM_InvokeMethod"); 3691 Handle method_handle; 3692 if (thread->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) { 3693 method_handle = Handle(THREAD, JNIHandles::resolve(method)); 3694 Handle receiver(THREAD, JNIHandles::resolve(obj)); 3695 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0))); 3696 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL); 3697 jobject res = JNIHandles::make_local(THREAD, result); 3698 if (JvmtiExport::should_post_vm_object_alloc()) { 3699 oop ret_type = java_lang_reflect_Method::return_type(method_handle()); 3700 assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!"); 3701 if (java_lang_Class::is_primitive(ret_type)) { 3702 // Only for primitive type vm allocates memory for java object. 3703 // See box() method. 3704 JvmtiExport::post_vm_object_alloc(thread, result); 3705 } 3706 } 3707 return res; 3708 } else { 3709 THROW_0(vmSymbols::java_lang_StackOverflowError()); 3710 } 3711 JVM_END 3712 3713 3714 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0)) 3715 JVMWrapper("JVM_NewInstanceFromConstructor"); 3716 oop constructor_mirror = JNIHandles::resolve(c); 3717 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0))); 3718 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL); 3719 jobject res = JNIHandles::make_local(THREAD, result); 3720 if (JvmtiExport::should_post_vm_object_alloc()) { 3721 JvmtiExport::post_vm_object_alloc(thread, result); 3722 } 3723 return res; 3724 JVM_END 3725 3726 // Atomic /////////////////////////////////////////////////////////////////////////////////////////// 3727 3728 JVM_LEAF(jboolean, JVM_SupportsCX8()) 3729 JVMWrapper("JVM_SupportsCX8"); 3730 return VM_Version::supports_cx8(); 3731 JVM_END 3732 3733 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls)) 3734 JVMWrapper("JVM_InitializeFromArchive"); 3735 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); 3736 assert(k->is_klass(), "just checking"); 3737 HeapShared::initialize_from_archived_subgraph(k); 3738 JVM_END 3739 3740 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env, 3741 jclass caller, 3742 jstring invokedName, 3743 jobject invokedType, 3744 jobject methodType, 3745 jobject implMethodMember, 3746 jobject instantiatedMethodType, 3747 jclass lambdaProxyClass)) 3748 JVMWrapper("JVM_RegisterLambdaProxyClassForArchiving"); 3749 #if INCLUDE_CDS 3750 if (!DynamicDumpSharedSpaces) { 3751 return; 3752 } 3753 3754 Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller)); 3755 InstanceKlass* caller_ik = InstanceKlass::cast(caller_k); 3756 if (caller_ik->is_hidden() || caller_ik->is_unsafe_anonymous()) { 3757 // VM anonymous classes and hidden classes not of type lambda proxy classes are currently not being archived. 3758 // If the caller_ik is of one of the above types, the corresponding lambda proxy class won't be 3759 // registered for archiving. 3760 return; 3761 } 3762 Klass* lambda_k = java_lang_Class::as_Klass(JNIHandles::resolve(lambdaProxyClass)); 3763 InstanceKlass* lambda_ik = InstanceKlass::cast(lambda_k); 3764 assert(lambda_ik->is_hidden(), "must be a hidden class"); 3765 assert(!lambda_ik->is_non_strong_hidden(), "expected a strong hidden class"); 3766 3767 Symbol* invoked_name = NULL; 3768 if (invokedName != NULL) { 3769 invoked_name = java_lang_String::as_symbol(JNIHandles::resolve_non_null(invokedName)); 3770 } 3771 Handle invoked_type_oop(THREAD, JNIHandles::resolve_non_null(invokedType)); 3772 Symbol* invoked_type = java_lang_invoke_MethodType::as_signature(invoked_type_oop(), true); 3773 3774 Handle method_type_oop(THREAD, JNIHandles::resolve_non_null(methodType)); 3775 Symbol* method_type = java_lang_invoke_MethodType::as_signature(method_type_oop(), true); 3776 3777 Handle impl_method_member_oop(THREAD, JNIHandles::resolve_non_null(implMethodMember)); 3778 assert(java_lang_invoke_MemberName::is_method(impl_method_member_oop()), "must be"); 3779 Method* m = java_lang_invoke_MemberName::vmtarget(impl_method_member_oop()); 3780 3781 Handle instantiated_method_type_oop(THREAD, JNIHandles::resolve_non_null(instantiatedMethodType)); 3782 Symbol* instantiated_method_type = java_lang_invoke_MethodType::as_signature(instantiated_method_type_oop(), true); 3783 3784 SystemDictionaryShared::add_lambda_proxy_class(caller_ik, lambda_ik, invoked_name, invoked_type, 3785 method_type, m, instantiated_method_type); 3786 #endif // INCLUDE_CDS 3787 JVM_END 3788 3789 JVM_ENTRY(jclass, JVM_LookupLambdaProxyClassFromArchive(JNIEnv* env, 3790 jclass caller, 3791 jstring invokedName, 3792 jobject invokedType, 3793 jobject methodType, 3794 jobject implMethodMember, 3795 jobject instantiatedMethodType, 3796 jboolean initialize)) 3797 JVMWrapper("JVM_LookupLambdaProxyClassFromArchive"); 3798 #if INCLUDE_CDS 3799 if (!DynamicArchive::is_mapped()) { 3800 return NULL; 3801 } 3802 3803 if (invokedName == NULL || invokedType == NULL || methodType == NULL || 3804 implMethodMember == NULL || instantiatedMethodType == NULL) { 3805 THROW_(vmSymbols::java_lang_NullPointerException(), NULL); 3806 } 3807 3808 Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller)); 3809 InstanceKlass* caller_ik = InstanceKlass::cast(caller_k); 3810 if (!caller_ik->is_shared()) { 3811 // there won't be a shared lambda class if the caller_ik is not in the shared archive. 3812 return NULL; 3813 } 3814 3815 Symbol* invoked_name = java_lang_String::as_symbol(JNIHandles::resolve_non_null(invokedName)); 3816 Handle invoked_type_oop(THREAD, JNIHandles::resolve_non_null(invokedType)); 3817 Symbol* invoked_type = java_lang_invoke_MethodType::as_signature(invoked_type_oop(), true); 3818 3819 Handle method_type_oop(THREAD, JNIHandles::resolve_non_null(methodType)); 3820 Symbol* method_type = java_lang_invoke_MethodType::as_signature(method_type_oop(), true); 3821 3822 Handle impl_method_member_oop(THREAD, JNIHandles::resolve_non_null(implMethodMember)); 3823 assert(java_lang_invoke_MemberName::is_method(impl_method_member_oop()), "must be"); 3824 Method* m = java_lang_invoke_MemberName::vmtarget(impl_method_member_oop()); 3825 3826 Handle instantiated_method_type_oop(THREAD, JNIHandles::resolve_non_null(instantiatedMethodType)); 3827 Symbol* instantiated_method_type = java_lang_invoke_MethodType::as_signature(instantiated_method_type_oop(), true); 3828 3829 InstanceKlass* lambda_ik = SystemDictionaryShared::get_shared_lambda_proxy_class(caller_ik, invoked_name, invoked_type, 3830 method_type, m, instantiated_method_type); 3831 jclass jcls = NULL; 3832 if (lambda_ik != NULL) { 3833 InstanceKlass* loaded_lambda = SystemDictionaryShared::prepare_shared_lambda_proxy_class(lambda_ik, caller_ik, initialize, THREAD); 3834 jcls = loaded_lambda == NULL ? NULL : (jclass) JNIHandles::make_local(THREAD, loaded_lambda->java_mirror()); 3835 } 3836 return jcls; 3837 #else 3838 return NULL; 3839 #endif // INCLUDE_CDS 3840 JVM_END 3841 3842 JVM_ENTRY(jboolean, JVM_IsCDSDumpingEnabled(JNIEnv* env)) 3843 JVMWrapper("JVM_IsCDSDumpingEnable"); 3844 return DynamicDumpSharedSpaces; 3845 JVM_END 3846 3847 JVM_ENTRY(jboolean, JVM_IsCDSSharingEnabled(JNIEnv* env)) 3848 JVMWrapper("JVM_IsCDSSharingEnable"); 3849 return UseSharedSpaces; 3850 JVM_END 3851 3852 JVM_ENTRY_NO_ENV(jlong, JVM_GetRandomSeedForCDSDump()) 3853 JVMWrapper("JVM_GetRandomSeedForCDSDump"); 3854 if (DumpSharedSpaces) { 3855 const char* release = Abstract_VM_Version::vm_release(); 3856 const char* dbg_level = Abstract_VM_Version::jdk_debug_level(); 3857 const char* version = VM_Version::internal_vm_info_string(); 3858 jlong seed = (jlong)(java_lang_String::hash_code((const jbyte*)release, (int)strlen(release)) ^ 3859 java_lang_String::hash_code((const jbyte*)dbg_level, (int)strlen(dbg_level)) ^ 3860 java_lang_String::hash_code((const jbyte*)version, (int)strlen(version))); 3861 seed += (jlong)Abstract_VM_Version::vm_major_version(); 3862 seed += (jlong)Abstract_VM_Version::vm_minor_version(); 3863 seed += (jlong)Abstract_VM_Version::vm_security_version(); 3864 seed += (jlong)Abstract_VM_Version::vm_patch_version(); 3865 if (seed == 0) { // don't let this ever be zero. 3866 seed = 0x87654321; 3867 } 3868 log_debug(cds)("JVM_GetRandomSeedForCDSDump() = " JLONG_FORMAT, seed); 3869 return seed; 3870 } else { 3871 return 0; 3872 } 3873 JVM_END 3874 3875 // Returns an array of all live Thread objects (VM internal JavaThreads, 3876 // jvmti agent threads, and JNI attaching threads are skipped) 3877 // See CR 6404306 regarding JNI attaching threads 3878 JVM_ENTRY(jobjectArray, JVM_GetAllThreads(JNIEnv *env, jclass dummy)) 3879 ResourceMark rm(THREAD); 3880 ThreadsListEnumerator tle(THREAD, false, false); 3881 JvmtiVMObjectAllocEventCollector oam; 3882 3883 int num_threads = tle.num_threads(); 3884 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Thread_klass(), num_threads, CHECK_NULL); 3885 objArrayHandle threads_ah(THREAD, r); 3886 3887 for (int i = 0; i < num_threads; i++) { 3888 Handle h = tle.get_threadObj(i); 3889 threads_ah->obj_at_put(i, h()); 3890 } 3891 3892 return (jobjectArray) JNIHandles::make_local(THREAD, threads_ah()); 3893 JVM_END 3894 3895 3896 // Support for java.lang.Thread.getStackTrace() and getAllStackTraces() methods 3897 // Return StackTraceElement[][], each element is the stack trace of a thread in 3898 // the corresponding entry in the given threads array 3899 JVM_ENTRY(jobjectArray, JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads)) 3900 JVMWrapper("JVM_DumpThreads"); 3901 JvmtiVMObjectAllocEventCollector oam; 3902 3903 // Check if threads is null 3904 if (threads == NULL) { 3905 THROW_(vmSymbols::java_lang_NullPointerException(), 0); 3906 } 3907 3908 objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads)); 3909 objArrayHandle ah(THREAD, a); 3910 int num_threads = ah->length(); 3911 // check if threads is non-empty array 3912 if (num_threads == 0) { 3913 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0); 3914 } 3915 3916 // check if threads is not an array of objects of Thread class 3917 Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass(); 3918 if (k != SystemDictionary::Thread_klass()) { 3919 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0); 3920 } 3921 3922 ResourceMark rm(THREAD); 3923 3924 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads); 3925 for (int i = 0; i < num_threads; i++) { 3926 oop thread_obj = ah->obj_at(i); 3927 instanceHandle h(THREAD, (instanceOop) thread_obj); 3928 thread_handle_array->append(h); 3929 } 3930 3931 // The JavaThread references in thread_handle_array are validated 3932 // in VM_ThreadDump::doit(). 3933 Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL); 3934 return (jobjectArray)JNIHandles::make_local(THREAD, stacktraces()); 3935 3936 JVM_END 3937 3938 // JVM monitoring and management support 3939 JVM_ENTRY_NO_ENV(void*, JVM_GetManagement(jint version)) 3940 return Management::get_jmm_interface(version); 3941 JVM_END 3942 3943 // com.sun.tools.attach.VirtualMachine agent properties support 3944 // 3945 // Initialize the agent properties with the properties maintained in the VM 3946 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties)) 3947 JVMWrapper("JVM_InitAgentProperties"); 3948 ResourceMark rm; 3949 3950 Handle props(THREAD, JNIHandles::resolve_non_null(properties)); 3951 3952 PUTPROP(props, "sun.java.command", Arguments::java_command()); 3953 PUTPROP(props, "sun.jvm.flags", Arguments::jvm_flags()); 3954 PUTPROP(props, "sun.jvm.args", Arguments::jvm_args()); 3955 return properties; 3956 JVM_END 3957 3958 JVM_ENTRY(jobjectArray, JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass)) 3959 { 3960 JVMWrapper("JVM_GetEnclosingMethodInfo"); 3961 JvmtiVMObjectAllocEventCollector oam; 3962 3963 if (ofClass == NULL) { 3964 return NULL; 3965 } 3966 Handle mirror(THREAD, JNIHandles::resolve_non_null(ofClass)); 3967 // Special handling for primitive objects 3968 if (java_lang_Class::is_primitive(mirror())) { 3969 return NULL; 3970 } 3971 Klass* k = java_lang_Class::as_Klass(mirror()); 3972 if (!k->is_instance_klass()) { 3973 return NULL; 3974 } 3975 InstanceKlass* ik = InstanceKlass::cast(k); 3976 int encl_method_class_idx = ik->enclosing_method_class_index(); 3977 if (encl_method_class_idx == 0) { 3978 return NULL; 3979 } 3980 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::Object_klass(), 3, CHECK_NULL); 3981 objArrayHandle dest(THREAD, dest_o); 3982 Klass* enc_k = ik->constants()->klass_at(encl_method_class_idx, CHECK_NULL); 3983 dest->obj_at_put(0, enc_k->java_mirror()); 3984 int encl_method_method_idx = ik->enclosing_method_method_index(); 3985 if (encl_method_method_idx != 0) { 3986 Symbol* sym = ik->constants()->symbol_at( 3987 extract_low_short_from_int( 3988 ik->constants()->name_and_type_at(encl_method_method_idx))); 3989 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL); 3990 dest->obj_at_put(1, str()); 3991 sym = ik->constants()->symbol_at( 3992 extract_high_short_from_int( 3993 ik->constants()->name_and_type_at(encl_method_method_idx))); 3994 str = java_lang_String::create_from_symbol(sym, CHECK_NULL); 3995 dest->obj_at_put(2, str()); 3996 } 3997 return (jobjectArray) JNIHandles::make_local(THREAD, dest()); 3998 } 3999 JVM_END 4000 4001 // Returns an array of java.lang.String objects containing the input arguments to the VM. 4002 JVM_ENTRY(jobjectArray, JVM_GetVmArguments(JNIEnv *env)) 4003 ResourceMark rm(THREAD); 4004 4005 if (Arguments::num_jvm_args() == 0 && Arguments::num_jvm_flags() == 0) { 4006 return NULL; 4007 } 4008 4009 char** vm_flags = Arguments::jvm_flags_array(); 4010 char** vm_args = Arguments::jvm_args_array(); 4011 int num_flags = Arguments::num_jvm_flags(); 4012 int num_args = Arguments::num_jvm_args(); 4013 4014 InstanceKlass* ik = SystemDictionary::String_klass(); 4015 objArrayOop r = oopFactory::new_objArray(ik, num_args + num_flags, CHECK_NULL); 4016 objArrayHandle result_h(THREAD, r); 4017 4018 int index = 0; 4019 for (int j = 0; j < num_flags; j++, index++) { 4020 Handle h = java_lang_String::create_from_platform_dependent_str(vm_flags[j], CHECK_NULL); 4021 result_h->obj_at_put(index, h()); 4022 } 4023 for (int i = 0; i < num_args; i++, index++) { 4024 Handle h = java_lang_String::create_from_platform_dependent_str(vm_args[i], CHECK_NULL); 4025 result_h->obj_at_put(index, h()); 4026 } 4027 return (jobjectArray) JNIHandles::make_local(THREAD, result_h()); 4028 JVM_END 4029 4030 JVM_ENTRY_NO_ENV(jint, JVM_FindSignal(const char *name)) 4031 return os::get_signal_number(name); 4032 JVM_END