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