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