1 /*
  2  * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "compiler/oopMap.hpp"
 27 #include "interpreter/interpreter.hpp"
 28 #include "memory/resourceArea.hpp"
 29 #include "memory/universe.hpp"
 30 #include "oops/markWord.hpp"
 31 #include "oops/method.hpp"
 32 #include "oops/oop.inline.hpp"
 33 #include "prims/methodHandles.hpp"
 34 #include "runtime/continuation.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/signature.hpp"
 40 #include "runtime/stackWatermarkSet.hpp"
 41 #include "runtime/stubCodeGenerator.hpp"
 42 #include "runtime/stubRoutines.hpp"
 43 #include "vmreg_x86.inline.hpp"
 44 #include "utilities/formatBuffer.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 // Profiling/safepoint support
 56 
 57 bool frame::safe_for_sender(JavaThread *thread) {
 58   if (is_heap_frame()) {
 59     return true;
 60   }
 61   address   sp = (address)_sp;
 62   address   fp = (address)_fp;
 63   address   unextended_sp = (address)_unextended_sp;
 64 
 65   // consider stack guards when trying to determine "safe" stack pointers
 66   // sp must be within the usable part of the stack (not in guards)
 67   if (!thread->is_in_usable_stack(sp)) {
 68     return false;
 69   }
 70 
 71   // unextended sp must be within the stack
 72   // Note: sp can be greater than unextended_sp in the case of
 73   // interpreted -> interpreted calls that go through a method handle linker,
 74   // since those pop the last argument (the appendix) from the stack.
 75   if (!thread->is_in_stack_range_incl(unextended_sp, sp - Interpreter::stackElementSize)) {
 76     return false;
 77   }
 78 
 79   // an fp must be within the stack and above (but not equal) sp
 80   // second evaluation on fp+ is added to handle situation where fp is -1
 81   bool fp_safe = thread->is_in_stack_range_excl(fp, sp) &&
 82                  thread->is_in_full_stack_checked(fp + (return_addr_offset * sizeof(void*)));
 83 
 84   // We know sp/unextended_sp are safe only fp is questionable here
 85 
 86   // If the current frame is known to the code cache then we can attempt to
 87   // construct the sender and do some validation of it. This goes a long way
 88   // toward eliminating issues when we get in frame construction code
 89 
 90   if (_cb != nullptr ) {
 91 
 92     // First check if frame is complete and tester is reliable
 93     // Unfortunately we can only check frame complete for runtime stubs and nmethod
 94     // other generic buffer blobs are more problematic so we just assume they are
 95     // ok. adapter blobs never have a frame complete and are never ok.
 96 
 97     if (!_cb->is_frame_complete_at(_pc)) {
 98       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
 99         return false;
100       }
101     }
102 
103     // Could just be some random pointer within the codeBlob
104     if (!_cb->code_contains(_pc)) {
105       return false;
106     }
107 
108     // Entry frame checks
109     if (is_entry_frame()) {
110       // an entry frame must have a valid fp.
111       return fp_safe && is_entry_frame_valid(thread);
112     } else if (is_upcall_stub_frame()) {
113       return fp_safe;
114     }
115 
116     intptr_t* sender_sp = nullptr;
117     intptr_t* sender_unextended_sp = nullptr;
118     address   sender_pc = nullptr;
119     intptr_t* saved_fp =  nullptr;
120 
121     if (is_interpreted_frame()) {
122       // fp must be safe
123       if (!fp_safe) {
124         return false;
125       }
126 
127       sender_pc = (address) this->fp()[return_addr_offset];
128       // for interpreted frames, the value below is the sender "raw" sp,
129       // which can be different from the sender unextended sp (the sp seen
130       // by the sender) because of current frame local variables
131       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
132       sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
133       saved_fp = (intptr_t*) this->fp()[link_offset];
134 
135     } else {
136       // must be some sort of compiled/runtime frame
137       // fp does not have to be safe (although it could be check for c1?)
138 
139       // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
140       if (_cb->frame_size() <= 0) {
141         return false;
142       }
143 
144       sender_sp = _unextended_sp + _cb->frame_size();
145       // Is sender_sp safe?
146       if (!thread->is_in_full_stack_checked((address)sender_sp)) {
147         return false;
148       }
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       intptr_t** saved_fp_addr = (intptr_t**) (sender_sp - frame::sender_sp_offset);
153       saved_fp = *saved_fp_addr;
154 
155       // Repair the sender sp if this is a method with scalarized inline type args
156       sender_sp = repair_sender_sp(sender_sp, saved_fp_addr);
157       sender_unextended_sp = sender_sp;
158     }
159     if (Continuation::is_return_barrier_entry(sender_pc)) {
160       // If our sender_pc is the return barrier, then our "real" sender is the continuation entry
161       frame s = Continuation::continuation_bottom_sender(thread, *this, sender_sp);
162       sender_sp = s.sp();
163       sender_pc = s.pc();
164     }
165 
166     // If the potential sender is the interpreter then we can do some more checking
167     if (Interpreter::contains(sender_pc)) {
168 
169       // ebp is always saved in a recognizable place in any code we generate. However
170       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved ebp
171       // is really a frame pointer.
172 
173       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
174         return false;
175       }
176 
177       // construct the potential sender
178 
179       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
180 
181       return sender.is_interpreted_frame_valid(thread);
182 
183     }
184 
185     // We must always be able to find a recognizable pc
186     CodeBlob* sender_blob = CodeCache::find_blob(sender_pc);
187     if (sender_pc == nullptr ||  sender_blob == nullptr) {
188       return false;
189     }
190 
191     // Could just be some random pointer within the codeBlob
192     if (!sender_blob->code_contains(sender_pc)) {
193       return false;
194     }
195 
196     // We should never be able to see an adapter if the current frame is something from code cache
197     if (sender_blob->is_adapter_blob()) {
198       return false;
199     }
200 
201     // Could be the call_stub
202     if (StubRoutines::returns_to_call_stub(sender_pc)) {
203       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
204         return false;
205       }
206 
207       // construct the potential sender
208 
209       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
210 
211       // Validate the JavaCallWrapper an entry frame must have
212       address jcw = (address)sender.entry_frame_call_wrapper();
213 
214       return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
215     } else if (sender_blob->is_upcall_stub()) {
216       return false;
217     }
218 
219     nmethod* nm = sender_blob->as_nmethod_or_null();
220     if (nm != nullptr) {
221         if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
222             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 //------------------------------------------------------------------------------
416 // frame::verify_deopt_original_pc
417 //
418 // Verifies the calculated original PC of a deoptimization PC for the
419 // given unextended SP.
420 #ifdef ASSERT
421 void frame::verify_deopt_original_pc(nmethod* nm, intptr_t* unextended_sp) {
422   frame fr;
423 
424   // This is ugly but it's better than to change {get,set}_original_pc
425   // to take an SP value as argument.  And it's only a debugging
426   // method anyway.
427   fr._unextended_sp = unextended_sp;
428 
429   address original_pc = nm->get_original_pc(&fr);
430   assert(nm->insts_contains_inclusive(original_pc),
431          "original PC must be in the main code section of the compiled method (or must be immediately following it) original_pc: " INTPTR_FORMAT " unextended_sp: " INTPTR_FORMAT " name: %s", p2i(original_pc), p2i(unextended_sp), nm->name());
432 }
433 #endif
434 
435 //------------------------------------------------------------------------------
436 // frame::adjust_unextended_sp
437 #ifdef ASSERT
438 void frame::adjust_unextended_sp() {
439   // On x86, sites calling method handle intrinsics and lambda forms are treated
440   // as any other call site. Therefore, no special action is needed when we are
441   // returning to any of these call sites.
442 
443   if (_cb != nullptr) {
444     nmethod* sender_nm = _cb->as_nmethod_or_null();
445     if (sender_nm != nullptr) {
446       // If the sender PC is a deoptimization point, get the original PC.
447       if (sender_nm->is_deopt_entry(_pc) ||
448           sender_nm->is_deopt_mh_entry(_pc)) {
449         verify_deopt_original_pc(sender_nm, _unextended_sp);
450       }
451     }
452   }
453 }
454 #endif
455 
456 //------------------------------------------------------------------------------
457 // frame::sender_for_interpreter_frame
458 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
459   // SP is the raw SP from the sender after adapter or interpreter
460   // extension.
461   intptr_t* sender_sp = this->sender_sp();
462 
463   // This is the sp before any possible extension (adapter/locals).
464   intptr_t* unextended_sp = interpreter_frame_sender_sp();
465   intptr_t* sender_fp = link();
466 
467 #if COMPILER2_OR_JVMCI
468   if (map->update_map()) {
469     update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
470   }
471 #endif // COMPILER2_OR_JVMCI
472 
473   address sender_pc = this->sender_pc();
474 
475   if (Continuation::is_return_barrier_entry(sender_pc)) {
476     if (map->walk_cont()) { // about to walk into an h-stack
477       return Continuation::top_frame(*this, map);
478     } else {
479       return Continuation::continuation_bottom_sender(map->thread(), *this, sender_sp);
480     }
481   }
482 
483   return frame(sender_sp, unextended_sp, sender_fp, sender_pc);
484 }
485 
486 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
487   assert(is_interpreted_frame(), "Not an interpreted frame");
488   // These are reasonable sanity checks
489   if (fp() == nullptr || (intptr_t(fp()) & (wordSize-1)) != 0) {
490     return false;
491   }
492   if (sp() == nullptr || (intptr_t(sp()) & (wordSize-1)) != 0) {
493     return false;
494   }
495   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
496     return false;
497   }
498   // These are hacks to keep us out of trouble.
499   // The problem with these is that they mask other problems
500   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
501     return false;
502   }
503 
504   // do some validation of frame elements
505   // first the method
506 
507   Method* m = safe_interpreter_frame_method();
508 
509   // validate the method we'd find in this potential sender
510   if (!Method::is_valid_method(m)) return false;
511 
512   // stack frames shouldn't be much larger than max_stack elements
513   // this test requires the use the unextended_sp which is the sp as seen by
514   // the current frame, and not sp which is the "raw" pc which could point
515   // further because of local variables of the callee method inserted after
516   // method arguments
517   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
518     return false;
519   }
520 
521   // validate bci/bcp
522 
523   address bcp = interpreter_frame_bcp();
524   if (m->validate_bci_from_bcp(bcp) < 0) {
525     return false;
526   }
527 
528   // validate ConstantPoolCache*
529   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
530   if (MetaspaceObj::is_valid(cp) == false) return false;
531 
532   // validate locals
533 
534   address locals =  (address)interpreter_frame_locals();
535   return thread->is_in_stack_range_incl(locals, (address)fp());
536 }
537 
538 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
539   assert(is_interpreted_frame(), "interpreted frame expected");
540   Method* method = interpreter_frame_method();
541   BasicType type = method->result_type();
542 
543   intptr_t* tos_addr;
544   if (method->is_native()) {
545     // Prior to calling into the runtime to report the method_exit the possible
546     // return value is pushed to the native stack. If the result is a jfloat/jdouble
547     // then ST0 is saved before EAX/EDX. See the note in generate_native_result
548     tos_addr = (intptr_t*)sp();
549     if (type == T_FLOAT || type == T_DOUBLE) {
550     // QQQ seems like this code is equivalent on the two platforms
551 #ifdef AMD64
552       // This is times two because we do a push(ltos) after pushing XMM0
553       // and that takes two interpreter stack slots.
554       tos_addr += 2 * Interpreter::stackElementWords;
555 #else
556       tos_addr += 2;
557 #endif // AMD64
558     }
559   } else {
560     tos_addr = (intptr_t*)interpreter_frame_tos_address();
561   }
562 
563   switch (type) {
564     case T_OBJECT  :
565     case T_ARRAY   : {
566       oop obj;
567       if (method->is_native()) {
568         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
569       } else {
570         oop* obj_p = (oop*)tos_addr;
571         obj = (obj_p == nullptr) ? (oop)nullptr : *obj_p;
572       }
573       assert(Universe::is_in_heap_or_null(obj), "sanity check");
574       *oop_result = obj;
575       break;
576     }
577     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
578     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
579     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
580     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
581     case T_INT     : value_result->i = *(jint*)tos_addr; break;
582     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
583     case T_FLOAT   : {
584 #ifdef AMD64
585         value_result->f = *(jfloat*)tos_addr;
586 #else
587       if (method->is_native()) {
588         jdouble d = *(jdouble*)tos_addr;  // Result was in ST0 so need to convert to jfloat
589         value_result->f = (jfloat)d;
590       } else {
591         value_result->f = *(jfloat*)tos_addr;
592       }
593 #endif // AMD64
594       break;
595     }
596     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
597     case T_VOID    : /* Nothing to do */ break;
598     default        : ShouldNotReachHere();
599   }
600 
601   return type;
602 }
603 
604 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
605   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
606   return &interpreter_frame_tos_address()[index];
607 }
608 
609 #ifndef PRODUCT
610 
611 #define DESCRIBE_FP_OFFSET(name) \
612   values.describe(frame_no, fp() + frame::name##_offset, #name, 1)
613 
614 void frame::describe_pd(FrameValues& values, int frame_no) {
615   if (is_interpreted_frame()) {
616     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
617     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
618     DESCRIBE_FP_OFFSET(interpreter_frame_method);
619     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
620     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
621     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
622     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
623     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
624     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
625 #ifdef AMD64
626   } else if (is_entry_frame()) {
627     // This could be more descriptive if we use the enum in
628     // stubGenerator to map to real names but it's most important to
629     // claim these frame slots so the error checking works.
630     for (int i = 0; i < entry_frame_after_call_words; i++) {
631       values.describe(frame_no, fp() - i, err_msg("call_stub word fp - %d", i));
632     }
633 #endif // AMD64
634   }
635 
636   if (is_java_frame() || Continuation::is_continuation_enterSpecial(*this)) {
637     intptr_t* ret_pc_loc;
638     intptr_t* fp_loc;
639     if (is_interpreted_frame()) {
640       ret_pc_loc = fp() + return_addr_offset;
641       fp_loc = fp();
642     } else {
643       ret_pc_loc = real_fp() - return_addr_offset;
644       fp_loc = real_fp() - sender_sp_offset;
645     }
646     address ret_pc = *(address*)ret_pc_loc;
647     values.describe(frame_no, ret_pc_loc,
648       Continuation::is_return_barrier_entry(ret_pc) ? "return address (return barrier)" : "return address");
649     values.describe(-1, fp_loc, "saved fp", 0); // "unowned" as value belongs to sender
650   }
651 }
652 
653 #endif // !PRODUCT
654 
655 intptr_t *frame::initial_deoptimization_info() {
656   // used to reset the saved FP
657   return fp();
658 }
659 
660 #ifndef PRODUCT
661 // This is a generic constructor which is only used by pns() in debug.cpp.
662 frame::frame(void* sp, void* fp, void* pc) {
663   init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
664 }
665 
666 #endif
667 
668 // Check for a method with scalarized inline type arguments that needs
669 // a stack repair and return the repaired sender stack pointer.
670 intptr_t* frame::repair_sender_sp(intptr_t* sender_sp, intptr_t** saved_fp_addr) const {
671   nmethod* nm = _cb->as_nmethod_or_null();
672   if (nm != nullptr && nm->needs_stack_repair()) {
673     // The stack increment resides just below the saved rbp on the stack
674     // and does not account for the return address.
675     intptr_t* real_frame_size_addr = (intptr_t*) (saved_fp_addr - 1);
676     int real_frame_size = ((*real_frame_size_addr) + wordSize) / wordSize;
677     assert(real_frame_size >= _cb->frame_size() && real_frame_size <= 1000000, "invalid frame size");
678     sender_sp = unextended_sp() + real_frame_size;
679   }
680   return sender_sp;
681 }
682 
683 void JavaFrameAnchor::make_walkable() {
684   // last frame set?
685   if (last_Java_sp() == nullptr) return;
686   // already walkable?
687   if (walkable()) return;
688   vmassert(last_Java_pc() == nullptr, "already walkable");
689   _last_Java_pc = (address)_last_Java_sp[-1];
690   vmassert(walkable(), "something went wrong");
691 }