1 /* 2 * Copyright (c) 1997, 2018, 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 "classfile/classLoader.hpp" 27 #include "classfile/classLoaderData.inline.hpp" 28 #include "classfile/classLoaderExt.hpp" 29 #include "classfile/javaAssertions.hpp" 30 #include "classfile/javaClasses.hpp" 31 #include "classfile/symbolTable.hpp" 32 #include "classfile/systemDictionary.hpp" 33 #if INCLUDE_CDS 34 #include "classfile/sharedClassUtil.hpp" 35 #include "classfile/systemDictionaryShared.hpp" 36 #endif 37 #include "classfile/vmSymbols.hpp" 38 #include "gc_interface/collectedHeap.inline.hpp" 39 #include "interpreter/bytecode.hpp" 40 #if INCLUDE_JFR 41 #include "jfr/jfrEvents.hpp" 42 #endif 43 #include "memory/oopFactory.hpp" 44 #include "memory/referenceType.hpp" 45 #include "memory/universe.inline.hpp" 46 #include "oops/fieldStreams.hpp" 47 #include "oops/instanceKlass.hpp" 48 #include "oops/objArrayKlass.hpp" 49 #include "oops/method.hpp" 50 #include "prims/jvm.h" 51 #include "prims/jvm_misc.hpp" 52 #include "prims/jvmtiExport.hpp" 53 #include "prims/jvmtiThreadState.hpp" 54 #include "prims/nativeLookup.hpp" 55 #include "prims/privilegedStack.hpp" 56 #include "runtime/arguments.hpp" 57 #include "runtime/dtraceJSDT.hpp" 58 #include "runtime/handles.inline.hpp" 59 #include "runtime/init.hpp" 60 #include "runtime/interfaceSupport.hpp" 61 #include "runtime/java.hpp" 62 #include "runtime/javaCalls.hpp" 63 #include "runtime/jfieldIDWorkaround.hpp" 64 #include "runtime/orderAccess.inline.hpp" 65 #include "runtime/os.hpp" 66 #include "runtime/perfData.hpp" 67 #include "runtime/reflection.hpp" 68 #include "runtime/vframe.hpp" 69 #include "runtime/vm_operations.hpp" 70 #include "services/attachListener.hpp" 71 #include "services/management.hpp" 72 #include "services/threadService.hpp" 73 #include "utilities/copy.hpp" 74 #include "utilities/defaultStream.hpp" 75 #include "utilities/dtrace.hpp" 76 #include "utilities/events.hpp" 77 #include "utilities/histogram.hpp" 78 #include "utilities/top.hpp" 79 #include "utilities/utf8.hpp" 80 #ifdef TARGET_OS_FAMILY_linux 81 # include "jvm_linux.h" 82 #endif 83 #ifdef TARGET_OS_FAMILY_solaris 84 # include "jvm_solaris.h" 85 #endif 86 #ifdef TARGET_OS_FAMILY_windows 87 # include "jvm_windows.h" 88 #endif 89 #ifdef TARGET_OS_FAMILY_aix 90 # include "jvm_aix.h" 91 #endif 92 #ifdef TARGET_OS_FAMILY_bsd 93 # include "jvm_bsd.h" 94 #endif 95 96 #if INCLUDE_ALL_GCS 97 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp" 98 #endif // INCLUDE_ALL_GCS 99 100 #include <errno.h> 101 102 #ifndef USDT2 103 HS_DTRACE_PROBE_DECL1(hotspot, thread__sleep__begin, long long); 104 HS_DTRACE_PROBE_DECL1(hotspot, thread__sleep__end, int); 105 HS_DTRACE_PROBE_DECL0(hotspot, thread__yield); 106 #endif /* !USDT2 */ 107 108 /* 109 NOTE about use of any ctor or function call that can trigger a safepoint/GC: 110 such ctors and calls MUST NOT come between an oop declaration/init and its 111 usage because if objects are move this may cause various memory stomps, bus 112 errors and segfaults. Here is a cookbook for causing so called "naked oop 113 failures": 114 115 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields<etc> { 116 JVMWrapper("JVM_GetClassDeclaredFields"); 117 118 // Object address to be held directly in mirror & not visible to GC 119 oop mirror = JNIHandles::resolve_non_null(ofClass); 120 121 // If this ctor can hit a safepoint, moving objects around, then 122 ComplexConstructor foo; 123 124 // Boom! mirror may point to JUNK instead of the intended object 125 (some dereference of mirror) 126 127 // Here's another call that may block for GC, making mirror stale 128 MutexLocker ml(some_lock); 129 130 // And here's an initializer that can result in a stale oop 131 // all in one step. 132 oop o = call_that_can_throw_exception(TRAPS); 133 134 135 The solution is to keep the oop declaration BELOW the ctor or function 136 call that might cause a GC, do another resolve to reassign the oop, or 137 consider use of a Handle instead of an oop so there is immunity from object 138 motion. But note that the "QUICK" entries below do not have a handlemark 139 and thus can only support use of handles passed in. 140 */ 141 142 static void trace_class_resolution_impl(Klass* to_class, TRAPS) { 143 ResourceMark rm; 144 int line_number = -1; 145 const char * source_file = NULL; 146 const char * trace = "explicit"; 147 InstanceKlass* caller = NULL; 148 JavaThread* jthread = JavaThread::current(); 149 if (jthread->has_last_Java_frame()) { 150 vframeStream vfst(jthread); 151 152 // scan up the stack skipping ClassLoader, AccessController and PrivilegedAction frames 153 TempNewSymbol access_controller = SymbolTable::new_symbol("java/security/AccessController", CHECK); 154 Klass* access_controller_klass = SystemDictionary::resolve_or_fail(access_controller, false, CHECK); 155 TempNewSymbol privileged_action = SymbolTable::new_symbol("java/security/PrivilegedAction", CHECK); 156 Klass* privileged_action_klass = SystemDictionary::resolve_or_fail(privileged_action, false, CHECK); 157 158 Method* last_caller = NULL; 159 160 while (!vfst.at_end()) { 161 Method* m = vfst.method(); 162 if (!vfst.method()->method_holder()->is_subclass_of(SystemDictionary::ClassLoader_klass())&& 163 !vfst.method()->method_holder()->is_subclass_of(access_controller_klass) && 164 !vfst.method()->method_holder()->is_subclass_of(privileged_action_klass)) { 165 break; 166 } 167 last_caller = m; 168 vfst.next(); 169 } 170 // if this is called from Class.forName0 and that is called from Class.forName, 171 // then print the caller of Class.forName. If this is Class.loadClass, then print 172 // that caller, otherwise keep quiet since this should be picked up elsewhere. 173 bool found_it = false; 174 if (!vfst.at_end() && 175 vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class() && 176 vfst.method()->name() == vmSymbols::forName0_name()) { 177 vfst.next(); 178 if (!vfst.at_end() && 179 vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class() && 180 vfst.method()->name() == vmSymbols::forName_name()) { 181 vfst.next(); 182 found_it = true; 183 } 184 } else if (last_caller != NULL && 185 last_caller->method_holder()->name() == 186 vmSymbols::java_lang_ClassLoader() && 187 (last_caller->name() == vmSymbols::loadClassInternal_name() || 188 last_caller->name() == vmSymbols::loadClass_name())) { 189 found_it = true; 190 } else if (!vfst.at_end()) { 191 if (vfst.method()->is_native()) { 192 // JNI call 193 found_it = true; 194 } 195 } 196 if (found_it && !vfst.at_end()) { 197 // found the caller 198 caller = vfst.method()->method_holder(); 199 line_number = vfst.method()->line_number_from_bci(vfst.bci()); 200 if (line_number == -1) { 201 // show method name if it's a native method 202 trace = vfst.method()->name_and_sig_as_C_string(); 203 } 204 Symbol* s = caller->source_file_name(); 205 if (s != NULL) { 206 source_file = s->as_C_string(); 207 } 208 } 209 } 210 if (caller != NULL) { 211 if (to_class != caller) { 212 const char * from = caller->external_name(); 213 const char * to = to_class->external_name(); 214 // print in a single call to reduce interleaving between threads 215 if (source_file != NULL) { 216 tty->print("RESOLVE %s %s %s:%d (%s)\n", from, to, source_file, line_number, trace); 217 } else { 218 tty->print("RESOLVE %s %s (%s)\n", from, to, trace); 219 } 220 } 221 } 222 } 223 224 void trace_class_resolution(Klass* to_class) { 225 EXCEPTION_MARK; 226 trace_class_resolution_impl(to_class, THREAD); 227 if (HAS_PENDING_EXCEPTION) { 228 CLEAR_PENDING_EXCEPTION; 229 } 230 } 231 232 // Wrapper to trace JVM functions 233 234 #ifdef ASSERT 235 class JVMTraceWrapper : public StackObj { 236 public: 237 JVMTraceWrapper(const char* format, ...) ATTRIBUTE_PRINTF(2, 3) { 238 if (TraceJVMCalls) { 239 va_list ap; 240 va_start(ap, format); 241 tty->print("JVM "); 242 tty->vprint_cr(format, ap); 243 va_end(ap); 244 } 245 } 246 }; 247 248 Histogram* JVMHistogram; 249 volatile jint JVMHistogram_lock = 0; 250 251 class JVMHistogramElement : public HistogramElement { 252 public: 253 JVMHistogramElement(const char* name); 254 }; 255 256 JVMHistogramElement::JVMHistogramElement(const char* elementName) { 257 _name = elementName; 258 uintx count = 0; 259 260 while (Atomic::cmpxchg(1, &JVMHistogram_lock, 0) != 0) { 261 while (OrderAccess::load_acquire(&JVMHistogram_lock) != 0) { 262 count +=1; 263 if ( (WarnOnStalledSpinLock > 0) 264 && (count % WarnOnStalledSpinLock == 0)) { 265 warning("JVMHistogram_lock seems to be stalled"); 266 } 267 } 268 } 269 270 if(JVMHistogram == NULL) 271 JVMHistogram = new Histogram("JVM Call Counts",100); 272 273 JVMHistogram->add_element(this); 274 Atomic::dec(&JVMHistogram_lock); 275 } 276 277 #define JVMCountWrapper(arg) \ 278 static JVMHistogramElement* e = new JVMHistogramElement(arg); \ 279 if (e != NULL) e->increment_count(); // Due to bug in VC++, we need a NULL check here eventhough it should never happen! 280 281 #define JVMWrapper(arg1) JVMCountWrapper(arg1); JVMTraceWrapper(arg1) 282 #define JVMWrapper2(arg1, arg2) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2) 283 #define JVMWrapper3(arg1, arg2, arg3) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2, arg3) 284 #define JVMWrapper4(arg1, arg2, arg3, arg4) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2, arg3, arg4) 285 #else 286 #define JVMWrapper(arg1) 287 #define JVMWrapper2(arg1, arg2) 288 #define JVMWrapper3(arg1, arg2, arg3) 289 #define JVMWrapper4(arg1, arg2, arg3, arg4) 290 #endif 291 292 293 // Interface version ///////////////////////////////////////////////////////////////////// 294 295 296 JVM_LEAF(jint, JVM_GetInterfaceVersion()) 297 return JVM_INTERFACE_VERSION; 298 JVM_END 299 300 301 // java.lang.System ////////////////////////////////////////////////////////////////////// 302 303 304 JVM_LEAF(jlong, JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored)) 305 JVMWrapper("JVM_CurrentTimeMillis"); 306 return os::javaTimeMillis(); 307 JVM_END 308 309 JVM_LEAF(jlong, JVM_NanoTime(JNIEnv *env, jclass ignored)) 310 JVMWrapper("JVM_NanoTime"); 311 return os::javaTimeNanos(); 312 JVM_END 313 314 315 JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos, 316 jobject dst, jint dst_pos, jint length)) 317 JVMWrapper("JVM_ArrayCopy"); 318 // Check if we have null pointers 319 if (src == NULL || dst == NULL) { 320 THROW(vmSymbols::java_lang_NullPointerException()); 321 } 322 arrayOop s = arrayOop(JNIHandles::resolve_non_null(src)); 323 arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst)); 324 assert(s->is_oop(), "JVM_ArrayCopy: src not an oop"); 325 assert(d->is_oop(), "JVM_ArrayCopy: dst not an oop"); 326 // Do copy 327 s->klass()->copy_array(s, src_pos, d, dst_pos, length, thread); 328 JVM_END 329 330 331 static void set_property(Handle props, const char* key, const char* value, TRAPS) { 332 JavaValue r(T_OBJECT); 333 // public synchronized Object put(Object key, Object value); 334 HandleMark hm(THREAD); 335 Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK); 336 Handle value_str = java_lang_String::create_from_platform_dependent_str((value != NULL ? value : ""), CHECK); 337 JavaCalls::call_virtual(&r, 338 props, 339 KlassHandle(THREAD, SystemDictionary::Properties_klass()), 340 vmSymbols::put_name(), 341 vmSymbols::object_object_object_signature(), 342 key_str, 343 value_str, 344 THREAD); 345 } 346 347 348 #define PUTPROP(props, name, value) set_property((props), (name), (value), CHECK_(properties)); 349 350 351 JVM_ENTRY(jobject, JVM_InitProperties(JNIEnv *env, jobject properties)) 352 JVMWrapper("JVM_InitProperties"); 353 ResourceMark rm; 354 355 Handle props(THREAD, JNIHandles::resolve_non_null(properties)); 356 357 // System property list includes both user set via -D option and 358 // jvm system specific properties. 359 for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) { 360 PUTPROP(props, p->key(), p->value()); 361 } 362 363 // Convert the -XX:MaxDirectMemorySize= command line flag 364 // to the sun.nio.MaxDirectMemorySize property. 365 // Do this after setting user properties to prevent people 366 // from setting the value with a -D option, as requested. 367 { 368 if (FLAG_IS_DEFAULT(MaxDirectMemorySize)) { 369 PUTPROP(props, "sun.nio.MaxDirectMemorySize", "-1"); 370 } else { 371 char as_chars[256]; 372 jio_snprintf(as_chars, sizeof(as_chars), UINTX_FORMAT, MaxDirectMemorySize); 373 PUTPROP(props, "sun.nio.MaxDirectMemorySize", as_chars); 374 } 375 } 376 377 // JVM monitoring and management support 378 // Add the sun.management.compiler property for the compiler's name 379 { 380 #undef CSIZE 381 #if defined(_LP64) || defined(_WIN64) 382 #define CSIZE "64-Bit " 383 #else 384 #define CSIZE 385 #endif // 64bit 386 387 #ifdef TIERED 388 const char* compiler_name = "HotSpot " CSIZE "Tiered Compilers"; 389 #else 390 #if defined(COMPILER1) 391 const char* compiler_name = "HotSpot " CSIZE "Client Compiler"; 392 #elif defined(COMPILER2) 393 const char* compiler_name = "HotSpot " CSIZE "Server Compiler"; 394 #else 395 const char* compiler_name = ""; 396 #endif // compilers 397 #endif // TIERED 398 399 if (*compiler_name != '\0' && 400 (Arguments::mode() != Arguments::_int)) { 401 PUTPROP(props, "sun.management.compiler", compiler_name); 402 } 403 } 404 405 const char* enableSharedLookupCache = "false"; 406 #if INCLUDE_CDS 407 if (ClassLoaderExt::is_lookup_cache_enabled()) { 408 enableSharedLookupCache = "true"; 409 } 410 #endif 411 PUTPROP(props, "sun.cds.enableSharedLookupCache", enableSharedLookupCache); 412 413 return properties; 414 JVM_END 415 416 417 /* 418 * Return the temporary directory that the VM uses for the attach 419 * and perf data files. 420 * 421 * It is important that this directory is well-known and the 422 * same for all VM instances. It cannot be affected by configuration 423 * variables such as java.io.tmpdir. 424 */ 425 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env)) 426 JVMWrapper("JVM_GetTemporaryDirectory"); 427 HandleMark hm(THREAD); 428 const char* temp_dir = os::get_temp_directory(); 429 Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL); 430 return (jstring) JNIHandles::make_local(env, h()); 431 JVM_END 432 433 434 // java.lang.Runtime ///////////////////////////////////////////////////////////////////////// 435 436 extern volatile jint vm_created; 437 438 JVM_ENTRY_NO_ENV(void, JVM_Exit(jint code)) 439 if (vm_created != 0 && (code == 0)) { 440 // The VM is about to exit. We call back into Java to check whether finalizers should be run 441 Universe::run_finalizers_on_exit(); 442 } 443 before_exit(thread); 444 vm_exit(code); 445 JVM_END 446 447 448 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt()) 449 JVMWrapper("JVM_BeforeHalt"); 450 #if INCLUDE_JFR 451 EventShutdown event; 452 if (event.should_commit()) { 453 event.set_reason("Shutdown requested from Java"); 454 event.commit(); 455 } 456 #endif 457 JVM_END 458 459 460 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code)) 461 before_exit(thread); 462 vm_exit(code); 463 JVM_END 464 465 466 JVM_LEAF(void, JVM_OnExit(void (*func)(void))) 467 register_on_exit_function(func); 468 JVM_END 469 470 471 JVM_ENTRY_NO_ENV(void, JVM_GC(void)) 472 JVMWrapper("JVM_GC"); 473 if (!DisableExplicitGC) { 474 Universe::heap()->collect(GCCause::_java_lang_system_gc); 475 } 476 JVM_END 477 478 479 JVM_LEAF(jlong, JVM_MaxObjectInspectionAge(void)) 480 JVMWrapper("JVM_MaxObjectInspectionAge"); 481 return Universe::heap()->millis_since_last_gc(); 482 JVM_END 483 484 485 JVM_LEAF(void, JVM_TraceInstructions(jboolean on)) 486 if (PrintJVMWarnings) warning("JVM_TraceInstructions not supported"); 487 JVM_END 488 489 490 JVM_LEAF(void, JVM_TraceMethodCalls(jboolean on)) 491 if (PrintJVMWarnings) warning("JVM_TraceMethodCalls not supported"); 492 JVM_END 493 494 static inline jlong convert_size_t_to_jlong(size_t val) { 495 // In the 64-bit vm, a size_t can overflow a jlong (which is signed). 496 NOT_LP64 (return (jlong)val;) 497 LP64_ONLY(return (jlong)MIN2(val, (size_t)max_jlong);) 498 } 499 500 JVM_ENTRY_NO_ENV(jlong, JVM_TotalMemory(void)) 501 JVMWrapper("JVM_TotalMemory"); 502 size_t n = Universe::heap()->capacity(); 503 return convert_size_t_to_jlong(n); 504 JVM_END 505 506 507 JVM_ENTRY_NO_ENV(jlong, JVM_FreeMemory(void)) 508 JVMWrapper("JVM_FreeMemory"); 509 CollectedHeap* ch = Universe::heap(); 510 size_t n; 511 { 512 MutexLocker x(Heap_lock); 513 n = ch->capacity() - ch->used(); 514 } 515 return convert_size_t_to_jlong(n); 516 JVM_END 517 518 519 JVM_ENTRY_NO_ENV(jlong, JVM_MaxMemory(void)) 520 JVMWrapper("JVM_MaxMemory"); 521 size_t n = Universe::heap()->max_capacity(); 522 return convert_size_t_to_jlong(n); 523 JVM_END 524 525 526 JVM_ENTRY_NO_ENV(jint, JVM_ActiveProcessorCount(void)) 527 JVMWrapper("JVM_ActiveProcessorCount"); 528 return os::active_processor_count(); 529 JVM_END 530 531 532 533 // java.lang.Throwable ////////////////////////////////////////////////////// 534 535 536 JVM_ENTRY(void, JVM_FillInStackTrace(JNIEnv *env, jobject receiver)) 537 JVMWrapper("JVM_FillInStackTrace"); 538 Handle exception(thread, JNIHandles::resolve_non_null(receiver)); 539 java_lang_Throwable::fill_in_stack_trace(exception); 540 JVM_END 541 542 543 JVM_ENTRY(jint, JVM_GetStackTraceDepth(JNIEnv *env, jobject throwable)) 544 JVMWrapper("JVM_GetStackTraceDepth"); 545 oop exception = JNIHandles::resolve(throwable); 546 return java_lang_Throwable::get_stack_trace_depth(exception, THREAD); 547 JVM_END 548 549 550 JVM_ENTRY(jobject, JVM_GetStackTraceElement(JNIEnv *env, jobject throwable, jint index)) 551 JVMWrapper("JVM_GetStackTraceElement"); 552 JvmtiVMObjectAllocEventCollector oam; // This ctor (throughout this module) may trigger a safepoint/GC 553 oop exception = JNIHandles::resolve(throwable); 554 oop element = java_lang_Throwable::get_stack_trace_element(exception, index, CHECK_NULL); 555 return JNIHandles::make_local(env, element); 556 JVM_END 557 558 559 // java.lang.Object /////////////////////////////////////////////// 560 561 562 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle)) 563 JVMWrapper("JVM_IHashCode"); 564 // as implemented in the classic virtual machine; return 0 if object is NULL 565 return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ; 566 JVM_END 567 568 569 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms)) 570 JVMWrapper("JVM_MonitorWait"); 571 Handle obj(THREAD, JNIHandles::resolve_non_null(handle)); 572 JavaThreadInObjectWaitState jtiows(thread, ms != 0); 573 if (JvmtiExport::should_post_monitor_wait()) { 574 JvmtiExport::post_monitor_wait((JavaThread *)THREAD, (oop)obj(), ms); 575 576 // The current thread already owns the monitor and it has not yet 577 // been added to the wait queue so the current thread cannot be 578 // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT 579 // event handler cannot accidentally consume an unpark() meant for 580 // the ParkEvent associated with this ObjectMonitor. 581 } 582 ObjectSynchronizer::wait(obj, ms, CHECK); 583 JVM_END 584 585 586 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle)) 587 JVMWrapper("JVM_MonitorNotify"); 588 Handle obj(THREAD, JNIHandles::resolve_non_null(handle)); 589 ObjectSynchronizer::notify(obj, CHECK); 590 JVM_END 591 592 593 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle)) 594 JVMWrapper("JVM_MonitorNotifyAll"); 595 Handle obj(THREAD, JNIHandles::resolve_non_null(handle)); 596 ObjectSynchronizer::notifyall(obj, CHECK); 597 JVM_END 598 599 600 static void fixup_cloned_reference(ReferenceType ref_type, oop src, oop clone) { 601 // If G1 is enabled then we need to register a non-null referent 602 // with the SATB barrier. 603 #if INCLUDE_ALL_GCS 604 if (UseG1GC) { 605 oop referent = java_lang_ref_Reference::referent(clone); 606 if (referent != NULL) { 607 G1SATBCardTableModRefBS::enqueue(referent); 608 } 609 } 610 #endif // INCLUDE_ALL_GCS 611 if ((java_lang_ref_Reference::next(clone) != NULL) || 612 (java_lang_ref_Reference::queue(clone) == java_lang_ref_ReferenceQueue::ENQUEUED_queue())) { 613 // If the source has been enqueued or is being enqueued, don't 614 // register the clone with a queue. 615 java_lang_ref_Reference::set_queue(clone, java_lang_ref_ReferenceQueue::NULL_queue()); 616 } 617 // discovered and next are list links; the clone is not in those lists. 618 java_lang_ref_Reference::set_discovered(clone, NULL); 619 java_lang_ref_Reference::set_next(clone, NULL); 620 } 621 622 JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle)) 623 JVMWrapper("JVM_Clone"); 624 Handle obj(THREAD, JNIHandles::resolve_non_null(handle)); 625 const KlassHandle klass (THREAD, obj->klass()); 626 JvmtiVMObjectAllocEventCollector oam; 627 628 #ifdef ASSERT 629 // Just checking that the cloneable flag is set correct 630 if (obj->is_array()) { 631 guarantee(klass->is_cloneable(), "all arrays are cloneable"); 632 } else { 633 guarantee(obj->is_instance(), "should be instanceOop"); 634 bool cloneable = klass->is_subtype_of(SystemDictionary::Cloneable_klass()); 635 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag"); 636 } 637 #endif 638 639 // Check if class of obj supports the Cloneable interface. 640 // All arrays are considered to be cloneable (See JLS 20.1.5) 641 if (!klass->is_cloneable()) { 642 ResourceMark rm(THREAD); 643 THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name()); 644 } 645 646 // Make shallow object copy 647 ReferenceType ref_type = REF_NONE; 648 const int size = obj->size(); 649 oop new_obj_oop = NULL; 650 if (obj->is_array()) { 651 const int length = ((arrayOop)obj())->length(); 652 new_obj_oop = CollectedHeap::array_allocate(klass, size, length, CHECK_NULL); 653 } else { 654 ref_type = InstanceKlass::cast(klass())->reference_type(); 655 assert((ref_type == REF_NONE) == 656 !klass->is_subclass_of(SystemDictionary::Reference_klass()), 657 "invariant"); 658 new_obj_oop = CollectedHeap::obj_allocate(klass, size, CHECK_NULL); 659 } 660 661 // 4839641 (4840070): We must do an oop-atomic copy, because if another thread 662 // is modifying a reference field in the clonee, a non-oop-atomic copy might 663 // be suspended in the middle of copying the pointer and end up with parts 664 // of two different pointers in the field. Subsequent dereferences will crash. 665 // 4846409: an oop-copy of objects with long or double fields or arrays of same 666 // won't copy the longs/doubles atomically in 32-bit vm's, so we copy jlongs instead 667 // of oops. We know objects are aligned on a minimum of an jlong boundary. 668 // The same is true of StubRoutines::object_copy and the various oop_copy 669 // variants, and of the code generated by the inline_native_clone intrinsic. 670 assert(MinObjAlignmentInBytes >= BytesPerLong, "objects misaligned"); 671 Copy::conjoint_jlongs_atomic((jlong*)obj(), (jlong*)new_obj_oop, 672 (size_t)align_object_size(size) / HeapWordsPerLong); 673 // Clear the header 674 new_obj_oop->init_mark(); 675 676 // Store check (mark entire object and let gc sort it out) 677 BarrierSet* bs = Universe::heap()->barrier_set(); 678 assert(bs->has_write_region_opt(), "Barrier set does not have write_region"); 679 bs->write_region(MemRegion((HeapWord*)new_obj_oop, size)); 680 681 // If cloning a Reference, set Reference fields to a safe state. 682 // Fixup must be completed before any safepoint. 683 if (ref_type != REF_NONE) { 684 fixup_cloned_reference(ref_type, obj(), new_obj_oop); 685 } 686 687 Handle new_obj(THREAD, new_obj_oop); 688 // Special handling for MemberNames. Since they contain Method* metadata, they 689 // must be registered so that RedefineClasses can fix metadata contained in them. 690 if (java_lang_invoke_MemberName::is_instance(new_obj()) && 691 java_lang_invoke_MemberName::is_method(new_obj())) { 692 Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(new_obj()); 693 // MemberName may be unresolved, so doesn't need registration until resolved. 694 if (method != NULL) { 695 methodHandle m(THREAD, method); 696 // This can safepoint and redefine method, so need both new_obj and method 697 // in a handle, for two different reasons. new_obj can move, method can be 698 // deleted if nothing is using it on the stack. 699 m->method_holder()->add_member_name(new_obj(), false); 700 } 701 } 702 703 // Caution: this involves a java upcall, so the clone should be 704 // "gc-robust" by this stage. 705 if (klass->has_finalizer()) { 706 assert(obj->is_instance(), "should be instanceOop"); 707 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL); 708 new_obj = Handle(THREAD, new_obj_oop); 709 } 710 711 return JNIHandles::make_local(env, new_obj()); 712 JVM_END 713 714 // java.lang.Compiler //////////////////////////////////////////////////// 715 716 // The initial cuts of the HotSpot VM will not support JITs, and all existing 717 // JITs would need extensive changes to work with HotSpot. The JIT-related JVM 718 // functions are all silently ignored unless JVM warnings are printed. 719 720 JVM_LEAF(void, JVM_InitializeCompiler (JNIEnv *env, jclass compCls)) 721 if (PrintJVMWarnings) warning("JVM_InitializeCompiler not supported"); 722 JVM_END 723 724 725 JVM_LEAF(jboolean, JVM_IsSilentCompiler(JNIEnv *env, jclass compCls)) 726 if (PrintJVMWarnings) warning("JVM_IsSilentCompiler not supported"); 727 return JNI_FALSE; 728 JVM_END 729 730 731 JVM_LEAF(jboolean, JVM_CompileClass(JNIEnv *env, jclass compCls, jclass cls)) 732 if (PrintJVMWarnings) warning("JVM_CompileClass not supported"); 733 return JNI_FALSE; 734 JVM_END 735 736 737 JVM_LEAF(jboolean, JVM_CompileClasses(JNIEnv *env, jclass cls, jstring jname)) 738 if (PrintJVMWarnings) warning("JVM_CompileClasses not supported"); 739 return JNI_FALSE; 740 JVM_END 741 742 743 JVM_LEAF(jobject, JVM_CompilerCommand(JNIEnv *env, jclass compCls, jobject arg)) 744 if (PrintJVMWarnings) warning("JVM_CompilerCommand not supported"); 745 return NULL; 746 JVM_END 747 748 749 JVM_LEAF(void, JVM_EnableCompiler(JNIEnv *env, jclass compCls)) 750 if (PrintJVMWarnings) warning("JVM_EnableCompiler not supported"); 751 JVM_END 752 753 754 JVM_LEAF(void, JVM_DisableCompiler(JNIEnv *env, jclass compCls)) 755 if (PrintJVMWarnings) warning("JVM_DisableCompiler not supported"); 756 JVM_END 757 758 759 760 // Error message support ////////////////////////////////////////////////////// 761 762 JVM_LEAF(jint, JVM_GetLastErrorString(char *buf, int len)) 763 JVMWrapper("JVM_GetLastErrorString"); 764 return (jint)os::lasterror(buf, len); 765 JVM_END 766 767 768 // java.io.File /////////////////////////////////////////////////////////////// 769 770 JVM_LEAF(char*, JVM_NativePath(char* path)) 771 JVMWrapper2("JVM_NativePath (%s)", path); 772 return os::native_path(path); 773 JVM_END 774 775 776 // java.nio.Bits /////////////////////////////////////////////////////////////// 777 778 #define MAX_OBJECT_SIZE \ 779 ( arrayOopDesc::header_size(T_DOUBLE) * HeapWordSize \ 780 + ((julong)max_jint * sizeof(double)) ) 781 782 static inline jlong field_offset_to_byte_offset(jlong field_offset) { 783 return field_offset; 784 } 785 786 static inline void assert_field_offset_sane(oop p, jlong field_offset) { 787 #ifdef ASSERT 788 jlong byte_offset = field_offset_to_byte_offset(field_offset); 789 790 if (p != NULL) { 791 assert(byte_offset >= 0 && byte_offset <= (jlong)MAX_OBJECT_SIZE, "sane offset"); 792 if (byte_offset == (jint)byte_offset) { 793 void* ptr_plus_disp = (address)p + byte_offset; 794 assert((void*)p->obj_field_addr<oop>((jint)byte_offset) == ptr_plus_disp, 795 "raw [ptr+disp] must be consistent with oop::field_base"); 796 } 797 jlong p_size = HeapWordSize * (jlong)(p->size()); 798 assert(byte_offset < p_size, err_msg("Unsafe access: offset " INT64_FORMAT 799 " > object's size " INT64_FORMAT, 800 (int64_t)byte_offset, (int64_t)p_size)); 801 } 802 #endif 803 } 804 805 static inline void* index_oop_from_field_offset_long(oop p, jlong field_offset) { 806 assert_field_offset_sane(p, field_offset); 807 jlong byte_offset = field_offset_to_byte_offset(field_offset); 808 809 if (sizeof(char*) == sizeof(jint)) { // (this constant folds!) 810 return (address)p + (jint) byte_offset; 811 } else { 812 return (address)p + byte_offset; 813 } 814 } 815 816 // This function is a leaf since if the source and destination are both in native memory 817 // the copy may potentially be very large, and we don't want to disable GC if we can avoid it. 818 // If either source or destination (or both) are on the heap, the function will enter VM using 819 // JVM_ENTRY_FROM_LEAF 820 JVM_LEAF(void, JVM_CopySwapMemory(JNIEnv *env, jobject srcObj, jlong srcOffset, 821 jobject dstObj, jlong dstOffset, jlong size, 822 jlong elemSize)) { 823 824 size_t sz = (size_t)size; 825 size_t esz = (size_t)elemSize; 826 827 if (srcObj == NULL && dstObj == NULL) { 828 // Both src & dst are in native memory 829 address src = (address)srcOffset; 830 address dst = (address)dstOffset; 831 832 Copy::conjoint_swap(src, dst, sz, esz); 833 } else { 834 // At least one of src/dst are on heap, transition to VM to access raw pointers 835 836 JVM_ENTRY_FROM_LEAF(env, void, JVM_CopySwapMemory) { 837 oop srcp = JNIHandles::resolve(srcObj); 838 oop dstp = JNIHandles::resolve(dstObj); 839 840 address src = (address)index_oop_from_field_offset_long(srcp, srcOffset); 841 address dst = (address)index_oop_from_field_offset_long(dstp, dstOffset); 842 843 Copy::conjoint_swap(src, dst, sz, esz); 844 } JVM_END 845 } 846 } JVM_END 847 848 849 // Misc. class handling /////////////////////////////////////////////////////////// 850 851 852 JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env, int depth)) 853 JVMWrapper("JVM_GetCallerClass"); 854 855 // Pre-JDK 8 and early builds of JDK 8 don't have a CallerSensitive annotation; or 856 // sun.reflect.Reflection.getCallerClass with a depth parameter is provided 857 // temporarily for existing code to use until a replacement API is defined. 858 if (SystemDictionary::reflect_CallerSensitive_klass() == NULL || depth != JVM_CALLER_DEPTH) { 859 Klass* k = thread->security_get_caller_class(depth); 860 return (k == NULL) ? NULL : (jclass) JNIHandles::make_local(env, k->java_mirror()); 861 } 862 863 // Getting the class of the caller frame. 864 // 865 // The call stack at this point looks something like this: 866 // 867 // [0] [ @CallerSensitive public sun.reflect.Reflection.getCallerClass ] 868 // [1] [ @CallerSensitive API.method ] 869 // [.] [ (skipped intermediate frames) ] 870 // [n] [ caller ] 871 vframeStream vfst(thread); 872 // Cf. LibraryCallKit::inline_native_Reflection_getCallerClass 873 for (int n = 0; !vfst.at_end(); vfst.security_next(), n++) { 874 Method* m = vfst.method(); 875 assert(m != NULL, "sanity"); 876 switch (n) { 877 case 0: 878 // This must only be called from Reflection.getCallerClass 879 if (m->intrinsic_id() != vmIntrinsics::_getCallerClass) { 880 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetCallerClass must only be called from Reflection.getCallerClass"); 881 } 882 // fall-through 883 case 1: 884 // Frame 0 and 1 must be caller sensitive. 885 if (!m->caller_sensitive()) { 886 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), err_msg("CallerSensitive annotation expected at frame %d", n)); 887 } 888 break; 889 default: 890 if (!m->is_ignored_by_security_stack_walk()) { 891 // We have reached the desired frame; return the holder class. 892 return (jclass) JNIHandles::make_local(env, m->method_holder()->java_mirror()); 893 } 894 break; 895 } 896 } 897 return NULL; 898 JVM_END 899 900 901 JVM_ENTRY(jclass, JVM_FindPrimitiveClass(JNIEnv* env, const char* utf)) 902 JVMWrapper("JVM_FindPrimitiveClass"); 903 oop mirror = NULL; 904 BasicType t = name2type(utf); 905 if (t != T_ILLEGAL && t != T_OBJECT && t != T_ARRAY) { 906 mirror = Universe::java_mirror(t); 907 } 908 if (mirror == NULL) { 909 THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), (char*) utf); 910 } else { 911 return (jclass) JNIHandles::make_local(env, mirror); 912 } 913 JVM_END 914 915 916 JVM_ENTRY(void, JVM_ResolveClass(JNIEnv* env, jclass cls)) 917 JVMWrapper("JVM_ResolveClass"); 918 if (PrintJVMWarnings) warning("JVM_ResolveClass not implemented"); 919 JVM_END 920 921 922 JVM_ENTRY(jboolean, JVM_KnownToNotExist(JNIEnv *env, jobject loader, const char *classname)) 923 JVMWrapper("JVM_KnownToNotExist"); 924 #if INCLUDE_CDS 925 return ClassLoaderExt::known_to_not_exist(env, loader, classname, CHECK_(false)); 926 #else 927 return false; 928 #endif 929 JVM_END 930 931 932 JVM_ENTRY(jobjectArray, JVM_GetResourceLookupCacheURLs(JNIEnv *env, jobject loader)) 933 JVMWrapper("JVM_GetResourceLookupCacheURLs"); 934 #if INCLUDE_CDS 935 return ClassLoaderExt::get_lookup_cache_urls(env, loader, CHECK_NULL); 936 #else 937 return NULL; 938 #endif 939 JVM_END 940 941 942 JVM_ENTRY(jintArray, JVM_GetResourceLookupCache(JNIEnv *env, jobject loader, const char *resource_name)) 943 JVMWrapper("JVM_GetResourceLookupCache"); 944 #if INCLUDE_CDS 945 return ClassLoaderExt::get_lookup_cache(env, loader, resource_name, CHECK_NULL); 946 #else 947 return NULL; 948 #endif 949 JVM_END 950 951 952 // Returns a class loaded by the bootstrap class loader; or null 953 // if not found. ClassNotFoundException is not thrown. 954 // 955 // Rationale behind JVM_FindClassFromBootLoader 956 // a> JVM_FindClassFromClassLoader was never exported in the export tables. 957 // b> because of (a) java.dll has a direct dependecy on the unexported 958 // private symbol "_JVM_FindClassFromClassLoader@20". 959 // c> the launcher cannot use the private symbol as it dynamically opens 960 // the entry point, so if something changes, the launcher will fail 961 // unexpectedly at runtime, it is safest for the launcher to dlopen a 962 // stable exported interface. 963 // d> re-exporting JVM_FindClassFromClassLoader as public, will cause its 964 // signature to change from _JVM_FindClassFromClassLoader@20 to 965 // JVM_FindClassFromClassLoader and will not be backward compatible 966 // with older JDKs. 967 // Thus a public/stable exported entry point is the right solution, 968 // public here means public in linker semantics, and is exported only 969 // to the JDK, and is not intended to be a public API. 970 971 JVM_ENTRY(jclass, JVM_FindClassFromBootLoader(JNIEnv* env, 972 const char* name)) 973 JVMWrapper2("JVM_FindClassFromBootLoader %s", name); 974 975 // Java libraries should ensure that name is never null... 976 if (name == NULL || (int)strlen(name) > Symbol::max_length()) { 977 // It's impossible to create this class; the name cannot fit 978 // into the constant pool. 979 return NULL; 980 } 981 982 TempNewSymbol h_name = SymbolTable::new_symbol(name, CHECK_NULL); 983 Klass* k = SystemDictionary::resolve_or_null(h_name, CHECK_NULL); 984 if (k == NULL) { 985 return NULL; 986 } 987 988 if (TraceClassResolution) { 989 trace_class_resolution(k); 990 } 991 return (jclass) JNIHandles::make_local(env, k->java_mirror()); 992 JVM_END 993 994 // Not used; JVM_FindClassFromCaller replaces this. 995 JVM_ENTRY(jclass, JVM_FindClassFromClassLoader(JNIEnv* env, const char* name, 996 jboolean init, jobject loader, 997 jboolean throwError)) 998 JVMWrapper3("JVM_FindClassFromClassLoader %s throw %s", name, 999 throwError ? "error" : "exception"); 1000 // Java libraries should ensure that name is never null... 1001 if (name == NULL || (int)strlen(name) > Symbol::max_length()) { 1002 // It's impossible to create this class; the name cannot fit 1003 // into the constant pool. 1004 if (throwError) { 1005 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name); 1006 } else { 1007 THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), name); 1008 } 1009 } 1010 TempNewSymbol h_name = SymbolTable::new_symbol(name, CHECK_NULL); 1011 Handle h_loader(THREAD, JNIHandles::resolve(loader)); 1012 jclass result = find_class_from_class_loader(env, h_name, init, h_loader, 1013 Handle(), throwError, THREAD); 1014 1015 if (TraceClassResolution && result != NULL) { 1016 trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result))); 1017 } 1018 return result; 1019 JVM_END 1020 1021 // Find a class with this name in this loader, using the caller's protection domain. 1022 JVM_ENTRY(jclass, JVM_FindClassFromCaller(JNIEnv* env, const char* name, 1023 jboolean init, jobject loader, 1024 jclass caller)) 1025 JVMWrapper2("JVM_FindClassFromCaller %s throws ClassNotFoundException", name); 1026 // Java libraries should ensure that name is never null... 1027 if (name == NULL || (int)strlen(name) > Symbol::max_length()) { 1028 // It's impossible to create this class; the name cannot fit 1029 // into the constant pool. 1030 THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), name); 1031 } 1032 1033 TempNewSymbol h_name = SymbolTable::new_symbol(name, CHECK_NULL); 1034 1035 oop loader_oop = JNIHandles::resolve(loader); 1036 oop from_class = JNIHandles::resolve(caller); 1037 oop protection_domain = NULL; 1038 // If loader is null, shouldn't call ClassLoader.checkPackageAccess; otherwise get 1039 // NPE. Put it in another way, the bootstrap class loader has all permission and 1040 // thus no checkPackageAccess equivalence in the VM class loader. 1041 // The caller is also passed as NULL by the java code if there is no security 1042 // manager to avoid the performance cost of getting the calling class. 1043 if (from_class != NULL && loader_oop != NULL) { 1044 protection_domain = java_lang_Class::as_Klass(from_class)->protection_domain(); 1045 } 1046 1047 Handle h_loader(THREAD, loader_oop); 1048 Handle h_prot(THREAD, protection_domain); 1049 jclass result = find_class_from_class_loader(env, h_name, init, h_loader, 1050 h_prot, false, THREAD); 1051 1052 if (TraceClassResolution && result != NULL) { 1053 trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result))); 1054 } 1055 return result; 1056 JVM_END 1057 1058 JVM_ENTRY(jclass, JVM_FindClassFromClass(JNIEnv *env, const char *name, 1059 jboolean init, jclass from)) 1060 JVMWrapper2("JVM_FindClassFromClass %s", name); 1061 if (name == NULL || (int)strlen(name) > Symbol::max_length()) { 1062 // It's impossible to create this class; the name cannot fit 1063 // into the constant pool. 1064 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name); 1065 } 1066 TempNewSymbol h_name = SymbolTable::new_symbol(name, CHECK_NULL); 1067 oop from_class_oop = JNIHandles::resolve(from); 1068 Klass* from_class = (from_class_oop == NULL) 1069 ? (Klass*)NULL 1070 : java_lang_Class::as_Klass(from_class_oop); 1071 oop class_loader = NULL; 1072 oop protection_domain = NULL; 1073 if (from_class != NULL) { 1074 class_loader = from_class->class_loader(); 1075 protection_domain = from_class->protection_domain(); 1076 } 1077 Handle h_loader(THREAD, class_loader); 1078 Handle h_prot (THREAD, protection_domain); 1079 jclass result = find_class_from_class_loader(env, h_name, init, h_loader, 1080 h_prot, true, thread); 1081 1082 if (TraceClassResolution && result != NULL) { 1083 // this function is generally only used for class loading during verification. 1084 ResourceMark rm; 1085 oop from_mirror = JNIHandles::resolve_non_null(from); 1086 Klass* from_class = java_lang_Class::as_Klass(from_mirror); 1087 const char * from_name = from_class->external_name(); 1088 1089 oop mirror = JNIHandles::resolve_non_null(result); 1090 Klass* to_class = java_lang_Class::as_Klass(mirror); 1091 const char * to = to_class->external_name(); 1092 tty->print("RESOLVE %s %s (verification)\n", from_name, to); 1093 } 1094 1095 return result; 1096 JVM_END 1097 1098 static void is_lock_held_by_thread(Handle loader, PerfCounter* counter, TRAPS) { 1099 if (loader.is_null()) { 1100 return; 1101 } 1102 1103 // check whether the current caller thread holds the lock or not. 1104 // If not, increment the corresponding counter 1105 if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader) != 1106 ObjectSynchronizer::owner_self) { 1107 counter->inc(); 1108 } 1109 } 1110 1111 // common code for JVM_DefineClass() and JVM_DefineClassWithSource() 1112 // and JVM_DefineClassWithSourceCond() 1113 static jclass jvm_define_class_common(JNIEnv *env, const char *name, 1114 jobject loader, const jbyte *buf, 1115 jsize len, jobject pd, const char *source, 1116 jboolean verify, TRAPS) { 1117 if (source == NULL) source = "__JVM_DefineClass__"; 1118 1119 assert(THREAD->is_Java_thread(), "must be a JavaThread"); 1120 JavaThread* jt = (JavaThread*) THREAD; 1121 1122 PerfClassTraceTime vmtimer(ClassLoader::perf_define_appclass_time(), 1123 ClassLoader::perf_define_appclass_selftime(), 1124 ClassLoader::perf_define_appclasses(), 1125 jt->get_thread_stat()->perf_recursion_counts_addr(), 1126 jt->get_thread_stat()->perf_timers_addr(), 1127 PerfClassTraceTime::DEFINE_CLASS); 1128 1129 if (UsePerfData) { 1130 ClassLoader::perf_app_classfile_bytes_read()->inc(len); 1131 } 1132 1133 // Since exceptions can be thrown, class initialization can take place 1134 // if name is NULL no check for class name in .class stream has to be made. 1135 TempNewSymbol class_name = NULL; 1136 if (name != NULL) { 1137 const int str_len = (int)strlen(name); 1138 if (str_len > Symbol::max_length()) { 1139 // It's impossible to create this class; the name cannot fit 1140 // into the constant pool. 1141 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name); 1142 } 1143 class_name = SymbolTable::new_symbol(name, str_len, CHECK_NULL); 1144 } 1145 1146 ResourceMark rm(THREAD); 1147 ClassFileStream st((u1*) buf, len, (char *)source); 1148 Handle class_loader (THREAD, JNIHandles::resolve(loader)); 1149 if (UsePerfData) { 1150 is_lock_held_by_thread(class_loader, 1151 ClassLoader::sync_JVMDefineClassLockFreeCounter(), 1152 THREAD); 1153 } 1154 Handle protection_domain (THREAD, JNIHandles::resolve(pd)); 1155 Klass* k = SystemDictionary::resolve_from_stream(class_name, class_loader, 1156 protection_domain, &st, 1157 verify != 0, 1158 CHECK_NULL); 1159 1160 if (TraceClassResolution && k != NULL) { 1161 trace_class_resolution(k); 1162 } 1163 1164 return (jclass) JNIHandles::make_local(env, k->java_mirror()); 1165 } 1166 1167 1168 JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd)) 1169 JVMWrapper2("JVM_DefineClass %s", name); 1170 1171 return jvm_define_class_common(env, name, loader, buf, len, pd, NULL, true, THREAD); 1172 JVM_END 1173 1174 1175 JVM_ENTRY(jclass, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source)) 1176 JVMWrapper2("JVM_DefineClassWithSource %s", name); 1177 1178 return jvm_define_class_common(env, name, loader, buf, len, pd, source, true, THREAD); 1179 JVM_END 1180 1181 JVM_ENTRY(jclass, JVM_DefineClassWithSourceCond(JNIEnv *env, const char *name, 1182 jobject loader, const jbyte *buf, 1183 jsize len, jobject pd, 1184 const char *source, jboolean verify)) 1185 JVMWrapper2("JVM_DefineClassWithSourceCond %s", name); 1186 1187 return jvm_define_class_common(env, name, loader, buf, len, pd, source, verify, THREAD); 1188 JVM_END 1189 1190 JVM_ENTRY(jclass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name)) 1191 JVMWrapper("JVM_FindLoadedClass"); 1192 ResourceMark rm(THREAD); 1193 1194 Handle h_name (THREAD, JNIHandles::resolve_non_null(name)); 1195 Handle string = java_lang_String::internalize_classname(h_name, CHECK_NULL); 1196 1197 const char* str = java_lang_String::as_utf8_string(string()); 1198 // Sanity check, don't expect null 1199 if (str == NULL) return NULL; 1200 1201 const int str_len = (int)strlen(str); 1202 if (str_len > Symbol::max_length()) { 1203 // It's impossible to create this class; the name cannot fit 1204 // into the constant pool. 1205 return NULL; 1206 } 1207 TempNewSymbol klass_name = SymbolTable::new_symbol(str, str_len, CHECK_NULL); 1208 1209 // Security Note: 1210 // The Java level wrapper will perform the necessary security check allowing 1211 // us to pass the NULL as the initiating class loader. 1212 Handle h_loader(THREAD, JNIHandles::resolve(loader)); 1213 if (UsePerfData) { 1214 is_lock_held_by_thread(h_loader, 1215 ClassLoader::sync_JVMFindLoadedClassLockFreeCounter(), 1216 THREAD); 1217 } 1218 1219 Klass* k = SystemDictionary::find_instance_or_array_klass(klass_name, 1220 h_loader, 1221 Handle(), 1222 CHECK_NULL); 1223 #if INCLUDE_CDS 1224 if (k == NULL) { 1225 // If the class is not already loaded, try to see if it's in the shared 1226 // archive for the current classloader (h_loader). 1227 instanceKlassHandle ik = SystemDictionaryShared::find_or_load_shared_class( 1228 klass_name, h_loader, CHECK_NULL); 1229 k = ik(); 1230 } 1231 #endif 1232 return (k == NULL) ? NULL : 1233 (jclass) JNIHandles::make_local(env, k->java_mirror()); 1234 JVM_END 1235 1236 1237 // Reflection support ////////////////////////////////////////////////////////////////////////////// 1238 1239 JVM_ENTRY(jstring, JVM_GetClassName(JNIEnv *env, jclass cls)) 1240 assert (cls != NULL, "illegal class"); 1241 JVMWrapper("JVM_GetClassName"); 1242 JvmtiVMObjectAllocEventCollector oam; 1243 ResourceMark rm(THREAD); 1244 const char* name; 1245 if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { 1246 name = type2name(java_lang_Class::primitive_type(JNIHandles::resolve(cls))); 1247 } else { 1248 // Consider caching interned string in Klass 1249 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); 1250 assert(k->is_klass(), "just checking"); 1251 name = k->external_name(); 1252 } 1253 oop result = StringTable::intern((char*) name, CHECK_NULL); 1254 return (jstring) JNIHandles::make_local(env, result); 1255 JVM_END 1256 1257 1258 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls)) 1259 JVMWrapper("JVM_GetClassInterfaces"); 1260 JvmtiVMObjectAllocEventCollector oam; 1261 oop mirror = JNIHandles::resolve_non_null(cls); 1262 1263 // Special handling for primitive objects 1264 if (java_lang_Class::is_primitive(mirror)) { 1265 // Primitive objects does not have any interfaces 1266 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL); 1267 return (jobjectArray) JNIHandles::make_local(env, r); 1268 } 1269 1270 KlassHandle klass(thread, java_lang_Class::as_Klass(mirror)); 1271 // Figure size of result array 1272 int size; 1273 if (klass->oop_is_instance()) { 1274 size = InstanceKlass::cast(klass())->local_interfaces()->length(); 1275 } else { 1276 assert(klass->oop_is_objArray() || klass->oop_is_typeArray(), "Illegal mirror klass"); 1277 size = 2; 1278 } 1279 1280 // Allocate result array 1281 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), size, CHECK_NULL); 1282 objArrayHandle result (THREAD, r); 1283 // Fill in result 1284 if (klass->oop_is_instance()) { 1285 // Regular instance klass, fill in all local interfaces 1286 for (int index = 0; index < size; index++) { 1287 Klass* k = InstanceKlass::cast(klass())->local_interfaces()->at(index); 1288 result->obj_at_put(index, k->java_mirror()); 1289 } 1290 } else { 1291 // All arrays implement java.lang.Cloneable and java.io.Serializable 1292 result->obj_at_put(0, SystemDictionary::Cloneable_klass()->java_mirror()); 1293 result->obj_at_put(1, SystemDictionary::Serializable_klass()->java_mirror()); 1294 } 1295 return (jobjectArray) JNIHandles::make_local(env, result()); 1296 JVM_END 1297 1298 1299 JVM_ENTRY(jobject, JVM_GetClassLoader(JNIEnv *env, jclass cls)) 1300 JVMWrapper("JVM_GetClassLoader"); 1301 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { 1302 return NULL; 1303 } 1304 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 1305 oop loader = k->class_loader(); 1306 return JNIHandles::make_local(env, loader); 1307 JVM_END 1308 1309 1310 JVM_QUICK_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls)) 1311 JVMWrapper("JVM_IsInterface"); 1312 oop mirror = JNIHandles::resolve_non_null(cls); 1313 if (java_lang_Class::is_primitive(mirror)) { 1314 return JNI_FALSE; 1315 } 1316 Klass* k = java_lang_Class::as_Klass(mirror); 1317 jboolean result = k->is_interface(); 1318 assert(!result || k->oop_is_instance(), 1319 "all interfaces are instance types"); 1320 // The compiler intrinsic for isInterface tests the 1321 // Klass::_access_flags bits in the same way. 1322 return result; 1323 JVM_END 1324 1325 1326 JVM_ENTRY(jobjectArray, JVM_GetClassSigners(JNIEnv *env, jclass cls)) 1327 JVMWrapper("JVM_GetClassSigners"); 1328 JvmtiVMObjectAllocEventCollector oam; 1329 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { 1330 // There are no signers for primitive types 1331 return NULL; 1332 } 1333 1334 objArrayOop signers = java_lang_Class::signers(JNIHandles::resolve_non_null(cls)); 1335 1336 // If there are no signers set in the class, or if the class 1337 // is an array, return NULL. 1338 if (signers == NULL) return NULL; 1339 1340 // copy of the signers array 1341 Klass* element = ObjArrayKlass::cast(signers->klass())->element_klass(); 1342 objArrayOop signers_copy = oopFactory::new_objArray(element, signers->length(), CHECK_NULL); 1343 for (int index = 0; index < signers->length(); index++) { 1344 signers_copy->obj_at_put(index, signers->obj_at(index)); 1345 } 1346 1347 // return the copy 1348 return (jobjectArray) JNIHandles::make_local(env, signers_copy); 1349 JVM_END 1350 1351 1352 JVM_ENTRY(void, JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers)) 1353 JVMWrapper("JVM_SetClassSigners"); 1354 if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { 1355 // This call is ignored for primitive types and arrays. 1356 // Signers are only set once, ClassLoader.java, and thus shouldn't 1357 // be called with an array. Only the bootstrap loader creates arrays. 1358 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 1359 if (k->oop_is_instance()) { 1360 java_lang_Class::set_signers(k->java_mirror(), objArrayOop(JNIHandles::resolve(signers))); 1361 } 1362 } 1363 JVM_END 1364 1365 1366 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls)) 1367 JVMWrapper("JVM_GetProtectionDomain"); 1368 if (JNIHandles::resolve(cls) == NULL) { 1369 THROW_(vmSymbols::java_lang_NullPointerException(), NULL); 1370 } 1371 1372 if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { 1373 // Primitive types does not have a protection domain. 1374 return NULL; 1375 } 1376 1377 oop pd = java_lang_Class::protection_domain(JNIHandles::resolve(cls)); 1378 return (jobject) JNIHandles::make_local(env, pd); 1379 JVM_END 1380 1381 1382 static bool is_authorized(Handle context, instanceKlassHandle klass, TRAPS) { 1383 // If there is a security manager and protection domain, check the access 1384 // in the protection domain, otherwise it is authorized. 1385 if (java_lang_System::has_security_manager()) { 1386 1387 // For bootstrapping, if pd implies method isn't in the JDK, allow 1388 // this context to revert to older behavior. 1389 // In this case the isAuthorized field in AccessControlContext is also not 1390 // present. 1391 if (Universe::protection_domain_implies_method() == NULL) { 1392 return true; 1393 } 1394 1395 // Whitelist certain access control contexts 1396 if (java_security_AccessControlContext::is_authorized(context)) { 1397 return true; 1398 } 1399 1400 oop prot = klass->protection_domain(); 1401 if (prot != NULL) { 1402 // Call pd.implies(new SecurityPermission("createAccessControlContext")) 1403 // in the new wrapper. 1404 methodHandle m(THREAD, Universe::protection_domain_implies_method()); 1405 Handle h_prot(THREAD, prot); 1406 JavaValue result(T_BOOLEAN); 1407 JavaCallArguments args(h_prot); 1408 JavaCalls::call(&result, m, &args, CHECK_false); 1409 return (result.get_jboolean() != 0); 1410 } 1411 } 1412 return true; 1413 } 1414 1415 // Create an AccessControlContext with a protection domain with null codesource 1416 // and null permissions - which gives no permissions. 1417 oop create_dummy_access_control_context(TRAPS) { 1418 InstanceKlass* pd_klass = InstanceKlass::cast(SystemDictionary::ProtectionDomain_klass()); 1419 Handle obj = pd_klass->allocate_instance_handle(CHECK_NULL); 1420 // Call constructor ProtectionDomain(null, null); 1421 JavaValue result(T_VOID); 1422 JavaCalls::call_special(&result, obj, KlassHandle(THREAD, pd_klass), 1423 vmSymbols::object_initializer_name(), 1424 vmSymbols::codesource_permissioncollection_signature(), 1425 Handle(), Handle(), CHECK_NULL); 1426 1427 // new ProtectionDomain[] {pd}; 1428 objArrayOop context = oopFactory::new_objArray(pd_klass, 1, CHECK_NULL); 1429 context->obj_at_put(0, obj()); 1430 1431 // new AccessControlContext(new ProtectionDomain[] {pd}) 1432 objArrayHandle h_context(THREAD, context); 1433 oop acc = java_security_AccessControlContext::create(h_context, false, Handle(), CHECK_NULL); 1434 return acc; 1435 } 1436 1437 JVM_ENTRY(jobject, JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject action, jobject context, jboolean wrapException)) 1438 JVMWrapper("JVM_DoPrivileged"); 1439 1440 if (action == NULL) { 1441 THROW_MSG_0(vmSymbols::java_lang_NullPointerException(), "Null action"); 1442 } 1443 1444 // Compute the frame initiating the do privileged operation and setup the privileged stack 1445 vframeStream vfst(thread); 1446 vfst.security_get_caller_frame(1); 1447 1448 if (vfst.at_end()) { 1449 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "no caller?"); 1450 } 1451 1452 Method* method = vfst.method(); 1453 instanceKlassHandle klass (THREAD, method->method_holder()); 1454 1455 // Check that action object understands "Object run()" 1456 Handle h_context; 1457 if (context != NULL) { 1458 h_context = Handle(THREAD, JNIHandles::resolve(context)); 1459 bool authorized = is_authorized(h_context, klass, CHECK_NULL); 1460 if (!authorized) { 1461 // Create an unprivileged access control object and call it's run function 1462 // instead. 1463 oop noprivs = create_dummy_access_control_context(CHECK_NULL); 1464 h_context = Handle(THREAD, noprivs); 1465 } 1466 } 1467 1468 // Check that action object understands "Object run()" 1469 Handle object (THREAD, JNIHandles::resolve(action)); 1470 1471 // get run() method 1472 Method* m_oop = object->klass()->uncached_lookup_method( 1473 vmSymbols::run_method_name(), 1474 vmSymbols::void_object_signature(), 1475 Klass::find_overpass); 1476 methodHandle m (THREAD, m_oop); 1477 if (m.is_null() || !m->is_method() || !m()->is_public() || m()->is_static()) { 1478 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "No run method"); 1479 } 1480 1481 // Stack allocated list of privileged stack elements 1482 PrivilegedElement pi; 1483 if (!vfst.at_end()) { 1484 pi.initialize(&vfst, h_context(), thread->privileged_stack_top(), CHECK_NULL); 1485 thread->set_privileged_stack_top(&pi); 1486 } 1487 1488 1489 // invoke the Object run() in the action object. We cannot use call_interface here, since the static type 1490 // is not really known - it is either java.security.PrivilegedAction or java.security.PrivilegedExceptionAction 1491 Handle pending_exception; 1492 JavaValue result(T_OBJECT); 1493 JavaCallArguments args(object); 1494 JavaCalls::call(&result, m, &args, THREAD); 1495 1496 // done with action, remove ourselves from the list 1497 if (!vfst.at_end()) { 1498 assert(thread->privileged_stack_top() != NULL && thread->privileged_stack_top() == &pi, "wrong top element"); 1499 thread->set_privileged_stack_top(thread->privileged_stack_top()->next()); 1500 } 1501 1502 if (HAS_PENDING_EXCEPTION) { 1503 pending_exception = Handle(THREAD, PENDING_EXCEPTION); 1504 CLEAR_PENDING_EXCEPTION; 1505 // JVMTI has already reported the pending exception 1506 // JVMTI internal flag reset is needed in order to report PrivilegedActionException 1507 if (THREAD->is_Java_thread()) { 1508 JvmtiExport::clear_detected_exception((JavaThread*) THREAD); 1509 } 1510 if ( pending_exception->is_a(SystemDictionary::Exception_klass()) && 1511 !pending_exception->is_a(SystemDictionary::RuntimeException_klass())) { 1512 // Throw a java.security.PrivilegedActionException(Exception e) exception 1513 JavaCallArguments args(pending_exception); 1514 THROW_ARG_0(vmSymbols::java_security_PrivilegedActionException(), 1515 vmSymbols::exception_void_signature(), 1516 &args); 1517 } 1518 } 1519 1520 if (pending_exception.not_null()) THROW_OOP_0(pending_exception()); 1521 return JNIHandles::make_local(env, (oop) result.get_jobject()); 1522 JVM_END 1523 1524 1525 // Returns the inherited_access_control_context field of the running thread. 1526 JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls)) 1527 JVMWrapper("JVM_GetInheritedAccessControlContext"); 1528 oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj()); 1529 return JNIHandles::make_local(env, result); 1530 JVM_END 1531 1532 class RegisterArrayForGC { 1533 private: 1534 JavaThread *_thread; 1535 public: 1536 RegisterArrayForGC(JavaThread *thread, GrowableArray<oop>* array) { 1537 _thread = thread; 1538 _thread->register_array_for_gc(array); 1539 } 1540 1541 ~RegisterArrayForGC() { 1542 _thread->register_array_for_gc(NULL); 1543 } 1544 }; 1545 1546 1547 JVM_ENTRY(jobject, JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls)) 1548 JVMWrapper("JVM_GetStackAccessControlContext"); 1549 if (!UsePrivilegedStack) return NULL; 1550 1551 ResourceMark rm(THREAD); 1552 GrowableArray<oop>* local_array = new GrowableArray<oop>(12); 1553 JvmtiVMObjectAllocEventCollector oam; 1554 1555 // count the protection domains on the execution stack. We collapse 1556 // duplicate consecutive protection domains into a single one, as 1557 // well as stopping when we hit a privileged frame. 1558 1559 // Use vframeStream to iterate through Java frames 1560 vframeStream vfst(thread); 1561 1562 oop previous_protection_domain = NULL; 1563 Handle privileged_context(thread, NULL); 1564 bool is_privileged = false; 1565 oop protection_domain = NULL; 1566 1567 for(; !vfst.at_end(); vfst.next()) { 1568 // get method of frame 1569 Method* method = vfst.method(); 1570 intptr_t* frame_id = vfst.frame_id(); 1571 1572 // check the privileged frames to see if we have a match 1573 if (thread->privileged_stack_top() && thread->privileged_stack_top()->frame_id() == frame_id) { 1574 // this frame is privileged 1575 is_privileged = true; 1576 privileged_context = Handle(thread, thread->privileged_stack_top()->privileged_context()); 1577 protection_domain = thread->privileged_stack_top()->protection_domain(); 1578 } else { 1579 protection_domain = method->method_holder()->protection_domain(); 1580 } 1581 1582 if ((previous_protection_domain != protection_domain) && (protection_domain != NULL)) { 1583 local_array->push(protection_domain); 1584 previous_protection_domain = protection_domain; 1585 } 1586 1587 if (is_privileged) break; 1588 } 1589 1590 1591 // either all the domains on the stack were system domains, or 1592 // we had a privileged system domain 1593 if (local_array->is_empty()) { 1594 if (is_privileged && privileged_context.is_null()) return NULL; 1595 1596 oop result = java_security_AccessControlContext::create(objArrayHandle(), is_privileged, privileged_context, CHECK_NULL); 1597 return JNIHandles::make_local(env, result); 1598 } 1599 1600 // the resource area must be registered in case of a gc 1601 RegisterArrayForGC ragc(thread, local_array); 1602 objArrayOop context = oopFactory::new_objArray(SystemDictionary::ProtectionDomain_klass(), 1603 local_array->length(), CHECK_NULL); 1604 objArrayHandle h_context(thread, context); 1605 for (int index = 0; index < local_array->length(); index++) { 1606 h_context->obj_at_put(index, local_array->at(index)); 1607 } 1608 1609 oop result = java_security_AccessControlContext::create(h_context, is_privileged, privileged_context, CHECK_NULL); 1610 1611 return JNIHandles::make_local(env, result); 1612 JVM_END 1613 1614 1615 JVM_QUICK_ENTRY(jboolean, JVM_IsArrayClass(JNIEnv *env, jclass cls)) 1616 JVMWrapper("JVM_IsArrayClass"); 1617 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 1618 return (k != NULL) && k->oop_is_array() ? true : false; 1619 JVM_END 1620 1621 1622 JVM_QUICK_ENTRY(jboolean, JVM_IsPrimitiveClass(JNIEnv *env, jclass cls)) 1623 JVMWrapper("JVM_IsPrimitiveClass"); 1624 oop mirror = JNIHandles::resolve_non_null(cls); 1625 return (jboolean) java_lang_Class::is_primitive(mirror); 1626 JVM_END 1627 1628 1629 JVM_ENTRY(jclass, JVM_GetComponentType(JNIEnv *env, jclass cls)) 1630 JVMWrapper("JVM_GetComponentType"); 1631 oop mirror = JNIHandles::resolve_non_null(cls); 1632 oop result = Reflection::array_component_type(mirror, CHECK_NULL); 1633 return (jclass) JNIHandles::make_local(env, result); 1634 JVM_END 1635 1636 1637 JVM_ENTRY(jint, JVM_GetClassModifiers(JNIEnv *env, jclass cls)) 1638 JVMWrapper("JVM_GetClassModifiers"); 1639 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { 1640 // Primitive type 1641 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC; 1642 } 1643 1644 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 1645 debug_only(int computed_modifiers = k->compute_modifier_flags(CHECK_0)); 1646 assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK"); 1647 return k->modifier_flags(); 1648 JVM_END 1649 1650 1651 // Inner class reflection /////////////////////////////////////////////////////////////////////////////// 1652 1653 JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass)) 1654 JvmtiVMObjectAllocEventCollector oam; 1655 // ofClass is a reference to a java_lang_Class object. The mirror object 1656 // of an InstanceKlass 1657 1658 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) || 1659 ! java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_instance()) { 1660 oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL); 1661 return (jobjectArray)JNIHandles::make_local(env, result); 1662 } 1663 1664 instanceKlassHandle k(thread, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))); 1665 InnerClassesIterator iter(k); 1666 1667 if (iter.length() == 0) { 1668 // Neither an inner nor outer class 1669 oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL); 1670 return (jobjectArray)JNIHandles::make_local(env, result); 1671 } 1672 1673 // find inner class info 1674 constantPoolHandle cp(thread, k->constants()); 1675 int length = iter.length(); 1676 1677 // Allocate temp. result array 1678 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), length/4, CHECK_NULL); 1679 objArrayHandle result (THREAD, r); 1680 int members = 0; 1681 1682 for (; !iter.done(); iter.next()) { 1683 int ioff = iter.inner_class_info_index(); 1684 int ooff = iter.outer_class_info_index(); 1685 1686 if (ioff != 0 && ooff != 0) { 1687 // Check to see if the name matches the class we're looking for 1688 // before attempting to find the class. 1689 if (cp->klass_name_at_matches(k, ooff)) { 1690 Klass* outer_klass = cp->klass_at(ooff, CHECK_NULL); 1691 if (outer_klass == k()) { 1692 Klass* ik = cp->klass_at(ioff, CHECK_NULL); 1693 instanceKlassHandle inner_klass (THREAD, ik); 1694 1695 // Throws an exception if outer klass has not declared k as 1696 // an inner klass 1697 Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL); 1698 1699 result->obj_at_put(members, inner_klass->java_mirror()); 1700 members++; 1701 } 1702 } 1703 } 1704 } 1705 1706 if (members != length) { 1707 // Return array of right length 1708 objArrayOop res = oopFactory::new_objArray(SystemDictionary::Class_klass(), members, CHECK_NULL); 1709 for(int i = 0; i < members; i++) { 1710 res->obj_at_put(i, result->obj_at(i)); 1711 } 1712 return (jobjectArray)JNIHandles::make_local(env, res); 1713 } 1714 1715 return (jobjectArray)JNIHandles::make_local(env, result()); 1716 JVM_END 1717 1718 1719 JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass)) 1720 { 1721 // ofClass is a reference to a java_lang_Class object. 1722 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) || 1723 ! java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_instance()) { 1724 return NULL; 1725 } 1726 1727 bool inner_is_member = false; 1728 Klass* outer_klass 1729 = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)) 1730 )->compute_enclosing_class(&inner_is_member, CHECK_NULL); 1731 if (outer_klass == NULL) return NULL; // already a top-level class 1732 if (!inner_is_member) return NULL; // an anonymous class (inside a method) 1733 return (jclass) JNIHandles::make_local(env, outer_klass->java_mirror()); 1734 } 1735 JVM_END 1736 1737 // should be in InstanceKlass.cpp, but is here for historical reasons 1738 Klass* InstanceKlass::compute_enclosing_class_impl(instanceKlassHandle k, 1739 bool* inner_is_member, 1740 TRAPS) { 1741 Thread* thread = THREAD; 1742 InnerClassesIterator iter(k); 1743 if (iter.length() == 0) { 1744 // No inner class info => no declaring class 1745 return NULL; 1746 } 1747 1748 constantPoolHandle i_cp(thread, k->constants()); 1749 1750 bool found = false; 1751 Klass* ok; 1752 instanceKlassHandle outer_klass; 1753 *inner_is_member = false; 1754 1755 // Find inner_klass attribute 1756 for (; !iter.done() && !found; iter.next()) { 1757 int ioff = iter.inner_class_info_index(); 1758 int ooff = iter.outer_class_info_index(); 1759 int noff = iter.inner_name_index(); 1760 if (ioff != 0) { 1761 // Check to see if the name matches the class we're looking for 1762 // before attempting to find the class. 1763 if (i_cp->klass_name_at_matches(k, ioff)) { 1764 Klass* inner_klass = i_cp->klass_at(ioff, CHECK_NULL); 1765 found = (k() == inner_klass); 1766 if (found && ooff != 0) { 1767 ok = i_cp->klass_at(ooff, CHECK_NULL); 1768 outer_klass = instanceKlassHandle(thread, ok); 1769 *inner_is_member = true; 1770 } 1771 } 1772 } 1773 } 1774 1775 if (found && outer_klass.is_null()) { 1776 // It may be anonymous; try for that. 1777 int encl_method_class_idx = k->enclosing_method_class_index(); 1778 if (encl_method_class_idx != 0) { 1779 ok = i_cp->klass_at(encl_method_class_idx, CHECK_NULL); 1780 outer_klass = instanceKlassHandle(thread, ok); 1781 *inner_is_member = false; 1782 } 1783 } 1784 1785 // If no inner class attribute found for this class. 1786 if (outer_klass.is_null()) return NULL; 1787 1788 // Throws an exception if outer klass has not declared k as an inner klass 1789 // We need evidence that each klass knows about the other, or else 1790 // the system could allow a spoof of an inner class to gain access rights. 1791 Reflection::check_for_inner_class(outer_klass, k, *inner_is_member, CHECK_NULL); 1792 return outer_klass(); 1793 } 1794 1795 JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls)) 1796 assert (cls != NULL, "illegal class"); 1797 JVMWrapper("JVM_GetClassSignature"); 1798 JvmtiVMObjectAllocEventCollector oam; 1799 ResourceMark rm(THREAD); 1800 // Return null for arrays and primatives 1801 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { 1802 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); 1803 if (k->oop_is_instance()) { 1804 Symbol* sym = InstanceKlass::cast(k)->generic_signature(); 1805 if (sym == NULL) return NULL; 1806 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL); 1807 return (jstring) JNIHandles::make_local(env, str()); 1808 } 1809 } 1810 return NULL; 1811 JVM_END 1812 1813 1814 JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls)) 1815 assert (cls != NULL, "illegal class"); 1816 JVMWrapper("JVM_GetClassAnnotations"); 1817 1818 // Return null for arrays and primitives 1819 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { 1820 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); 1821 if (k->oop_is_instance()) { 1822 typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL); 1823 return (jbyteArray) JNIHandles::make_local(env, a); 1824 } 1825 } 1826 return NULL; 1827 JVM_END 1828 1829 1830 static bool jvm_get_field_common(jobject field, fieldDescriptor& fd, TRAPS) { 1831 // some of this code was adapted from from jni_FromReflectedField 1832 1833 oop reflected = JNIHandles::resolve_non_null(field); 1834 oop mirror = java_lang_reflect_Field::clazz(reflected); 1835 Klass* k = java_lang_Class::as_Klass(mirror); 1836 int slot = java_lang_reflect_Field::slot(reflected); 1837 int modifiers = java_lang_reflect_Field::modifiers(reflected); 1838 1839 KlassHandle kh(THREAD, k); 1840 intptr_t offset = InstanceKlass::cast(kh())->field_offset(slot); 1841 1842 if (modifiers & JVM_ACC_STATIC) { 1843 // for static fields we only look in the current class 1844 if (!InstanceKlass::cast(kh())->find_local_field_from_offset(offset, true, &fd)) { 1845 assert(false, "cannot find static field"); 1846 return false; 1847 } 1848 } else { 1849 // for instance fields we start with the current class and work 1850 // our way up through the superclass chain 1851 if (!InstanceKlass::cast(kh())->find_field_from_offset(offset, false, &fd)) { 1852 assert(false, "cannot find instance field"); 1853 return false; 1854 } 1855 } 1856 return true; 1857 } 1858 1859 JVM_ENTRY(jbyteArray, JVM_GetFieldAnnotations(JNIEnv *env, jobject field)) 1860 // field is a handle to a java.lang.reflect.Field object 1861 assert(field != NULL, "illegal field"); 1862 JVMWrapper("JVM_GetFieldAnnotations"); 1863 1864 fieldDescriptor fd; 1865 bool gotFd = jvm_get_field_common(field, fd, CHECK_NULL); 1866 if (!gotFd) { 1867 return NULL; 1868 } 1869 1870 return (jbyteArray) JNIHandles::make_local(env, Annotations::make_java_array(fd.annotations(), THREAD)); 1871 JVM_END 1872 1873 1874 static Method* jvm_get_method_common(jobject method) { 1875 // some of this code was adapted from from jni_FromReflectedMethod 1876 1877 oop reflected = JNIHandles::resolve_non_null(method); 1878 oop mirror = NULL; 1879 int slot = 0; 1880 1881 if (reflected->klass() == SystemDictionary::reflect_Constructor_klass()) { 1882 mirror = java_lang_reflect_Constructor::clazz(reflected); 1883 slot = java_lang_reflect_Constructor::slot(reflected); 1884 } else { 1885 assert(reflected->klass() == SystemDictionary::reflect_Method_klass(), 1886 "wrong type"); 1887 mirror = java_lang_reflect_Method::clazz(reflected); 1888 slot = java_lang_reflect_Method::slot(reflected); 1889 } 1890 Klass* k = java_lang_Class::as_Klass(mirror); 1891 1892 Method* m = InstanceKlass::cast(k)->method_with_idnum(slot); 1893 assert(m != NULL, "cannot find method"); 1894 return m; // caller has to deal with NULL in product mode 1895 } 1896 1897 1898 JVM_ENTRY(jbyteArray, JVM_GetMethodAnnotations(JNIEnv *env, jobject method)) 1899 JVMWrapper("JVM_GetMethodAnnotations"); 1900 1901 // method is a handle to a java.lang.reflect.Method object 1902 Method* m = jvm_get_method_common(method); 1903 if (m == NULL) { 1904 return NULL; 1905 } 1906 1907 return (jbyteArray) JNIHandles::make_local(env, 1908 Annotations::make_java_array(m->annotations(), THREAD)); 1909 JVM_END 1910 1911 1912 JVM_ENTRY(jbyteArray, JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method)) 1913 JVMWrapper("JVM_GetMethodDefaultAnnotationValue"); 1914 1915 // method is a handle to a java.lang.reflect.Method object 1916 Method* m = jvm_get_method_common(method); 1917 if (m == NULL) { 1918 return NULL; 1919 } 1920 1921 return (jbyteArray) JNIHandles::make_local(env, 1922 Annotations::make_java_array(m->annotation_default(), THREAD)); 1923 JVM_END 1924 1925 1926 JVM_ENTRY(jbyteArray, JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method)) 1927 JVMWrapper("JVM_GetMethodParameterAnnotations"); 1928 1929 // method is a handle to a java.lang.reflect.Method object 1930 Method* m = jvm_get_method_common(method); 1931 if (m == NULL) { 1932 return NULL; 1933 } 1934 1935 return (jbyteArray) JNIHandles::make_local(env, 1936 Annotations::make_java_array(m->parameter_annotations(), THREAD)); 1937 JVM_END 1938 1939 /* Type use annotations support (JDK 1.8) */ 1940 1941 JVM_ENTRY(jbyteArray, JVM_GetClassTypeAnnotations(JNIEnv *env, jclass cls)) 1942 assert (cls != NULL, "illegal class"); 1943 JVMWrapper("JVM_GetClassTypeAnnotations"); 1944 ResourceMark rm(THREAD); 1945 // Return null for arrays and primitives 1946 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { 1947 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); 1948 if (k->oop_is_instance()) { 1949 AnnotationArray* type_annotations = InstanceKlass::cast(k)->class_type_annotations(); 1950 if (type_annotations != NULL) { 1951 typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL); 1952 return (jbyteArray) JNIHandles::make_local(env, a); 1953 } 1954 } 1955 } 1956 return NULL; 1957 JVM_END 1958 1959 JVM_ENTRY(jbyteArray, JVM_GetMethodTypeAnnotations(JNIEnv *env, jobject method)) 1960 assert (method != NULL, "illegal method"); 1961 JVMWrapper("JVM_GetMethodTypeAnnotations"); 1962 1963 // method is a handle to a java.lang.reflect.Method object 1964 Method* m = jvm_get_method_common(method); 1965 if (m == NULL) { 1966 return NULL; 1967 } 1968 1969 AnnotationArray* type_annotations = m->type_annotations(); 1970 if (type_annotations != NULL) { 1971 typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL); 1972 return (jbyteArray) JNIHandles::make_local(env, a); 1973 } 1974 1975 return NULL; 1976 JVM_END 1977 1978 JVM_ENTRY(jbyteArray, JVM_GetFieldTypeAnnotations(JNIEnv *env, jobject field)) 1979 assert (field != NULL, "illegal field"); 1980 JVMWrapper("JVM_GetFieldTypeAnnotations"); 1981 1982 fieldDescriptor fd; 1983 bool gotFd = jvm_get_field_common(field, fd, CHECK_NULL); 1984 if (!gotFd) { 1985 return NULL; 1986 } 1987 1988 return (jbyteArray) JNIHandles::make_local(env, Annotations::make_java_array(fd.type_annotations(), THREAD)); 1989 JVM_END 1990 1991 static void bounds_check(constantPoolHandle cp, jint index, TRAPS) { 1992 if (!cp->is_within_bounds(index)) { 1993 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds"); 1994 } 1995 } 1996 1997 JVM_ENTRY(jobjectArray, JVM_GetMethodParameters(JNIEnv *env, jobject method)) 1998 { 1999 JVMWrapper("JVM_GetMethodParameters"); 2000 // method is a handle to a java.lang.reflect.Method object 2001 Method* method_ptr = jvm_get_method_common(method); 2002 methodHandle mh (THREAD, method_ptr); 2003 Handle reflected_method (THREAD, JNIHandles::resolve_non_null(method)); 2004 const int num_params = mh->method_parameters_length(); 2005 2006 if (0 != num_params) { 2007 // make sure all the symbols are properly formatted 2008 for (int i = 0; i < num_params; i++) { 2009 MethodParametersElement* params = mh->method_parameters_start(); 2010 int index = params[i].name_cp_index; 2011 bounds_check(mh->constants(), index, CHECK_NULL); 2012 2013 if (0 != index && !mh->constants()->tag_at(index).is_utf8()) { 2014 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), 2015 "Wrong type at constant pool index"); 2016 } 2017 2018 } 2019 2020 objArrayOop result_oop = oopFactory::new_objArray(SystemDictionary::reflect_Parameter_klass(), num_params, CHECK_NULL); 2021 objArrayHandle result (THREAD, result_oop); 2022 2023 for (int i = 0; i < num_params; i++) { 2024 MethodParametersElement* params = mh->method_parameters_start(); 2025 // For a 0 index, give a NULL symbol 2026 Symbol* sym = 0 != params[i].name_cp_index ? 2027 mh->constants()->symbol_at(params[i].name_cp_index) : NULL; 2028 int flags = params[i].flags; 2029 oop param = Reflection::new_parameter(reflected_method, i, sym, 2030 flags, CHECK_NULL); 2031 result->obj_at_put(i, param); 2032 } 2033 return (jobjectArray)JNIHandles::make_local(env, result()); 2034 } else { 2035 return (jobjectArray)NULL; 2036 } 2037 } 2038 JVM_END 2039 2040 // New (JDK 1.4) reflection implementation ///////////////////////////////////// 2041 2042 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly)) 2043 { 2044 JVMWrapper("JVM_GetClassDeclaredFields"); 2045 JvmtiVMObjectAllocEventCollector oam; 2046 2047 // Exclude primitive types and array types 2048 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) || 2049 java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_array()) { 2050 // Return empty array 2051 oop res = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), 0, CHECK_NULL); 2052 return (jobjectArray) JNIHandles::make_local(env, res); 2053 } 2054 2055 instanceKlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))); 2056 constantPoolHandle cp(THREAD, k->constants()); 2057 2058 // Ensure class is linked 2059 k->link_class(CHECK_NULL); 2060 2061 // 4496456 We need to filter out java.lang.Throwable.backtrace 2062 bool skip_backtrace = false; 2063 2064 // Allocate result 2065 int num_fields; 2066 2067 if (publicOnly) { 2068 num_fields = 0; 2069 for (JavaFieldStream fs(k()); !fs.done(); fs.next()) { 2070 if (fs.access_flags().is_public()) ++num_fields; 2071 } 2072 } else { 2073 num_fields = k->java_fields_count(); 2074 2075 if (k() == SystemDictionary::Throwable_klass()) { 2076 num_fields--; 2077 skip_backtrace = true; 2078 } 2079 } 2080 2081 objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), num_fields, CHECK_NULL); 2082 objArrayHandle result (THREAD, r); 2083 2084 int out_idx = 0; 2085 fieldDescriptor fd; 2086 for (JavaFieldStream fs(k); !fs.done(); fs.next()) { 2087 if (skip_backtrace) { 2088 // 4496456 skip java.lang.Throwable.backtrace 2089 int offset = fs.offset(); 2090 if (offset == java_lang_Throwable::get_backtrace_offset()) continue; 2091 } 2092 2093 if (!publicOnly || fs.access_flags().is_public()) { 2094 fd.reinitialize(k(), fs.index()); 2095 oop field = Reflection::new_field(&fd, UseNewReflection, CHECK_NULL); 2096 result->obj_at_put(out_idx, field); 2097 ++out_idx; 2098 } 2099 } 2100 assert(out_idx == num_fields, "just checking"); 2101 return (jobjectArray) JNIHandles::make_local(env, result()); 2102 } 2103 JVM_END 2104 2105 static bool select_method(methodHandle method, bool want_constructor) { 2106 if (want_constructor) { 2107 return (method->is_initializer() && !method->is_static()); 2108 } else { 2109 return (!method->is_initializer() && !method->is_overpass()); 2110 } 2111 } 2112 2113 static jobjectArray get_class_declared_methods_helper( 2114 JNIEnv *env, 2115 jclass ofClass, jboolean publicOnly, 2116 bool want_constructor, 2117 Klass* klass, TRAPS) { 2118 2119 JvmtiVMObjectAllocEventCollector oam; 2120 2121 // Exclude primitive types and array types 2122 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) 2123 || java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_array()) { 2124 // Return empty array 2125 oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL); 2126 return (jobjectArray) JNIHandles::make_local(env, res); 2127 } 2128 2129 instanceKlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))); 2130 2131 // Ensure class is linked 2132 k->link_class(CHECK_NULL); 2133 2134 Array<Method*>* methods = k->methods(); 2135 int methods_length = methods->length(); 2136 2137 // Save original method_idnum in case of redefinition, which can change 2138 // the idnum of obsolete methods. The new method will have the same idnum 2139 // but if we refresh the methods array, the counts will be wrong. 2140 ResourceMark rm(THREAD); 2141 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length); 2142 int num_methods = 0; 2143 2144 for (int i = 0; i < methods_length; i++) { 2145 methodHandle method(THREAD, methods->at(i)); 2146 if (select_method(method, want_constructor)) { 2147 if (!publicOnly || method->is_public()) { 2148 idnums->push(method->method_idnum()); 2149 ++num_methods; 2150 } 2151 } 2152 } 2153 2154 // Allocate result 2155 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL); 2156 objArrayHandle result (THREAD, r); 2157 2158 // Now just put the methods that we selected above, but go by their idnum 2159 // in case of redefinition. The methods can be redefined at any safepoint, 2160 // so above when allocating the oop array and below when creating reflect 2161 // objects. 2162 for (int i = 0; i < num_methods; i++) { 2163 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i))); 2164 if (method.is_null()) { 2165 // Method may have been deleted and seems this API can handle null 2166 // Otherwise should probably put a method that throws NSME 2167 result->obj_at_put(i, NULL); 2168 } else { 2169 oop m; 2170 if (want_constructor) { 2171 m = Reflection::new_constructor(method, CHECK_NULL); 2172 } else { 2173 m = Reflection::new_method(method, UseNewReflection, false, CHECK_NULL); 2174 } 2175 result->obj_at_put(i, m); 2176 } 2177 } 2178 2179 return (jobjectArray) JNIHandles::make_local(env, result()); 2180 } 2181 2182 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly)) 2183 { 2184 JVMWrapper("JVM_GetClassDeclaredMethods"); 2185 return get_class_declared_methods_helper(env, ofClass, publicOnly, 2186 /*want_constructor*/ false, 2187 SystemDictionary::reflect_Method_klass(), THREAD); 2188 } 2189 JVM_END 2190 2191 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly)) 2192 { 2193 JVMWrapper("JVM_GetClassDeclaredConstructors"); 2194 return get_class_declared_methods_helper(env, ofClass, publicOnly, 2195 /*want_constructor*/ true, 2196 SystemDictionary::reflect_Constructor_klass(), THREAD); 2197 } 2198 JVM_END 2199 2200 JVM_ENTRY(jint, JVM_GetClassAccessFlags(JNIEnv *env, jclass cls)) 2201 { 2202 JVMWrapper("JVM_GetClassAccessFlags"); 2203 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { 2204 // Primitive type 2205 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC; 2206 } 2207 2208 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2209 return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS; 2210 } 2211 JVM_END 2212 2213 2214 // Constant pool access ////////////////////////////////////////////////////////// 2215 2216 JVM_ENTRY(jobject, JVM_GetClassConstantPool(JNIEnv *env, jclass cls)) 2217 { 2218 JVMWrapper("JVM_GetClassConstantPool"); 2219 JvmtiVMObjectAllocEventCollector oam; 2220 2221 // Return null for primitives and arrays 2222 if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { 2223 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2224 if (k->oop_is_instance()) { 2225 instanceKlassHandle k_h(THREAD, k); 2226 Handle jcp = sun_reflect_ConstantPool::create(CHECK_NULL); 2227 sun_reflect_ConstantPool::set_cp(jcp(), k_h->constants()); 2228 return JNIHandles::make_local(jcp()); 2229 } 2230 } 2231 return NULL; 2232 } 2233 JVM_END 2234 2235 2236 JVM_ENTRY(jint, JVM_ConstantPoolGetSize(JNIEnv *env, jobject obj, jobject unused)) 2237 { 2238 JVMWrapper("JVM_ConstantPoolGetSize"); 2239 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2240 return cp->length(); 2241 } 2242 JVM_END 2243 2244 2245 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2246 { 2247 JVMWrapper("JVM_ConstantPoolGetClassAt"); 2248 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2249 bounds_check(cp, index, CHECK_NULL); 2250 constantTag tag = cp->tag_at(index); 2251 if (!tag.is_klass() && !tag.is_unresolved_klass()) { 2252 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2253 } 2254 Klass* k = cp->klass_at(index, CHECK_NULL); 2255 return (jclass) JNIHandles::make_local(k->java_mirror()); 2256 } 2257 JVM_END 2258 2259 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index)) 2260 { 2261 JVMWrapper("JVM_ConstantPoolGetClassAtIfLoaded"); 2262 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2263 bounds_check(cp, index, CHECK_NULL); 2264 constantTag tag = cp->tag_at(index); 2265 if (!tag.is_klass() && !tag.is_unresolved_klass()) { 2266 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2267 } 2268 Klass* k = ConstantPool::klass_at_if_loaded(cp, index); 2269 if (k == NULL) return NULL; 2270 return (jclass) JNIHandles::make_local(k->java_mirror()); 2271 } 2272 JVM_END 2273 2274 static jobject get_method_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) { 2275 constantTag tag = cp->tag_at(index); 2276 if (!tag.is_method() && !tag.is_interface_method()) { 2277 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2278 } 2279 int klass_ref = cp->uncached_klass_ref_index_at(index); 2280 Klass* k_o; 2281 if (force_resolution) { 2282 k_o = cp->klass_at(klass_ref, CHECK_NULL); 2283 } else { 2284 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref); 2285 if (k_o == NULL) return NULL; 2286 } 2287 instanceKlassHandle k(THREAD, k_o); 2288 Symbol* name = cp->uncached_name_ref_at(index); 2289 Symbol* sig = cp->uncached_signature_ref_at(index); 2290 methodHandle m (THREAD, k->find_method(name, sig)); 2291 if (m.is_null()) { 2292 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class"); 2293 } 2294 oop method; 2295 if (!m->is_initializer() || m->is_static()) { 2296 method = Reflection::new_method(m, true, true, CHECK_NULL); 2297 } else { 2298 method = Reflection::new_constructor(m, CHECK_NULL); 2299 } 2300 return JNIHandles::make_local(method); 2301 } 2302 2303 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2304 { 2305 JVMWrapper("JVM_ConstantPoolGetMethodAt"); 2306 JvmtiVMObjectAllocEventCollector oam; 2307 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2308 bounds_check(cp, index, CHECK_NULL); 2309 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL); 2310 return res; 2311 } 2312 JVM_END 2313 2314 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index)) 2315 { 2316 JVMWrapper("JVM_ConstantPoolGetMethodAtIfLoaded"); 2317 JvmtiVMObjectAllocEventCollector oam; 2318 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2319 bounds_check(cp, index, CHECK_NULL); 2320 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL); 2321 return res; 2322 } 2323 JVM_END 2324 2325 static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) { 2326 constantTag tag = cp->tag_at(index); 2327 if (!tag.is_field()) { 2328 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2329 } 2330 int klass_ref = cp->uncached_klass_ref_index_at(index); 2331 Klass* k_o; 2332 if (force_resolution) { 2333 k_o = cp->klass_at(klass_ref, CHECK_NULL); 2334 } else { 2335 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref); 2336 if (k_o == NULL) return NULL; 2337 } 2338 instanceKlassHandle k(THREAD, k_o); 2339 Symbol* name = cp->uncached_name_ref_at(index); 2340 Symbol* sig = cp->uncached_signature_ref_at(index); 2341 fieldDescriptor fd; 2342 Klass* target_klass = k->find_field(name, sig, &fd); 2343 if (target_klass == NULL) { 2344 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class"); 2345 } 2346 oop field = Reflection::new_field(&fd, true, CHECK_NULL); 2347 return JNIHandles::make_local(field); 2348 } 2349 2350 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject obj, jobject unusedl, jint index)) 2351 { 2352 JVMWrapper("JVM_ConstantPoolGetFieldAt"); 2353 JvmtiVMObjectAllocEventCollector oam; 2354 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2355 bounds_check(cp, index, CHECK_NULL); 2356 jobject res = get_field_at_helper(cp, index, true, CHECK_NULL); 2357 return res; 2358 } 2359 JVM_END 2360 2361 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index)) 2362 { 2363 JVMWrapper("JVM_ConstantPoolGetFieldAtIfLoaded"); 2364 JvmtiVMObjectAllocEventCollector oam; 2365 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2366 bounds_check(cp, index, CHECK_NULL); 2367 jobject res = get_field_at_helper(cp, index, false, CHECK_NULL); 2368 return res; 2369 } 2370 JVM_END 2371 2372 JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2373 { 2374 JVMWrapper("JVM_ConstantPoolGetMemberRefInfoAt"); 2375 JvmtiVMObjectAllocEventCollector oam; 2376 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2377 bounds_check(cp, index, CHECK_NULL); 2378 constantTag tag = cp->tag_at(index); 2379 if (!tag.is_field_or_method()) { 2380 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2381 } 2382 int klass_ref = cp->uncached_klass_ref_index_at(index); 2383 Symbol* klass_name = cp->klass_name_at(klass_ref); 2384 Symbol* member_name = cp->uncached_name_ref_at(index); 2385 Symbol* member_sig = cp->uncached_signature_ref_at(index); 2386 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::String_klass(), 3, CHECK_NULL); 2387 objArrayHandle dest(THREAD, dest_o); 2388 Handle str = java_lang_String::create_from_symbol(klass_name, CHECK_NULL); 2389 dest->obj_at_put(0, str()); 2390 str = java_lang_String::create_from_symbol(member_name, CHECK_NULL); 2391 dest->obj_at_put(1, str()); 2392 str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL); 2393 dest->obj_at_put(2, str()); 2394 return (jobjectArray) JNIHandles::make_local(dest()); 2395 } 2396 JVM_END 2397 2398 JVM_ENTRY(jint, JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2399 { 2400 JVMWrapper("JVM_ConstantPoolGetIntAt"); 2401 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2402 bounds_check(cp, index, CHECK_0); 2403 constantTag tag = cp->tag_at(index); 2404 if (!tag.is_int()) { 2405 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2406 } 2407 return cp->int_at(index); 2408 } 2409 JVM_END 2410 2411 JVM_ENTRY(jlong, JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2412 { 2413 JVMWrapper("JVM_ConstantPoolGetLongAt"); 2414 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2415 bounds_check(cp, index, CHECK_(0L)); 2416 constantTag tag = cp->tag_at(index); 2417 if (!tag.is_long()) { 2418 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2419 } 2420 return cp->long_at(index); 2421 } 2422 JVM_END 2423 2424 JVM_ENTRY(jfloat, JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2425 { 2426 JVMWrapper("JVM_ConstantPoolGetFloatAt"); 2427 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2428 bounds_check(cp, index, CHECK_(0.0f)); 2429 constantTag tag = cp->tag_at(index); 2430 if (!tag.is_float()) { 2431 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2432 } 2433 return cp->float_at(index); 2434 } 2435 JVM_END 2436 2437 JVM_ENTRY(jdouble, JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2438 { 2439 JVMWrapper("JVM_ConstantPoolGetDoubleAt"); 2440 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2441 bounds_check(cp, index, CHECK_(0.0)); 2442 constantTag tag = cp->tag_at(index); 2443 if (!tag.is_double()) { 2444 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2445 } 2446 return cp->double_at(index); 2447 } 2448 JVM_END 2449 2450 JVM_ENTRY(jstring, JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject obj, jobject unused, jint index)) 2451 { 2452 JVMWrapper("JVM_ConstantPoolGetStringAt"); 2453 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2454 bounds_check(cp, index, CHECK_NULL); 2455 constantTag tag = cp->tag_at(index); 2456 if (!tag.is_string()) { 2457 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2458 } 2459 oop str = cp->string_at(index, CHECK_NULL); 2460 return (jstring) JNIHandles::make_local(str); 2461 } 2462 JVM_END 2463 2464 JVM_ENTRY(jstring, JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject obj, jobject unused, jint index)) 2465 { 2466 JVMWrapper("JVM_ConstantPoolGetUTF8At"); 2467 JvmtiVMObjectAllocEventCollector oam; 2468 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); 2469 bounds_check(cp, index, CHECK_NULL); 2470 constantTag tag = cp->tag_at(index); 2471 if (!tag.is_symbol()) { 2472 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); 2473 } 2474 Symbol* sym = cp->symbol_at(index); 2475 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL); 2476 return (jstring) JNIHandles::make_local(str()); 2477 } 2478 JVM_END 2479 2480 2481 // Assertion support. ////////////////////////////////////////////////////////// 2482 2483 JVM_ENTRY(jboolean, JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls)) 2484 JVMWrapper("JVM_DesiredAssertionStatus"); 2485 assert(cls != NULL, "bad class"); 2486 2487 oop r = JNIHandles::resolve(cls); 2488 assert(! java_lang_Class::is_primitive(r), "primitive classes not allowed"); 2489 if (java_lang_Class::is_primitive(r)) return false; 2490 2491 Klass* k = java_lang_Class::as_Klass(r); 2492 assert(k->oop_is_instance(), "must be an instance klass"); 2493 if (! k->oop_is_instance()) return false; 2494 2495 ResourceMark rm(THREAD); 2496 const char* name = k->name()->as_C_string(); 2497 bool system_class = k->class_loader() == NULL; 2498 return JavaAssertions::enabled(name, system_class); 2499 2500 JVM_END 2501 2502 2503 // Return a new AssertionStatusDirectives object with the fields filled in with 2504 // command-line assertion arguments (i.e., -ea, -da). 2505 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused)) 2506 JVMWrapper("JVM_AssertionStatusDirectives"); 2507 JvmtiVMObjectAllocEventCollector oam; 2508 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL); 2509 return JNIHandles::make_local(env, asd); 2510 JVM_END 2511 2512 // Verification //////////////////////////////////////////////////////////////////////////////// 2513 2514 // Reflection for the verifier ///////////////////////////////////////////////////////////////// 2515 2516 // RedefineClasses support: bug 6214132 caused verification to fail. 2517 // All functions from this section should call the jvmtiThreadSate function: 2518 // Klass* class_to_verify_considering_redefinition(Klass* klass). 2519 // The function returns a Klass* of the _scratch_class if the verifier 2520 // was invoked in the middle of the class redefinition. 2521 // Otherwise it returns its argument value which is the _the_class Klass*. 2522 // Please, refer to the description in the jvmtiThreadSate.hpp. 2523 2524 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls)) 2525 JVMWrapper("JVM_GetClassNameUTF"); 2526 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2527 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2528 return k->name()->as_utf8(); 2529 JVM_END 2530 2531 2532 JVM_QUICK_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types)) 2533 JVMWrapper("JVM_GetClassCPTypes"); 2534 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2535 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2536 // types will have length zero if this is not an InstanceKlass 2537 // (length is determined by call to JVM_GetClassCPEntriesCount) 2538 if (k->oop_is_instance()) { 2539 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2540 for (int index = cp->length() - 1; index >= 0; index--) { 2541 constantTag tag = cp->tag_at(index); 2542 types[index] = (tag.is_unresolved_klass()) ? JVM_CONSTANT_Class : tag.value(); 2543 } 2544 } 2545 JVM_END 2546 2547 2548 JVM_QUICK_ENTRY(jint, JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls)) 2549 JVMWrapper("JVM_GetClassCPEntriesCount"); 2550 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2551 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2552 if (!k->oop_is_instance()) 2553 return 0; 2554 return InstanceKlass::cast(k)->constants()->length(); 2555 JVM_END 2556 2557 2558 JVM_QUICK_ENTRY(jint, JVM_GetClassFieldsCount(JNIEnv *env, jclass cls)) 2559 JVMWrapper("JVM_GetClassFieldsCount"); 2560 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2561 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2562 if (!k->oop_is_instance()) 2563 return 0; 2564 return InstanceKlass::cast(k)->java_fields_count(); 2565 JVM_END 2566 2567 2568 JVM_QUICK_ENTRY(jint, JVM_GetClassMethodsCount(JNIEnv *env, jclass cls)) 2569 JVMWrapper("JVM_GetClassMethodsCount"); 2570 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2571 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2572 if (!k->oop_is_instance()) 2573 return 0; 2574 return InstanceKlass::cast(k)->methods()->length(); 2575 JVM_END 2576 2577 2578 // The following methods, used for the verifier, are never called with 2579 // array klasses, so a direct cast to InstanceKlass is safe. 2580 // Typically, these methods are called in a loop with bounds determined 2581 // by the results of JVM_GetClass{Fields,Methods}Count, which return 2582 // zero for arrays. 2583 JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions)) 2584 JVMWrapper("JVM_GetMethodIxExceptionIndexes"); 2585 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2586 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2587 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2588 int length = method->checked_exceptions_length(); 2589 if (length > 0) { 2590 CheckedExceptionElement* table= method->checked_exceptions_start(); 2591 for (int i = 0; i < length; i++) { 2592 exceptions[i] = table[i].class_cp_index; 2593 } 2594 } 2595 JVM_END 2596 2597 2598 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index)) 2599 JVMWrapper("JVM_GetMethodIxExceptionsCount"); 2600 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2601 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2602 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2603 return method->checked_exceptions_length(); 2604 JVM_END 2605 2606 2607 JVM_QUICK_ENTRY(void, JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code)) 2608 JVMWrapper("JVM_GetMethodIxByteCode"); 2609 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2610 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2611 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2612 memcpy(code, method->code_base(), method->code_size()); 2613 JVM_END 2614 2615 2616 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index)) 2617 JVMWrapper("JVM_GetMethodIxByteCodeLength"); 2618 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2619 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2620 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2621 return method->code_size(); 2622 JVM_END 2623 2624 2625 JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry)) 2626 JVMWrapper("JVM_GetMethodIxExceptionTableEntry"); 2627 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2628 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2629 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2630 ExceptionTable extable(method); 2631 entry->start_pc = extable.start_pc(entry_index); 2632 entry->end_pc = extable.end_pc(entry_index); 2633 entry->handler_pc = extable.handler_pc(entry_index); 2634 entry->catchType = extable.catch_type_index(entry_index); 2635 JVM_END 2636 2637 2638 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index)) 2639 JVMWrapper("JVM_GetMethodIxExceptionTableLength"); 2640 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2641 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2642 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2643 return method->exception_table_length(); 2644 JVM_END 2645 2646 2647 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index)) 2648 JVMWrapper("JVM_GetMethodIxModifiers"); 2649 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2650 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2651 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2652 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS; 2653 JVM_END 2654 2655 2656 JVM_QUICK_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index)) 2657 JVMWrapper("JVM_GetFieldIxModifiers"); 2658 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2659 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2660 return InstanceKlass::cast(k)->field_access_flags(field_index) & JVM_RECOGNIZED_FIELD_MODIFIERS; 2661 JVM_END 2662 2663 2664 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index)) 2665 JVMWrapper("JVM_GetMethodIxLocalsCount"); 2666 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2667 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2668 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2669 return method->max_locals(); 2670 JVM_END 2671 2672 2673 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index)) 2674 JVMWrapper("JVM_GetMethodIxArgsSize"); 2675 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2676 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2677 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2678 return method->size_of_parameters(); 2679 JVM_END 2680 2681 2682 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index)) 2683 JVMWrapper("JVM_GetMethodIxMaxStack"); 2684 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2685 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2686 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2687 return method->verifier_max_stack(); 2688 JVM_END 2689 2690 2691 JVM_QUICK_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index)) 2692 JVMWrapper("JVM_IsConstructorIx"); 2693 ResourceMark rm(THREAD); 2694 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2695 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2696 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2697 return method->name() == vmSymbols::object_initializer_name(); 2698 JVM_END 2699 2700 2701 JVM_QUICK_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index)) 2702 JVMWrapper("JVM_IsVMGeneratedMethodIx"); 2703 ResourceMark rm(THREAD); 2704 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2705 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2706 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2707 return method->is_overpass(); 2708 JVM_END 2709 2710 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index)) 2711 JVMWrapper("JVM_GetMethodIxIxUTF"); 2712 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2713 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2714 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2715 return method->name()->as_utf8(); 2716 JVM_END 2717 2718 2719 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index)) 2720 JVMWrapper("JVM_GetMethodIxSignatureUTF"); 2721 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2722 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2723 Method* method = InstanceKlass::cast(k)->methods()->at(method_index); 2724 return method->signature()->as_utf8(); 2725 JVM_END 2726 2727 /** 2728 * All of these JVM_GetCP-xxx methods are used by the old verifier to 2729 * read entries in the constant pool. Since the old verifier always 2730 * works on a copy of the code, it will not see any rewriting that 2731 * may possibly occur in the middle of verification. So it is important 2732 * that nothing it calls tries to use the cpCache instead of the raw 2733 * constant pool, so we must use cp->uncached_x methods when appropriate. 2734 */ 2735 JVM_ENTRY(const char*, JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index)) 2736 JVMWrapper("JVM_GetCPFieldNameUTF"); 2737 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2738 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2739 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2740 switch (cp->tag_at(cp_index).value()) { 2741 case JVM_CONSTANT_Fieldref: 2742 return cp->uncached_name_ref_at(cp_index)->as_utf8(); 2743 default: 2744 fatal("JVM_GetCPFieldNameUTF: illegal constant"); 2745 } 2746 ShouldNotReachHere(); 2747 return NULL; 2748 JVM_END 2749 2750 2751 JVM_ENTRY(const char*, JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index)) 2752 JVMWrapper("JVM_GetCPMethodNameUTF"); 2753 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2754 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2755 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2756 switch (cp->tag_at(cp_index).value()) { 2757 case JVM_CONSTANT_InterfaceMethodref: 2758 case JVM_CONSTANT_Methodref: 2759 return cp->uncached_name_ref_at(cp_index)->as_utf8(); 2760 default: 2761 fatal("JVM_GetCPMethodNameUTF: illegal constant"); 2762 } 2763 ShouldNotReachHere(); 2764 return NULL; 2765 JVM_END 2766 2767 2768 JVM_ENTRY(const char*, JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)) 2769 JVMWrapper("JVM_GetCPMethodSignatureUTF"); 2770 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2771 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2772 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2773 switch (cp->tag_at(cp_index).value()) { 2774 case JVM_CONSTANT_InterfaceMethodref: 2775 case JVM_CONSTANT_Methodref: 2776 return cp->uncached_signature_ref_at(cp_index)->as_utf8(); 2777 default: 2778 fatal("JVM_GetCPMethodSignatureUTF: illegal constant"); 2779 } 2780 ShouldNotReachHere(); 2781 return NULL; 2782 JVM_END 2783 2784 2785 JVM_ENTRY(const char*, JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)) 2786 JVMWrapper("JVM_GetCPFieldSignatureUTF"); 2787 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2788 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2789 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2790 switch (cp->tag_at(cp_index).value()) { 2791 case JVM_CONSTANT_Fieldref: 2792 return cp->uncached_signature_ref_at(cp_index)->as_utf8(); 2793 default: 2794 fatal("JVM_GetCPFieldSignatureUTF: illegal constant"); 2795 } 2796 ShouldNotReachHere(); 2797 return NULL; 2798 JVM_END 2799 2800 2801 JVM_ENTRY(const char*, JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)) 2802 JVMWrapper("JVM_GetCPClassNameUTF"); 2803 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2804 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2805 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2806 Symbol* classname = cp->klass_name_at(cp_index); 2807 return classname->as_utf8(); 2808 JVM_END 2809 2810 2811 JVM_ENTRY(const char*, JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)) 2812 JVMWrapper("JVM_GetCPFieldClassNameUTF"); 2813 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2814 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2815 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2816 switch (cp->tag_at(cp_index).value()) { 2817 case JVM_CONSTANT_Fieldref: { 2818 int class_index = cp->uncached_klass_ref_index_at(cp_index); 2819 Symbol* classname = cp->klass_name_at(class_index); 2820 return classname->as_utf8(); 2821 } 2822 default: 2823 fatal("JVM_GetCPFieldClassNameUTF: illegal constant"); 2824 } 2825 ShouldNotReachHere(); 2826 return NULL; 2827 JVM_END 2828 2829 2830 JVM_ENTRY(const char*, JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)) 2831 JVMWrapper("JVM_GetCPMethodClassNameUTF"); 2832 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2833 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2834 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2835 switch (cp->tag_at(cp_index).value()) { 2836 case JVM_CONSTANT_Methodref: 2837 case JVM_CONSTANT_InterfaceMethodref: { 2838 int class_index = cp->uncached_klass_ref_index_at(cp_index); 2839 Symbol* classname = cp->klass_name_at(class_index); 2840 return classname->as_utf8(); 2841 } 2842 default: 2843 fatal("JVM_GetCPMethodClassNameUTF: illegal constant"); 2844 } 2845 ShouldNotReachHere(); 2846 return NULL; 2847 JVM_END 2848 2849 2850 JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)) 2851 JVMWrapper("JVM_GetCPFieldModifiers"); 2852 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2853 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls)); 2854 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2855 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread); 2856 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2857 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants(); 2858 switch (cp->tag_at(cp_index).value()) { 2859 case JVM_CONSTANT_Fieldref: { 2860 Symbol* name = cp->uncached_name_ref_at(cp_index); 2861 Symbol* signature = cp->uncached_signature_ref_at(cp_index); 2862 for (JavaFieldStream fs(k_called); !fs.done(); fs.next()) { 2863 if (fs.name() == name && fs.signature() == signature) { 2864 return fs.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS; 2865 } 2866 } 2867 return -1; 2868 } 2869 default: 2870 fatal("JVM_GetCPFieldModifiers: illegal constant"); 2871 } 2872 ShouldNotReachHere(); 2873 return 0; 2874 JVM_END 2875 2876 2877 JVM_QUICK_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)) 2878 JVMWrapper("JVM_GetCPMethodModifiers"); 2879 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); 2880 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls)); 2881 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); 2882 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread); 2883 ConstantPool* cp = InstanceKlass::cast(k)->constants(); 2884 switch (cp->tag_at(cp_index).value()) { 2885 case JVM_CONSTANT_Methodref: 2886 case JVM_CONSTANT_InterfaceMethodref: { 2887 Symbol* name = cp->uncached_name_ref_at(cp_index); 2888 Symbol* signature = cp->uncached_signature_ref_at(cp_index); 2889 Array<Method*>* methods = InstanceKlass::cast(k_called)->methods(); 2890 int methods_count = methods->length(); 2891 for (int i = 0; i < methods_count; i++) { 2892 Method* method = methods->at(i); 2893 if (method->name() == name && method->signature() == signature) { 2894 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS; 2895 } 2896 } 2897 return -1; 2898 } 2899 default: 2900 fatal("JVM_GetCPMethodModifiers: illegal constant"); 2901 } 2902 ShouldNotReachHere(); 2903 return 0; 2904 JVM_END 2905 2906 2907 // Misc ////////////////////////////////////////////////////////////////////////////////////////////// 2908 2909 JVM_LEAF(void, JVM_ReleaseUTF(const char *utf)) 2910 // So long as UTF8::convert_to_utf8 returns resource strings, we don't have to do anything 2911 JVM_END 2912 2913 2914 JVM_ENTRY(jboolean, JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2)) 2915 JVMWrapper("JVM_IsSameClassPackage"); 2916 oop class1_mirror = JNIHandles::resolve_non_null(class1); 2917 oop class2_mirror = JNIHandles::resolve_non_null(class2); 2918 Klass* klass1 = java_lang_Class::as_Klass(class1_mirror); 2919 Klass* klass2 = java_lang_Class::as_Klass(class2_mirror); 2920 return (jboolean) Reflection::is_same_class_package(klass1, klass2); 2921 JVM_END 2922 2923 2924 // IO functions //////////////////////////////////////////////////////////////////////////////////////// 2925 2926 JVM_LEAF(jint, JVM_Open(const char *fname, jint flags, jint mode)) 2927 JVMWrapper2("JVM_Open (%s)", fname); 2928 2929 //%note jvm_r6 2930 int result = os::open(fname, flags, mode); 2931 if (result >= 0) { 2932 return result; 2933 } else { 2934 switch(errno) { 2935 case EEXIST: 2936 return JVM_EEXIST; 2937 default: 2938 return -1; 2939 } 2940 } 2941 JVM_END 2942 2943 2944 JVM_LEAF(jint, JVM_Close(jint fd)) 2945 JVMWrapper2("JVM_Close (0x%x)", fd); 2946 //%note jvm_r6 2947 return os::close(fd); 2948 JVM_END 2949 2950 2951 JVM_LEAF(jint, JVM_Read(jint fd, char *buf, jint nbytes)) 2952 JVMWrapper2("JVM_Read (0x%x)", fd); 2953 2954 //%note jvm_r6 2955 return (jint)os::restartable_read(fd, buf, nbytes); 2956 JVM_END 2957 2958 2959 JVM_LEAF(jint, JVM_Write(jint fd, char *buf, jint nbytes)) 2960 JVMWrapper2("JVM_Write (0x%x)", fd); 2961 2962 //%note jvm_r6 2963 return (jint)os::write(fd, buf, nbytes); 2964 JVM_END 2965 2966 2967 JVM_LEAF(jint, JVM_Available(jint fd, jlong *pbytes)) 2968 JVMWrapper2("JVM_Available (0x%x)", fd); 2969 //%note jvm_r6 2970 return os::available(fd, pbytes); 2971 JVM_END 2972 2973 2974 JVM_LEAF(jlong, JVM_Lseek(jint fd, jlong offset, jint whence)) 2975 JVMWrapper4("JVM_Lseek (0x%x, " INT64_FORMAT ", %d)", fd, (int64_t) offset, whence); 2976 //%note jvm_r6 2977 return os::lseek(fd, offset, whence); 2978 JVM_END 2979 2980 2981 JVM_LEAF(jint, JVM_SetLength(jint fd, jlong length)) 2982 JVMWrapper3("JVM_SetLength (0x%x, " INT64_FORMAT ")", fd, (int64_t) length); 2983 return os::ftruncate(fd, length); 2984 JVM_END 2985 2986 2987 JVM_LEAF(jint, JVM_Sync(jint fd)) 2988 JVMWrapper2("JVM_Sync (0x%x)", fd); 2989 //%note jvm_r6 2990 return os::fsync(fd); 2991 JVM_END 2992 2993 2994 // Printing support ////////////////////////////////////////////////// 2995 extern "C" { 2996 2997 ATTRIBUTE_PRINTF(3, 0) 2998 int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) { 2999 // Reject count values that are negative signed values converted to 3000 // unsigned; see bug 4399518, 4417214 3001 if ((intptr_t)count <= 0) return -1; 3002 3003 int result = os::vsnprintf(str, count, fmt, args); 3004 if (result > 0 && (size_t)result >= count) { 3005 result = -1; 3006 } 3007 3008 return result; 3009 } 3010 3011 ATTRIBUTE_PRINTF(3, 0) 3012 int jio_snprintf(char *str, size_t count, const char *fmt, ...) { 3013 va_list args; 3014 int len; 3015 va_start(args, fmt); 3016 len = jio_vsnprintf(str, count, fmt, args); 3017 va_end(args); 3018 return len; 3019 } 3020 3021 ATTRIBUTE_PRINTF(2,3) 3022 int jio_fprintf(FILE* f, const char *fmt, ...) { 3023 int len; 3024 va_list args; 3025 va_start(args, fmt); 3026 len = jio_vfprintf(f, fmt, args); 3027 va_end(args); 3028 return len; 3029 } 3030 3031 ATTRIBUTE_PRINTF(2, 0) 3032 int jio_vfprintf(FILE* f, const char *fmt, va_list args) { 3033 if (Arguments::vfprintf_hook() != NULL) { 3034 return Arguments::vfprintf_hook()(f, fmt, args); 3035 } else { 3036 return vfprintf(f, fmt, args); 3037 } 3038 } 3039 3040 ATTRIBUTE_PRINTF(1, 2) 3041 JNIEXPORT int jio_printf(const char *fmt, ...) { 3042 int len; 3043 va_list args; 3044 va_start(args, fmt); 3045 len = jio_vfprintf(defaultStream::output_stream(), fmt, args); 3046 va_end(args); 3047 return len; 3048 } 3049 3050 3051 // HotSpot specific jio method 3052 void jio_print(const char* s) { 3053 // Try to make this function as atomic as possible. 3054 if (Arguments::vfprintf_hook() != NULL) { 3055 jio_fprintf(defaultStream::output_stream(), "%s", s); 3056 } else { 3057 // Make an unused local variable to avoid warning from gcc 4.x compiler. 3058 size_t count = ::write(defaultStream::output_fd(), s, (int)strlen(s)); 3059 } 3060 } 3061 3062 } // Extern C 3063 3064 // java.lang.Thread ////////////////////////////////////////////////////////////////////////////// 3065 3066 // In most of the JVM Thread support functions we need to be sure to lock the Threads_lock 3067 // to prevent the target thread from exiting after we have a pointer to the C++ Thread or 3068 // OSThread objects. The exception to this rule is when the target object is the thread 3069 // doing the operation, in which case we know that the thread won't exit until the 3070 // operation is done (all exits being voluntary). There are a few cases where it is 3071 // rather silly to do operations on yourself, like resuming yourself or asking whether 3072 // you are alive. While these can still happen, they are not subject to deadlocks if 3073 // the lock is held while the operation occurs (this is not the case for suspend, for 3074 // instance), and are very unlikely. Because IsAlive needs to be fast and its 3075 // implementation is local to this file, we always lock Threads_lock for that one. 3076 3077 static void thread_entry(JavaThread* thread, TRAPS) { 3078 HandleMark hm(THREAD); 3079 Handle obj(THREAD, thread->threadObj()); 3080 JavaValue result(T_VOID); 3081 JavaCalls::call_virtual(&result, 3082 obj, 3083 KlassHandle(THREAD, SystemDictionary::Thread_klass()), 3084 vmSymbols::run_method_name(), 3085 vmSymbols::void_method_signature(), 3086 THREAD); 3087 } 3088 3089 3090 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread)) 3091 JVMWrapper("JVM_StartThread"); 3092 JavaThread *native_thread = NULL; 3093 3094 // We cannot hold the Threads_lock when we throw an exception, 3095 // due to rank ordering issues. Example: we might need to grab the 3096 // Heap_lock while we construct the exception. 3097 bool throw_illegal_thread_state = false; 3098 3099 // We must release the Threads_lock before we can post a jvmti event 3100 // in Thread::start. 3101 { 3102 // Ensure that the C++ Thread and OSThread structures aren't freed before 3103 // we operate. 3104 MutexLocker mu(Threads_lock); 3105 3106 // Since JDK 5 the java.lang.Thread threadStatus is used to prevent 3107 // re-starting an already started thread, so we should usually find 3108 // that the JavaThread is null. However for a JNI attached thread 3109 // there is a small window between the Thread object being created 3110 // (with its JavaThread set) and the update to its threadStatus, so we 3111 // have to check for this 3112 if (java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) { 3113 throw_illegal_thread_state = true; 3114 } else { 3115 // We could also check the stillborn flag to see if this thread was already stopped, but 3116 // for historical reasons we let the thread detect that itself when it starts running 3117 3118 jlong size = 3119 java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread)); 3120 // Allocate the C++ Thread structure and create the native thread. The 3121 // stack size retrieved from java is signed, but the constructor takes 3122 // size_t (an unsigned type), so avoid passing negative values which would 3123 // result in really large stacks. 3124 size_t sz = size > 0 ? (size_t) size : 0; 3125 native_thread = new JavaThread(&thread_entry, sz); 3126 3127 // At this point it may be possible that no osthread was created for the 3128 // JavaThread due to lack of memory. Check for this situation and throw 3129 // an exception if necessary. Eventually we may want to change this so 3130 // that we only grab the lock if the thread was created successfully - 3131 // then we can also do this check and throw the exception in the 3132 // JavaThread constructor. 3133 if (native_thread->osthread() != NULL) { 3134 // Note: the current thread is not being used within "prepare". 3135 native_thread->prepare(jthread); 3136 } 3137 } 3138 } 3139 3140 if (throw_illegal_thread_state) { 3141 THROW(vmSymbols::java_lang_IllegalThreadStateException()); 3142 } 3143 3144 assert(native_thread != NULL, "Starting null thread?"); 3145 3146 if (native_thread->osthread() == NULL) { 3147 // No one should hold a reference to the 'native_thread'. 3148 delete native_thread; 3149 if (JvmtiExport::should_post_resource_exhausted()) { 3150 JvmtiExport::post_resource_exhausted( 3151 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS, 3152 "unable to create new native thread"); 3153 } 3154 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), 3155 "unable to create new native thread"); 3156 } 3157 3158 Thread::start(native_thread); 3159 3160 JVM_END 3161 3162 // JVM_Stop is implemented using a VM_Operation, so threads are forced to safepoints 3163 // before the quasi-asynchronous exception is delivered. This is a little obtrusive, 3164 // but is thought to be reliable and simple. In the case, where the receiver is the 3165 // same thread as the sender, no safepoint is needed. 3166 JVM_ENTRY(void, JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable)) 3167 JVMWrapper("JVM_StopThread"); 3168 3169 oop java_throwable = JNIHandles::resolve(throwable); 3170 if (java_throwable == NULL) { 3171 THROW(vmSymbols::java_lang_NullPointerException()); 3172 } 3173 oop java_thread = JNIHandles::resolve_non_null(jthread); 3174 JavaThread* receiver = java_lang_Thread::thread(java_thread); 3175 Events::log_exception(JavaThread::current(), 3176 "JVM_StopThread thread JavaThread " INTPTR_FORMAT " as oop " INTPTR_FORMAT " [exception " INTPTR_FORMAT "]", 3177 p2i(receiver), p2i((address)java_thread), p2i(throwable)); 3178 // First check if thread is alive 3179 if (receiver != NULL) { 3180 // Check if exception is getting thrown at self (use oop equality, since the 3181 // target object might exit) 3182 if (java_thread == thread->threadObj()) { 3183 THROW_OOP(java_throwable); 3184 } else { 3185 // Enques a VM_Operation to stop all threads and then deliver the exception... 3186 Thread::send_async_exception(java_thread, JNIHandles::resolve(throwable)); 3187 } 3188 } 3189 else { 3190 // Either: 3191 // - target thread has not been started before being stopped, or 3192 // - target thread already terminated 3193 // We could read the threadStatus to determine which case it is 3194 // but that is overkill as it doesn't matter. We must set the 3195 // stillborn flag for the first case, and if the thread has already 3196 // exited setting this flag has no affect 3197 java_lang_Thread::set_stillborn(java_thread); 3198 } 3199 JVM_END 3200 3201 3202 JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread)) 3203 JVMWrapper("JVM_IsThreadAlive"); 3204 3205 oop thread_oop = JNIHandles::resolve_non_null(jthread); 3206 return java_lang_Thread::is_alive(thread_oop); 3207 JVM_END 3208 3209 3210 JVM_ENTRY(void, JVM_SuspendThread(JNIEnv* env, jobject jthread)) 3211 JVMWrapper("JVM_SuspendThread"); 3212 oop java_thread = JNIHandles::resolve_non_null(jthread); 3213 JavaThread* receiver = java_lang_Thread::thread(java_thread); 3214 3215 if (receiver != NULL) { 3216 // thread has run and has not exited (still on threads list) 3217 3218 { 3219 MutexLockerEx ml(receiver->SR_lock(), Mutex::_no_safepoint_check_flag); 3220 if (receiver->is_external_suspend()) { 3221 // Don't allow nested external suspend requests. We can't return 3222 // an error from this interface so just ignore the problem. 3223 return; 3224 } 3225 if (receiver->is_exiting()) { // thread is in the process of exiting 3226 return; 3227 } 3228 receiver->set_external_suspend(); 3229 } 3230 3231 // java_suspend() will catch threads in the process of exiting 3232 // and will ignore them. 3233 receiver->java_suspend(); 3234 3235 // It would be nice to have the following assertion in all the 3236 // time, but it is possible for a racing resume request to have 3237 // resumed this thread right after we suspended it. Temporarily 3238 // enable this assertion if you are chasing a different kind of 3239 // bug. 3240 // 3241 // assert(java_lang_Thread::thread(receiver->threadObj()) == NULL || 3242 // receiver->is_being_ext_suspended(), "thread is not suspended"); 3243 } 3244 JVM_END 3245 3246 3247 JVM_ENTRY(void, JVM_ResumeThread(JNIEnv* env, jobject jthread)) 3248 JVMWrapper("JVM_ResumeThread"); 3249 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate. 3250 // We need to *always* get the threads lock here, since this operation cannot be allowed during 3251 // a safepoint. The safepoint code relies on suspending a thread to examine its state. If other 3252 // threads randomly resumes threads, then a thread might not be suspended when the safepoint code 3253 // looks at it. 3254 MutexLocker ml(Threads_lock); 3255 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)); 3256 if (thr != NULL) { 3257 // the thread has run and is not in the process of exiting 3258 thr->java_resume(); 3259 } 3260 JVM_END 3261 3262 3263 JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio)) 3264 JVMWrapper("JVM_SetThreadPriority"); 3265 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate 3266 MutexLocker ml(Threads_lock); 3267 oop java_thread = JNIHandles::resolve_non_null(jthread); 3268 java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio); 3269 JavaThread* thr = java_lang_Thread::thread(java_thread); 3270 if (thr != NULL) { // Thread not yet started; priority pushed down when it is 3271 Thread::set_priority(thr, (ThreadPriority)prio); 3272 } 3273 JVM_END 3274 3275 3276 JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass)) 3277 JVMWrapper("JVM_Yield"); 3278 if (os::dont_yield()) return; 3279 #ifndef USDT2 3280 HS_DTRACE_PROBE0(hotspot, thread__yield); 3281 #else /* USDT2 */ 3282 HOTSPOT_THREAD_YIELD(); 3283 #endif /* USDT2 */ 3284 // When ConvertYieldToSleep is off (default), this matches the classic VM use of yield. 3285 // Critical for similar threading behaviour 3286 if (ConvertYieldToSleep) { 3287 os::sleep(thread, MinSleepInterval, false); 3288 } else { 3289 os::yield(); 3290 } 3291 JVM_END 3292 3293 #if INCLUDE_JFR 3294 static void post_thread_sleep_event(EventThreadSleep* event, jlong millis) { 3295 assert(event != NULL, "invariant"); 3296 assert(event->should_commit(), "invariant"); 3297 event->set_time(millis); 3298 event->commit(); 3299 } 3300 #endif 3301 3302 JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis)) 3303 JVMWrapper("JVM_Sleep"); 3304 3305 if (millis < 0) { 3306 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative"); 3307 } 3308 3309 if (Thread::is_interrupted (THREAD, true) && !HAS_PENDING_EXCEPTION) { 3310 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted"); 3311 } 3312 3313 // Save current thread state and restore it at the end of this block. 3314 // And set new thread state to SLEEPING. 3315 JavaThreadSleepState jtss(thread); 3316 3317 #ifndef USDT2 3318 HS_DTRACE_PROBE1(hotspot, thread__sleep__begin, millis); 3319 #else /* USDT2 */ 3320 HOTSPOT_THREAD_SLEEP_BEGIN( 3321 millis); 3322 #endif /* USDT2 */ 3323 3324 #if INCLUDE_JFR 3325 EventThreadSleep event; 3326 #endif 3327 3328 if (millis == 0) { 3329 // When ConvertSleepToYield is on, this matches the classic VM implementation of 3330 // JVM_Sleep. Critical for similar threading behaviour (Win32) 3331 // It appears that in certain GUI contexts, it may be beneficial to do a short sleep 3332 // for SOLARIS 3333 if (ConvertSleepToYield) { 3334 os::yield(); 3335 } else { 3336 ThreadState old_state = thread->osthread()->get_state(); 3337 thread->osthread()->set_state(SLEEPING); 3338 os::sleep(thread, MinSleepInterval, false); 3339 thread->osthread()->set_state(old_state); 3340 } 3341 } else { 3342 ThreadState old_state = thread->osthread()->get_state(); 3343 thread->osthread()->set_state(SLEEPING); 3344 if (os::sleep(thread, millis, true) == OS_INTRPT) { 3345 // An asynchronous exception (e.g., ThreadDeathException) could have been thrown on 3346 // us while we were sleeping. We do not overwrite those. 3347 if (!HAS_PENDING_EXCEPTION) { 3348 #if INCLUDE_JFR 3349 if (event.should_commit()) { 3350 post_thread_sleep_event(&event, millis); 3351 } 3352 #endif 3353 #ifndef USDT2 3354 HS_DTRACE_PROBE1(hotspot, thread__sleep__end,1); 3355 #else /* USDT2 */ 3356 HOTSPOT_THREAD_SLEEP_END( 3357 1); 3358 #endif /* USDT2 */ 3359 // TODO-FIXME: THROW_MSG returns which means we will not call set_state() 3360 // to properly restore the thread state. That's likely wrong. 3361 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted"); 3362 } 3363 } 3364 thread->osthread()->set_state(old_state); 3365 } 3366 #if INCLUDE_JFR 3367 if (event.should_commit()) { 3368 post_thread_sleep_event(&event, millis); 3369 } 3370 #endif 3371 #ifndef USDT2 3372 HS_DTRACE_PROBE1(hotspot, thread__sleep__end,0); 3373 #else /* USDT2 */ 3374 HOTSPOT_THREAD_SLEEP_END( 3375 0); 3376 #endif /* USDT2 */ 3377 JVM_END 3378 3379 JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass)) 3380 JVMWrapper("JVM_CurrentThread"); 3381 oop jthread = thread->threadObj(); 3382 assert (thread != NULL, "no current thread!"); 3383 return JNIHandles::make_local(env, jthread); 3384 JVM_END 3385 3386 3387 JVM_ENTRY(jint, JVM_CountStackFrames(JNIEnv* env, jobject jthread)) 3388 JVMWrapper("JVM_CountStackFrames"); 3389 3390 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate 3391 oop java_thread = JNIHandles::resolve_non_null(jthread); 3392 bool throw_illegal_thread_state = false; 3393 int count = 0; 3394 3395 { 3396 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock); 3397 // We need to re-resolve the java_thread, since a GC might have happened during the 3398 // acquire of the lock 3399 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)); 3400 3401 if (thr == NULL) { 3402 // do nothing 3403 } else if(! thr->is_external_suspend() || ! thr->frame_anchor()->walkable()) { 3404 // Check whether this java thread has been suspended already. If not, throws 3405 // IllegalThreadStateException. We defer to throw that exception until 3406 // Threads_lock is released since loading exception class has to leave VM. 3407 // The correct way to test a thread is actually suspended is 3408 // wait_for_ext_suspend_completion(), but we can't call that while holding 3409 // the Threads_lock. The above tests are sufficient for our purposes 3410 // provided the walkability of the stack is stable - which it isn't 3411 // 100% but close enough for most practical purposes. 3412 throw_illegal_thread_state = true; 3413 } else { 3414 // Count all java activation, i.e., number of vframes 3415 for(vframeStream vfst(thr); !vfst.at_end(); vfst.next()) { 3416 // Native frames are not counted 3417 if (!vfst.method()->is_native()) count++; 3418 } 3419 } 3420 } 3421 3422 if (throw_illegal_thread_state) { 3423 THROW_MSG_0(vmSymbols::java_lang_IllegalThreadStateException(), 3424 "this thread is not suspended"); 3425 } 3426 return count; 3427 JVM_END 3428 3429 // Consider: A better way to implement JVM_Interrupt() is to acquire 3430 // Threads_lock to resolve the jthread into a Thread pointer, fetch 3431 // Thread->platformevent, Thread->native_thr, Thread->parker, etc., 3432 // drop Threads_lock, and the perform the unpark() and thr_kill() operations 3433 // outside the critical section. Threads_lock is hot so we want to minimize 3434 // the hold-time. A cleaner interface would be to decompose interrupt into 3435 // two steps. The 1st phase, performed under Threads_lock, would return 3436 // a closure that'd be invoked after Threads_lock was dropped. 3437 // This tactic is safe as PlatformEvent and Parkers are type-stable (TSM) and 3438 // admit spurious wakeups. 3439 3440 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread)) 3441 JVMWrapper("JVM_Interrupt"); 3442 3443 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate 3444 oop java_thread = JNIHandles::resolve_non_null(jthread); 3445 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock); 3446 // We need to re-resolve the java_thread, since a GC might have happened during the 3447 // acquire of the lock 3448 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)); 3449 if (thr != NULL) { 3450 Thread::interrupt(thr); 3451 } 3452 JVM_END 3453 3454 3455 JVM_QUICK_ENTRY(jboolean, JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted)) 3456 JVMWrapper("JVM_IsInterrupted"); 3457 3458 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate 3459 oop java_thread = JNIHandles::resolve_non_null(jthread); 3460 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock); 3461 // We need to re-resolve the java_thread, since a GC might have happened during the 3462 // acquire of the lock 3463 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)); 3464 if (thr == NULL) { 3465 return JNI_FALSE; 3466 } else { 3467 return (jboolean) Thread::is_interrupted(thr, clear_interrupted != 0); 3468 } 3469 JVM_END 3470 3471 3472 // Return true iff the current thread has locked the object passed in 3473 3474 JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj)) 3475 JVMWrapper("JVM_HoldsLock"); 3476 assert(THREAD->is_Java_thread(), "sanity check"); 3477 if (obj == NULL) { 3478 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE); 3479 } 3480 Handle h_obj(THREAD, JNIHandles::resolve(obj)); 3481 return ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, h_obj); 3482 JVM_END 3483 3484 3485 JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass)) 3486 JVMWrapper("JVM_DumpAllStacks"); 3487 VM_PrintThreads op; 3488 VMThread::execute(&op); 3489 if (JvmtiExport::should_post_data_dump()) { 3490 JvmtiExport::post_data_dump(); 3491 } 3492 JVM_END 3493 3494 JVM_ENTRY(void, JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring name)) 3495 JVMWrapper("JVM_SetNativeThreadName"); 3496 ResourceMark rm(THREAD); 3497 oop java_thread = JNIHandles::resolve_non_null(jthread); 3498 JavaThread* thr = java_lang_Thread::thread(java_thread); 3499 // Thread naming only supported for the current thread, doesn't work for 3500 // target threads. 3501 if (Thread::current() == thr && !thr->has_attached_via_jni()) { 3502 // we don't set the name of an attached thread to avoid stepping 3503 // on other programs 3504 const char *thread_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name)); 3505 os::set_native_thread_name(thread_name); 3506 } 3507 JVM_END 3508 3509 // java.lang.SecurityManager /////////////////////////////////////////////////////////////////////// 3510 3511 static bool is_trusted_frame(JavaThread* jthread, vframeStream* vfst) { 3512 assert(jthread->is_Java_thread(), "must be a Java thread"); 3513 if (jthread->privileged_stack_top() == NULL) return false; 3514 if (jthread->privileged_stack_top()->frame_id() == vfst->frame_id()) { 3515 oop loader = jthread->privileged_stack_top()->class_loader(); 3516 if (loader == NULL) return true; 3517 bool trusted = java_lang_ClassLoader::is_trusted_loader(loader); 3518 if (trusted) return true; 3519 } 3520 return false; 3521 } 3522 3523 JVM_ENTRY(jclass, JVM_CurrentLoadedClass(JNIEnv *env)) 3524 JVMWrapper("JVM_CurrentLoadedClass"); 3525 ResourceMark rm(THREAD); 3526 3527 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) { 3528 // if a method in a class in a trusted loader is in a doPrivileged, return NULL 3529 bool trusted = is_trusted_frame(thread, &vfst); 3530 if (trusted) return NULL; 3531 3532 Method* m = vfst.method(); 3533 if (!m->is_native()) { 3534 InstanceKlass* holder = m->method_holder(); 3535 oop loader = holder->class_loader(); 3536 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) { 3537 return (jclass) JNIHandles::make_local(env, holder->java_mirror()); 3538 } 3539 } 3540 } 3541 return NULL; 3542 JVM_END 3543 3544 3545 JVM_ENTRY(jobject, JVM_CurrentClassLoader(JNIEnv *env)) 3546 JVMWrapper("JVM_CurrentClassLoader"); 3547 ResourceMark rm(THREAD); 3548 3549 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) { 3550 3551 // if a method in a class in a trusted loader is in a doPrivileged, return NULL 3552 bool trusted = is_trusted_frame(thread, &vfst); 3553 if (trusted) return NULL; 3554 3555 Method* m = vfst.method(); 3556 if (!m->is_native()) { 3557 InstanceKlass* holder = m->method_holder(); 3558 assert(holder->is_klass(), "just checking"); 3559 oop loader = holder->class_loader(); 3560 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) { 3561 return JNIHandles::make_local(env, loader); 3562 } 3563 } 3564 } 3565 return NULL; 3566 JVM_END 3567 3568 3569 JVM_ENTRY(jobjectArray, JVM_GetClassContext(JNIEnv *env)) 3570 JVMWrapper("JVM_GetClassContext"); 3571 ResourceMark rm(THREAD); 3572 JvmtiVMObjectAllocEventCollector oam; 3573 vframeStream vfst(thread); 3574 3575 if (SystemDictionary::reflect_CallerSensitive_klass() != NULL) { 3576 // This must only be called from SecurityManager.getClassContext 3577 Method* m = vfst.method(); 3578 if (!(m->method_holder() == SystemDictionary::SecurityManager_klass() && 3579 m->name() == vmSymbols::getClassContext_name() && 3580 m->signature() == vmSymbols::void_class_array_signature())) { 3581 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetClassContext must only be called from SecurityManager.getClassContext"); 3582 } 3583 } 3584 3585 // Collect method holders 3586 GrowableArray<KlassHandle>* klass_array = new GrowableArray<KlassHandle>(); 3587 for (; !vfst.at_end(); vfst.security_next()) { 3588 Method* m = vfst.method(); 3589 // Native frames are not returned 3590 if (!m->is_ignored_by_security_stack_walk() && !m->is_native()) { 3591 Klass* holder = m->method_holder(); 3592 assert(holder->is_klass(), "just checking"); 3593 klass_array->append(holder); 3594 } 3595 } 3596 3597 // Create result array of type [Ljava/lang/Class; 3598 objArrayOop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), klass_array->length(), CHECK_NULL); 3599 // Fill in mirrors corresponding to method holders 3600 for (int i = 0; i < klass_array->length(); i++) { 3601 result->obj_at_put(i, klass_array->at(i)->java_mirror()); 3602 } 3603 3604 return (jobjectArray) JNIHandles::make_local(env, result); 3605 JVM_END 3606 3607 3608 JVM_ENTRY(jint, JVM_ClassDepth(JNIEnv *env, jstring name)) 3609 JVMWrapper("JVM_ClassDepth"); 3610 ResourceMark rm(THREAD); 3611 Handle h_name (THREAD, JNIHandles::resolve_non_null(name)); 3612 Handle class_name_str = java_lang_String::internalize_classname(h_name, CHECK_0); 3613 3614 const char* str = java_lang_String::as_utf8_string(class_name_str()); 3615 TempNewSymbol class_name_sym = SymbolTable::probe(str, (int)strlen(str)); 3616 if (class_name_sym == NULL) { 3617 return -1; 3618 } 3619 3620 int depth = 0; 3621 3622 for(vframeStream vfst(thread); !vfst.at_end(); vfst.next()) { 3623 if (!vfst.method()->is_native()) { 3624 InstanceKlass* holder = vfst.method()->method_holder(); 3625 assert(holder->is_klass(), "just checking"); 3626 if (holder->name() == class_name_sym) { 3627 return depth; 3628 } 3629 depth++; 3630 } 3631 } 3632 return -1; 3633 JVM_END 3634 3635 3636 JVM_ENTRY(jint, JVM_ClassLoaderDepth(JNIEnv *env)) 3637 JVMWrapper("JVM_ClassLoaderDepth"); 3638 ResourceMark rm(THREAD); 3639 int depth = 0; 3640 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) { 3641 // if a method in a class in a trusted loader is in a doPrivileged, return -1 3642 bool trusted = is_trusted_frame(thread, &vfst); 3643 if (trusted) return -1; 3644 3645 Method* m = vfst.method(); 3646 if (!m->is_native()) { 3647 InstanceKlass* holder = m->method_holder(); 3648 assert(holder->is_klass(), "just checking"); 3649 oop loader = holder->class_loader(); 3650 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) { 3651 return depth; 3652 } 3653 depth++; 3654 } 3655 } 3656 return -1; 3657 JVM_END 3658 3659 3660 // java.lang.Package //////////////////////////////////////////////////////////////// 3661 3662 3663 JVM_ENTRY(jstring, JVM_GetSystemPackage(JNIEnv *env, jstring name)) 3664 JVMWrapper("JVM_GetSystemPackage"); 3665 ResourceMark rm(THREAD); 3666 JvmtiVMObjectAllocEventCollector oam; 3667 char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name)); 3668 oop result = ClassLoader::get_system_package(str, CHECK_NULL); 3669 return (jstring) JNIHandles::make_local(result); 3670 JVM_END 3671 3672 3673 JVM_ENTRY(jobjectArray, JVM_GetSystemPackages(JNIEnv *env)) 3674 JVMWrapper("JVM_GetSystemPackages"); 3675 JvmtiVMObjectAllocEventCollector oam; 3676 objArrayOop result = ClassLoader::get_system_packages(CHECK_NULL); 3677 return (jobjectArray) JNIHandles::make_local(result); 3678 JVM_END 3679 3680 3681 // ObjectInputStream /////////////////////////////////////////////////////////////// 3682 3683 bool force_verify_field_access(Klass* current_class, Klass* field_class, AccessFlags access, bool classloader_only) { 3684 if (current_class == NULL) { 3685 return true; 3686 } 3687 if ((current_class == field_class) || access.is_public()) { 3688 return true; 3689 } 3690 3691 if (access.is_protected()) { 3692 // See if current_class is a subclass of field_class 3693 if (current_class->is_subclass_of(field_class)) { 3694 return true; 3695 } 3696 } 3697 3698 return (!access.is_private() && InstanceKlass::cast(current_class)->is_same_class_package(field_class)); 3699 } 3700 3701 3702 // JVM_AllocateNewObject and JVM_AllocateNewArray are unused as of 1.4 3703 JVM_ENTRY(jobject, JVM_AllocateNewObject(JNIEnv *env, jobject receiver, jclass currClass, jclass initClass)) 3704 JVMWrapper("JVM_AllocateNewObject"); 3705 JvmtiVMObjectAllocEventCollector oam; 3706 // Receiver is not used 3707 oop curr_mirror = JNIHandles::resolve_non_null(currClass); 3708 oop init_mirror = JNIHandles::resolve_non_null(initClass); 3709 3710 // Cannot instantiate primitive types 3711 if (java_lang_Class::is_primitive(curr_mirror) || java_lang_Class::is_primitive(init_mirror)) { 3712 ResourceMark rm(THREAD); 3713 THROW_0(vmSymbols::java_lang_InvalidClassException()); 3714 } 3715 3716 // Arrays not allowed here, must use JVM_AllocateNewArray 3717 if (java_lang_Class::as_Klass(curr_mirror)->oop_is_array() || 3718 java_lang_Class::as_Klass(init_mirror)->oop_is_array()) { 3719 ResourceMark rm(THREAD); 3720 THROW_0(vmSymbols::java_lang_InvalidClassException()); 3721 } 3722 3723 instanceKlassHandle curr_klass (THREAD, java_lang_Class::as_Klass(curr_mirror)); 3724 instanceKlassHandle init_klass (THREAD, java_lang_Class::as_Klass(init_mirror)); 3725 3726 assert(curr_klass->is_subclass_of(init_klass()), "just checking"); 3727 3728 // Interfaces, abstract classes, and java.lang.Class classes cannot be instantiated directly. 3729 curr_klass->check_valid_for_instantiation(false, CHECK_NULL); 3730 3731 // Make sure klass is initialized, since we are about to instantiate one of them. 3732 curr_klass->initialize(CHECK_NULL); 3733 3734 methodHandle m (THREAD, 3735 init_klass->find_method(vmSymbols::object_initializer_name(), 3736 vmSymbols::void_method_signature())); 3737 if (m.is_null()) { 3738 ResourceMark rm(THREAD); 3739 THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), 3740 Method::name_and_sig_as_C_string(init_klass(), 3741 vmSymbols::object_initializer_name(), 3742 vmSymbols::void_method_signature())); 3743 } 3744 3745 if (curr_klass == init_klass && !m->is_public()) { 3746 // Calling the constructor for class 'curr_klass'. 3747 // Only allow calls to a public no-arg constructor. 3748 // This path corresponds to creating an Externalizable object. 3749 THROW_0(vmSymbols::java_lang_IllegalAccessException()); 3750 } 3751 3752 if (!force_verify_field_access(curr_klass(), init_klass(), m->access_flags(), false)) { 3753 // subclass 'curr_klass' does not have access to no-arg constructor of 'initcb' 3754 THROW_0(vmSymbols::java_lang_IllegalAccessException()); 3755 } 3756 3757 Handle obj = curr_klass->allocate_instance_handle(CHECK_NULL); 3758 // Call constructor m. This might call a constructor higher up in the hierachy 3759 JavaCalls::call_default_constructor(thread, m, obj, CHECK_NULL); 3760 3761 return JNIHandles::make_local(obj()); 3762 JVM_END 3763 3764 3765 JVM_ENTRY(jobject, JVM_AllocateNewArray(JNIEnv *env, jobject obj, jclass currClass, jint length)) 3766 JVMWrapper("JVM_AllocateNewArray"); 3767 JvmtiVMObjectAllocEventCollector oam; 3768 oop mirror = JNIHandles::resolve_non_null(currClass); 3769 3770 if (java_lang_Class::is_primitive(mirror)) { 3771 THROW_0(vmSymbols::java_lang_InvalidClassException()); 3772 } 3773 Klass* k = java_lang_Class::as_Klass(mirror); 3774 oop result; 3775 3776 if (k->oop_is_typeArray()) { 3777 // typeArray 3778 result = TypeArrayKlass::cast(k)->allocate(length, CHECK_NULL); 3779 } else if (k->oop_is_objArray()) { 3780 // objArray 3781 ObjArrayKlass* oak = ObjArrayKlass::cast(k); 3782 oak->initialize(CHECK_NULL); // make sure class is initialized (matches Classic VM behavior) 3783 result = oak->allocate(length, CHECK_NULL); 3784 } else { 3785 THROW_0(vmSymbols::java_lang_InvalidClassException()); 3786 } 3787 return JNIHandles::make_local(env, result); 3788 JVM_END 3789 3790 3791 // Returns first non-privileged class loader on the stack (excluding reflection 3792 // generated frames) or null if only classes loaded by the boot class loader 3793 // and extension class loader are found on the stack. 3794 3795 JVM_ENTRY(jobject, JVM_LatestUserDefinedLoader(JNIEnv *env)) 3796 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) { 3797 // UseNewReflection 3798 vfst.skip_reflection_related_frames(); // Only needed for 1.4 reflection 3799 oop loader = vfst.method()->method_holder()->class_loader(); 3800 if (loader != NULL && !SystemDictionary::is_ext_class_loader(loader)) { 3801 return JNIHandles::make_local(env, loader); 3802 } 3803 } 3804 return NULL; 3805 JVM_END 3806 3807 3808 // Load a class relative to the most recent class on the stack with a non-null 3809 // classloader. 3810 // This function has been deprecated and should not be considered part of the 3811 // specified JVM interface. 3812 3813 JVM_ENTRY(jclass, JVM_LoadClass0(JNIEnv *env, jobject receiver, 3814 jclass currClass, jstring currClassName)) 3815 JVMWrapper("JVM_LoadClass0"); 3816 // Receiver is not used 3817 ResourceMark rm(THREAD); 3818 3819 // Class name argument is not guaranteed to be in internal format 3820 Handle classname (THREAD, JNIHandles::resolve_non_null(currClassName)); 3821 Handle string = java_lang_String::internalize_classname(classname, CHECK_NULL); 3822 3823 const char* str = java_lang_String::as_utf8_string(string()); 3824 3825 if (str == NULL || (int)strlen(str) > Symbol::max_length()) { 3826 // It's impossible to create this class; the name cannot fit 3827 // into the constant pool. 3828 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), str); 3829 } 3830 3831 TempNewSymbol name = SymbolTable::new_symbol(str, CHECK_NULL); 3832 Handle curr_klass (THREAD, JNIHandles::resolve(currClass)); 3833 // Find the most recent class on the stack with a non-null classloader 3834 oop loader = NULL; 3835 oop protection_domain = NULL; 3836 if (curr_klass.is_null()) { 3837 for (vframeStream vfst(thread); 3838 !vfst.at_end() && loader == NULL; 3839 vfst.next()) { 3840 if (!vfst.method()->is_native()) { 3841 InstanceKlass* holder = vfst.method()->method_holder(); 3842 loader = holder->class_loader(); 3843 protection_domain = holder->protection_domain(); 3844 } 3845 } 3846 } else { 3847 Klass* curr_klass_oop = java_lang_Class::as_Klass(curr_klass()); 3848 loader = InstanceKlass::cast(curr_klass_oop)->class_loader(); 3849 protection_domain = InstanceKlass::cast(curr_klass_oop)->protection_domain(); 3850 } 3851 Handle h_loader(THREAD, loader); 3852 Handle h_prot (THREAD, protection_domain); 3853 jclass result = find_class_from_class_loader(env, name, true, h_loader, h_prot, 3854 false, thread); 3855 if (TraceClassResolution && result != NULL) { 3856 trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result))); 3857 } 3858 return result; 3859 JVM_END 3860 3861 3862 // Array /////////////////////////////////////////////////////////////////////////////////////////// 3863 3864 3865 // resolve array handle and check arguments 3866 static inline arrayOop check_array(JNIEnv *env, jobject arr, bool type_array_only, TRAPS) { 3867 if (arr == NULL) { 3868 THROW_0(vmSymbols::java_lang_NullPointerException()); 3869 } 3870 oop a = JNIHandles::resolve_non_null(arr); 3871 if (!a->is_array() || (type_array_only && !a->is_typeArray())) { 3872 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array"); 3873 } 3874 return arrayOop(a); 3875 } 3876 3877 3878 JVM_ENTRY(jint, JVM_GetArrayLength(JNIEnv *env, jobject arr)) 3879 JVMWrapper("JVM_GetArrayLength"); 3880 arrayOop a = check_array(env, arr, false, CHECK_0); 3881 return a->length(); 3882 JVM_END 3883 3884 3885 JVM_ENTRY(jobject, JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index)) 3886 JVMWrapper("JVM_Array_Get"); 3887 JvmtiVMObjectAllocEventCollector oam; 3888 arrayOop a = check_array(env, arr, false, CHECK_NULL); 3889 jvalue value; 3890 BasicType type = Reflection::array_get(&value, a, index, CHECK_NULL); 3891 oop box = Reflection::box(&value, type, CHECK_NULL); 3892 return JNIHandles::make_local(env, box); 3893 JVM_END 3894 3895 3896 JVM_ENTRY(jvalue, JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode)) 3897 JVMWrapper("JVM_GetPrimitiveArrayElement"); 3898 jvalue value; 3899 value.i = 0; // to initialize value before getting used in CHECK 3900 arrayOop a = check_array(env, arr, true, CHECK_(value)); 3901 assert(a->is_typeArray(), "just checking"); 3902 BasicType type = Reflection::array_get(&value, a, index, CHECK_(value)); 3903 BasicType wide_type = (BasicType) wCode; 3904 if (type != wide_type) { 3905 Reflection::widen(&value, type, wide_type, CHECK_(value)); 3906 } 3907 return value; 3908 JVM_END 3909 3910 3911 JVM_ENTRY(void, JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val)) 3912 JVMWrapper("JVM_SetArrayElement"); 3913 arrayOop a = check_array(env, arr, false, CHECK); 3914 oop box = JNIHandles::resolve(val); 3915 jvalue value; 3916 value.i = 0; // to initialize value before getting used in CHECK 3917 BasicType value_type; 3918 if (a->is_objArray()) { 3919 // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array 3920 value_type = Reflection::unbox_for_regular_object(box, &value); 3921 } else { 3922 value_type = Reflection::unbox_for_primitive(box, &value, CHECK); 3923 } 3924 Reflection::array_set(&value, a, index, value_type, CHECK); 3925 JVM_END 3926 3927 3928 JVM_ENTRY(void, JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode)) 3929 JVMWrapper("JVM_SetPrimitiveArrayElement"); 3930 arrayOop a = check_array(env, arr, true, CHECK); 3931 assert(a->is_typeArray(), "just checking"); 3932 BasicType value_type = (BasicType) vCode; 3933 Reflection::array_set(&v, a, index, value_type, CHECK); 3934 JVM_END 3935 3936 3937 JVM_ENTRY(jobject, JVM_NewArray(JNIEnv *env, jclass eltClass, jint length)) 3938 JVMWrapper("JVM_NewArray"); 3939 JvmtiVMObjectAllocEventCollector oam; 3940 oop element_mirror = JNIHandles::resolve(eltClass); 3941 oop result = Reflection::reflect_new_array(element_mirror, length, CHECK_NULL); 3942 return JNIHandles::make_local(env, result); 3943 JVM_END 3944 3945 3946 JVM_ENTRY(jobject, JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim)) 3947 JVMWrapper("JVM_NewMultiArray"); 3948 JvmtiVMObjectAllocEventCollector oam; 3949 arrayOop dim_array = check_array(env, dim, true, CHECK_NULL); 3950 oop element_mirror = JNIHandles::resolve(eltClass); 3951 assert(dim_array->is_typeArray(), "just checking"); 3952 oop result = Reflection::reflect_new_multi_array(element_mirror, typeArrayOop(dim_array), CHECK_NULL); 3953 return JNIHandles::make_local(env, result); 3954 JVM_END 3955 3956 3957 // Networking library support //////////////////////////////////////////////////////////////////// 3958 3959 JVM_LEAF(jint, JVM_InitializeSocketLibrary()) 3960 JVMWrapper("JVM_InitializeSocketLibrary"); 3961 return 0; 3962 JVM_END 3963 3964 3965 JVM_LEAF(jint, JVM_Socket(jint domain, jint type, jint protocol)) 3966 JVMWrapper("JVM_Socket"); 3967 return os::socket(domain, type, protocol); 3968 JVM_END 3969 3970 3971 JVM_LEAF(jint, JVM_SocketClose(jint fd)) 3972 JVMWrapper2("JVM_SocketClose (0x%x)", fd); 3973 //%note jvm_r6 3974 return os::socket_close(fd); 3975 JVM_END 3976 3977 3978 JVM_LEAF(jint, JVM_SocketShutdown(jint fd, jint howto)) 3979 JVMWrapper2("JVM_SocketShutdown (0x%x)", fd); 3980 //%note jvm_r6 3981 return os::socket_shutdown(fd, howto); 3982 JVM_END 3983 3984 3985 JVM_LEAF(jint, JVM_Recv(jint fd, char *buf, jint nBytes, jint flags)) 3986 JVMWrapper2("JVM_Recv (0x%x)", fd); 3987 //%note jvm_r6 3988 return os::recv(fd, buf, (size_t)nBytes, (uint)flags); 3989 JVM_END 3990 3991 3992 JVM_LEAF(jint, JVM_Send(jint fd, char *buf, jint nBytes, jint flags)) 3993 JVMWrapper2("JVM_Send (0x%x)", fd); 3994 //%note jvm_r6 3995 return os::send(fd, buf, (size_t)nBytes, (uint)flags); 3996 JVM_END 3997 3998 3999 JVM_LEAF(jint, JVM_Timeout(int fd, long timeout)) 4000 JVMWrapper2("JVM_Timeout (0x%x)", fd); 4001 //%note jvm_r6 4002 return os::timeout(fd, timeout); 4003 JVM_END 4004 4005 4006 JVM_LEAF(jint, JVM_Listen(jint fd, jint count)) 4007 JVMWrapper2("JVM_Listen (0x%x)", fd); 4008 //%note jvm_r6 4009 return os::listen(fd, count); 4010 JVM_END 4011 4012 4013 JVM_LEAF(jint, JVM_Connect(jint fd, struct sockaddr *him, jint len)) 4014 JVMWrapper2("JVM_Connect (0x%x)", fd); 4015 //%note jvm_r6 4016 return os::connect(fd, him, (socklen_t)len); 4017 JVM_END 4018 4019 4020 JVM_LEAF(jint, JVM_Bind(jint fd, struct sockaddr *him, jint len)) 4021 JVMWrapper2("JVM_Bind (0x%x)", fd); 4022 //%note jvm_r6 4023 return os::bind(fd, him, (socklen_t)len); 4024 JVM_END 4025 4026 4027 JVM_LEAF(jint, JVM_Accept(jint fd, struct sockaddr *him, jint *len)) 4028 JVMWrapper2("JVM_Accept (0x%x)", fd); 4029 //%note jvm_r6 4030 socklen_t socklen = (socklen_t)(*len); 4031 jint result = os::accept(fd, him, &socklen); 4032 *len = (jint)socklen; 4033 return result; 4034 JVM_END 4035 4036 4037 JVM_LEAF(jint, JVM_RecvFrom(jint fd, char *buf, int nBytes, int flags, struct sockaddr *from, int *fromlen)) 4038 JVMWrapper2("JVM_RecvFrom (0x%x)", fd); 4039 //%note jvm_r6 4040 socklen_t socklen = (socklen_t)(*fromlen); 4041 jint result = os::recvfrom(fd, buf, (size_t)nBytes, (uint)flags, from, &socklen); 4042 *fromlen = (int)socklen; 4043 return result; 4044 JVM_END 4045 4046 4047 JVM_LEAF(jint, JVM_GetSockName(jint fd, struct sockaddr *him, int *len)) 4048 JVMWrapper2("JVM_GetSockName (0x%x)", fd); 4049 //%note jvm_r6 4050 socklen_t socklen = (socklen_t)(*len); 4051 jint result = os::get_sock_name(fd, him, &socklen); 4052 *len = (int)socklen; 4053 return result; 4054 JVM_END 4055 4056 4057 JVM_LEAF(jint, JVM_SendTo(jint fd, char *buf, int len, int flags, struct sockaddr *to, int tolen)) 4058 JVMWrapper2("JVM_SendTo (0x%x)", fd); 4059 //%note jvm_r6 4060 return os::sendto(fd, buf, (size_t)len, (uint)flags, to, (socklen_t)tolen); 4061 JVM_END 4062 4063 4064 JVM_LEAF(jint, JVM_SocketAvailable(jint fd, jint *pbytes)) 4065 JVMWrapper2("JVM_SocketAvailable (0x%x)", fd); 4066 //%note jvm_r6 4067 return os::socket_available(fd, pbytes); 4068 JVM_END 4069 4070 4071 JVM_LEAF(jint, JVM_GetSockOpt(jint fd, int level, int optname, char *optval, int *optlen)) 4072 JVMWrapper2("JVM_GetSockOpt (0x%x)", fd); 4073 //%note jvm_r6 4074 socklen_t socklen = (socklen_t)(*optlen); 4075 jint result = os::get_sock_opt(fd, level, optname, optval, &socklen); 4076 *optlen = (int)socklen; 4077 return result; 4078 JVM_END 4079 4080 4081 JVM_LEAF(jint, JVM_SetSockOpt(jint fd, int level, int optname, const char *optval, int optlen)) 4082 JVMWrapper2("JVM_GetSockOpt (0x%x)", fd); 4083 //%note jvm_r6 4084 return os::set_sock_opt(fd, level, optname, optval, (socklen_t)optlen); 4085 JVM_END 4086 4087 4088 JVM_LEAF(int, JVM_GetHostName(char* name, int namelen)) 4089 JVMWrapper("JVM_GetHostName"); 4090 return os::get_host_name(name, namelen); 4091 JVM_END 4092 4093 4094 // Library support /////////////////////////////////////////////////////////////////////////// 4095 4096 JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name)) 4097 //%note jvm_ct 4098 JVMWrapper2("JVM_LoadLibrary (%s)", name); 4099 char ebuf[1024]; 4100 void *load_result; 4101 { 4102 ThreadToNativeFromVM ttnfvm(thread); 4103 load_result = os::dll_load(name, ebuf, sizeof ebuf); 4104 } 4105 if (load_result == NULL) { 4106 char msg[1024]; 4107 jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf); 4108 // Since 'ebuf' may contain a string encoded using 4109 // platform encoding scheme, we need to pass 4110 // Exceptions::unsafe_to_utf8 to the new_exception method 4111 // as the last argument. See bug 6367357. 4112 Handle h_exception = 4113 Exceptions::new_exception(thread, 4114 vmSymbols::java_lang_UnsatisfiedLinkError(), 4115 msg, Exceptions::unsafe_to_utf8); 4116 4117 THROW_HANDLE_0(h_exception); 4118 } 4119 return load_result; 4120 JVM_END 4121 4122 4123 JVM_LEAF(void, JVM_UnloadLibrary(void* handle)) 4124 JVMWrapper("JVM_UnloadLibrary"); 4125 os::dll_unload(handle); 4126 JVM_END 4127 4128 4129 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name)) 4130 JVMWrapper2("JVM_FindLibraryEntry (%s)", name); 4131 return os::dll_lookup(handle, name); 4132 JVM_END 4133 4134 4135 // Floating point support //////////////////////////////////////////////////////////////////// 4136 4137 JVM_LEAF(jboolean, JVM_IsNaN(jdouble a)) 4138 JVMWrapper("JVM_IsNaN"); 4139 return g_isnan(a); 4140 JVM_END 4141 4142 4143 // JNI version /////////////////////////////////////////////////////////////////////////////// 4144 4145 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version)) 4146 JVMWrapper2("JVM_IsSupportedJNIVersion (%d)", version); 4147 return Threads::is_supported_jni_version_including_1_1(version); 4148 JVM_END 4149 4150 4151 // String support /////////////////////////////////////////////////////////////////////////// 4152 4153 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str)) 4154 JVMWrapper("JVM_InternString"); 4155 JvmtiVMObjectAllocEventCollector oam; 4156 if (str == NULL) return NULL; 4157 oop string = JNIHandles::resolve_non_null(str); 4158 oop result = StringTable::intern(string, CHECK_NULL); 4159 return (jstring) JNIHandles::make_local(env, result); 4160 JVM_END 4161 4162 4163 // Raw monitor support ////////////////////////////////////////////////////////////////////// 4164 4165 // The lock routine below calls lock_without_safepoint_check in order to get a raw lock 4166 // without interfering with the safepoint mechanism. The routines are not JVM_LEAF because 4167 // they might be called by non-java threads. The JVM_LEAF installs a NoHandleMark check 4168 // that only works with java threads. 4169 4170 4171 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void) { 4172 VM_Exit::block_if_vm_exited(); 4173 JVMWrapper("JVM_RawMonitorCreate"); 4174 return new Mutex(Mutex::native, "JVM_RawMonitorCreate"); 4175 } 4176 4177 4178 JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void *mon) { 4179 VM_Exit::block_if_vm_exited(); 4180 JVMWrapper("JVM_RawMonitorDestroy"); 4181 delete ((Mutex*) mon); 4182 } 4183 4184 4185 JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon) { 4186 VM_Exit::block_if_vm_exited(); 4187 JVMWrapper("JVM_RawMonitorEnter"); 4188 ((Mutex*) mon)->jvm_raw_lock(); 4189 return 0; 4190 } 4191 4192 4193 JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon) { 4194 VM_Exit::block_if_vm_exited(); 4195 JVMWrapper("JVM_RawMonitorExit"); 4196 ((Mutex*) mon)->jvm_raw_unlock(); 4197 } 4198 4199 4200 // Support for Serialization 4201 4202 typedef jfloat (JNICALL *IntBitsToFloatFn )(JNIEnv* env, jclass cb, jint value); 4203 typedef jdouble (JNICALL *LongBitsToDoubleFn)(JNIEnv* env, jclass cb, jlong value); 4204 typedef jint (JNICALL *FloatToIntBitsFn )(JNIEnv* env, jclass cb, jfloat value); 4205 typedef jlong (JNICALL *DoubleToLongBitsFn)(JNIEnv* env, jclass cb, jdouble value); 4206 4207 static IntBitsToFloatFn int_bits_to_float_fn = NULL; 4208 static LongBitsToDoubleFn long_bits_to_double_fn = NULL; 4209 static FloatToIntBitsFn float_to_int_bits_fn = NULL; 4210 static DoubleToLongBitsFn double_to_long_bits_fn = NULL; 4211 4212 4213 void initialize_converter_functions() { 4214 if (JDK_Version::is_gte_jdk14x_version()) { 4215 // These functions only exist for compatibility with 1.3.1 and earlier 4216 return; 4217 } 4218 4219 // called from universe_post_init() 4220 assert( 4221 int_bits_to_float_fn == NULL && 4222 long_bits_to_double_fn == NULL && 4223 float_to_int_bits_fn == NULL && 4224 double_to_long_bits_fn == NULL , 4225 "initialization done twice" 4226 ); 4227 // initialize 4228 int_bits_to_float_fn = CAST_TO_FN_PTR(IntBitsToFloatFn , NativeLookup::base_library_lookup("java/lang/Float" , "intBitsToFloat" , "(I)F")); 4229 long_bits_to_double_fn = CAST_TO_FN_PTR(LongBitsToDoubleFn, NativeLookup::base_library_lookup("java/lang/Double", "longBitsToDouble", "(J)D")); 4230 float_to_int_bits_fn = CAST_TO_FN_PTR(FloatToIntBitsFn , NativeLookup::base_library_lookup("java/lang/Float" , "floatToIntBits" , "(F)I")); 4231 double_to_long_bits_fn = CAST_TO_FN_PTR(DoubleToLongBitsFn, NativeLookup::base_library_lookup("java/lang/Double", "doubleToLongBits", "(D)J")); 4232 // verify 4233 assert( 4234 int_bits_to_float_fn != NULL && 4235 long_bits_to_double_fn != NULL && 4236 float_to_int_bits_fn != NULL && 4237 double_to_long_bits_fn != NULL , 4238 "initialization failed" 4239 ); 4240 } 4241 4242 4243 4244 // Shared JNI/JVM entry points ////////////////////////////////////////////////////////////// 4245 4246 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init, 4247 Handle loader, Handle protection_domain, 4248 jboolean throwError, TRAPS) { 4249 // Security Note: 4250 // The Java level wrapper will perform the necessary security check allowing 4251 // us to pass the NULL as the initiating class loader. The VM is responsible for 4252 // the checkPackageAccess relative to the initiating class loader via the 4253 // protection_domain. The protection_domain is passed as NULL by the java code 4254 // if there is no security manager in 3-arg Class.forName(). 4255 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL); 4256 4257 KlassHandle klass_handle(THREAD, klass); 4258 // Check if we should initialize the class 4259 if (init && klass_handle->oop_is_instance()) { 4260 klass_handle->initialize(CHECK_NULL); 4261 } 4262 return (jclass) JNIHandles::make_local(env, klass_handle->java_mirror()); 4263 } 4264 4265 4266 // Internal SQE debugging support /////////////////////////////////////////////////////////// 4267 4268 #ifndef PRODUCT 4269 4270 extern "C" { 4271 JNIEXPORT jboolean JNICALL JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get); 4272 JNIEXPORT jboolean JNICALL JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get); 4273 JNIEXPORT void JNICALL JVM_VMBreakPoint(JNIEnv *env, jobject obj); 4274 } 4275 4276 JVM_LEAF(jboolean, JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get)) 4277 JVMWrapper("JVM_AccessBoolVMFlag"); 4278 return is_get ? CommandLineFlags::boolAt((char*) name, (bool*) value) : CommandLineFlags::boolAtPut((char*) name, (bool*) value, Flag::INTERNAL); 4279 JVM_END 4280 4281 JVM_LEAF(jboolean, JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get)) 4282 JVMWrapper("JVM_AccessVMIntFlag"); 4283 intx v; 4284 jboolean result = is_get ? CommandLineFlags::intxAt((char*) name, &v) : CommandLineFlags::intxAtPut((char*) name, &v, Flag::INTERNAL); 4285 *value = (jint)v; 4286 return result; 4287 JVM_END 4288 4289 4290 JVM_ENTRY(void, JVM_VMBreakPoint(JNIEnv *env, jobject obj)) 4291 JVMWrapper("JVM_VMBreakPoint"); 4292 oop the_obj = JNIHandles::resolve(obj); 4293 BREAKPOINT; 4294 JVM_END 4295 4296 4297 #endif 4298 4299 4300 // Method /////////////////////////////////////////////////////////////////////////////////////////// 4301 4302 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0)) 4303 JVMWrapper("JVM_InvokeMethod"); 4304 Handle method_handle; 4305 if (thread->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) { 4306 method_handle = Handle(THREAD, JNIHandles::resolve(method)); 4307 Handle receiver(THREAD, JNIHandles::resolve(obj)); 4308 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0))); 4309 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL); 4310 jobject res = JNIHandles::make_local(env, result); 4311 if (JvmtiExport::should_post_vm_object_alloc()) { 4312 oop ret_type = java_lang_reflect_Method::return_type(method_handle()); 4313 assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!"); 4314 if (java_lang_Class::is_primitive(ret_type)) { 4315 // Only for primitive type vm allocates memory for java object. 4316 // See box() method. 4317 JvmtiExport::post_vm_object_alloc(JavaThread::current(), result); 4318 } 4319 } 4320 return res; 4321 } else { 4322 THROW_0(vmSymbols::java_lang_StackOverflowError()); 4323 } 4324 JVM_END 4325 4326 4327 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0)) 4328 JVMWrapper("JVM_NewInstanceFromConstructor"); 4329 oop constructor_mirror = JNIHandles::resolve(c); 4330 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0))); 4331 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL); 4332 jobject res = JNIHandles::make_local(env, result); 4333 if (JvmtiExport::should_post_vm_object_alloc()) { 4334 JvmtiExport::post_vm_object_alloc(JavaThread::current(), result); 4335 } 4336 return res; 4337 JVM_END 4338 4339 // Atomic /////////////////////////////////////////////////////////////////////////////////////////// 4340 4341 JVM_LEAF(jboolean, JVM_SupportsCX8()) 4342 JVMWrapper("JVM_SupportsCX8"); 4343 return VM_Version::supports_cx8(); 4344 JVM_END 4345 4346 4347 JVM_ENTRY(jboolean, JVM_CX8Field(JNIEnv *env, jobject obj, jfieldID fid, jlong oldVal, jlong newVal)) 4348 JVMWrapper("JVM_CX8Field"); 4349 jlong res; 4350 oop o = JNIHandles::resolve(obj); 4351 intptr_t fldOffs = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid); 4352 volatile jlong* addr = (volatile jlong*)((address)o + fldOffs); 4353 4354 assert(VM_Version::supports_cx8(), "cx8 not supported"); 4355 res = Atomic::cmpxchg(newVal, addr, oldVal); 4356 4357 return res == oldVal; 4358 JVM_END 4359 4360 // DTrace /////////////////////////////////////////////////////////////////// 4361 4362 JVM_ENTRY(jint, JVM_DTraceGetVersion(JNIEnv* env)) 4363 JVMWrapper("JVM_DTraceGetVersion"); 4364 return (jint)JVM_TRACING_DTRACE_VERSION; 4365 JVM_END 4366 4367 JVM_ENTRY(jlong,JVM_DTraceActivate( 4368 JNIEnv* env, jint version, jstring module_name, jint providers_count, 4369 JVM_DTraceProvider* providers)) 4370 JVMWrapper("JVM_DTraceActivate"); 4371 return DTraceJSDT::activate( 4372 version, module_name, providers_count, providers, CHECK_0); 4373 JVM_END 4374 4375 JVM_ENTRY(jboolean,JVM_DTraceIsProbeEnabled(JNIEnv* env, jmethodID method)) 4376 JVMWrapper("JVM_DTraceIsProbeEnabled"); 4377 return DTraceJSDT::is_probe_enabled(method); 4378 JVM_END 4379 4380 JVM_ENTRY(void,JVM_DTraceDispose(JNIEnv* env, jlong handle)) 4381 JVMWrapper("JVM_DTraceDispose"); 4382 DTraceJSDT::dispose(handle); 4383 JVM_END 4384 4385 JVM_ENTRY(jboolean,JVM_DTraceIsSupported(JNIEnv* env)) 4386 JVMWrapper("JVM_DTraceIsSupported"); 4387 return DTraceJSDT::is_supported(); 4388 JVM_END 4389 4390 // Returns an array of all live Thread objects (VM internal JavaThreads, 4391 // jvmti agent threads, and JNI attaching threads are skipped) 4392 // See CR 6404306 regarding JNI attaching threads 4393 JVM_ENTRY(jobjectArray, JVM_GetAllThreads(JNIEnv *env, jclass dummy)) 4394 ResourceMark rm(THREAD); 4395 ThreadsListEnumerator tle(THREAD, false, false); 4396 JvmtiVMObjectAllocEventCollector oam; 4397 4398 int num_threads = tle.num_threads(); 4399 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Thread_klass(), num_threads, CHECK_NULL); 4400 objArrayHandle threads_ah(THREAD, r); 4401 4402 for (int i = 0; i < num_threads; i++) { 4403 Handle h = tle.get_threadObj(i); 4404 threads_ah->obj_at_put(i, h()); 4405 } 4406 4407 return (jobjectArray) JNIHandles::make_local(env, threads_ah()); 4408 JVM_END 4409 4410 4411 // Support for java.lang.Thread.getStackTrace() and getAllStackTraces() methods 4412 // Return StackTraceElement[][], each element is the stack trace of a thread in 4413 // the corresponding entry in the given threads array 4414 JVM_ENTRY(jobjectArray, JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads)) 4415 JVMWrapper("JVM_DumpThreads"); 4416 JvmtiVMObjectAllocEventCollector oam; 4417 4418 // Check if threads is null 4419 if (threads == NULL) { 4420 THROW_(vmSymbols::java_lang_NullPointerException(), 0); 4421 } 4422 4423 objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads)); 4424 objArrayHandle ah(THREAD, a); 4425 int num_threads = ah->length(); 4426 // check if threads is non-empty array 4427 if (num_threads == 0) { 4428 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0); 4429 } 4430 4431 // check if threads is not an array of objects of Thread class 4432 Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass(); 4433 if (k != SystemDictionary::Thread_klass()) { 4434 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0); 4435 } 4436 4437 ResourceMark rm(THREAD); 4438 4439 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads); 4440 for (int i = 0; i < num_threads; i++) { 4441 oop thread_obj = ah->obj_at(i); 4442 instanceHandle h(THREAD, (instanceOop) thread_obj); 4443 thread_handle_array->append(h); 4444 } 4445 4446 Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL); 4447 return (jobjectArray)JNIHandles::make_local(env, stacktraces()); 4448 4449 JVM_END 4450 4451 // JVM monitoring and management support 4452 JVM_ENTRY_NO_ENV(void*, JVM_GetManagement(jint version)) 4453 return Management::get_jmm_interface(version); 4454 JVM_END 4455 4456 // com.sun.tools.attach.VirtualMachine agent properties support 4457 // 4458 // Initialize the agent properties with the properties maintained in the VM 4459 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties)) 4460 JVMWrapper("JVM_InitAgentProperties"); 4461 ResourceMark rm; 4462 4463 Handle props(THREAD, JNIHandles::resolve_non_null(properties)); 4464 4465 PUTPROP(props, "sun.java.command", Arguments::java_command()); 4466 PUTPROP(props, "sun.jvm.flags", Arguments::jvm_flags()); 4467 PUTPROP(props, "sun.jvm.args", Arguments::jvm_args()); 4468 return properties; 4469 JVM_END 4470 4471 JVM_ENTRY(jobjectArray, JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass)) 4472 { 4473 JVMWrapper("JVM_GetEnclosingMethodInfo"); 4474 JvmtiVMObjectAllocEventCollector oam; 4475 4476 if (ofClass == NULL) { 4477 return NULL; 4478 } 4479 Handle mirror(THREAD, JNIHandles::resolve_non_null(ofClass)); 4480 // Special handling for primitive objects 4481 if (java_lang_Class::is_primitive(mirror())) { 4482 return NULL; 4483 } 4484 Klass* k = java_lang_Class::as_Klass(mirror()); 4485 if (!k->oop_is_instance()) { 4486 return NULL; 4487 } 4488 instanceKlassHandle ik_h(THREAD, k); 4489 int encl_method_class_idx = ik_h->enclosing_method_class_index(); 4490 if (encl_method_class_idx == 0) { 4491 return NULL; 4492 } 4493 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::Object_klass(), 3, CHECK_NULL); 4494 objArrayHandle dest(THREAD, dest_o); 4495 Klass* enc_k = ik_h->constants()->klass_at(encl_method_class_idx, CHECK_NULL); 4496 dest->obj_at_put(0, enc_k->java_mirror()); 4497 int encl_method_method_idx = ik_h->enclosing_method_method_index(); 4498 if (encl_method_method_idx != 0) { 4499 Symbol* sym = ik_h->constants()->symbol_at( 4500 extract_low_short_from_int( 4501 ik_h->constants()->name_and_type_at(encl_method_method_idx))); 4502 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL); 4503 dest->obj_at_put(1, str()); 4504 sym = ik_h->constants()->symbol_at( 4505 extract_high_short_from_int( 4506 ik_h->constants()->name_and_type_at(encl_method_method_idx))); 4507 str = java_lang_String::create_from_symbol(sym, CHECK_NULL); 4508 dest->obj_at_put(2, str()); 4509 } 4510 return (jobjectArray) JNIHandles::make_local(dest()); 4511 } 4512 JVM_END 4513 4514 JVM_ENTRY(jintArray, JVM_GetThreadStateValues(JNIEnv* env, 4515 jint javaThreadState)) 4516 { 4517 // If new thread states are added in future JDK and VM versions, 4518 // this should check if the JDK version is compatible with thread 4519 // states supported by the VM. Return NULL if not compatible. 4520 // 4521 // This function must map the VM java_lang_Thread::ThreadStatus 4522 // to the Java thread state that the JDK supports. 4523 // 4524 4525 typeArrayHandle values_h; 4526 switch (javaThreadState) { 4527 case JAVA_THREAD_STATE_NEW : { 4528 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL); 4529 values_h = typeArrayHandle(THREAD, r); 4530 values_h->int_at_put(0, java_lang_Thread::NEW); 4531 break; 4532 } 4533 case JAVA_THREAD_STATE_RUNNABLE : { 4534 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL); 4535 values_h = typeArrayHandle(THREAD, r); 4536 values_h->int_at_put(0, java_lang_Thread::RUNNABLE); 4537 break; 4538 } 4539 case JAVA_THREAD_STATE_BLOCKED : { 4540 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL); 4541 values_h = typeArrayHandle(THREAD, r); 4542 values_h->int_at_put(0, java_lang_Thread::BLOCKED_ON_MONITOR_ENTER); 4543 break; 4544 } 4545 case JAVA_THREAD_STATE_WAITING : { 4546 typeArrayOop r = oopFactory::new_typeArray(T_INT, 2, CHECK_NULL); 4547 values_h = typeArrayHandle(THREAD, r); 4548 values_h->int_at_put(0, java_lang_Thread::IN_OBJECT_WAIT); 4549 values_h->int_at_put(1, java_lang_Thread::PARKED); 4550 break; 4551 } 4552 case JAVA_THREAD_STATE_TIMED_WAITING : { 4553 typeArrayOop r = oopFactory::new_typeArray(T_INT, 3, CHECK_NULL); 4554 values_h = typeArrayHandle(THREAD, r); 4555 values_h->int_at_put(0, java_lang_Thread::SLEEPING); 4556 values_h->int_at_put(1, java_lang_Thread::IN_OBJECT_WAIT_TIMED); 4557 values_h->int_at_put(2, java_lang_Thread::PARKED_TIMED); 4558 break; 4559 } 4560 case JAVA_THREAD_STATE_TERMINATED : { 4561 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL); 4562 values_h = typeArrayHandle(THREAD, r); 4563 values_h->int_at_put(0, java_lang_Thread::TERMINATED); 4564 break; 4565 } 4566 default: 4567 // Unknown state - probably incompatible JDK version 4568 return NULL; 4569 } 4570 4571 return (jintArray) JNIHandles::make_local(env, values_h()); 4572 } 4573 JVM_END 4574 4575 4576 JVM_ENTRY(jobjectArray, JVM_GetThreadStateNames(JNIEnv* env, 4577 jint javaThreadState, 4578 jintArray values)) 4579 { 4580 // If new thread states are added in future JDK and VM versions, 4581 // this should check if the JDK version is compatible with thread 4582 // states supported by the VM. Return NULL if not compatible. 4583 // 4584 // This function must map the VM java_lang_Thread::ThreadStatus 4585 // to the Java thread state that the JDK supports. 4586 // 4587 4588 ResourceMark rm; 4589 4590 // Check if threads is null 4591 if (values == NULL) { 4592 THROW_(vmSymbols::java_lang_NullPointerException(), 0); 4593 } 4594 4595 typeArrayOop v = typeArrayOop(JNIHandles::resolve_non_null(values)); 4596 typeArrayHandle values_h(THREAD, v); 4597 4598 objArrayHandle names_h; 4599 switch (javaThreadState) { 4600 case JAVA_THREAD_STATE_NEW : { 4601 assert(values_h->length() == 1 && 4602 values_h->int_at(0) == java_lang_Thread::NEW, 4603 "Invalid threadStatus value"); 4604 4605 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), 4606 1, /* only 1 substate */ 4607 CHECK_NULL); 4608 names_h = objArrayHandle(THREAD, r); 4609 Handle name = java_lang_String::create_from_str("NEW", CHECK_NULL); 4610 names_h->obj_at_put(0, name()); 4611 break; 4612 } 4613 case JAVA_THREAD_STATE_RUNNABLE : { 4614 assert(values_h->length() == 1 && 4615 values_h->int_at(0) == java_lang_Thread::RUNNABLE, 4616 "Invalid threadStatus value"); 4617 4618 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), 4619 1, /* only 1 substate */ 4620 CHECK_NULL); 4621 names_h = objArrayHandle(THREAD, r); 4622 Handle name = java_lang_String::create_from_str("RUNNABLE", CHECK_NULL); 4623 names_h->obj_at_put(0, name()); 4624 break; 4625 } 4626 case JAVA_THREAD_STATE_BLOCKED : { 4627 assert(values_h->length() == 1 && 4628 values_h->int_at(0) == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER, 4629 "Invalid threadStatus value"); 4630 4631 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), 4632 1, /* only 1 substate */ 4633 CHECK_NULL); 4634 names_h = objArrayHandle(THREAD, r); 4635 Handle name = java_lang_String::create_from_str("BLOCKED", CHECK_NULL); 4636 names_h->obj_at_put(0, name()); 4637 break; 4638 } 4639 case JAVA_THREAD_STATE_WAITING : { 4640 assert(values_h->length() == 2 && 4641 values_h->int_at(0) == java_lang_Thread::IN_OBJECT_WAIT && 4642 values_h->int_at(1) == java_lang_Thread::PARKED, 4643 "Invalid threadStatus value"); 4644 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), 4645 2, /* number of substates */ 4646 CHECK_NULL); 4647 names_h = objArrayHandle(THREAD, r); 4648 Handle name0 = java_lang_String::create_from_str("WAITING.OBJECT_WAIT", 4649 CHECK_NULL); 4650 Handle name1 = java_lang_String::create_from_str("WAITING.PARKED", 4651 CHECK_NULL); 4652 names_h->obj_at_put(0, name0()); 4653 names_h->obj_at_put(1, name1()); 4654 break; 4655 } 4656 case JAVA_THREAD_STATE_TIMED_WAITING : { 4657 assert(values_h->length() == 3 && 4658 values_h->int_at(0) == java_lang_Thread::SLEEPING && 4659 values_h->int_at(1) == java_lang_Thread::IN_OBJECT_WAIT_TIMED && 4660 values_h->int_at(2) == java_lang_Thread::PARKED_TIMED, 4661 "Invalid threadStatus value"); 4662 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), 4663 3, /* number of substates */ 4664 CHECK_NULL); 4665 names_h = objArrayHandle(THREAD, r); 4666 Handle name0 = java_lang_String::create_from_str("TIMED_WAITING.SLEEPING", 4667 CHECK_NULL); 4668 Handle name1 = java_lang_String::create_from_str("TIMED_WAITING.OBJECT_WAIT", 4669 CHECK_NULL); 4670 Handle name2 = java_lang_String::create_from_str("TIMED_WAITING.PARKED", 4671 CHECK_NULL); 4672 names_h->obj_at_put(0, name0()); 4673 names_h->obj_at_put(1, name1()); 4674 names_h->obj_at_put(2, name2()); 4675 break; 4676 } 4677 case JAVA_THREAD_STATE_TERMINATED : { 4678 assert(values_h->length() == 1 && 4679 values_h->int_at(0) == java_lang_Thread::TERMINATED, 4680 "Invalid threadStatus value"); 4681 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), 4682 1, /* only 1 substate */ 4683 CHECK_NULL); 4684 names_h = objArrayHandle(THREAD, r); 4685 Handle name = java_lang_String::create_from_str("TERMINATED", CHECK_NULL); 4686 names_h->obj_at_put(0, name()); 4687 break; 4688 } 4689 default: 4690 // Unknown state - probably incompatible JDK version 4691 return NULL; 4692 } 4693 return (jobjectArray) JNIHandles::make_local(env, names_h()); 4694 } 4695 JVM_END 4696 4697 JVM_ENTRY(void, JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size)) 4698 { 4699 memset(info, 0, info_size); 4700 4701 info->jvm_version = Abstract_VM_Version::jvm_version(); 4702 info->update_version = 0; /* 0 in HotSpot Express VM */ 4703 info->special_update_version = 0; /* 0 in HotSpot Express VM */ 4704 4705 // when we add a new capability in the jvm_version_info struct, we should also 4706 // consider to expose this new capability in the sun.rt.jvmCapabilities jvmstat 4707 // counter defined in runtimeService.cpp. 4708 info->is_attachable = AttachListener::is_attach_supported(); 4709 } 4710 JVM_END