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