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