1 /*
  2  * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "compiler/oopMap.hpp"
 28 #include "interpreter/interpreter.hpp"
 29 #include "memory/resourceArea.hpp"
 30 #include "memory/universe.hpp"
 31 #include "oops/markWord.hpp"
 32 #include "oops/method.hpp"
 33 #include "oops/oop.inline.hpp"
 34 #include "prims/methodHandles.hpp"
 35 #include "runtime/frame.inline.hpp"
 36 #include "runtime/handles.inline.hpp"
 37 #include "runtime/javaCalls.hpp"
 38 #include "runtime/monitorChunk.hpp"
 39 #include "runtime/os.inline.hpp"
 40 #include "runtime/signature.hpp"
 41 #include "runtime/stackWatermarkSet.hpp"
 42 #include "runtime/stubCodeGenerator.hpp"
 43 #include "runtime/stubRoutines.hpp"
 44 #include "vmreg_aarch64.inline.hpp"
 45 #ifdef COMPILER1
 46 #include "c1/c1_Runtime1.hpp"
 47 #include "runtime/vframeArray.hpp"
 48 #endif
 49 
 50 #ifdef ASSERT
 51 void RegisterMap::check_location_valid() {
 52 }
 53 #endif
 54 
 55 
 56 // Profiling/safepoint support
 57 
 58 bool frame::safe_for_sender(JavaThread *thread) {
 59   if (is_heap_frame()) {
 60     return true;
 61   }
 62   address   sp = (address)_sp;
 63   address   fp = (address)_fp;
 64   address   unextended_sp = (address)_unextended_sp;
 65 
 66   // consider stack guards when trying to determine "safe" stack pointers
 67   // sp must be within the usable part of the stack (not in guards)
 68   if (!thread->is_in_usable_stack(sp)) {
 69     return false;
 70   }
 71 
 72   // When we are running interpreted code the machine stack pointer, SP, is
 73   // set low enough so that the Java expression stack can grow and shrink
 74   // without ever exceeding the machine stack bounds.  So, ESP >= SP.
 75 
 76   // When we call out of an interpreted method, SP is incremented so that
 77   // the space between SP and ESP is removed.  The SP saved in the callee's
 78   // frame is the SP *before* this increment.  So, when we walk a stack of
 79   // interpreter frames the sender's SP saved in a frame might be less than
 80   // the SP at the point of call.
 81 
 82   // So unextended sp must be within the stack but we need not to check
 83   // that unextended sp >= sp
 84   if (!thread->is_in_full_stack_checked(unextended_sp)) {
 85     return false;
 86   }
 87 
 88   // an fp must be within the stack and above (but not equal) sp
 89   // second evaluation on fp+ is added to handle situation where fp is -1
 90   bool fp_safe = thread->is_in_stack_range_excl(fp, sp) &&
 91                  thread->is_in_full_stack_checked(fp + (return_addr_offset * sizeof(void*)));
 92 
 93   // We know sp/unextended_sp are safe only fp is questionable here
 94 
 95   // If the current frame is known to the code cache then we can attempt to
 96   // to construct the sender and do some validation of it. This goes a long way
 97   // toward eliminating issues when we get in frame construction code
 98 
 99   if (_cb != nullptr ) {
100 
101     // First check if frame is complete and tester is reliable
102     // Unfortunately we can only check frame complete for runtime stubs and nmethod
103     // other generic buffer blobs are more problematic so we just assume they are
104     // ok. adapter blobs never have a frame complete and are never ok.
105 
106     if (!_cb->is_frame_complete_at(_pc)) {
107       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
108         return false;
109       }
110     }
111 
112     // Could just be some random pointer within the codeBlob
113     if (!_cb->code_contains(_pc)) {
114       return false;
115     }
116 
117     // Entry frame checks
118     if (is_entry_frame()) {
119       // an entry frame must have a valid fp.
120       return fp_safe && is_entry_frame_valid(thread);
121     } else if (is_upcall_stub_frame()) {
122       return fp_safe;
123     }
124 
125     intptr_t* sender_sp = nullptr;
126     intptr_t* sender_unextended_sp = nullptr;
127     address   sender_pc = nullptr;
128     intptr_t* saved_fp =  nullptr;
129 
130     if (is_interpreted_frame()) {
131       // fp must be safe
132       if (!fp_safe) {
133         return false;
134       }
135 
136       // for interpreted frames, the value below is the sender "raw" sp,
137       // which can be different from the sender unextended sp (the sp seen
138       // by the sender) because of current frame local variables
139       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
140       sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
141       saved_fp = (intptr_t*) this->fp()[link_offset];
142       sender_pc = pauth_strip_verifiable((address) this->fp()[return_addr_offset]);
143     } else {
144       // must be some sort of compiled/runtime frame
145       // fp does not have to be safe (although it could be check for c1?)
146 
147       // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
148       if (_cb->frame_size() <= 0) {
149         return false;
150       }
151 
152       sender_sp = _unextended_sp + _cb->frame_size();
153       // Is sender_sp safe?
154       if (!thread->is_in_full_stack_checked((address)sender_sp)) {
155         return false;
156       }
157       sender_unextended_sp = sender_sp;
158       // Note: frame::sender_sp_offset is only valid for compiled frame
159       saved_fp = (intptr_t*) *(sender_sp - frame::sender_sp_offset);
160       // Note: PAC authentication may fail in case broken frame is passed in.
161       // Just strip it for now.
162       sender_pc = pauth_strip_pointer((address) *(sender_sp - 1));
163     }
164 
165     if (Continuation::is_return_barrier_entry(sender_pc)) {
166       // If our sender_pc is the return barrier, then our "real" sender is the continuation entry
167       frame s = Continuation::continuation_bottom_sender(thread, *this, sender_sp);
168       sender_sp = s.sp();
169       sender_pc = s.pc();
170     }
171 
172     // If the potential sender is the interpreter then we can do some more checking
173     if (Interpreter::contains(sender_pc)) {
174 
175       // fp is always saved in a recognizable place in any code we generate. However
176       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved fp
177       // is really a frame pointer.
178 
179       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
180         return false;
181       }
182 
183       // construct the potential sender
184 
185       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
186 
187       return sender.is_interpreted_frame_valid(thread);
188 
189     }
190 
191     // We must always be able to find a recognizable pc
192     CodeBlob* sender_blob = CodeCache::find_blob(sender_pc);
193     if (sender_pc == nullptr ||  sender_blob == nullptr) {
194       return false;
195     }
196 
197     // Could just be some random pointer within the codeBlob
198     if (!sender_blob->code_contains(sender_pc)) {
199       return false;
200     }
201 
202     // We should never be able to see an adapter if the current frame is something from code cache
203     if (sender_blob->is_adapter_blob()) {
204       return false;
205     }
206 
207     // Could be the call_stub
208     if (StubRoutines::returns_to_call_stub(sender_pc)) {
209       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
210         return false;
211       }
212 
213       // construct the potential sender
214 
215       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
216 
217       // Validate the JavaCallWrapper an entry frame must have
218       address jcw = (address)sender.entry_frame_call_wrapper();
219 
220       return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
221     } else if (sender_blob->is_upcall_stub()) {
222       return false;
223     }
224 
225     nmethod* nm = sender_blob->as_nmethod_or_null();
226     if (nm != nullptr) {
227       if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
228           nm->method()->is_method_handle_intrinsic()) {
229         return false;
230       }
231     }
232 
233     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
234     // because the return address counts against the callee's frame.
235 
236     if (sender_blob->frame_size() <= 0) {
237       assert(!sender_blob->is_nmethod(), "should count return address at least");
238       return false;
239     }
240 
241     // We should never be able to see anything here except an nmethod. If something in the
242     // code cache (current frame) is called by an entity within the code cache that entity
243     // should not be anything but the call stub (already covered), the interpreter (already covered)
244     // or an nmethod.
245 
246     if (!sender_blob->is_nmethod()) {
247         return false;
248     }
249 
250     // Could put some more validation for the potential non-interpreted sender
251     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
252 
253     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
254 
255     // We've validated the potential sender that would be created
256     return true;
257   }
258 
259   // Must be native-compiled frame. Since sender will try and use fp to find
260   // linkages it must be safe
261 
262   if (!fp_safe) {
263     return false;
264   }
265 
266   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
267 
268   if ( (address) this->fp()[return_addr_offset] == nullptr) return false;
269 
270 
271   // could try and do some more potential verification of native frame if we could think of some...
272 
273   return true;
274 
275 }
276 
277 void frame::patch_pc(Thread* thread, address pc) {
278   assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
279   address* pc_addr = &(((address*) sp())[-1]);
280   address signed_pc = pauth_sign_return_address(pc);
281   address pc_old = pauth_strip_verifiable(*pc_addr);
282 
283   if (TracePcPatching) {
284     tty->print("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
285                   p2i(pc_addr), p2i(pc_old), p2i(pc));
286     if (VM_Version::use_rop_protection()) {
287       tty->print(" [signed " INTPTR_FORMAT " -> " INTPTR_FORMAT "]", p2i(*pc_addr), p2i(signed_pc));
288     }
289     tty->print_cr("");
290   }
291 
292   assert(!Continuation::is_return_barrier_entry(pc_old), "return barrier");
293 
294   // Either the return address is the original one or we are going to
295   // patch in the same address that's already there.
296   assert(_pc == pc_old || pc == pc_old || pc_old == nullptr, "");
297   DEBUG_ONLY(address old_pc = _pc;)
298   *pc_addr = signed_pc;
299   _pc = pc; // must be set before call to get_deopt_original_pc
300   address original_pc = get_deopt_original_pc();
301   if (original_pc != nullptr) {
302     assert(original_pc == old_pc, "expected original PC to be stored before patching");
303     _deopt_state = is_deoptimized;
304     _pc = original_pc;
305   } else {
306     _deopt_state = not_deoptimized;
307   }
308 }
309 
310 intptr_t* frame::entry_frame_argument_at(int offset) const {
311   // convert offset to index to deal with tsi
312   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
313   // Entry frame's arguments are always in relation to unextended_sp()
314   return &unextended_sp()[index];
315 }
316 
317 // locals
318 
319 void frame::interpreter_frame_set_locals(intptr_t* locs)  {
320   assert(is_interpreted_frame(), "interpreted frame expected");
321   // set relativized locals
322   ptr_at_put(interpreter_frame_locals_offset, (intptr_t) (locs - fp()));
323 }
324 
325 // sender_sp
326 
327 intptr_t* frame::interpreter_frame_sender_sp() const {
328   assert(is_interpreted_frame(), "interpreted frame expected");
329   return (intptr_t*) at(interpreter_frame_sender_sp_offset);
330 }
331 
332 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
333   assert(is_interpreted_frame(), "interpreted frame expected");
334   ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
335 }
336 
337 
338 // monitor elements
339 
340 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
341   return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
342 }
343 
344 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
345   BasicObjectLock* result = (BasicObjectLock*) at_relative(interpreter_frame_monitor_block_top_offset);
346   // make sure the pointer points inside the frame
347   assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
348   assert((intptr_t*) result < fp(),  "monitor end should be strictly below the frame pointer");
349   return result;
350 }
351 
352 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
353   assert(is_interpreted_frame(), "interpreted frame expected");
354   // set relativized monitor_block_top
355   ptr_at_put(interpreter_frame_monitor_block_top_offset, (intptr_t*)value - fp());
356   assert(at_absolute(interpreter_frame_monitor_block_top_offset) <= interpreter_frame_monitor_block_top_offset, "");
357 }
358 
359 // Used by template based interpreter deoptimization
360 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
361   assert(is_interpreted_frame(), "interpreted frame expected");
362   // set relativized last_sp
363   ptr_at_put(interpreter_frame_last_sp_offset, sp != nullptr ? (sp - fp()) : 0);
364 }
365 
366 // Used by template based interpreter deoptimization
367 void frame::interpreter_frame_set_extended_sp(intptr_t* sp) {
368   assert(is_interpreted_frame(), "interpreted frame expected");
369   // set relativized extended_sp
370   ptr_at_put(interpreter_frame_extended_sp_offset, (sp - fp()));
371 }
372 
373 frame frame::sender_for_entry_frame(RegisterMap* map) const {
374   assert(map != nullptr, "map must be set");
375   // Java frame called from C; skip all C frames and return top C
376   // frame of that chunk as the sender
377   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
378   assert(!entry_frame_is_first(), "next Java fp must be non zero");
379   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
380   // Since we are walking the stack now this nested anchor is obviously walkable
381   // even if it wasn't when it was stacked.
382   jfa->make_walkable();
383   map->clear();
384   assert(map->include_argument_oops(), "should be set by clear");
385   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
386   fr.set_sp_is_trusted();
387 
388   return fr;
389 }
390 
391 UpcallStub::FrameData* UpcallStub::frame_data_for_frame(const frame& frame) const {
392   assert(frame.is_upcall_stub_frame(), "wrong frame");
393   // need unextended_sp here, since normal sp is wrong for interpreter callees
394   return reinterpret_cast<UpcallStub::FrameData*>(
395     reinterpret_cast<address>(frame.unextended_sp()) + in_bytes(_frame_data_offset));
396 }
397 
398 bool frame::upcall_stub_frame_is_first() const {
399   assert(is_upcall_stub_frame(), "must be optimzed entry frame");
400   UpcallStub* blob = _cb->as_upcall_stub();
401   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
402   return jfa->last_Java_sp() == nullptr;
403 }
404 
405 frame frame::sender_for_upcall_stub_frame(RegisterMap* map) const {
406   assert(map != nullptr, "map must be set");
407   UpcallStub* blob = _cb->as_upcall_stub();
408   // Java frame called from C; skip all C frames and return top C
409   // frame of that chunk as the sender
410   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
411   assert(!upcall_stub_frame_is_first(), "must have a frame anchor to go back to");
412   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
413   // Since we are walking the stack now this nested anchor is obviously walkable
414   // even if it wasn't when it was stacked.
415   jfa->make_walkable();
416   map->clear();
417   assert(map->include_argument_oops(), "should be set by clear");
418   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
419 
420   return fr;
421 }
422 
423 #if defined(ASSERT)
424 static address get_register_address_in_stub(const frame& stub_fr, VMReg reg) {
425   RegisterMap map(nullptr,
426                   RegisterMap::UpdateMap::include,
427                   RegisterMap::ProcessFrames::skip,
428                   RegisterMap::WalkContinuation::skip);
429   stub_fr.oop_map()->update_register_map(&stub_fr, &map);
430   return map.location(reg, stub_fr.sp());
431 }
432 #endif
433 
434 JavaThread** frame::saved_thread_address(const frame& f) {
435   CodeBlob* cb = f.cb();
436   assert(cb != nullptr && cb->is_runtime_stub(), "invalid frame");
437 
438   JavaThread** thread_addr;
439 #ifdef COMPILER1
440   if (cb == Runtime1::blob_for(C1StubId::monitorenter_id) ||
441       cb == Runtime1::blob_for(C1StubId::monitorenter_nofpu_id)) {
442     thread_addr = (JavaThread**)(f.sp() + Runtime1::runtime_blob_current_thread_offset(f));
443   } else
444 #endif
445   {
446     // c2 only saves rbp in the stub frame so nothing to do.
447     thread_addr = nullptr;
448   }
449   assert(get_register_address_in_stub(f, SharedRuntime::thread_register()) == (address)thread_addr, "wrong thread address");
450   return thread_addr;
451 }
452 
453 //------------------------------------------------------------------------------
454 // frame::verify_deopt_original_pc
455 //
456 // Verifies the calculated original PC of a deoptimization PC for the
457 // given unextended SP.
458 #ifdef ASSERT
459 void frame::verify_deopt_original_pc(nmethod* nm, intptr_t* unextended_sp) {
460   frame fr;
461 
462   // This is ugly but it's better than to change {get,set}_original_pc
463   // to take an SP value as argument.  And it's only a debugging
464   // method anyway.
465   fr._unextended_sp = unextended_sp;
466 
467   address original_pc = nm->get_original_pc(&fr);
468   assert(nm->insts_contains_inclusive(original_pc),
469          "original PC must be in the main code section of the compiled method (or must be immediately following it)");
470 }
471 #endif
472 
473 //------------------------------------------------------------------------------
474 // frame::adjust_unextended_sp
475 #ifdef ASSERT
476 void frame::adjust_unextended_sp() {
477   // On aarch64, sites calling method handle intrinsics and lambda forms are treated
478   // as any other call site. Therefore, no special action is needed when we are
479   // returning to any of these call sites.
480 
481   if (_cb != nullptr) {
482     nmethod* sender_nm = _cb->as_nmethod_or_null();
483     if (sender_nm != nullptr) {
484       // If the sender PC is a deoptimization point, get the original PC.
485       if (sender_nm->is_deopt_entry(_pc) ||
486           sender_nm->is_deopt_mh_entry(_pc)) {
487         verify_deopt_original_pc(sender_nm, _unextended_sp);
488       }
489     }
490   }
491 }
492 #endif
493 
494 
495 //------------------------------------------------------------------------------
496 // frame::sender_for_interpreter_frame
497 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
498   // SP is the raw SP from the sender after adapter or interpreter
499   // extension.
500   intptr_t* sender_sp = this->sender_sp();
501 
502   // This is the sp before any possible extension (adapter/locals).
503   intptr_t* unextended_sp = interpreter_frame_sender_sp();
504   intptr_t* sender_fp = link();
505 
506 #if COMPILER2_OR_JVMCI
507   if (map->update_map()) {
508     update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
509   }
510 #endif // COMPILER2_OR_JVMCI
511 
512   // For ROP protection, Interpreter will have signed the sender_pc,
513   // but there is no requirement to authenticate it here.
514   address sender_pc = pauth_strip_verifiable(sender_pc_maybe_signed());
515 
516   if (Continuation::is_return_barrier_entry(sender_pc)) {
517     if (map->walk_cont()) { // about to walk into an h-stack
518       return Continuation::top_frame(*this, map);
519     } else {
520       return Continuation::continuation_bottom_sender(map->thread(), *this, sender_sp);
521     }
522   }
523 
524   return frame(sender_sp, unextended_sp, sender_fp, sender_pc);
525 }
526 
527 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
528   assert(is_interpreted_frame(), "Not an interpreted frame");
529   // These are reasonable sanity checks
530   if (fp() == nullptr || (intptr_t(fp()) & (wordSize-1)) != 0) {
531     return false;
532   }
533   if (sp() == nullptr || (intptr_t(sp()) & (wordSize-1)) != 0) {
534     return false;
535   }
536   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
537     return false;
538   }
539   // These are hacks to keep us out of trouble.
540   // The problem with these is that they mask other problems
541   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
542     return false;
543   }
544 
545   // do some validation of frame elements
546 
547   // first the method
548 
549   Method* m = safe_interpreter_frame_method();
550 
551   // validate the method we'd find in this potential sender
552   if (!Method::is_valid_method(m)) return false;
553 
554   // stack frames shouldn't be much larger than max_stack elements
555   // this test requires the use of unextended_sp which is the sp as seen by
556   // the current frame, and not sp which is the "raw" pc which could point
557   // further because of local variables of the callee method inserted after
558   // method arguments
559   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
560     return false;
561   }
562 
563   // validate bci/bcx
564 
565   address  bcp    = interpreter_frame_bcp();
566   if (m->validate_bci_from_bcp(bcp) < 0) {
567     return false;
568   }
569 
570   // validate constantPoolCache*
571   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
572   if (MetaspaceObj::is_valid(cp) == false) return false;
573 
574   // validate locals
575 
576   address locals =  (address)interpreter_frame_locals();
577   return thread->is_in_stack_range_incl(locals, (address)fp());
578 }
579 
580 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
581   assert(is_interpreted_frame(), "interpreted frame expected");
582   Method* method = interpreter_frame_method();
583   BasicType type = method->result_type();
584 
585   intptr_t* tos_addr;
586   if (method->is_native()) {
587     // TODO : ensure AARCH64 does the same as Intel here i.e. push v0 then r0
588     // Prior to calling into the runtime to report the method_exit the possible
589     // return value is pushed to the native stack. If the result is a jfloat/jdouble
590     // then ST0 is saved before EAX/EDX. See the note in generate_native_result
591     tos_addr = (intptr_t*)sp();
592     if (type == T_FLOAT || type == T_DOUBLE) {
593       // This is times two because we do a push(ltos) after pushing XMM0
594       // and that takes two interpreter stack slots.
595       tos_addr += 2 * Interpreter::stackElementWords;
596     }
597   } else {
598     tos_addr = (intptr_t*)interpreter_frame_tos_address();
599   }
600 
601   switch (type) {
602     case T_OBJECT  :
603     case T_ARRAY   : {
604       oop obj;
605       if (method->is_native()) {
606         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
607       } else {
608         oop* obj_p = (oop*)tos_addr;
609         obj = (obj_p == nullptr) ? (oop)nullptr : *obj_p;
610       }
611       assert(Universe::is_in_heap_or_null(obj), "sanity check");
612       *oop_result = obj;
613       break;
614     }
615     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
616     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
617     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
618     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
619     case T_INT     : value_result->i = *(jint*)tos_addr; break;
620     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
621     case T_FLOAT   : {
622         value_result->f = *(jfloat*)tos_addr;
623       break;
624     }
625     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
626     case T_VOID    : /* Nothing to do */ break;
627     default        : ShouldNotReachHere();
628   }
629 
630   return type;
631 }
632 
633 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
634   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
635   return &interpreter_frame_tos_address()[index];
636 }
637 
638 #ifndef PRODUCT
639 
640 #define DESCRIBE_FP_OFFSET(name) \
641   values.describe(frame_no, fp() + frame::name##_offset, #name)
642 
643 void frame::describe_pd(FrameValues& values, int frame_no) {
644   if (is_interpreted_frame()) {
645     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
646     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
647     DESCRIBE_FP_OFFSET(interpreter_frame_method);
648     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
649     DESCRIBE_FP_OFFSET(interpreter_frame_extended_sp);
650     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
651     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
652     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
653     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
654     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
655   }
656 
657   if (is_java_frame() || Continuation::is_continuation_enterSpecial(*this)) {
658     intptr_t* ret_pc_loc;
659     intptr_t* fp_loc;
660     if (is_interpreted_frame()) {
661       ret_pc_loc = fp() + return_addr_offset;
662       fp_loc = fp();
663     } else {
664       ret_pc_loc = real_fp() - return_addr_offset;
665       fp_loc = real_fp() - sender_sp_offset;
666     }
667     address ret_pc = *(address*)ret_pc_loc;
668     values.describe(frame_no, ret_pc_loc,
669       Continuation::is_return_barrier_entry(ret_pc) ? "return address (return barrier)" : "return address");
670     values.describe(-1, fp_loc, "saved fp", 0); // "unowned" as value belongs to sender
671   }
672 }
673 #endif
674 
675 intptr_t *frame::initial_deoptimization_info() {
676   // Not used on aarch64, but we must return something.
677   return nullptr;
678 }
679 
680 #undef DESCRIBE_FP_OFFSET
681 
682 #define DESCRIBE_FP_OFFSET(name)                     \
683   {                                                  \
684     uintptr_t *p = (uintptr_t *)fp;                  \
685     printf(INTPTR_FORMAT " " INTPTR_FORMAT " %s\n",  \
686            (uintptr_t)(p + frame::name##_offset),    \
687            p[frame::name##_offset], #name);          \
688   }
689 
690 static THREAD_LOCAL uintptr_t nextfp;
691 static THREAD_LOCAL uintptr_t nextpc;
692 static THREAD_LOCAL uintptr_t nextsp;
693 static THREAD_LOCAL RegisterMap *reg_map;
694 
695 static void printbc(Method *m, intptr_t bcx) {
696   const char *name;
697   char buf[16];
698   if (m->validate_bci_from_bcp((address)bcx) < 0
699       || !m->contains((address)bcx)) {
700     name = "???";
701     snprintf(buf, sizeof buf, "(bad)");
702   } else {
703     int bci = m->bci_from((address)bcx);
704     snprintf(buf, sizeof buf, "%d", bci);
705     name = Bytecodes::name(m->code_at(bci));
706   }
707   ResourceMark rm;
708   printf("%s : %s ==> %s\n", m->name_and_sig_as_C_string(), buf, name);
709 }
710 
711 static void internal_pf(uintptr_t sp, uintptr_t fp, uintptr_t pc, uintptr_t bcx) {
712   if (! fp)
713     return;
714 
715   DESCRIBE_FP_OFFSET(return_addr);
716   DESCRIBE_FP_OFFSET(link);
717   DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
718   DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
719   DESCRIBE_FP_OFFSET(interpreter_frame_method);
720   DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
721   DESCRIBE_FP_OFFSET(interpreter_frame_extended_sp);
722   DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
723   DESCRIBE_FP_OFFSET(interpreter_frame_cache);
724   DESCRIBE_FP_OFFSET(interpreter_frame_locals);
725   DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
726   DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
727   uintptr_t *p = (uintptr_t *)fp;
728 
729   // We want to see all frames, native and Java.  For compiled and
730   // interpreted frames we have special information that allows us to
731   // unwind them; for everything else we assume that the native frame
732   // pointer chain is intact.
733   frame this_frame((intptr_t*)sp, (intptr_t*)fp, (address)pc);
734   if (this_frame.is_compiled_frame() ||
735       this_frame.is_interpreted_frame()) {
736     frame sender = this_frame.sender(reg_map);
737     nextfp = (uintptr_t)sender.fp();
738     nextpc = (uintptr_t)sender.pc();
739     nextsp = (uintptr_t)sender.unextended_sp();
740   } else {
741     nextfp = p[frame::link_offset];
742     nextpc = p[frame::return_addr_offset];
743     nextsp = (uintptr_t)&p[frame::sender_sp_offset];
744   }
745 
746   if (bcx == -1ULL)
747     bcx = p[frame::interpreter_frame_bcp_offset];
748 
749   if (Interpreter::contains((address)pc)) {
750     Method* m = (Method*)p[frame::interpreter_frame_method_offset];
751     if(m && m->is_method()) {
752       printbc(m, bcx);
753     } else
754       printf("not a Method\n");
755   } else {
756     CodeBlob *cb = CodeCache::find_blob((address)pc);
757     if (cb != nullptr) {
758       if (cb->is_nmethod()) {
759         ResourceMark rm;
760         nmethod* nm = (nmethod*)cb;
761         printf("nmethod %s\n", nm->method()->name_and_sig_as_C_string());
762       } else if (cb->name()) {
763         printf("CodeBlob %s\n", cb->name());
764       }
765     }
766   }
767 }
768 
769 extern "C" void npf() {
770   CodeBlob *cb = CodeCache::find_blob((address)nextpc);
771   // C2 does not always chain the frame pointers when it can, instead
772   // preferring to use fixed offsets from SP, so a simple leave() does
773   // not work.  Instead, it adds the frame size to SP then pops FP and
774   // LR.  We have to do the same thing to get a good call chain.
775   if (cb && cb->frame_size())
776     nextfp = nextsp + wordSize * (cb->frame_size() - 2);
777   internal_pf (nextsp, nextfp, nextpc, -1);
778 }
779 
780 extern "C" void pf(uintptr_t sp, uintptr_t fp, uintptr_t pc,
781                    uintptr_t bcx, uintptr_t thread) {
782   if (!reg_map) {
783     reg_map = NEW_C_HEAP_OBJ(RegisterMap, mtInternal);
784     ::new (reg_map) RegisterMap(reinterpret_cast<JavaThread*>(thread),
785                                 RegisterMap::UpdateMap::skip,
786                                 RegisterMap::ProcessFrames::include,
787                                 RegisterMap::WalkContinuation::skip);
788   } else {
789     *reg_map = RegisterMap(reinterpret_cast<JavaThread*>(thread),
790                            RegisterMap::UpdateMap::skip,
791                            RegisterMap::ProcessFrames::include,
792                            RegisterMap::WalkContinuation::skip);
793   }
794 
795   {
796     CodeBlob *cb = CodeCache::find_blob((address)pc);
797     if (cb && cb->frame_size())
798       fp = sp + wordSize * (cb->frame_size() - 2);
799   }
800   internal_pf(sp, fp, pc, bcx);
801 }
802 
803 // support for printing out where we are in a Java method
804 // needs to be passed current fp and bcp register values
805 // prints method name, bc index and bytecode name
806 extern "C" void pm(uintptr_t fp, uintptr_t bcx) {
807   DESCRIBE_FP_OFFSET(interpreter_frame_method);
808   uintptr_t *p = (uintptr_t *)fp;
809   Method* m = (Method*)p[frame::interpreter_frame_method_offset];
810   printbc(m, bcx);
811 }
812 
813 #ifndef PRODUCT
814 // This is a generic constructor which is only used by pns() in debug.cpp.
815 frame::frame(void* sp, void* fp, void* pc) {
816   init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
817 }
818 
819 #endif
820 
821 void JavaFrameAnchor::make_walkable() {
822   // last frame set?
823   if (last_Java_sp() == nullptr) return;
824   // already walkable?
825   if (walkable()) return;
826   vmassert(last_Java_sp() != nullptr, "not called from Java code?");
827   vmassert(last_Java_pc() == nullptr, "already walkable");
828   _last_Java_pc = (address)_last_Java_sp[-1];
829   vmassert(walkable(), "something went wrong");
830 }