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