1 /* 2 * Copyright (c) 1997, 2019, 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 #ifndef SHARE_RUNTIME_INTERFACESUPPORT_INLINE_HPP 26 #define SHARE_RUNTIME_INTERFACESUPPORT_INLINE_HPP 27 28 #include "runtime/handles.inline.hpp" 29 #include "runtime/mutexLocker.hpp" 30 #include "runtime/orderAccess.hpp" 31 #include "runtime/os.hpp" 32 #include "runtime/safepointMechanism.inline.hpp" 33 #include "runtime/safepointVerifiers.hpp" 34 #include "runtime/thread.hpp" 35 #include "runtime/vmOperations.hpp" 36 #include "utilities/globalDefinitions.hpp" 37 #include "utilities/histogram.hpp" 38 #include "utilities/macros.hpp" 39 #include "utilities/preserveException.hpp" 40 41 // Wrapper for all entry points to the virtual machine. 42 43 // InterfaceSupport provides functionality used by the VM_LEAF_BASE and 44 // VM_ENTRY_BASE macros. These macros are used to guard entry points into 45 // the VM and perform checks upon leave of the VM. 46 47 48 class InterfaceSupport: AllStatic { 49 # ifdef ASSERT 50 public: 51 static long _scavenge_alot_counter; 52 static long _fullgc_alot_counter; 53 static long _number_of_calls; 54 static long _fullgc_alot_invocation; 55 56 // Helper methods used to implement +ScavengeALot and +FullGCALot 57 static void check_gc_alot() { if (ScavengeALot || FullGCALot) gc_alot(); } 58 static void gc_alot(); 59 60 static void walk_stack_from(vframe* start_vf); 61 static void walk_stack(); 62 63 static void zombieAll(); 64 static void deoptimizeAll(); 65 static void stress_derived_pointers(); 66 static void verify_stack(); 67 static void verify_last_frame(); 68 # endif 69 }; 70 71 72 // Basic class for all thread transition classes. 73 74 class ThreadStateTransition : public StackObj { 75 protected: 76 JavaThread* _thread; 77 public: 78 ThreadStateTransition(JavaThread *thread) { 79 _thread = thread; 80 assert(thread != NULL && thread->is_Java_thread(), "must be Java thread"); 81 } 82 83 // Change threadstate in a manner, so safepoint can detect changes. 84 // Time-critical: called on exit from every runtime routine 85 static inline void transition(JavaThread *thread, JavaThreadState from, JavaThreadState to) { 86 assert(from != _thread_in_Java, "use transition_from_java"); 87 assert(from != _thread_in_native, "use transition_from_native"); 88 assert((from & 1) == 0 && (to & 1) == 0, "odd numbers are transitions states"); 89 assert(thread->thread_state() == from, "coming from wrong thread state"); 90 91 // Check NoSafepointVerifier 92 // This also clears unhandled oops if CheckUnhandledOops is used. 93 thread->check_possible_safepoint(); 94 95 // Change to transition state and ensure it is seen by the VM thread. 96 thread->set_thread_state_fence((JavaThreadState)(from + 1)); 97 98 SafepointMechanism::block_if_requested(thread); 99 thread->set_thread_state(to); 100 } 101 102 // Same as above, but assumes from = _thread_in_Java. This is simpler, since we 103 // never block on entry to the VM. This will break the code, since e.g. preserve arguments 104 // have not been setup. 105 static inline void transition_from_java(JavaThread *thread, JavaThreadState to) { 106 assert(thread->thread_state() == _thread_in_Java, "coming from wrong thread state"); 107 thread->set_thread_state(to); 108 } 109 110 static inline void transition_from_native(JavaThread *thread, JavaThreadState to) { 111 assert((to & 1) == 0, "odd numbers are transitions states"); 112 assert(thread->thread_state() == _thread_in_native, "coming from wrong thread state"); 113 // Change to transition state and ensure it is seen by the VM thread. 114 thread->set_thread_state_fence(_thread_in_native_trans); 115 116 // We never install asynchronous exceptions when coming (back) in 117 // to the runtime from native code because the runtime is not set 118 // up to handle exceptions floating around at arbitrary points. 119 if (SafepointMechanism::should_block(thread) || thread->is_suspend_after_native()) { 120 JavaThread::check_safepoint_and_suspend_for_native_trans(thread); 121 } 122 123 thread->set_thread_state(to); 124 } 125 protected: 126 void trans(JavaThreadState from, JavaThreadState to) { transition(_thread, from, to); } 127 void trans_from_java(JavaThreadState to) { transition_from_java(_thread, to); } 128 void trans_from_native(JavaThreadState to) { transition_from_native(_thread, to); } 129 }; 130 131 class ThreadInVMForHandshake : public ThreadStateTransition { 132 const JavaThreadState _original_state; 133 134 void transition_back() { 135 // This can be invoked from transition states and must return to the original state properly 136 assert(_thread->thread_state() == _thread_in_vm, "should only call when leaving VM after handshake"); 137 // Change to transition state and ensure it is seen by the VM thread. 138 _thread->set_thread_state_fence(_thread_in_vm_trans); 139 140 SafepointMechanism::block_if_requested(_thread); 141 142 _thread->set_thread_state(_original_state); 143 144 if (_original_state != _thread_blocked_trans && _original_state != _thread_in_vm_trans && 145 _thread->has_special_runtime_exit_condition()) { 146 _thread->handle_special_runtime_exit_condition( 147 !_thread->is_at_poll_safepoint() && (_original_state != _thread_in_native_trans)); 148 } 149 } 150 151 public: 152 153 ThreadInVMForHandshake(JavaThread* thread) : ThreadStateTransition(thread), 154 _original_state(thread->thread_state()) { 155 156 if (thread->has_last_Java_frame()) { 157 thread->frame_anchor()->make_walkable(thread); 158 } 159 160 thread->set_thread_state(_thread_in_vm); 161 } 162 163 ~ThreadInVMForHandshake() { 164 transition_back(); 165 } 166 167 }; 168 169 class ThreadInVMfromJava : public ThreadStateTransition { 170 public: 171 ThreadInVMfromJava(JavaThread* thread) : ThreadStateTransition(thread) { 172 trans_from_java(_thread_in_vm); 173 } 174 ~ThreadInVMfromJava() { 175 if (_thread->stack_yellow_reserved_zone_disabled()) { 176 _thread->enable_stack_yellow_reserved_zone(); 177 } 178 trans(_thread_in_vm, _thread_in_Java); 179 // Check for pending. async. exceptions or suspends. 180 if (_thread->has_special_runtime_exit_condition()) _thread->handle_special_runtime_exit_condition(); 181 } 182 }; 183 184 185 class ThreadInVMfromUnknown { 186 JavaThread* _thread; 187 public: 188 ThreadInVMfromUnknown() : _thread(NULL) { 189 Thread* t = Thread::current(); 190 if (t->is_Java_thread()) { 191 JavaThread* t2 = (JavaThread*) t; 192 if (t2->thread_state() == _thread_in_native) { 193 _thread = t2; 194 ThreadStateTransition::transition_from_native(t2, _thread_in_vm); 195 // Used to have a HandleMarkCleaner but that is dangerous as 196 // it could free a handle in our (indirect, nested) caller. 197 // We expect any handles will be short lived and figure we 198 // don't need an actual HandleMark. 199 } 200 } 201 } 202 ~ThreadInVMfromUnknown() { 203 if (_thread) { 204 ThreadStateTransition::transition(_thread, _thread_in_vm, _thread_in_native); 205 } 206 } 207 }; 208 209 210 class ThreadInVMfromNative : public ThreadStateTransition { 211 public: 212 ThreadInVMfromNative(JavaThread* thread) : ThreadStateTransition(thread) { 213 trans_from_native(_thread_in_vm); 214 } 215 ~ThreadInVMfromNative() { 216 trans(_thread_in_vm, _thread_in_native); 217 } 218 }; 219 220 221 class ThreadToNativeFromVM : public ThreadStateTransition { 222 public: 223 ThreadToNativeFromVM(JavaThread *thread) : ThreadStateTransition(thread) { 224 // We are leaving the VM at this point and going directly to native code. 225 // Block, if we are in the middle of a safepoint synchronization. 226 assert(!thread->owns_locks(), "must release all locks when leaving VM"); 227 thread->frame_anchor()->make_walkable(thread); 228 trans(_thread_in_vm, _thread_in_native); 229 // Check for pending. async. exceptions or suspends. 230 if (_thread->has_special_runtime_exit_condition()) _thread->handle_special_runtime_exit_condition(false); 231 } 232 233 ~ThreadToNativeFromVM() { 234 trans_from_native(_thread_in_vm); 235 assert(!_thread->is_pending_jni_exception_check(), "Pending JNI Exception Check"); 236 // We don't need to clear_walkable because it will happen automagically when we return to java 237 } 238 }; 239 240 241 class ThreadBlockInVM : public ThreadStateTransition { 242 public: 243 ThreadBlockInVM(JavaThread *thread) 244 : ThreadStateTransition(thread) { 245 // Once we are blocked vm expects stack to be walkable 246 thread->frame_anchor()->make_walkable(thread); 247 trans(_thread_in_vm, _thread_blocked); 248 } 249 ~ThreadBlockInVM() { 250 trans(_thread_blocked, _thread_in_vm); 251 OrderAccess::cross_modify_fence(); 252 // We don't need to clear_walkable because it will happen automagically when we return to java 253 } 254 }; 255 256 // Unlike ThreadBlockInVM, this class is designed to avoid certain deadlock scenarios while making 257 // transitions inside class Mutex in cases where we need to block for a safepoint or handshake. It 258 // receives an extra argument compared to ThreadBlockInVM, the address of a pointer to the monitor we 259 // are trying to acquire. This will be used to access and release the mutex if needed to avoid 260 // said deadlocks. 261 // It works like ThreadBlockInVM but differs from it in two ways: 262 // - When transitioning in (constructor), it checks for safepoints without blocking, i.e., calls 263 // back if needed to allow a pending safepoint to continue but does not block in it. 264 // - When transitioning back (destructor), if there is a pending safepoint or handshake it releases 265 // the monitor that is only partially acquired. 266 class ThreadBlockInVMWithDeadlockCheck : public ThreadStateTransition { 267 private: 268 Mutex** _in_flight_mutex_addr; 269 270 void release_monitor() { 271 assert(_in_flight_mutex_addr != NULL, "_in_flight_mutex_addr should have been set on constructor"); 272 Mutex* in_flight_mutex = *_in_flight_mutex_addr; 273 if (in_flight_mutex != NULL) { 274 in_flight_mutex->release_for_safepoint(); 275 *_in_flight_mutex_addr = NULL; 276 } 277 } 278 public: 279 ThreadBlockInVMWithDeadlockCheck(JavaThread* thread, Mutex** in_flight_mutex_addr) 280 : ThreadStateTransition(thread), _in_flight_mutex_addr(in_flight_mutex_addr) { 281 // Once we are blocked vm expects stack to be walkable 282 thread->frame_anchor()->make_walkable(thread); 283 284 // All unsafe states are treated the same by the VMThread 285 // so we can skip the _thread_in_vm_trans state here. Since 286 // we don't read poll, it's enough to order the stores. 287 OrderAccess::storestore(); 288 289 thread->set_thread_state(_thread_blocked); 290 } 291 ~ThreadBlockInVMWithDeadlockCheck() { 292 // Change to transition state and ensure it is seen by the VM thread. 293 _thread->set_thread_state_fence((JavaThreadState)(_thread_blocked_trans)); 294 295 if (SafepointMechanism::should_block(_thread)) { 296 release_monitor(); 297 SafepointMechanism::block_if_requested(_thread); 298 } 299 300 _thread->set_thread_state(_thread_in_vm); 301 OrderAccess::cross_modify_fence(); 302 } 303 }; 304 305 306 // This special transition class is only used to prevent asynchronous exceptions 307 // from being installed on vm exit in situations where we can't tolerate them. 308 // See bugs: 4324348, 4854693, 4998314, 5040492, 5050705. 309 class ThreadInVMfromJavaNoAsyncException : public ThreadStateTransition { 310 public: 311 ThreadInVMfromJavaNoAsyncException(JavaThread* thread) : ThreadStateTransition(thread) { 312 trans_from_java(_thread_in_vm); 313 } 314 ~ThreadInVMfromJavaNoAsyncException() { 315 if (_thread->stack_yellow_reserved_zone_disabled()) { 316 _thread->enable_stack_yellow_reserved_zone(); 317 } 318 trans(_thread_in_vm, _thread_in_Java); 319 // NOTE: We do not check for pending. async. exceptions. 320 // If we did and moved the pending async exception over into the 321 // pending exception field, we would need to deopt (currently C2 322 // only). However, to do so would require that we transition back 323 // to the _thread_in_vm state. Instead we postpone the handling of 324 // the async exception. 325 326 327 // Check for pending. suspends only. 328 if (_thread->has_special_runtime_exit_condition()) 329 _thread->handle_special_runtime_exit_condition(false); 330 } 331 }; 332 333 // Debug class instantiated in JRT_ENTRY macro. 334 // Can be used to verify properties on enter/exit of the VM. 335 336 #ifdef ASSERT 337 class VMEntryWrapper { 338 public: 339 VMEntryWrapper(); 340 ~VMEntryWrapper(); 341 }; 342 343 344 class VMNativeEntryWrapper { 345 public: 346 VMNativeEntryWrapper(); 347 ~VMNativeEntryWrapper(); 348 }; 349 350 class RuntimeHistogramElement : public HistogramElement { 351 public: 352 RuntimeHistogramElement(const char* name); 353 }; 354 #endif // ASSERT 355 356 #ifdef ASSERT 357 #define TRACE_CALL(result_type, header) \ 358 InterfaceSupport::_number_of_calls++; \ 359 if (CountRuntimeCalls) { \ 360 static RuntimeHistogramElement* e = new RuntimeHistogramElement(#header); \ 361 if (e != NULL) e->increment_count(); \ 362 } 363 #else 364 #define TRACE_CALL(result_type, header) \ 365 /* do nothing */ 366 #endif // ASSERT 367 368 369 // LEAF routines do not lock, GC or throw exceptions 370 371 #define VM_LEAF_BASE(result_type, header) \ 372 TRACE_CALL(result_type, header) \ 373 debug_only(NoHandleMark __hm;) \ 374 os::verify_stack_alignment(); \ 375 /* begin of body */ 376 377 #define VM_ENTRY_BASE_FROM_LEAF(result_type, header, thread) \ 378 TRACE_CALL(result_type, header) \ 379 debug_only(ResetNoHandleMark __rnhm;) \ 380 HandleMarkCleaner __hm(thread); \ 381 Thread* THREAD = thread; \ 382 os::verify_stack_alignment(); \ 383 /* begin of body */ 384 385 386 // ENTRY routines may lock, GC and throw exceptions 387 388 #define VM_ENTRY_BASE(result_type, header, thread) \ 389 TRACE_CALL(result_type, header) \ 390 HandleMarkCleaner __hm(thread); \ 391 Thread* THREAD = thread; \ 392 os::verify_stack_alignment(); \ 393 /* begin of body */ 394 395 396 // QUICK_ENTRY routines behave like ENTRY but without a handle mark 397 398 #define VM_QUICK_ENTRY_BASE(result_type, header, thread) \ 399 TRACE_CALL(result_type, header) \ 400 debug_only(NoHandleMark __hm;) \ 401 Thread* THREAD = thread; \ 402 os::verify_stack_alignment(); \ 403 /* begin of body */ 404 405 406 #define JRT_ENTRY(result_type, header) \ 407 result_type header { \ 408 ThreadInVMfromJava __tiv(thread); \ 409 VM_ENTRY_BASE(result_type, header, thread) \ 410 debug_only(VMEntryWrapper __vew;) 411 412 // JRT_LEAF currently can be called from either _thread_in_Java or 413 // _thread_in_native mode. 414 // 415 // JRT_LEAF rules: 416 // A JRT_LEAF method may not interfere with safepointing by 417 // 1) acquiring or blocking on a Mutex or JavaLock - checked 418 // 2) allocating heap memory - checked 419 // 3) executing a VM operation - checked 420 // 4) executing a system call (including malloc) that could block or grab a lock 421 // 5) invoking GC 422 // 6) reaching a safepoint 423 // 7) running too long 424 // Nor may any method it calls. 425 426 #define JRT_LEAF(result_type, header) \ 427 result_type header { \ 428 VM_LEAF_BASE(result_type, header) \ 429 debug_only(NoSafepointVerifier __nsv;) 430 431 432 #define JRT_ENTRY_NO_ASYNC(result_type, header) \ 433 result_type header { \ 434 ThreadInVMfromJavaNoAsyncException __tiv(thread); \ 435 VM_ENTRY_BASE(result_type, header, thread) \ 436 debug_only(VMEntryWrapper __vew;) 437 438 // Same as JRT Entry but allows for return value after the safepoint 439 // to get back into Java from the VM 440 #define JRT_BLOCK_ENTRY(result_type, header) \ 441 result_type header { \ 442 TRACE_CALL(result_type, header) \ 443 HandleMarkCleaner __hm(thread); 444 445 #define JRT_BLOCK \ 446 { \ 447 ThreadInVMfromJava __tiv(thread); \ 448 Thread* THREAD = thread; \ 449 debug_only(VMEntryWrapper __vew;) 450 451 #define JRT_BLOCK_NO_ASYNC \ 452 { \ 453 ThreadInVMfromJavaNoAsyncException __tiv(thread); \ 454 Thread* THREAD = thread; \ 455 debug_only(VMEntryWrapper __vew;) 456 457 #define JRT_BLOCK_END } 458 459 #define JRT_END } 460 461 // Definitions for JNI 462 463 #define JNI_ENTRY(result_type, header) \ 464 JNI_ENTRY_NO_PRESERVE(result_type, header) \ 465 WeakPreserveExceptionMark __wem(thread); 466 467 #define JNI_ENTRY_NO_PRESERVE(result_type, header) \ 468 extern "C" { \ 469 result_type JNICALL header { \ 470 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 471 assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \ 472 ThreadInVMfromNative __tiv(thread); \ 473 debug_only(VMNativeEntryWrapper __vew;) \ 474 VM_ENTRY_BASE(result_type, header, thread) 475 476 477 // Ensure that the VMNativeEntryWrapper constructor, which can cause 478 // a GC, is called outside the NoHandleMark (set via VM_QUICK_ENTRY_BASE). 479 #define JNI_QUICK_ENTRY(result_type, header) \ 480 extern "C" { \ 481 result_type JNICALL header { \ 482 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 483 assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \ 484 ThreadInVMfromNative __tiv(thread); \ 485 debug_only(VMNativeEntryWrapper __vew;) \ 486 VM_QUICK_ENTRY_BASE(result_type, header, thread) 487 488 489 #define JNI_LEAF(result_type, header) \ 490 extern "C" { \ 491 result_type JNICALL header { \ 492 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 493 assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \ 494 VM_LEAF_BASE(result_type, header) 495 496 497 // Close the routine and the extern "C" 498 #define JNI_END } } 499 500 501 502 // Definitions for JVM 503 504 #define JVM_ENTRY(result_type, header) \ 505 extern "C" { \ 506 result_type JNICALL header { \ 507 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 508 ThreadInVMfromNative __tiv(thread); \ 509 debug_only(VMNativeEntryWrapper __vew;) \ 510 VM_ENTRY_BASE(result_type, header, thread) 511 512 513 #define JVM_ENTRY_NO_ENV(result_type, header) \ 514 extern "C" { \ 515 result_type JNICALL header { \ 516 JavaThread* thread = JavaThread::current(); \ 517 ThreadInVMfromNative __tiv(thread); \ 518 debug_only(VMNativeEntryWrapper __vew;) \ 519 VM_ENTRY_BASE(result_type, header, thread) 520 521 522 #define JVM_QUICK_ENTRY(result_type, header) \ 523 extern "C" { \ 524 result_type JNICALL header { \ 525 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 526 ThreadInVMfromNative __tiv(thread); \ 527 debug_only(VMNativeEntryWrapper __vew;) \ 528 VM_QUICK_ENTRY_BASE(result_type, header, thread) 529 530 531 #define JVM_LEAF(result_type, header) \ 532 extern "C" { \ 533 result_type JNICALL header { \ 534 VM_Exit::block_if_vm_exited(); \ 535 VM_LEAF_BASE(result_type, header) 536 537 538 #define JVM_ENTRY_FROM_LEAF(env, result_type, header) \ 539 { { \ 540 JavaThread* thread=JavaThread::thread_from_jni_environment(env); \ 541 ThreadInVMfromNative __tiv(thread); \ 542 debug_only(VMNativeEntryWrapper __vew;) \ 543 VM_ENTRY_BASE_FROM_LEAF(result_type, header, thread) 544 545 546 #define JVM_END } } 547 548 #endif // SHARE_RUNTIME_INTERFACESUPPORT_INLINE_HPP