1 /*
  2  * Copyright (c) 1997, 2022, 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   address   sp = (address)_sp;
 60   address   fp = (address)_fp;
 61   address   unextended_sp = (address)_unextended_sp;
 62 
 63   // consider stack guards when trying to determine "safe" stack pointers
 64   // sp must be within the usable part of the stack (not in guards)
 65   if (!thread->is_in_usable_stack(sp)) {
 66     return false;
 67   }
 68 
 69   // When we are running interpreted code the machine stack pointer, SP, is
 70   // set low enough so that the Java expression stack can grow and shrink
 71   // without ever exceeding the machine stack bounds.  So, ESP >= SP.
 72 
 73   // When we call out of an interpreted method, SP is incremented so that
 74   // the space between SP and ESP is removed.  The SP saved in the callee's
 75   // frame is the SP *before* this increment.  So, when we walk a stack of
 76   // interpreter frames the sender's SP saved in a frame might be less than
 77   // the SP at the point of call.
 78 
 79   // So unextended sp must be within the stack but we need not to check
 80   // that unextended sp >= sp
 81   if (!thread->is_in_full_stack_checked(unextended_sp)) {
 82     return false;
 83   }
 84 
 85   // an fp must be within the stack and above (but not equal) sp
 86   // second evaluation on fp+ is added to handle situation where fp is -1
 87   bool fp_safe = thread->is_in_stack_range_excl(fp, sp) &&
 88                  thread->is_in_full_stack_checked(fp + (return_addr_offset * sizeof(void*)));
 89 
 90   // We know sp/unextended_sp are safe only fp is questionable here
 91 
 92   // If the current frame is known to the code cache then we can attempt to
 93   // to construct the sender and do some validation of it. This goes a long way
 94   // toward eliminating issues when we get in frame construction code
 95 
 96   if (_cb != NULL ) {
 97 
 98     // First check if frame is complete and tester is reliable
 99     // Unfortunately we can only check frame complete for runtime stubs and nmethod
100     // other generic buffer blobs are more problematic so we just assume they are
101     // ok. adapter blobs never have a frame complete and are never ok.
102 
103     if (!_cb->is_frame_complete_at(_pc)) {
104       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
105         return false;
106       }
107     }
108 
109     // Could just be some random pointer within the codeBlob
110     if (!_cb->code_contains(_pc)) {
111       return false;
112     }
113 
114     // Entry frame checks
115     if (is_entry_frame()) {
116       // an entry frame must have a valid fp.
117       return fp_safe && is_entry_frame_valid(thread);
118     } else if (is_optimized_entry_frame()) {
119       return fp_safe;
120     }
121 
122     intptr_t* sender_sp = NULL;
123     intptr_t* sender_unextended_sp = NULL;
124     address   sender_pc = NULL;
125     intptr_t* saved_fp =  NULL;
126 
127     if (is_interpreted_frame()) {
128       // fp must be safe
129       if (!fp_safe) {
130         return false;
131       }
132 
133       // for interpreted frames, the value below is the sender "raw" sp,
134       // which can be different from the sender unextended sp (the sp seen
135       // by the sender) because of current frame local variables
136       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
137       sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
138       saved_fp = (intptr_t*) this->fp()[link_offset];
139       sender_pc = pauth_strip_verifiable((address) this->fp()[return_addr_offset], (address)saved_fp);
140 
141     } else {
142       // must be some sort of compiled/runtime frame
143       // fp does not have to be safe (although it could be check for c1?)
144 
145       // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
146       if (_cb->frame_size() <= 0) {
147         return false;
148       }
149 
150       sender_sp = _unextended_sp + _cb->frame_size();
151       // Is sender_sp safe?
152       if (!thread->is_in_full_stack_checked((address)sender_sp)) {
153         return false;
154       }
155       sender_unextended_sp = sender_sp;
156       // Note: frame::sender_sp_offset is only valid for compiled frame
157       saved_fp = (intptr_t*) *(sender_sp - frame::sender_sp_offset);
158       sender_pc = pauth_strip_verifiable((address) *(sender_sp-1), (address)saved_fp);
159     }
160 
161 
162     // If the potential sender is the interpreter then we can do some more checking
163     if (Interpreter::contains(sender_pc)) {
164 
165       // fp is always saved in a recognizable place in any code we generate. However
166       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved fp
167       // is really a frame pointer.
168 
169       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
170         return false;
171       }
172 
173       // construct the potential sender
174 
175       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
176 
177       return sender.is_interpreted_frame_valid(thread);
178 
179     }
180 
181     // We must always be able to find a recognizable pc
182     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
183     if (sender_pc == NULL ||  sender_blob == NULL) {
184       return false;
185     }
186 
187     // Could be a zombie method
188     if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
189       return false;
190     }
191 
192     // Could just be some random pointer within the codeBlob
193     if (!sender_blob->code_contains(sender_pc)) {
194       return false;
195     }
196 
197     // We should never be able to see an adapter if the current frame is something from code cache
198     if (sender_blob->is_adapter_blob()) {
199       return false;
200     }
201 
202     // Could be the call_stub
203     if (StubRoutines::returns_to_call_stub(sender_pc)) {
204       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
205         return false;
206       }
207 
208       // construct the potential sender
209 
210       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
211 
212       // Validate the JavaCallWrapper an entry frame must have
213       address jcw = (address)sender.entry_frame_call_wrapper();
214 
215       return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
216     } else if (sender_blob->is_optimized_entry_blob()) {
217       return false;
218     }
219 
220     CompiledMethod* nm = sender_blob->as_compiled_method_or_null();
221     if (nm != NULL) {
222       if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
223           nm->method()->is_method_handle_intrinsic()) {
224         return false;
225       }
226     }
227 
228     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
229     // because the return address counts against the callee's frame.
230 
231     if (sender_blob->frame_size() <= 0) {
232       assert(!sender_blob->is_compiled(), "should count return address at least");
233       return false;
234     }
235 
236     // We should never be able to see anything here except an nmethod. If something in the
237     // code cache (current frame) is called by an entity within the code cache that entity
238     // should not be anything but the call stub (already covered), the interpreter (already covered)
239     // or an nmethod.
240 
241     if (!sender_blob->is_compiled()) {
242         return false;
243     }
244 
245     // Could put some more validation for the potential non-interpreted sender
246     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
247 
248     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
249 
250     // We've validated the potential sender that would be created
251     return true;
252   }
253 
254   // Must be native-compiled frame. Since sender will try and use fp to find
255   // linkages it must be safe
256 
257   if (!fp_safe) {
258     return false;
259   }
260 
261   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
262 
263   if ( (address) this->fp()[return_addr_offset] == NULL) return false;
264 
265 
266   // could try and do some more potential verification of native frame if we could think of some...
267 
268   return true;
269 
270 }
271 
272 void frame::patch_pc(Thread* thread, address pc) {
273   assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
274   address* pc_addr = &(((address*) sp())[-1]);
275   address signing_sp = (((address*) sp())[-2]);
276   address signed_pc = pauth_sign_return_address(pc, (address)signing_sp);
277   address pc_old = pauth_strip_verifiable(*pc_addr, (address)signing_sp);
278   if (TracePcPatching) {
279     tty->print("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
280                   p2i(pc_addr), p2i(pc_old), p2i(pc));
281     if (VM_Version::use_rop_protection()) {
282       tty->print(" [signed " INTPTR_FORMAT " -> " INTPTR_FORMAT "]", p2i(*pc_addr), p2i(signed_pc));
283     }
284     tty->print_cr("");
285   }
286 
287   // Either the return address is the original one or we are going to
288   // patch in the same address that's already there.
289   assert(_pc == pc_old || pc == pc_old, "must be");
290   *pc_addr = signed_pc;
291   address original_pc = CompiledMethod::get_deopt_original_pc(this);
292   if (original_pc != NULL) {
293     assert(original_pc == _pc, "expected original PC to be stored before patching");
294     _deopt_state = is_deoptimized;
295     // leave _pc as is
296   } else {
297     _deopt_state = not_deoptimized;
298     _pc = pc;
299   }
300 }
301 
302 bool frame::is_interpreted_frame() const  {
303   return Interpreter::contains(pc());
304 }
305 
306 int frame::frame_size(RegisterMap* map) const {
307   frame sender = this->sender(map);
308   return sender.sp() - sp();
309 }
310 
311 intptr_t* frame::entry_frame_argument_at(int offset) const {
312   // convert offset to index to deal with tsi
313   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
314   // Entry frame's arguments are always in relation to unextended_sp()
315   return &unextended_sp()[index];
316 }
317 
318 // sender_sp
319 intptr_t* frame::interpreter_frame_sender_sp() const {
320   assert(is_interpreted_frame(), "interpreted frame expected");
321   return (intptr_t*) at(interpreter_frame_sender_sp_offset);
322 }
323 
324 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
325   assert(is_interpreted_frame(), "interpreted frame expected");
326   ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
327 }
328 
329 
330 // monitor elements
331 
332 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
333   return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
334 }
335 
336 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
337   BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
338   // make sure the pointer points inside the frame
339   assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
340   assert((intptr_t*) result < fp(),  "monitor end should be strictly below the frame pointer");
341   return result;
342 }
343 
344 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
345   *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
346 }
347 
348 // Used by template based interpreter deoptimization
349 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
350     *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
351 }
352 
353 frame frame::sender_for_entry_frame(RegisterMap* map) const {
354   assert(map != NULL, "map must be set");
355   // Java frame called from C; skip all C frames and return top C
356   // frame of that chunk as the sender
357   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
358   assert(!entry_frame_is_first(), "next Java fp must be non zero");
359   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
360   // Since we are walking the stack now this nested anchor is obviously walkable
361   // even if it wasn't when it was stacked.
362   jfa->make_walkable();
363   map->clear();
364   assert(map->include_argument_oops(), "should be set by clear");
365   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
366   fr.set_sp_is_trusted();
367 
368   return fr;
369 }
370 
371 OptimizedEntryBlob::FrameData* OptimizedEntryBlob::frame_data_for_frame(const frame& frame) const {
372   assert(frame.is_optimized_entry_frame(), "wrong frame");
373   // need unextended_sp here, since normal sp is wrong for interpreter callees
374   return reinterpret_cast<OptimizedEntryBlob::FrameData*>(
375     reinterpret_cast<char*>(frame.unextended_sp()) + in_bytes(_frame_data_offset));
376 }
377 
378 bool frame::optimized_entry_frame_is_first() const {
379   assert(is_optimized_entry_frame(), "must be optimzed entry frame");
380   OptimizedEntryBlob* blob = _cb->as_optimized_entry_blob();
381   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
382   return jfa->last_Java_sp() == NULL;
383 }
384 
385 frame frame::sender_for_optimized_entry_frame(RegisterMap* map) const {
386   assert(map != NULL, "map must be set");
387   OptimizedEntryBlob* blob = _cb->as_optimized_entry_blob();
388   // Java frame called from C; skip all C frames and return top C
389   // frame of that chunk as the sender
390   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
391   assert(!optimized_entry_frame_is_first(), "must have a frame anchor to go back to");
392   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
393   // Since we are walking the stack now this nested anchor is obviously walkable
394   // even if it wasn't when it was stacked.
395   if (!jfa->walkable()) {
396     // Capture _last_Java_pc (if needed) and mark anchor walkable.
397     jfa->capture_last_Java_pc();
398   }
399   map->clear();
400   assert(map->include_argument_oops(), "should be set by clear");
401   vmassert(jfa->last_Java_pc() != NULL, "not walkable");
402   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
403 
404   return fr;
405 }
406 
407 //------------------------------------------------------------------------------
408 // frame::verify_deopt_original_pc
409 //
410 // Verifies the calculated original PC of a deoptimization PC for the
411 // given unextended SP.
412 #ifdef ASSERT
413 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
414   frame fr;
415 
416   // This is ugly but it's better than to change {get,set}_original_pc
417   // to take an SP value as argument.  And it's only a debugging
418   // method anyway.
419   fr._unextended_sp = unextended_sp;
420 
421   address original_pc = nm->get_original_pc(&fr);
422   assert(nm->insts_contains_inclusive(original_pc),
423          "original PC must be in the main code section of the the compiled method (or must be immediately following it)");
424 }
425 #endif
426 
427 //------------------------------------------------------------------------------
428 // frame::adjust_unextended_sp
429 void frame::adjust_unextended_sp() {
430   // On aarch64, sites calling method handle intrinsics and lambda forms are treated
431   // as any other call site. Therefore, no special action is needed when we are
432   // returning to any of these call sites.
433 
434   if (_cb != NULL) {
435     CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
436     if (sender_cm != NULL) {
437       // If the sender PC is a deoptimization point, get the original PC.
438       if (sender_cm->is_deopt_entry(_pc) ||
439           sender_cm->is_deopt_mh_entry(_pc)) {
440         DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp));
441       }
442     }
443   }
444 }
445 
446 //------------------------------------------------------------------------------
447 // frame::update_map_with_saved_link
448 void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {
449   // The interpreter and compiler(s) always save fp in a known
450   // location on entry. We must record where that location is
451   // so that if fp was live on callout from c2 we can find
452   // the saved copy no matter what it called.
453 
454   // Since the interpreter always saves fp if we record where it is then
455   // we don't have to always save fp on entry and exit to c2 compiled
456   // code, on entry will be enough.
457   map->set_location(rfp->as_VMReg(), (address) link_addr);
458   // this is weird "H" ought to be at a higher address however the
459   // oopMaps seems to have the "H" regs at the same address and the
460   // vanilla register.
461   // XXXX make this go away
462   if (true) {
463     map->set_location(rfp->as_VMReg()->next(), (address) link_addr);
464   }
465 }
466 
467 
468 //------------------------------------------------------------------------------
469 // frame::sender_for_interpreter_frame
470 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
471   // SP is the raw SP from the sender after adapter or interpreter
472   // extension.
473   intptr_t* sender_sp = this->sender_sp();
474 
475   // This is the sp before any possible extension (adapter/locals).
476   intptr_t* unextended_sp = interpreter_frame_sender_sp();
477 
478 #if COMPILER2_OR_JVMCI
479   if (map->update_map()) {
480     update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
481   }
482 #endif // COMPILER2_OR_JVMCI
483 
484   // For ROP protection, Interpreter will have signed the sender_pc, but there is no requirement to authenticate it here.
485   address sender_pc = pauth_strip_verifiable(sender_pc_maybe_signed(), (address)link());
486 
487   return frame(sender_sp, unextended_sp, link(), sender_pc);
488 }
489 
490 //------------------------------------------------------------------------------
491 // frame::sender_for_compiled_frame
492 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
493   // When the sp of a compiled frame is correct, we can get the correct sender sp
494   // by unextended sp + frame size.
495   // For the following two scenarios, the sp of a compiled frame is correct:
496   //  a) This compiled frame is built from the anchor.
497   //  b) This compiled frame is built from a callee frame, and the callee frame can
498   //    calculate its sp correctly.
499   //
500   // For b), if the callee frame is a native code frame (such as leaf call), the sp of
501   // the compiled frame cannot be calculated correctly. There is currently no suitable
502   // solution to solve this problem perfectly. But when PreserveFramePointer is enabled,
503   // we can get the correct sender sp by fp + 2 (that is sender_sp()).
504 
505   assert(_cb->frame_size() >= 0, "must have non-zero frame size");
506   intptr_t* l_sender_sp = (!PreserveFramePointer || _sp_is_trusted) ? unextended_sp() + _cb->frame_size()
507                                                                     : sender_sp();
508   intptr_t* unextended_sp = l_sender_sp;
509 
510   // the return_address is always the word on the stack
511 
512   // For ROP protection, C1/C2 will have signed the sender_pc, but there is no requirement to authenticate it here.
513   address sender_pc = pauth_strip_verifiable((address) *(l_sender_sp-1), (address) *(l_sender_sp-2));
514 
515   intptr_t** saved_fp_addr = (intptr_t**) (l_sender_sp - frame::sender_sp_offset);
516 
517   // assert (sender_sp() == l_sender_sp, "should be");
518   // assert (*saved_fp_addr == link(), "should be");
519 
520   if (map->update_map()) {
521     // Tell GC to use argument oopmaps for some runtime stubs that need it.
522     // For C1, the runtime stub might not have oop maps, so set this flag
523     // outside of update_register_map.
524     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
525     if (_cb->oop_maps() != NULL) {
526       OopMapSet::update_register_map(this, map);
527     }
528 
529     // Since the prolog does the save and restore of FP there is no
530     // oopmap for it so we must fill in its location as if there was
531     // an oopmap entry since if our caller was compiled code there
532     // could be live jvm state in it.
533     update_map_with_saved_link(map, saved_fp_addr);
534   }
535 
536   return frame(l_sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
537 }
538 
539 //------------------------------------------------------------------------------
540 // frame::sender_raw
541 frame frame::sender_raw(RegisterMap* map) const {
542   // Default is we done have to follow them. The sender_for_xxx will
543   // update it accordingly
544    map->set_include_argument_oops(false);
545 
546   if (is_entry_frame())
547     return sender_for_entry_frame(map);
548   if (is_optimized_entry_frame())
549     return sender_for_optimized_entry_frame(map);
550   if (is_interpreted_frame())
551     return sender_for_interpreter_frame(map);
552   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
553 
554   // This test looks odd: why is it not is_compiled_frame() ?  That's
555   // because stubs also have OOP maps.
556   if (_cb != NULL) {
557     return sender_for_compiled_frame(map);
558   }
559 
560   // Must be native-compiled frame, i.e. the marshaling code for native
561   // methods that exists in the core system.
562 
563   // Native code may or may not have signed the return address, we have no way to be sure or what
564   // signing methods they used. Instead, just ensure the stripped value is used.
565 
566   return frame(sender_sp(), link(), sender_pc());
567 }
568 
569 frame frame::sender(RegisterMap* map) const {
570   frame result = sender_raw(map);
571 
572   if (map->process_frames()) {
573     StackWatermarkSet::on_iteration(map->thread(), result);
574   }
575 
576   return result;
577 }
578 
579 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
580   assert(is_interpreted_frame(), "Not an interpreted frame");
581   // These are reasonable sanity checks
582   if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
583     return false;
584   }
585   if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
586     return false;
587   }
588   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
589     return false;
590   }
591   // These are hacks to keep us out of trouble.
592   // The problem with these is that they mask other problems
593   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
594     return false;
595   }
596 
597   // do some validation of frame elements
598 
599   // first the method
600 
601   Method* m = *interpreter_frame_method_addr();
602 
603   // validate the method we'd find in this potential sender
604   if (!Method::is_valid_method(m)) return false;
605 
606   // stack frames shouldn't be much larger than max_stack elements
607   // this test requires the use of unextended_sp which is the sp as seen by
608   // the current frame, and not sp which is the "raw" pc which could point
609   // further because of local variables of the callee method inserted after
610   // method arguments
611   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
612     return false;
613   }
614 
615   // validate bci/bcx
616 
617   address  bcp    = interpreter_frame_bcp();
618   if (m->validate_bci_from_bcp(bcp) < 0) {
619     return false;
620   }
621 
622   // validate constantPoolCache*
623   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
624   if (MetaspaceObj::is_valid(cp) == false) return false;
625 
626   // validate locals
627 
628   address locals =  (address) *interpreter_frame_locals_addr();
629   return thread->is_in_stack_range_incl(locals, (address)fp());
630 }
631 
632 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
633   assert(is_interpreted_frame(), "interpreted frame expected");
634   Method* method = interpreter_frame_method();
635   BasicType type = method->result_type();
636 
637   intptr_t* tos_addr;
638   if (method->is_native()) {
639     // TODO : ensure AARCH64 does the same as Intel here i.e. push v0 then r0
640     // Prior to calling into the runtime to report the method_exit the possible
641     // return value is pushed to the native stack. If the result is a jfloat/jdouble
642     // then ST0 is saved before EAX/EDX. See the note in generate_native_result
643     tos_addr = (intptr_t*)sp();
644     if (type == T_FLOAT || type == T_DOUBLE) {
645       // This is times two because we do a push(ltos) after pushing XMM0
646       // and that takes two interpreter stack slots.
647       tos_addr += 2 * Interpreter::stackElementWords;
648     }
649   } else {
650     tos_addr = (intptr_t*)interpreter_frame_tos_address();
651   }
652 
653   switch (type) {
654     case T_OBJECT  :
655     case T_ARRAY   : {
656       oop obj;
657       if (method->is_native()) {
658         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
659       } else {
660         oop* obj_p = (oop*)tos_addr;
661         obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
662       }
663       assert(Universe::is_in_heap_or_null(obj), "sanity check");
664       *oop_result = obj;
665       break;
666     }
667     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
668     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
669     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
670     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
671     case T_INT     : value_result->i = *(jint*)tos_addr; break;
672     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
673     case T_FLOAT   : {
674         value_result->f = *(jfloat*)tos_addr;
675       break;
676     }
677     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
678     case T_VOID    : /* Nothing to do */ break;
679     default        : ShouldNotReachHere();
680   }
681 
682   return type;
683 }
684 
685 
686 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
687   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
688   return &interpreter_frame_tos_address()[index];
689 }
690 
691 #ifndef PRODUCT
692 
693 #define DESCRIBE_FP_OFFSET(name) \
694   values.describe(frame_no, fp() + frame::name##_offset, #name)
695 
696 void frame::describe_pd(FrameValues& values, int frame_no) {
697   if (is_interpreted_frame()) {
698     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
699     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
700     DESCRIBE_FP_OFFSET(interpreter_frame_method);
701     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
702     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
703     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
704     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
705     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
706     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
707   }
708 }
709 #endif
710 
711 intptr_t *frame::initial_deoptimization_info() {
712   // Not used on aarch64, but we must return something.
713   return NULL;
714 }
715 
716 intptr_t* frame::real_fp() const {
717   if (_cb != NULL) {
718     // use the frame size if valid
719     int size = _cb->frame_size();
720     if (size > 0) {
721       return unextended_sp() + size;
722     }
723   }
724   // else rely on fp()
725   assert(! is_compiled_frame(), "unknown compiled frame size");
726   return fp();
727 }
728 
729 #undef DESCRIBE_FP_OFFSET
730 
731 #define DESCRIBE_FP_OFFSET(name)                     \
732   {                                                  \
733     uintptr_t *p = (uintptr_t *)fp;                  \
734     printf(INTPTR_FORMAT " " INTPTR_FORMAT " %s\n",  \
735            (uintptr_t)(p + frame::name##_offset),    \
736            p[frame::name##_offset], #name);          \
737   }
738 
739 static THREAD_LOCAL uintptr_t nextfp;
740 static THREAD_LOCAL uintptr_t nextpc;
741 static THREAD_LOCAL uintptr_t nextsp;
742 static THREAD_LOCAL RegisterMap *reg_map;
743 
744 static void printbc(Method *m, intptr_t bcx) {
745   const char *name;
746   char buf[16];
747   if (m->validate_bci_from_bcp((address)bcx) < 0
748       || !m->contains((address)bcx)) {
749     name = "???";
750     snprintf(buf, sizeof buf, "(bad)");
751   } else {
752     int bci = m->bci_from((address)bcx);
753     snprintf(buf, sizeof buf, "%d", bci);
754     name = Bytecodes::name(m->code_at(bci));
755   }
756   ResourceMark rm;
757   printf("%s : %s ==> %s\n", m->name_and_sig_as_C_string(), buf, name);
758 }
759 
760 void internal_pf(uintptr_t sp, uintptr_t fp, uintptr_t pc, uintptr_t bcx) {
761   if (! fp)
762     return;
763 
764   DESCRIBE_FP_OFFSET(return_addr);
765   DESCRIBE_FP_OFFSET(link);
766   DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
767   DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
768   DESCRIBE_FP_OFFSET(interpreter_frame_method);
769   DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
770   DESCRIBE_FP_OFFSET(interpreter_frame_cache);
771   DESCRIBE_FP_OFFSET(interpreter_frame_locals);
772   DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
773   DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
774   uintptr_t *p = (uintptr_t *)fp;
775 
776   // We want to see all frames, native and Java.  For compiled and
777   // interpreted frames we have special information that allows us to
778   // unwind them; for everything else we assume that the native frame
779   // pointer chain is intact.
780   frame this_frame((intptr_t*)sp, (intptr_t*)fp, (address)pc);
781   if (this_frame.is_compiled_frame() ||
782       this_frame.is_interpreted_frame()) {
783     frame sender = this_frame.sender(reg_map);
784     nextfp = (uintptr_t)sender.fp();
785     nextpc = (uintptr_t)sender.pc();
786     nextsp = (uintptr_t)sender.unextended_sp();
787   } else {
788     nextfp = p[frame::link_offset];
789     nextpc = p[frame::return_addr_offset];
790     nextsp = (uintptr_t)&p[frame::sender_sp_offset];
791   }
792 
793   if (bcx == -1ULL)
794     bcx = p[frame::interpreter_frame_bcp_offset];
795 
796   if (Interpreter::contains((address)pc)) {
797     Method* m = (Method*)p[frame::interpreter_frame_method_offset];
798     if(m && m->is_method()) {
799       printbc(m, bcx);
800     } else
801       printf("not a Method\n");
802   } else {
803     CodeBlob *cb = CodeCache::find_blob((address)pc);
804     if (cb != NULL) {
805       if (cb->is_nmethod()) {
806         ResourceMark rm;
807         nmethod* nm = (nmethod*)cb;
808         printf("nmethod %s\n", nm->method()->name_and_sig_as_C_string());
809       } else if (cb->name()) {
810         printf("CodeBlob %s\n", cb->name());
811       }
812     }
813   }
814 }
815 
816 extern "C" void npf() {
817   CodeBlob *cb = CodeCache::find_blob((address)nextpc);
818   // C2 does not always chain the frame pointers when it can, instead
819   // preferring to use fixed offsets from SP, so a simple leave() does
820   // not work.  Instead, it adds the frame size to SP then pops FP and
821   // LR.  We have to do the same thing to get a good call chain.
822   if (cb && cb->frame_size())
823     nextfp = nextsp + wordSize * (cb->frame_size() - 2);
824   internal_pf (nextsp, nextfp, nextpc, -1);
825 }
826 
827 extern "C" void pf(uintptr_t sp, uintptr_t fp, uintptr_t pc,
828                    uintptr_t bcx, uintptr_t thread) {
829   if (!reg_map) {
830     reg_map = NEW_C_HEAP_OBJ(RegisterMap, mtInternal);
831     ::new (reg_map) RegisterMap((JavaThread*)thread, false);
832   } else {
833     *reg_map = RegisterMap((JavaThread*)thread, false);
834   }
835 
836   {
837     CodeBlob *cb = CodeCache::find_blob((address)pc);
838     if (cb && cb->frame_size())
839       fp = sp + wordSize * (cb->frame_size() - 2);
840   }
841   internal_pf(sp, fp, pc, bcx);
842 }
843 
844 // support for printing out where we are in a Java method
845 // needs to be passed current fp and bcp register values
846 // prints method name, bc index and bytecode name
847 extern "C" void pm(uintptr_t fp, uintptr_t bcx) {
848   DESCRIBE_FP_OFFSET(interpreter_frame_method);
849   uintptr_t *p = (uintptr_t *)fp;
850   Method* m = (Method*)p[frame::interpreter_frame_method_offset];
851   printbc(m, bcx);
852 }
853 
854 #ifndef PRODUCT
855 // This is a generic constructor which is only used by pns() in debug.cpp.
856 frame::frame(void* sp, void* fp, void* pc) {
857   init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
858 }
859 
860 #endif
861 
862 void JavaFrameAnchor::make_walkable() {
863   // last frame set?
864   if (last_Java_sp() == NULL) return;
865   // already walkable?
866   if (walkable()) return;
867   vmassert(last_Java_sp() != NULL, "not called from Java code?");
868   vmassert(last_Java_pc() == NULL, "already walkable");
869   _last_Java_pc = (address)_last_Java_sp[-1];
870   vmassert(walkable(), "something went wrong");
871 }
872