1 /*
  2  * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "compiler/oopMap.hpp"
 26 #include "interpreter/interpreter.hpp"
 27 #include "memory/resourceArea.hpp"
 28 #include "memory/universe.hpp"
 29 #include "oops/markWord.hpp"
 30 #include "oops/method.hpp"
 31 #include "oops/oop.inline.hpp"
 32 #include "prims/methodHandles.hpp"
 33 #include "runtime/continuation.hpp"
 34 #include "runtime/frame.inline.hpp"
 35 #include "runtime/handles.inline.hpp"
 36 #include "runtime/javaCalls.hpp"
 37 #include "runtime/monitorChunk.hpp"
 38 #include "runtime/signature.hpp"
 39 #include "runtime/stackWatermarkSet.hpp"
 40 #include "runtime/stubCodeGenerator.hpp"
 41 #include "runtime/stubRoutines.hpp"
 42 #include "vmreg_x86.inline.hpp"
 43 #include "utilities/formatBuffer.hpp"
 44 #ifdef COMPILER1
 45 #include "c1/c1_Runtime1.hpp"
 46 #include "runtime/vframeArray.hpp"
 47 #endif
 48 
 49 #ifdef ASSERT
 50 void RegisterMap::check_location_valid() {
 51 }
 52 #endif
 53 
 54 // Profiling/safepoint support
 55 
 56 bool frame::safe_for_sender(JavaThread *thread) {
 57   if (is_heap_frame()) {
 58     return true;
 59   }
 60   address   sp = (address)_sp;
 61   address   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(sp)) {
 67     return false;
 68   }
 69 
 70   // unextended sp must be within the stack
 71   // Note: sp can be greater than unextended_sp in the case of
 72   // interpreted -> interpreted calls that go through a method handle linker,
 73   // since those pop the last argument (the appendix) from the stack.
 74   if (!thread->is_in_stack_range_incl(unextended_sp, sp - Interpreter::stackElementSize)) {
 75     return false;
 76   }
 77 
 78   // an fp must be within the stack and above (but not equal) sp
 79   // second evaluation on fp+ is added to handle situation where fp is -1
 80   bool fp_safe = thread->is_in_stack_range_excl(fp, sp) &&
 81                  thread->is_in_full_stack_checked(fp + (return_addr_offset * sizeof(void*)));
 82 
 83   // We know sp/unextended_sp are safe only fp is questionable here
 84 
 85   // If the current frame is known to the code cache then we can attempt to
 86   // construct the sender and do some validation of it. This goes a long way
 87   // toward eliminating issues when we get in frame construction code
 88 
 89   if (_cb != nullptr ) {
 90 
 91     // First check if frame is complete and tester is reliable
 92     // Unfortunately we can only check frame complete for runtime stubs and nmethod
 93     // other generic buffer blobs are more problematic so we just assume they are
 94     // ok. adapter blobs never have a frame complete and are never ok.
 95 
 96     if (!_cb->is_frame_complete_at(_pc)) {
 97       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
 98         return false;
 99       }
100     }
101 
102     // Could just be some random pointer within the codeBlob
103     if (!_cb->code_contains(_pc)) {
104       return false;
105     }
106 
107     // Entry frame checks
108     if (is_entry_frame()) {
109       // an entry frame must have a valid fp.
110       return fp_safe && is_entry_frame_valid(thread);
111     } else if (is_upcall_stub_frame()) {
112       return fp_safe;
113     }
114 
115     intptr_t* sender_sp = nullptr;
116     intptr_t* sender_unextended_sp = nullptr;
117     address   sender_pc = nullptr;
118     intptr_t* saved_fp =  nullptr;
119 
120     if (is_interpreted_frame()) {
121       // fp must be safe
122       if (!fp_safe) {
123         return false;
124       }
125 
126       sender_pc = (address) this->fp()[return_addr_offset];
127       // for interpreted frames, the value below is the sender "raw" sp,
128       // which can be different from the sender unextended sp (the sp seen
129       // by the sender) because of current frame local variables
130       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
131       sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
132       saved_fp = (intptr_t*) this->fp()[link_offset];
133 
134     } else {
135       // must be some sort of compiled/runtime frame
136       // fp does not have to be safe (although it could be check for c1?)
137 
138       // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
139       if (_cb->frame_size() <= 0) {
140         return false;
141       }
142 
143       sender_sp = _unextended_sp + _cb->frame_size();
144       // Is sender_sp safe?
145       if (!thread->is_in_full_stack_checked((address)sender_sp)) {
146         return false;
147       }
148       sender_unextended_sp = sender_sp;
149       // On Intel the return_address is always the word on the stack
150       sender_pc = (address) *(sender_sp-1);
151       // Note: frame::sender_sp_offset is only valid for compiled frame
152       saved_fp = (intptr_t*) *(sender_sp - frame::sender_sp_offset);
153     }
154 
155     if (Continuation::is_return_barrier_entry(sender_pc)) {
156       // sender_pc might be invalid so check that the frame
157       // actually belongs to a Continuation.
158       if (!Continuation::is_frame_in_continuation(thread, *this)) {
159         return false;
160       }
161       // If our sender_pc is the return barrier, then our "real" sender is the continuation entry
162       frame s = Continuation::continuation_bottom_sender(thread, *this, sender_sp);
163       sender_sp = s.sp();
164       sender_pc = s.pc();
165     }
166 
167     // If the potential sender is the interpreter then we can do some more checking
168     if (Interpreter::contains(sender_pc)) {
169 
170       // ebp is always saved in a recognizable place in any code we generate. However
171       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved ebp
172       // is really a frame pointer.
173 
174       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
175         return false;
176       }
177 
178       // construct the potential sender
179 
180       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
181 
182       return sender.is_interpreted_frame_valid(thread);
183 
184     }
185 
186     // We must always be able to find a recognizable pc
187     CodeBlob* sender_blob = CodeCache::find_blob(sender_pc);
188     if (sender_pc == nullptr ||  sender_blob == nullptr) {
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_upcall_stub()) {
217       return false;
218     }
219 
220     nmethod* nm = sender_blob->as_nmethod_or_null();
221     if (nm != nullptr) {
222         if (nm->is_deopt_entry(sender_pc) || nm->method()->is_method_handle_intrinsic()) {
223             return false;
224         }
225     }
226 
227     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
228     // because the return address counts against the callee's frame.
229 
230     if (sender_blob->frame_size() <= 0) {
231       assert(!sender_blob->is_nmethod(), "should count return address at least");
232       return false;
233     }
234 
235     // We should never be able to see anything here except an nmethod. If something in the
236     // code cache (current frame) is called by an entity within the code cache that entity
237     // should not be anything but the call stub (already covered), the interpreter (already covered)
238     // or an nmethod.
239 
240     if (!sender_blob->is_nmethod()) {
241         return false;
242     }
243 
244     // Could put some more validation for the potential non-interpreted sender
245     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
246 
247     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
248 
249     // We've validated the potential sender that would be created
250     return true;
251   }
252 
253   // Must be native-compiled frame. Since sender will try and use fp to find
254   // linkages it must be safe
255 
256   if (!fp_safe) {
257     return false;
258   }
259 
260   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
261 
262   if ( (address) this->fp()[return_addr_offset] == nullptr) return false;
263 
264 
265   // could try and do some more potential verification of native frame if we could think of some...
266 
267   return true;
268 
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 
276   if (TracePcPatching) {
277     tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
278                   p2i(pc_addr), p2i(*pc_addr), p2i(pc));
279   }
280   // Either the return address is the original one or we are going to
281   // patch in the same address that's already there.
282 
283   assert(!Continuation::is_return_barrier_entry(*pc_addr), "return barrier");
284 
285   assert(_pc == *pc_addr || pc == *pc_addr || *pc_addr == nullptr, "");
286   DEBUG_ONLY(address old_pc = _pc;)
287   *pc_addr = pc;
288   _pc = pc; // must be set before call to get_deopt_original_pc
289   address original_pc = get_deopt_original_pc();
290   if (original_pc != nullptr) {
291     assert(original_pc == old_pc, "expected original PC to be stored before patching");
292     _deopt_state = is_deoptimized;
293     _pc = original_pc;
294   } else {
295     _deopt_state = not_deoptimized;
296   }
297   assert(!is_compiled_frame() || !_cb->as_nmethod()->is_deopt_entry(_pc), "must be");
298 
299 #ifdef ASSERT
300   {
301     frame f(this->sp(), this->unextended_sp(), this->fp(), pc);
302     assert(f.is_deoptimized_frame() == this->is_deoptimized_frame() && f.pc() == this->pc() && f.raw_pc() == this->raw_pc(),
303       "must be (f.is_deoptimized_frame(): %d this->is_deoptimized_frame(): %d "
304       "f.pc(): " INTPTR_FORMAT " this->pc(): " INTPTR_FORMAT " f.raw_pc(): " INTPTR_FORMAT " this->raw_pc(): " INTPTR_FORMAT ")",
305       f.is_deoptimized_frame(), this->is_deoptimized_frame(), p2i(f.pc()), p2i(this->pc()), p2i(f.raw_pc()), p2i(this->raw_pc()));
306   }
307 #endif
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: result: " INTPTR_FORMAT " fp: " INTPTR_FORMAT, p2i(result), p2i(fp()));
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 frame frame::sender_for_entry_frame(RegisterMap* map) const {
367   assert(map != nullptr, "map must be set");
368   // Java frame called from C; skip all C frames and return top C
369   // frame of that chunk as the sender
370   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
371   assert(!entry_frame_is_first(), "next Java fp must be non zero");
372   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
373   // Since we are walking the stack now this nested anchor is obviously walkable
374   // even if it wasn't when it was stacked.
375   jfa->make_walkable();
376   map->clear();
377   assert(map->include_argument_oops(), "should be set by clear");
378   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
379 
380   return fr;
381 }
382 
383 UpcallStub::FrameData* UpcallStub::frame_data_for_frame(const frame& frame) const {
384   assert(frame.is_upcall_stub_frame(), "wrong frame");
385   // need unextended_sp here, since normal sp is wrong for interpreter callees
386   return reinterpret_cast<UpcallStub::FrameData*>(
387     reinterpret_cast<address>(frame.unextended_sp()) + in_bytes(_frame_data_offset));
388 }
389 
390 bool frame::upcall_stub_frame_is_first() const {
391   assert(is_upcall_stub_frame(), "must be optimzed entry frame");
392   UpcallStub* blob = _cb->as_upcall_stub();
393   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
394   return jfa->last_Java_sp() == nullptr;
395 }
396 
397 frame frame::sender_for_upcall_stub_frame(RegisterMap* map) const {
398   assert(map != nullptr, "map must be set");
399   UpcallStub* blob = _cb->as_upcall_stub();
400   // Java frame called from C; skip all C frames and return top C
401   // frame of that chunk as the sender
402   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
403   assert(!upcall_stub_frame_is_first(), "must have a frame anchor to go back to");
404   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
405   // Since we are walking the stack now this nested anchor is obviously walkable
406   // even if it wasn't when it was stacked.
407   jfa->make_walkable();
408   map->clear();
409   assert(map->include_argument_oops(), "should be set by clear");
410   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
411 
412   return fr;
413 }
414 
415 #if defined(ASSERT)
416 static address get_register_address_in_stub(const frame& stub_fr, VMReg reg) {
417   RegisterMap map(nullptr,
418                   RegisterMap::UpdateMap::include,
419                   RegisterMap::ProcessFrames::skip,
420                   RegisterMap::WalkContinuation::skip);
421   stub_fr.oop_map()->update_register_map(&stub_fr, &map);
422   return map.location(reg, stub_fr.sp());
423 }
424 #endif
425 
426 JavaThread** frame::saved_thread_address(const frame& f) {
427   CodeBlob* cb = f.cb();
428   assert(cb != nullptr && cb->is_runtime_stub(), "invalid frame");
429 
430   JavaThread** thread_addr;
431 #ifdef COMPILER1
432   if (cb == Runtime1::blob_for(StubId::c1_monitorenter_id) ||
433       cb == Runtime1::blob_for(StubId::c1_monitorenter_nofpu_id)) {
434     thread_addr = (JavaThread**)(f.sp() + Runtime1::runtime_blob_current_thread_offset(f));
435   } else
436 #endif
437   {
438     // c2 only saves rbp in the stub frame so nothing to do.
439     thread_addr = nullptr;
440   }
441   assert(get_register_address_in_stub(f, SharedRuntime::thread_register()) == (address)thread_addr, "wrong thread address");
442   return thread_addr;
443 }
444 
445 //------------------------------------------------------------------------------
446 // frame::sender_for_interpreter_frame
447 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
448   // SP is the raw SP from the sender after adapter or interpreter
449   // extension.
450   intptr_t* sender_sp = this->sender_sp();
451 
452   // This is the sp before any possible extension (adapter/locals).
453   intptr_t* unextended_sp = interpreter_frame_sender_sp();
454   intptr_t* sender_fp = link();
455 
456 #if COMPILER2_OR_JVMCI
457   if (map->update_map()) {
458     update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
459   }
460 #endif // COMPILER2_OR_JVMCI
461 
462   address sender_pc = this->sender_pc();
463 
464   if (Continuation::is_return_barrier_entry(sender_pc)) {
465     if (map->walk_cont()) { // about to walk into an h-stack
466       return Continuation::top_frame(*this, map);
467     } else {
468       return Continuation::continuation_bottom_sender(map->thread(), *this, sender_sp);
469     }
470   }
471 
472   return frame(sender_sp, unextended_sp, sender_fp, sender_pc);
473 }
474 
475 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
476   assert(is_interpreted_frame(), "Not an interpreted frame");
477   // These are reasonable sanity checks
478   if (fp() == nullptr || (intptr_t(fp()) & (wordSize-1)) != 0) {
479     return false;
480   }
481   if (sp() == nullptr || (intptr_t(sp()) & (wordSize-1)) != 0) {
482     return false;
483   }
484   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
485     return false;
486   }
487   // These are hacks to keep us out of trouble.
488   // The problem with these is that they mask other problems
489   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
490     return false;
491   }
492 
493   // do some validation of frame elements
494   // first the method
495 
496   Method* m = safe_interpreter_frame_method();
497 
498   // validate the method we'd find in this potential sender
499   if (!Method::is_valid_method(m)) return false;
500 
501   // stack frames shouldn't be much larger than max_stack elements
502   // this test requires the use the unextended_sp which is the sp as seen by
503   // the current frame, and not sp which is the "raw" pc which could point
504   // further because of local variables of the callee method inserted after
505   // method arguments
506   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
507     return false;
508   }
509 
510   // validate bci/bcp
511 
512   address bcp = interpreter_frame_bcp();
513   if (m->validate_bci_from_bcp(bcp) < 0) {
514     return false;
515   }
516 
517   // validate ConstantPoolCache*
518   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
519   if (MetaspaceObj::is_valid(cp) == false) return false;
520 
521   // validate locals
522 
523   address locals =  (address)interpreter_frame_locals();
524   return thread->is_in_stack_range_incl(locals, (address)fp());
525 }
526 
527 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
528   assert(is_interpreted_frame(), "interpreted frame expected");
529   Method* method = interpreter_frame_method();
530   BasicType type = method->result_type();
531 
532   intptr_t* tos_addr;
533   if (method->is_native()) {
534     // Prior to calling into the runtime to report the method_exit the possible
535     // return value is pushed to the native stack. If the result is a jfloat/jdouble
536     // then ST0 is saved before EAX/EDX. See the note in generate_native_result
537     tos_addr = (intptr_t*)sp();
538     if (type == T_FLOAT || type == T_DOUBLE) {
539     // QQQ seems like this code is equivalent on the two platforms
540 #ifdef AMD64
541       // This is times two because we do a push(ltos) after pushing XMM0
542       // and that takes two interpreter stack slots.
543       tos_addr += 2 * Interpreter::stackElementWords;
544 #else
545       tos_addr += 2;
546 #endif // AMD64
547     }
548   } else {
549     tos_addr = (intptr_t*)interpreter_frame_tos_address();
550   }
551 
552   switch (type) {
553     case T_OBJECT  :
554     case T_ARRAY   : {
555       oop obj;
556       if (method->is_native()) {
557         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
558       } else {
559         oop* obj_p = (oop*)tos_addr;
560         obj = (obj_p == nullptr) ? (oop)nullptr : *obj_p;
561       }
562       assert(Universe::is_in_heap_or_null(obj), "sanity check");
563       *oop_result = obj;
564       break;
565     }
566     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
567     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
568     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
569     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
570     case T_INT     : value_result->i = *(jint*)tos_addr; break;
571     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
572     case T_FLOAT   : {
573 #ifdef AMD64
574         value_result->f = *(jfloat*)tos_addr;
575 #else
576       if (method->is_native()) {
577         jdouble d = *(jdouble*)tos_addr;  // Result was in ST0 so need to convert to jfloat
578         value_result->f = (jfloat)d;
579       } else {
580         value_result->f = *(jfloat*)tos_addr;
581       }
582 #endif // AMD64
583       break;
584     }
585     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
586     case T_VOID    : /* Nothing to do */ break;
587     default        : ShouldNotReachHere();
588   }
589 
590   return type;
591 }
592 
593 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
594   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
595   return &interpreter_frame_tos_address()[index];
596 }
597 
598 #ifndef PRODUCT
599 
600 #define DESCRIBE_FP_OFFSET(name) \
601   values.describe(frame_no, fp() + frame::name##_offset, #name, 1)
602 
603 void frame::describe_pd(FrameValues& values, int frame_no) {
604   if (is_interpreted_frame()) {
605     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
606     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
607     DESCRIBE_FP_OFFSET(interpreter_frame_method);
608     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
609     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
610     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
611     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
612     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
613     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
614 #ifdef AMD64
615   } else if (is_entry_frame()) {
616     // This could be more descriptive if we use the enum in
617     // stubGenerator to map to real names but it's most important to
618     // claim these frame slots so the error checking works.
619     for (int i = 0; i < entry_frame_after_call_words; i++) {
620       values.describe(frame_no, fp() - i, err_msg("call_stub word fp - %d", i));
621     }
622 #endif // AMD64
623   }
624 
625   if (is_java_frame() || Continuation::is_continuation_enterSpecial(*this)) {
626     intptr_t* ret_pc_loc;
627     intptr_t* fp_loc;
628     if (is_interpreted_frame()) {
629       ret_pc_loc = fp() + return_addr_offset;
630       fp_loc = fp();
631     } else {
632       ret_pc_loc = real_fp() - return_addr_offset;
633       fp_loc = real_fp() - sender_sp_offset;
634     }
635     address ret_pc = *(address*)ret_pc_loc;
636     values.describe(frame_no, ret_pc_loc,
637       Continuation::is_return_barrier_entry(ret_pc) ? "return address (return barrier)" : "return address");
638     values.describe(-1, fp_loc, "saved fp", 0); // "unowned" as value belongs to sender
639   }
640 }
641 
642 #endif // !PRODUCT
643 
644 intptr_t *frame::initial_deoptimization_info() {
645   // used to reset the saved FP
646   return fp();
647 }
648 
649 #ifndef PRODUCT
650 // This is a generic constructor which is only used by pns() in debug.cpp.
651 frame::frame(void* sp, void* fp, void* pc) {
652   init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
653 }
654 
655 #endif
656 
657 void JavaFrameAnchor::make_walkable() {
658   // last frame set?
659   if (last_Java_sp() == nullptr) return;
660   // already walkable?
661   if (walkable()) return;
662   _last_Java_pc = (address)_last_Java_sp[-1];
663   vmassert(walkable(), "something went wrong");
664 }