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