1 /*
  2  * Copyright (c) 2017, 2023, 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/javaClasses.hpp"
 27 #include "classfile/vmSymbols.hpp"
 28 #include "jvm_io.h"
 29 #include "logging/log.hpp"
 30 #include "logging/logStream.hpp"
 31 #include "memory/resourceArea.hpp"
 32 #include "runtime/atomic.hpp"
 33 #include "runtime/globals.hpp"
 34 #include "runtime/handshake.hpp"
 35 #include "runtime/interfaceSupport.inline.hpp"
 36 #include "runtime/javaThread.inline.hpp"
 37 #include "runtime/os.hpp"
 38 #include "runtime/osThread.hpp"
 39 #include "runtime/stackWatermarkSet.hpp"
 40 #include "runtime/task.hpp"
 41 #include "runtime/threadSMR.hpp"
 42 #include "runtime/vmThread.hpp"
 43 #include "utilities/formatBuffer.hpp"
 44 #include "utilities/filterQueue.inline.hpp"
 45 #include "utilities/globalDefinitions.hpp"
 46 #include "utilities/preserveException.hpp"
 47 #include "utilities/systemMemoryBarrier.hpp"
 48 
 49 class HandshakeOperation : public CHeapObj<mtThread> {
 50   friend class HandshakeState;
 51  protected:
 52   HandshakeClosure*   _handshake_cl;
 53   // Keeps track of emitted and completed handshake operations.
 54   // Once it reaches zero all handshake operations have been performed.
 55   int32_t             _pending_threads;
 56   JavaThread*         _target;
 57   Thread*             _requester;
 58 
 59   // Must use AsyncHandshakeOperation when using AsyncHandshakeClosure.
 60   HandshakeOperation(AsyncHandshakeClosure* cl, JavaThread* target, Thread* requester) :
 61     _handshake_cl(cl),
 62     _pending_threads(1),
 63     _target(target),
 64     _requester(requester) {}
 65 
 66  public:
 67   HandshakeOperation(HandshakeClosure* cl, JavaThread* target, Thread* requester) :
 68     _handshake_cl(cl),
 69     _pending_threads(1),
 70     _target(target),
 71     _requester(requester) {}
 72   virtual ~HandshakeOperation() {}
 73   void prepare(JavaThread* current_target, Thread* executing_thread);
 74   void do_handshake(JavaThread* thread);
 75   bool is_completed() {
 76     int32_t val = Atomic::load(&_pending_threads);
 77     assert(val >= 0, "_pending_threads=%d cannot be negative", val);
 78     return val == 0;
 79   }
 80   void add_target_count(int count) { Atomic::add(&_pending_threads, count); }
 81   int32_t pending_threads()        { return Atomic::load(&_pending_threads); }
 82   const char* name()               { return _handshake_cl->name(); }
 83   bool is_async()                  { return _handshake_cl->is_async(); }
 84   bool is_suspend()                { return _handshake_cl->is_suspend(); }
 85   bool is_async_exception()        { return _handshake_cl->is_async_exception(); }
 86 };
 87 
 88 class AsyncHandshakeOperation : public HandshakeOperation {
 89  private:
 90   jlong _start_time_ns;
 91  public:
 92   AsyncHandshakeOperation(AsyncHandshakeClosure* cl, JavaThread* target, jlong start_ns)
 93     : HandshakeOperation(cl, target, nullptr), _start_time_ns(start_ns) {}
 94   virtual ~AsyncHandshakeOperation() { delete _handshake_cl; }
 95   jlong start_time() const           { return _start_time_ns; }
 96 };
 97 
 98 // Performing handshakes requires a custom yielding strategy because without it
 99 // there is a clear performance regression vs plain spinning. We keep track of
100 // when we last saw progress by looking at why each targeted thread has not yet
101 // completed its handshake. After spinning for a while with no progress we will
102 // yield, but as long as there is progress, we keep spinning. Thus we avoid
103 // yielding when there is potential work to be done or the handshake is close
104 // to being finished.
105 class HandshakeSpinYield : public StackObj {
106  private:
107   jlong _start_time_ns;
108   jlong _last_spin_start_ns;
109   jlong _spin_time_ns;
110 
111   int _result_count[2][HandshakeState::_number_states];
112   int _prev_result_pos;
113 
114   int current_result_pos() { return (_prev_result_pos + 1) & 0x1; }
115 
116   void wait_raw(jlong now) {
117     // We start with fine-grained nanosleeping until a millisecond has
118     // passed, at which point we resort to plain naked_short_sleep.
119     if (now - _start_time_ns < NANOSECS_PER_MILLISEC) {
120       os::naked_short_nanosleep(10 * (NANOUNITS / MICROUNITS));
121     } else {
122       os::naked_short_sleep(1);
123     }
124   }
125 
126   void wait_blocked(JavaThread* self, jlong now) {
127     ThreadBlockInVM tbivm(self);
128     wait_raw(now);
129   }
130 
131   bool state_changed() {
132     for (int i = 0; i < HandshakeState::_number_states; i++) {
133       if (_result_count[0][i] != _result_count[1][i]) {
134         return true;
135       }
136     }
137     return false;
138   }
139 
140   void reset_state() {
141     _prev_result_pos++;
142     for (int i = 0; i < HandshakeState::_number_states; i++) {
143       _result_count[current_result_pos()][i] = 0;
144     }
145   }
146 
147  public:
148   HandshakeSpinYield(jlong start_time) :
149     _start_time_ns(start_time), _last_spin_start_ns(start_time),
150     _spin_time_ns(0), _result_count(), _prev_result_pos(0) {
151 
152     const jlong max_spin_time_ns = 100 /* us */ * (NANOUNITS / MICROUNITS);
153     int free_cpus = os::active_processor_count() - 1;
154     _spin_time_ns = (5 /* us */ * (NANOUNITS / MICROUNITS)) * free_cpus; // zero on UP
155     _spin_time_ns = _spin_time_ns > max_spin_time_ns ? max_spin_time_ns : _spin_time_ns;
156   }
157 
158   void add_result(HandshakeState::ProcessResult pr) {
159     _result_count[current_result_pos()][pr]++;
160   }
161 
162   void process() {
163     jlong now = os::javaTimeNanos();
164     if (state_changed()) {
165       reset_state();
166       // We spin for x amount of time since last state change.
167       _last_spin_start_ns = now;
168       return;
169     }
170     jlong wait_target = _last_spin_start_ns + _spin_time_ns;
171     if (wait_target < now) {
172       // On UP this is always true.
173       Thread* self = Thread::current();
174       if (self->is_Java_thread()) {
175         wait_blocked(JavaThread::cast(self), now);
176       } else {
177         wait_raw(now);
178       }
179       _last_spin_start_ns = os::javaTimeNanos();
180     }
181     reset_state();
182   }
183 };
184 
185 static void handle_timeout(HandshakeOperation* op, JavaThread* target) {
186   JavaThreadIteratorWithHandle jtiwh;
187 
188   log_error(handshake)("Handshake timeout: %s(" INTPTR_FORMAT "), pending threads: " INT32_FORMAT,
189                        op->name(), p2i(op), op->pending_threads());
190 
191   if (target == nullptr) {
192     for ( ; JavaThread* thr = jtiwh.next(); ) {
193       if (thr->handshake_state()->operation_pending(op)) {
194         log_error(handshake)("JavaThread " INTPTR_FORMAT " has not cleared handshake op: " INTPTR_FORMAT, p2i(thr), p2i(op));
195         // Remember the last one found for more diagnostics below.
196         target = thr;
197       }
198     }
199   } else {
200     log_error(handshake)("JavaThread " INTPTR_FORMAT " has not cleared handshake op: " INTPTR_FORMAT, p2i(target), p2i(op));
201   }
202 
203   if (target != nullptr) {
204     if (os::signal_thread(target, SIGILL, "cannot be handshaked")) {
205       // Give target a chance to report the error and terminate the VM.
206       os::naked_sleep(3000);
207     }
208   } else {
209     log_error(handshake)("No thread with an unfinished handshake op(" INTPTR_FORMAT ") found.", p2i(op));
210   }
211   fatal("Handshake timeout");
212 }
213 
214 static void check_handshake_timeout(jlong start_time, HandshakeOperation* op, JavaThread* target = nullptr) {
215   // Check if handshake operation has timed out
216   jlong timeout_ns = millis_to_nanos(HandshakeTimeout);
217   if (timeout_ns > 0) {
218     if (os::javaTimeNanos() >= (start_time + timeout_ns)) {
219       handle_timeout(op, target);
220     }
221   }
222 }
223 
224 static void log_handshake_info(jlong start_time_ns, const char* name, int targets, int emitted_handshakes_executed, const char* extra = nullptr) {
225   if (log_is_enabled(Info, handshake)) {
226     jlong completion_time = os::javaTimeNanos() - start_time_ns;
227     log_info(handshake)("Handshake \"%s\", Targeted threads: %d, Executed by requesting thread: %d, Total completion time: " JLONG_FORMAT " ns%s%s",
228                         name, targets,
229                         emitted_handshakes_executed,
230                         completion_time,
231                         extra != nullptr ? ", " : "",
232                         extra != nullptr ? extra : "");
233   }
234 }
235 
236 class VM_HandshakeAllThreads: public VM_Operation {
237   HandshakeOperation* const _op;
238  public:
239   VM_HandshakeAllThreads(HandshakeOperation* op) : _op(op) {}
240 
241   const char* cause() const { return _op->name(); }
242 
243   bool evaluate_at_safepoint() const { return false; }
244 
245   void doit() {
246     jlong start_time_ns = os::javaTimeNanos();
247 
248     JavaThreadIteratorWithHandle jtiwh;
249     int number_of_threads_issued = 0;
250     for (JavaThread* thr = jtiwh.next(); thr != nullptr; thr = jtiwh.next()) {
251       thr->handshake_state()->add_operation(_op);
252       number_of_threads_issued++;
253     }
254     if (UseSystemMemoryBarrier) {
255       SystemMemoryBarrier::emit();
256     }
257 
258     if (number_of_threads_issued < 1) {
259       log_handshake_info(start_time_ns, _op->name(), 0, 0, "no threads alive");
260       return;
261     }
262     // _op was created with a count == 1 so don't double count.
263     _op->add_target_count(number_of_threads_issued - 1);
264 
265     log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThread");
266     HandshakeSpinYield hsy(start_time_ns);
267     // Keeps count on how many of own emitted handshakes
268     // this thread execute.
269     int emitted_handshakes_executed = 0;
270     do {
271       // Check if handshake operation has timed out
272       check_handshake_timeout(start_time_ns, _op);
273 
274       // Have VM thread perform the handshake operation for blocked threads.
275       // Observing a blocked state may of course be transient but the processing is guarded
276       // by mutexes and we optimistically begin by working on the blocked threads
277       jtiwh.rewind();
278       for (JavaThread* thr = jtiwh.next(); thr != nullptr; thr = jtiwh.next()) {
279         // A new thread on the ThreadsList will not have an operation,
280         // hence it is skipped in handshake_try_process.
281         HandshakeState::ProcessResult pr = thr->handshake_state()->try_process(_op);
282         hsy.add_result(pr);
283         if (pr == HandshakeState::_succeeded) {
284           emitted_handshakes_executed++;
285         }
286       }
287       hsy.process();
288     } while (!_op->is_completed());
289 
290     // This pairs up with the release store in do_handshake(). It prevents future
291     // loads from floating above the load of _pending_threads in is_completed()
292     // and thus prevents reading stale data modified in the handshake closure
293     // by the Handshakee.
294     OrderAccess::acquire();
295 
296     log_handshake_info(start_time_ns, _op->name(), number_of_threads_issued, emitted_handshakes_executed);
297   }
298 
299   VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
300 };
301 
302 void HandshakeOperation::prepare(JavaThread* current_target, Thread* executing_thread) {
303   if (current_target->is_terminated()) {
304     // Will never execute any handshakes on this thread.
305     return;
306   }
307   if (current_target != executing_thread) {
308     // Only when the target is not executing the handshake itself.
309     StackWatermarkSet::start_processing(current_target, StackWatermarkKind::gc);
310   }
311   if (_requester != nullptr && _requester != executing_thread && _requester->is_Java_thread()) {
312     // The handshake closure may contain oop Handles from the _requester.
313     // We must make sure we can use them.
314     StackWatermarkSet::start_processing(JavaThread::cast(_requester), StackWatermarkKind::gc);
315   }
316 }
317 
318 void HandshakeOperation::do_handshake(JavaThread* thread) {
319   jlong start_time_ns = 0;
320   if (log_is_enabled(Debug, handshake, task)) {
321     start_time_ns = os::javaTimeNanos();
322   }
323 
324   // Only actually execute the operation for non terminated threads.
325   if (!thread->is_terminated()) {
326     _handshake_cl->do_thread(thread);
327   }
328 
329   if (start_time_ns != 0) {
330     jlong completion_time = os::javaTimeNanos() - start_time_ns;
331     log_debug(handshake, task)("Operation: %s for thread " PTR_FORMAT ", is_vm_thread: %s, completed in " JLONG_FORMAT " ns",
332                                name(), p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()), completion_time);
333   }
334 
335   // Inform VMThread/Handshaker that we have completed the operation.
336   // When this is executed by the Handshakee we need a release store
337   // here to make sure memory operations executed in the handshake
338   // closure are visible to the VMThread/Handshaker after it reads
339   // that the operation has completed.
340   Atomic::dec(&_pending_threads);
341   // Trailing fence, used to make sure removal of the operation strictly
342   // happened after we completed the operation.
343 
344   // It is no longer safe to refer to 'this' as the VMThread/Handshaker may have destroyed this operation
345 }
346 
347 void Handshake::execute(HandshakeClosure* hs_cl) {
348   HandshakeOperation cto(hs_cl, nullptr, Thread::current());
349   VM_HandshakeAllThreads handshake(&cto);
350   VMThread::execute(&handshake);
351 }
352 
353 void Handshake::execute(HandshakeClosure* hs_cl, JavaThread* target) {
354   // tlh == nullptr means we rely on a ThreadsListHandle somewhere
355   // in the caller's context (and we sanity check for that).
356   Handshake::execute(hs_cl, nullptr, target);
357 }
358 
359 void Handshake::execute(HandshakeClosure* hs_cl, ThreadsListHandle* tlh, JavaThread* target) {
360   JavaThread* self = JavaThread::current();
361   HandshakeOperation op(hs_cl, target, Thread::current());
362 
363   jlong start_time_ns = os::javaTimeNanos();
364 
365   guarantee(target != nullptr, "must be");
366   if (tlh == nullptr) {
367     guarantee(Thread::is_JavaThread_protected_by_TLH(target),
368               "missing ThreadsListHandle in calling context.");
369     target->handshake_state()->add_operation(&op);
370   } else if (tlh->includes(target)) {
371     target->handshake_state()->add_operation(&op);
372   } else {
373     char buf[128];
374     jio_snprintf(buf, sizeof(buf),  "(thread= " INTPTR_FORMAT " dead)", p2i(target));
375     log_handshake_info(start_time_ns, op.name(), 0, 0, buf);
376     return;
377   }
378 
379   // Separate the arming of the poll in add_operation() above from
380   // the read of JavaThread state in the try_process() call below.
381   if (UseSystemMemoryBarrier) {
382     SystemMemoryBarrier::emit();
383   }
384 
385   // Keeps count on how many of own emitted handshakes
386   // this thread execute.
387   int emitted_handshakes_executed = 0;
388   HandshakeSpinYield hsy(start_time_ns);
389   while (!op.is_completed()) {
390     HandshakeState::ProcessResult pr = target->handshake_state()->try_process(&op);
391     if (pr == HandshakeState::_succeeded) {
392       emitted_handshakes_executed++;
393     }
394     if (op.is_completed()) {
395       break;
396     }
397 
398     // Check if handshake operation has timed out
399     check_handshake_timeout(start_time_ns, &op, target);
400 
401     hsy.add_result(pr);
402     // Check for pending handshakes to avoid possible deadlocks where our
403     // target is trying to handshake us.
404     if (SafepointMechanism::should_process(self)) {
405       // Will not suspend here.
406       ThreadBlockInVM tbivm(self);
407     }
408     hsy.process();
409   }
410 
411   // This pairs up with the release store in do_handshake(). It prevents future
412   // loads from floating above the load of _pending_threads in is_completed()
413   // and thus prevents reading stale data modified in the handshake closure
414   // by the Handshakee.
415   OrderAccess::acquire();
416 
417   log_handshake_info(start_time_ns, op.name(), 1, emitted_handshakes_executed);
418 }
419 
420 void Handshake::execute(AsyncHandshakeClosure* hs_cl, JavaThread* target) {
421   jlong start_time_ns = os::javaTimeNanos();
422   AsyncHandshakeOperation* op = new AsyncHandshakeOperation(hs_cl, target, start_time_ns);
423 
424   guarantee(target != nullptr, "must be");
425 
426   Thread* current = Thread::current();
427   if (current != target) {
428     // Another thread is handling the request and it must be protecting
429     // the target.
430     guarantee(Thread::is_JavaThread_protected_by_TLH(target),
431               "missing ThreadsListHandle in calling context.");
432   }
433   // Implied else:
434   // The target is handling the request itself so it can't be dead.
435 
436   target->handshake_state()->add_operation(op);
437 }
438 
439 // Filters
440 static bool non_self_executable_filter(HandshakeOperation* op) {
441   return !op->is_async();
442 }
443 static bool no_async_exception_filter(HandshakeOperation* op) {
444   return !op->is_async_exception();
445 }
446 static bool async_exception_filter(HandshakeOperation* op) {
447   return op->is_async_exception();
448 }
449 static bool no_suspend_no_async_exception_filter(HandshakeOperation* op) {
450   return !op->is_suspend() && !op->is_async_exception();
451 }
452 static bool all_ops_filter(HandshakeOperation* op) {
453   return true;
454 }
455 
456 HandshakeState::HandshakeState(JavaThread* target) :
457   _handshakee(target),
458   _queue(),
459   _lock(Monitor::nosafepoint, "HandshakeState_lock"),
460   _active_handshaker(),
461   _async_exceptions_blocked(false),
462   _suspended(false),
463   _async_suspend_handshake(false) {
464 }
465 
466 HandshakeState::~HandshakeState() {
467   while (has_operation()) {
468     HandshakeOperation* op = _queue.pop(all_ops_filter);
469     guarantee(op->is_async(), "Only async operations may still be present on queue");
470     delete op;
471   }
472 }
473 
474 void HandshakeState::add_operation(HandshakeOperation* op) {
475   // Adds are done lock free and so is arming.
476   _queue.push(op);
477   SafepointMechanism::arm_local_poll_release(_handshakee);
478 }
479 
480 bool HandshakeState::operation_pending(HandshakeOperation* op) {
481   MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
482   MatchOp mo(op);
483   return _queue.contains(mo);
484 }
485 
486 HandshakeOperation* HandshakeState::get_op_for_self(bool allow_suspend, bool check_async_exception) {
487   assert(_handshakee == Thread::current(), "Must be called by self");
488   assert(_lock.owned_by_self(), "Lock must be held");
489   assert(allow_suspend || !check_async_exception, "invalid case");
490 #if INCLUDE_JVMTI
491   if (allow_suspend && _handshakee->is_disable_suspend()) {
492     // filter out suspend operations while JavaThread is in disable_suspend mode
493     allow_suspend = false;
494   }
495 #endif
496   if (!allow_suspend) {
497     return _queue.peek(no_suspend_no_async_exception_filter);
498   } else if (check_async_exception && !_async_exceptions_blocked) {
499     return _queue.peek();
500   } else {
501     return _queue.peek(no_async_exception_filter);
502   }
503 }
504 
505 bool HandshakeState::has_operation(bool allow_suspend, bool check_async_exception) {
506   // We must not block here as that could lead to deadlocks if we already hold an
507   // "external" mutex. If the try_lock fails then we assume that there is an operation
508   // and force the caller to check more carefully in a safer context. If we can't get
509   // the lock it means another thread is trying to handshake with us, so it can't
510   // happen during thread termination and destruction.
511   bool ret = true;
512   if (_lock.try_lock()) {
513     ret = get_op_for_self(allow_suspend, check_async_exception) != nullptr;
514     _lock.unlock();
515   }
516   return ret;
517 }
518 
519 bool HandshakeState::has_async_exception_operation() {
520   if (!has_operation()) return false;
521   ConditionalMutexLocker ml(&_lock, !_lock.owned_by_self(), Mutex::_no_safepoint_check_flag);
522   return _queue.peek(async_exception_filter) != nullptr;
523 }
524 
525 void HandshakeState::clean_async_exception_operation() {
526   while (has_async_exception_operation()) {
527     MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
528     HandshakeOperation* op;
529     op = _queue.peek(async_exception_filter);
530     remove_op(op);
531     delete op;
532   }
533 }
534 
535 bool HandshakeState::have_non_self_executable_operation() {
536   assert(_handshakee != Thread::current(), "Must not be called by self");
537   assert(_lock.owned_by_self(), "Lock must be held");
538   return _queue.contains(non_self_executable_filter);
539 }
540 
541 HandshakeOperation* HandshakeState::get_op() {
542   assert(_handshakee != Thread::current(), "Must not be called by self");
543   assert(_lock.owned_by_self(), "Lock must be held");
544   return _queue.peek(non_self_executable_filter);
545 };
546 
547 void HandshakeState::remove_op(HandshakeOperation* op) {
548   assert(_lock.owned_by_self(), "Lock must be held");
549   MatchOp mo(op);
550   HandshakeOperation* ret = _queue.pop(mo);
551   assert(ret == op, "Popped op must match requested op");
552 };
553 
554 bool HandshakeState::process_by_self(bool allow_suspend, bool check_async_exception) {
555   assert(Thread::current() == _handshakee, "should call from _handshakee");
556   assert(!_handshakee->is_terminated(), "should not be a terminated thread");
557 
558   _handshakee->frame_anchor()->make_walkable();
559   // Threads shouldn't block if they are in the middle of printing, but...
560   ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id());
561 
562   while (has_operation()) {
563     // Handshakes cannot safely safepoint. The exceptions to this rule are
564     // the asynchronous suspension and unsafe access error handshakes.
565     MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
566 
567     HandshakeOperation* op = get_op_for_self(allow_suspend, check_async_exception);
568     if (op != nullptr) {
569       assert(op->_target == nullptr || op->_target == Thread::current(), "Wrong thread");
570       bool async = op->is_async();
571       log_trace(handshake)("Proc handshake %s " INTPTR_FORMAT " on " INTPTR_FORMAT " by self",
572                            async ? "asynchronous" : "synchronous", p2i(op), p2i(_handshakee));
573       op->prepare(_handshakee, _handshakee);
574       if (!async) {
575         HandleMark hm(_handshakee);
576         PreserveExceptionMark pem(_handshakee);
577         op->do_handshake(_handshakee); // acquire, op removed after
578         remove_op(op);
579       } else {
580         // An asynchronous handshake may put the JavaThread in blocked state (safepoint safe).
581         // The destructor ~PreserveExceptionMark touches the exception oop so it must not be executed,
582         // since a safepoint may be in-progress when returning from the async handshake.
583         remove_op(op);
584         op->do_handshake(_handshakee);
585         log_handshake_info(((AsyncHandshakeOperation*)op)->start_time(), op->name(), 1, 0, "asynchronous");
586         delete op;
587         return true; // Must check for safepoints
588       }
589     } else {
590       return false;
591     }
592   }
593   return false;
594 }
595 
596 bool HandshakeState::can_process_handshake() {
597   // handshake_safe may only be called with polls armed.
598   // Handshaker controls this by first claiming the handshake via claim_handshake().
599   return SafepointSynchronize::handshake_safe(_handshakee);
600 }
601 
602 bool HandshakeState::possibly_can_process_handshake() {
603   // Note that this method is allowed to produce false positives.
604   if (_handshakee->is_terminated()) {
605     return true;
606   }
607   switch (_handshakee->thread_state()) {
608   case _thread_in_native:
609     // native threads are safe if they have no java stack or have walkable stack
610     return !_handshakee->has_last_Java_frame() || _handshakee->frame_anchor()->walkable();
611 
612   case _thread_blocked:
613     return true;
614 
615   default:
616     return false;
617   }
618 }
619 
620 bool HandshakeState::claim_handshake() {
621   if (!_lock.try_lock()) {
622     return false;
623   }
624   // Operations are added lock free and then the poll is armed.
625   // If all handshake operations for the handshakee are finished and someone
626   // just adds an operation we may see it here. But if the handshakee is not
627   // armed yet it is not safe to proceed.
628   if (have_non_self_executable_operation()) {
629     OrderAccess::loadload(); // Matches the implicit storestore in add_operation()
630     if (SafepointMechanism::local_poll_armed(_handshakee)) {
631       return true;
632     }
633   }
634   _lock.unlock();
635   return false;
636 }
637 
638 HandshakeState::ProcessResult HandshakeState::try_process(HandshakeOperation* match_op) {
639   if (!has_operation()) {
640     // JT has already cleared its handshake
641     return HandshakeState::_no_operation;
642   }
643 
644   if (!possibly_can_process_handshake()) {
645     // JT is observed in an unsafe state, it must notice the handshake itself
646     return HandshakeState::_not_safe;
647   }
648 
649   // Claim the mutex if there still an operation to be executed.
650   if (!claim_handshake()) {
651     return HandshakeState::_claim_failed;
652   }
653 
654   // If we own the mutex at this point and while owning the mutex we
655   // can observe a safe state the thread cannot possibly continue without
656   // getting caught by the mutex.
657   if (!can_process_handshake()) {
658     _lock.unlock();
659     return HandshakeState::_not_safe;
660   }
661 
662   Thread* current_thread = Thread::current();
663 
664   HandshakeOperation* op = get_op();
665 
666   assert(op != nullptr, "Must have an op");
667   assert(SafepointMechanism::local_poll_armed(_handshakee), "Must be");
668   assert(op->_target == nullptr || _handshakee == op->_target, "Wrong thread");
669 
670   log_trace(handshake)("Processing handshake " INTPTR_FORMAT " by %s(%s)", p2i(op),
671                        op == match_op ? "handshaker" : "cooperative",
672                        current_thread->is_VM_thread() ? "VM Thread" : "JavaThread");
673 
674   op->prepare(_handshakee, current_thread);
675 
676   set_active_handshaker(current_thread);
677   op->do_handshake(_handshakee); // acquire, op removed after
678   set_active_handshaker(nullptr);
679   remove_op(op);
680 
681   _lock.unlock();
682 
683   log_trace(handshake)("%s(" INTPTR_FORMAT ") executed an op for JavaThread: " INTPTR_FORMAT " %s target op: " INTPTR_FORMAT,
684                        current_thread->is_VM_thread() ? "VM Thread" : "JavaThread",
685                        p2i(current_thread), p2i(_handshakee),
686                        op == match_op ? "including" : "excluding", p2i(match_op));
687 
688   return op == match_op ? HandshakeState::_succeeded : HandshakeState::_processed;
689 }
690 
691 void HandshakeState::do_self_suspend() {
692   assert(Thread::current() == _handshakee, "should call from _handshakee");
693   assert(_lock.owned_by_self(), "Lock must be held");
694   assert(!_handshakee->has_last_Java_frame() || _handshakee->frame_anchor()->walkable(), "should have walkable stack");
695   assert(_handshakee->thread_state() == _thread_blocked, "Caller should have transitioned to _thread_blocked");
696 
697   while (is_suspended()) {
698     log_trace(thread, suspend)("JavaThread:" INTPTR_FORMAT " suspended", p2i(_handshakee));
699     _lock.wait_without_safepoint_check();
700   }
701   log_trace(thread, suspend)("JavaThread:" INTPTR_FORMAT " resumed", p2i(_handshakee));
702 }
703 
704 // This is the closure that prevents a suspended JavaThread from
705 // escaping the suspend request.
706 class ThreadSelfSuspensionHandshake : public AsyncHandshakeClosure {
707  public:
708   ThreadSelfSuspensionHandshake() : AsyncHandshakeClosure("ThreadSelfSuspensionHandshake") {}
709   void do_thread(Thread* thr) {
710     JavaThread* current = JavaThread::cast(thr);
711     assert(current == Thread::current(), "Must be self executed.");
712     JavaThreadState jts = current->thread_state();
713 
714     current->set_thread_state(_thread_blocked);
715     current->handshake_state()->do_self_suspend();
716     current->set_thread_state(jts);
717     current->handshake_state()->set_async_suspend_handshake(false);
718   }
719   virtual bool is_suspend() { return true; }
720 };
721 
722 bool HandshakeState::suspend_with_handshake() {
723   assert(_handshakee->threadObj() != nullptr, "cannot suspend with a null threadObj");
724   if (_handshakee->is_exiting()) {
725     log_trace(thread, suspend)("JavaThread:" INTPTR_FORMAT " exiting", p2i(_handshakee));
726     return false;
727   }
728   if (has_async_suspend_handshake()) {
729     if (is_suspended()) {
730       // Target is already suspended.
731       log_trace(thread, suspend)("JavaThread:" INTPTR_FORMAT " already suspended", p2i(_handshakee));
732       return false;
733     } else {
734       // Target is going to wake up and leave suspension.
735       // Let's just stop the thread from doing that.
736       log_trace(thread, suspend)("JavaThread:" INTPTR_FORMAT " re-suspended", p2i(_handshakee));
737       set_suspended(true);
738       return true;
739     }
740   }
741   // no suspend request
742   assert(!is_suspended(), "cannot be suspended without a suspend request");
743   // Thread is safe, so it must execute the request, thus we can count it as suspended
744   // from this point.
745   set_suspended(true);
746   set_async_suspend_handshake(true);
747   log_trace(thread, suspend)("JavaThread:" INTPTR_FORMAT " suspended, arming ThreadSuspension", p2i(_handshakee));
748   ThreadSelfSuspensionHandshake* ts = new ThreadSelfSuspensionHandshake();
749   Handshake::execute(ts, _handshakee);
750   return true;
751 }
752 
753 // This is the closure that synchronously honors the suspend request.
754 class SuspendThreadHandshake : public HandshakeClosure {
755   bool _did_suspend;
756 public:
757   SuspendThreadHandshake() : HandshakeClosure("SuspendThread"), _did_suspend(false) {}
758   void do_thread(Thread* thr) {
759     JavaThread* target = JavaThread::cast(thr);
760     _did_suspend = target->handshake_state()->suspend_with_handshake();
761   }
762   bool did_suspend() { return _did_suspend; }
763 };
764 
765 bool HandshakeState::suspend() {
766   JVMTI_ONLY(assert(!_handshakee->is_in_VTMS_transition(), "no suspend allowed in VTMS transition");)
767   JavaThread* self = JavaThread::current();
768   if (_handshakee == self) {
769     // If target is the current thread we can bypass the handshake machinery
770     // and just suspend directly
771     ThreadBlockInVM tbivm(self);
772     MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
773     set_suspended(true);
774     do_self_suspend();
775     return true;
776   } else {
777     SuspendThreadHandshake st;
778     Handshake::execute(&st, _handshakee);
779     return st.did_suspend();
780   }
781 }
782 
783 bool HandshakeState::resume() {
784   if (!is_suspended()) {
785     return false;
786   }
787   MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
788   if (!is_suspended()) {
789     assert(!_handshakee->is_suspended(), "cannot be suspended without a suspend request");
790     return false;
791   }
792   // Resume the thread.
793   set_suspended(false);
794   _lock.notify();
795   return true;
796 }
797 
798 void HandshakeState::handle_unsafe_access_error() {
799   if (is_suspended()) {
800     // A suspend handshake was added to the queue after the
801     // unsafe access error. Since the suspender has already
802     // considered this JT as suspended and assumes it won't go
803     // back to Java until resumed we cannot create the exception
804     // object yet. Add a new unsafe access error operation to
805     // the end of the queue and try again in the next attempt.
806     Handshake::execute(new UnsafeAccessErrorHandshake(), _handshakee);
807     log_info(handshake)("JavaThread " INTPTR_FORMAT " skipping unsafe access processing due to suspend.", p2i(_handshakee));
808     return;
809   }
810   // Release the handshake lock before constructing the oop to
811   // avoid deadlocks since that can block. This will allow the
812   // JavaThread to execute normally as if it was outside a handshake.
813   // We will reacquire the handshake lock at return from ~MutexUnlocker.
814   MutexUnlocker ml(&_lock, Mutex::_no_safepoint_check_flag);
815   // We may be at method entry which requires we save the do-not-unlock flag.
816   UnlockFlagSaver fs(_handshakee);
817   Handle h_exception = Exceptions::new_exception(_handshakee, vmSymbols::java_lang_InternalError(), "a fault occurred in an unsafe memory access operation");
818   if (h_exception()->is_a(vmClasses::InternalError_klass())) {
819     java_lang_InternalError::set_during_unsafe_access(h_exception());
820   }
821   _handshakee->handle_async_exception(h_exception());
822 }