1 /*
  2  * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
  4  * Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #include "precompiled.hpp"
 28 #include "compiler/oopMap.hpp"
 29 #include "interpreter/interpreter.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "memory/universe.hpp"
 32 #include "oops/markWord.hpp"
 33 #include "oops/method.hpp"
 34 #include "oops/oop.inline.hpp"
 35 #include "prims/methodHandles.hpp"
 36 #include "runtime/frame.inline.hpp"
 37 #include "runtime/handles.inline.hpp"
 38 #include "runtime/javaCalls.hpp"
 39 #include "runtime/monitorChunk.hpp"
 40 #include "runtime/os.inline.hpp"
 41 #include "runtime/signature.hpp"
 42 #include "runtime/stackWatermarkSet.hpp"
 43 #include "runtime/stubCodeGenerator.hpp"
 44 #include "runtime/stubRoutines.hpp"
 45 #include "vmreg_riscv.inline.hpp"
 46 #ifdef COMPILER1
 47 #include "c1/c1_Runtime1.hpp"
 48 #include "runtime/vframeArray.hpp"
 49 #endif
 50 
 51 #ifdef ASSERT
 52 void RegisterMap::check_location_valid() {
 53 }
 54 #endif
 55 
 56 
 57 // Profiling/safepoint support
 58 
 59 bool frame::safe_for_sender(JavaThread *thread) {
 60   address   addr_sp = (address)_sp;
 61   address   addr_fp = (address)_fp;
 62   address   unextended_sp = (address)_unextended_sp;
 63 
 64   // consider stack guards when trying to determine "safe" stack pointers
 65   // sp must be within the usable part of the stack (not in guards)
 66   if (!thread->is_in_usable_stack(addr_sp)) {
 67     return false;
 68   }
 69 
 70   // When we are running interpreted code the machine stack pointer, SP, is
 71   // set low enough so that the Java expression stack can grow and shrink
 72   // without ever exceeding the machine stack bounds.  So, ESP >= SP.
 73 
 74   // When we call out of an interpreted method, SP is incremented so that
 75   // the space between SP and ESP is removed.  The SP saved in the callee's
 76   // frame is the SP *before* this increment.  So, when we walk a stack of
 77   // interpreter frames the sender's SP saved in a frame might be less than
 78   // the SP at the point of call.
 79 
 80   // So unextended sp must be within the stack but we need not to check
 81   // that unextended sp >= sp
 82 
 83   if (!thread->is_in_full_stack_checked(unextended_sp)) {
 84     return false;
 85   }
 86 
 87   // an fp must be within the stack and above (but not equal) sp
 88   // second evaluation on fp+ is added to handle situation where fp is -1
 89   bool fp_safe = thread->is_in_stack_range_excl(addr_fp, addr_sp) &&
 90                  thread->is_in_full_stack_checked(addr_fp + (return_addr_offset * sizeof(void*)));
 91 
 92   // We know sp/unextended_sp are safe only fp is questionable here
 93 
 94   // If the current frame is known to the code cache then we can attempt to
 95   // to construct the sender and do some validation of it. This goes a long way
 96   // toward eliminating issues when we get in frame construction code
 97 
 98   if (_cb != NULL) {
 99 
100     // First check if frame is complete and tester is reliable
101     // Unfortunately we can only check frame complete for runtime stubs and nmethod
102     // other generic buffer blobs are more problematic so we just assume they are
103     // ok. adapter blobs never have a frame complete and are never ok.
104 
105     if (!_cb->is_frame_complete_at(_pc)) {
106       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
107         return false;
108       }
109     }
110 
111     // Could just be some random pointer within the codeBlob
112     if (!_cb->code_contains(_pc)) {
113       return false;
114     }
115 
116     // Entry frame checks
117     if (is_entry_frame()) {
118       // an entry frame must have a valid fp.
119       return fp_safe && is_entry_frame_valid(thread);
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       sender_pc = (address)this->fp()[return_addr_offset];
134       // for interpreted frames, the value below is the sender "raw" sp,
135       // which can be different from the sender unextended sp (the sp seen
136       // by the sender) because of current frame local variables
137       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
138       sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
139       saved_fp = (intptr_t*) this->fp()[link_offset];
140     } else {
141       // must be some sort of compiled/runtime frame
142       // fp does not have to be safe (although it could be check for c1?)
143 
144       // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
145       if (_cb->frame_size() <= 0) {
146         return false;
147       }
148 
149       sender_sp = _unextended_sp + _cb->frame_size();
150       // Is sender_sp safe?
151       if (!thread->is_in_full_stack_checked((address)sender_sp)) {
152         return false;
153       }
154 
155       sender_unextended_sp = sender_sp;
156       sender_pc = (address) *(sender_sp - 1);
157       saved_fp = (intptr_t*) *(sender_sp - 2);
158     }
159 
160 
161     // If the potential sender is the interpreter then we can do some more checking
162     if (Interpreter::contains(sender_pc)) {
163 
164       // fp is always saved in a recognizable place in any code we generate. However
165       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved fp
166       // is really a frame pointer.
167       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
168         return false;
169       }
170 
171       // construct the potential sender
172       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
173 
174       return sender.is_interpreted_frame_valid(thread);
175     }
176 
177     // We must always be able to find a recognizable pc
178     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
179     if (sender_pc == NULL || sender_blob == NULL) {
180       return false;
181     }
182 
183     // Could be a zombie method
184     if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
185       return false;
186     }
187 
188     // Could just be some random pointer within the codeBlob
189     if (!sender_blob->code_contains(sender_pc)) {
190       return false;
191     }
192 
193     // We should never be able to see an adapter if the current frame is something from code cache
194     if (sender_blob->is_adapter_blob()) {
195       return false;
196     }
197 
198     // Could be the call_stub
199     if (StubRoutines::returns_to_call_stub(sender_pc)) {
200       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
201         return false;
202       }
203 
204       // construct the potential sender
205       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
206 
207       // Validate the JavaCallWrapper an entry frame must have
208       address jcw = (address)sender.entry_frame_call_wrapper();
209 
210       bool jcw_safe = (jcw < thread->stack_base()) && (jcw > (address)sender.fp());
211 
212       return jcw_safe;
213     }
214 
215     CompiledMethod* nm = sender_blob->as_compiled_method_or_null();
216     if (nm != NULL) {
217       if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
218           nm->method()->is_method_handle_intrinsic()) {
219         return false;
220       }
221     }
222 
223     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
224     // because the return address counts against the callee's frame.
225     if (sender_blob->frame_size() <= 0) {
226       assert(!sender_blob->is_compiled(), "should count return address at least");
227       return false;
228     }
229 
230     // We should never be able to see anything here except an nmethod. If something in the
231     // code cache (current frame) is called by an entity within the code cache that entity
232     // should not be anything but the call stub (already covered), the interpreter (already covered)
233     // or an nmethod.
234     if (!sender_blob->is_compiled()) {
235         return false;
236     }
237 
238     // Could put some more validation for the potential non-interpreted sender
239     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
240 
241     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
242 
243     // We've validated the potential sender that would be created
244     return true;
245   }
246 
247   // Must be native-compiled frame. Since sender will try and use fp to find
248   // linkages it must be safe
249   if (!fp_safe) {
250     return false;
251   }
252 
253   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
254   if ((address)this->fp()[return_addr_offset] == NULL) { return false; }
255 
256   return true;
257 }
258 
259 void frame::patch_pc(Thread* thread, address pc) {
260   assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
261   address* pc_addr = &(((address*) sp())[-1]);
262   if (TracePcPatching) {
263     tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
264                   p2i(pc_addr), p2i(*pc_addr), p2i(pc));
265   }
266   // Either the return address is the original one or we are going to
267   // patch in the same address that's already there.
268   assert(_pc == *pc_addr || pc == *pc_addr, "must be");
269   *pc_addr = pc;
270   address original_pc = CompiledMethod::get_deopt_original_pc(this);
271   if (original_pc != NULL) {
272     assert(original_pc == _pc, "expected original PC to be stored before patching");
273     _deopt_state = is_deoptimized;
274     // leave _pc as is
275   } else {
276     _deopt_state = not_deoptimized;
277     _pc = pc;
278   }
279 }
280 
281 bool frame::is_interpreted_frame() const  {
282   return Interpreter::contains(pc());
283 }
284 
285 int frame::frame_size(RegisterMap* map) const {
286   frame sender = this->sender(map);
287   return sender.sp() - sp();
288 }
289 
290 intptr_t* frame::entry_frame_argument_at(int offset) const {
291   // convert offset to index to deal with tsi
292   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
293   // Entry frame's arguments are always in relation to unextended_sp()
294   return &unextended_sp()[index];
295 }
296 
297 // sender_sp
298 intptr_t* frame::interpreter_frame_sender_sp() const {
299   assert(is_interpreted_frame(), "interpreted frame expected");
300   return (intptr_t*) at(interpreter_frame_sender_sp_offset);
301 }
302 
303 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
304   assert(is_interpreted_frame(), "interpreted frame expected");
305   ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
306 }
307 
308 
309 // monitor elements
310 
311 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
312   return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
313 }
314 
315 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
316   BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
317   // make sure the pointer points inside the frame
318   assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
319   assert((intptr_t*) result < fp(),  "monitor end should be strictly below the frame pointer");
320   return result;
321 }
322 
323 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
324   *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
325 }
326 
327 // Used by template based interpreter deoptimization
328 void frame::interpreter_frame_set_last_sp(intptr_t* last_sp) {
329   *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = last_sp;
330 }
331 
332 frame frame::sender_for_entry_frame(RegisterMap* map) const {
333   assert(map != NULL, "map must be set");
334   // Java frame called from C; skip all C frames and return top C
335   // frame of that chunk as the sender
336   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
337   assert(!entry_frame_is_first(), "next Java fp must be non zero");
338   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
339   // Since we are walking the stack now this nested anchor is obviously walkable
340   // even if it wasn't when it was stacked.
341   jfa->make_walkable();
342   map->clear();
343   assert(map->include_argument_oops(), "should be set by clear");
344   vmassert(jfa->last_Java_pc() != NULL, "not walkable");
345   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
346   return fr;
347 }
348 
349 OptimizedEntryBlob::FrameData* OptimizedEntryBlob::frame_data_for_frame(const frame& frame) const {
350   ShouldNotCallThis();
351   return nullptr;
352 }
353 
354 bool frame::optimized_entry_frame_is_first() const {
355   ShouldNotCallThis();
356   return false;
357 }
358 
359 frame frame::sender_for_optimized_entry_frame(RegisterMap* map) const {
360   ShouldNotCallThis();
361   return {};
362 }
363 
364 //------------------------------------------------------------------------------
365 // frame::verify_deopt_original_pc
366 //
367 // Verifies the calculated original PC of a deoptimization PC for the
368 // given unextended SP.
369 #ifdef ASSERT
370 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
371   frame fr;
372 
373   // This is ugly but it's better than to change {get,set}_original_pc
374   // to take an SP value as argument.  And it's only a debugging
375   // method anyway.
376   fr._unextended_sp = unextended_sp;
377 
378   assert_cond(nm != NULL);
379   address original_pc = nm->get_original_pc(&fr);
380   assert(nm->insts_contains_inclusive(original_pc),
381          "original PC must be in the main code section of the the compiled method (or must be immediately following it)");
382 }
383 #endif
384 
385 //------------------------------------------------------------------------------
386 // frame::adjust_unextended_sp
387 void frame::adjust_unextended_sp() {
388   // On riscv, sites calling method handle intrinsics and lambda forms are treated
389   // as any other call site. Therefore, no special action is needed when we are
390   // returning to any of these call sites.
391 
392   if (_cb != NULL) {
393     CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
394     if (sender_cm != NULL) {
395       // If the sender PC is a deoptimization point, get the original PC.
396       if (sender_cm->is_deopt_entry(_pc) ||
397           sender_cm->is_deopt_mh_entry(_pc)) {
398         DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp));
399       }
400     }
401   }
402 }
403 
404 //------------------------------------------------------------------------------
405 // frame::update_map_with_saved_link
406 void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {
407   // The interpreter and compiler(s) always save fp in a known
408   // location on entry. We must record where that location is
409   // so that if fp was live on callout from c2 we can find
410   // the saved copy no matter what it called.
411 
412   // Since the interpreter always saves fp if we record where it is then
413   // we don't have to always save fp on entry and exit to c2 compiled
414   // code, on entry will be enough.
415   assert(map != NULL, "map must be set");
416   map->set_location(::fp->as_VMReg(), (address) link_addr);
417   // this is weird "H" ought to be at a higher address however the
418   // oopMaps seems to have the "H" regs at the same address and the
419   // vanilla register.
420   map->set_location(::fp->as_VMReg()->next(), (address) link_addr);
421 }
422 
423 
424 //------------------------------------------------------------------------------
425 // frame::sender_for_interpreter_frame
426 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
427   // SP is the raw SP from the sender after adapter or interpreter
428   // extension.
429   intptr_t* sender_sp = this->sender_sp();
430 
431   // This is the sp before any possible extension (adapter/locals).
432   intptr_t* unextended_sp = interpreter_frame_sender_sp();
433 
434 #ifdef COMPILER2
435   assert(map != NULL, "map must be set");
436   if (map->update_map()) {
437     update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
438   }
439 #endif // COMPILER2
440 
441   return frame(sender_sp, unextended_sp, link(), sender_pc());
442 }
443 
444 
445 //------------------------------------------------------------------------------
446 // frame::sender_for_compiled_frame
447 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
448   // we cannot rely upon the last fp having been saved to the thread
449   // in C2 code but it will have been pushed onto the stack. so we
450   // have to find it relative to the unextended sp
451 
452   assert(_cb->frame_size() >= 0, "must have non-zero frame size");
453   intptr_t* l_sender_sp = unextended_sp() + _cb->frame_size();
454   intptr_t* unextended_sp = l_sender_sp;
455 
456   // the return_address is always the word on the stack
457   address sender_pc = (address) *(l_sender_sp + frame::return_addr_offset);
458 
459   intptr_t** saved_fp_addr = (intptr_t**) (l_sender_sp + frame::link_offset);
460 
461   assert(map != NULL, "map must be set");
462   if (map->update_map()) {
463     // Tell GC to use argument oopmaps for some runtime stubs that need it.
464     // For C1, the runtime stub might not have oop maps, so set this flag
465     // outside of update_register_map.
466     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
467     if (_cb->oop_maps() != NULL) {
468       OopMapSet::update_register_map(this, map);
469     }
470 
471     // Since the prolog does the save and restore of FP there is no
472     // oopmap for it so we must fill in its location as if there was
473     // an oopmap entry since if our caller was compiled code there
474     // could be live jvm state in it.
475     update_map_with_saved_link(map, saved_fp_addr);
476   }
477 
478   return frame(l_sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
479 }
480 
481 //------------------------------------------------------------------------------
482 // frame::sender_raw
483 frame frame::sender_raw(RegisterMap* map) const {
484   // Default is we done have to follow them. The sender_for_xxx will
485   // update it accordingly
486   assert(map != NULL, "map must be set");
487   map->set_include_argument_oops(false);
488 
489   if (is_entry_frame()) {
490     return sender_for_entry_frame(map);
491   }
492   if (is_interpreted_frame()) {
493     return sender_for_interpreter_frame(map);
494   }
495   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
496 
497   // This test looks odd: why is it not is_compiled_frame() ?  That's
498   // because stubs also have OOP maps.
499   if (_cb != NULL) {
500     return sender_for_compiled_frame(map);
501   }
502 
503   // Must be native-compiled frame, i.e. the marshaling code for native
504   // methods that exists in the core system.
505   return frame(sender_sp(), link(), sender_pc());
506 }
507 
508 frame frame::sender(RegisterMap* map) const {
509   frame result = sender_raw(map);
510 
511   if (map->process_frames()) {
512     StackWatermarkSet::on_iteration(map->thread(), result);
513   }
514 
515   return result;
516 }
517 
518 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
519   assert(is_interpreted_frame(), "Not an interpreted frame");
520   // These are reasonable sanity checks
521   if (fp() == NULL || (intptr_t(fp()) & (wordSize-1)) != 0) {
522     return false;
523   }
524   if (sp() == NULL || (intptr_t(sp()) & (wordSize-1)) != 0) {
525     return false;
526   }
527   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
528     return false;
529   }
530   // These are hacks to keep us out of trouble.
531   // The problem with these is that they mask other problems
532   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
533     return false;
534   }
535 
536   // do some validation of frame elements
537 
538   // first the method
539   Method* m = *interpreter_frame_method_addr();
540   // validate the method we'd find in this potential sender
541   if (!Method::is_valid_method(m)) {
542     return false;
543   }
544 
545   // stack frames shouldn't be much larger than max_stack elements
546   // this test requires the use of unextended_sp which is the sp as seen by
547   // the current frame, and not sp which is the "raw" pc which could point
548   // further because of local variables of the callee method inserted after
549   // method arguments
550   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
551     return false;
552   }
553 
554   // validate bci/bcx
555   address bcp = interpreter_frame_bcp();
556   if (m->validate_bci_from_bcp(bcp) < 0) {
557     return false;
558   }
559 
560   // validate constantPoolCache*
561   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
562   if (MetaspaceObj::is_valid(cp) == false) {
563     return false;
564   }
565 
566   // validate locals
567   address locals = (address) *interpreter_frame_locals_addr();
568   if (locals > thread->stack_base() || locals < (address) fp()) {
569     return false;
570   }
571 
572   // We'd have to be pretty unlucky to be mislead at this point
573   return true;
574 }
575 
576 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
577   assert(is_interpreted_frame(), "interpreted frame expected");
578   Method* method = interpreter_frame_method();
579   BasicType type = method->result_type();
580 
581   intptr_t* tos_addr = NULL;
582   if (method->is_native()) {
583     tos_addr = (intptr_t*)sp();
584     if (type == T_FLOAT || type == T_DOUBLE) {
585       // This is because we do a push(ltos) after push(dtos) in generate_native_entry.
586       tos_addr += 2 * Interpreter::stackElementWords;
587     }
588   } else {
589     tos_addr = (intptr_t*)interpreter_frame_tos_address();
590   }
591 
592   switch (type) {
593     case T_OBJECT  :
594     case T_ARRAY   : {
595       oop obj;
596       if (method->is_native()) {
597         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
598       } else {
599         oop* obj_p = (oop*)tos_addr;
600         obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
601       }
602       assert(Universe::is_in_heap_or_null(obj), "sanity check");
603       *oop_result = obj;
604       break;
605     }
606     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
607     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
608     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
609     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
610     case T_INT     : value_result->i = *(jint*)tos_addr; break;
611     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
612     case T_FLOAT   : {
613         value_result->f = *(jfloat*)tos_addr;
614       break;
615     }
616     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
617     case T_VOID    : /* Nothing to do */ break;
618     default        : ShouldNotReachHere();
619   }
620 
621   return type;
622 }
623 
624 
625 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
626   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
627   return &interpreter_frame_tos_address()[index];
628 }
629 
630 #ifndef PRODUCT
631 
632 #define DESCRIBE_FP_OFFSET(name) \
633   values.describe(frame_no, fp() + frame::name##_offset, #name)
634 
635 void frame::describe_pd(FrameValues& values, int frame_no) {
636   if (is_interpreted_frame()) {
637     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
638     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
639     DESCRIBE_FP_OFFSET(interpreter_frame_method);
640     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
641     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
642     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
643     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
644     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
645     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
646   }
647 }
648 #endif
649 
650 intptr_t *frame::initial_deoptimization_info() {
651   // Not used on riscv, but we must return something.
652   return NULL;
653 }
654 
655 intptr_t* frame::real_fp() const {
656   if (_cb != NULL) {
657     // use the frame size if valid
658     int size = _cb->frame_size();
659     if (size > 0) {
660       return unextended_sp() + size;
661     }
662   }
663   // else rely on fp()
664   assert(!is_compiled_frame(), "unknown compiled frame size");
665   return fp();
666 }
667 
668 #undef DESCRIBE_FP_OFFSET
669 
670 #ifndef PRODUCT
671 // This is a generic constructor which is only used by pns() in debug.cpp.
672 frame::frame(void* ptr_sp, void* ptr_fp, void* pc) {
673   init((intptr_t*)ptr_sp, (intptr_t*)ptr_fp, (address)pc);
674 }
675 
676 void frame::pd_ps() {}
677 #endif
678 
679 void JavaFrameAnchor::make_walkable() {
680   // last frame set?
681   if (last_Java_sp() == NULL) { return; }
682   // already walkable?
683   if (walkable()) { return; }
684   vmassert(last_Java_sp() != NULL, "not called from Java code?");
685   vmassert(last_Java_pc() == NULL, "already walkable");
686   _last_Java_pc = (address)_last_Java_sp[-1];
687   vmassert(walkable(), "something went wrong");
688 }