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       // On Intel the return_address is always the word on the stack
149       sender_pc = (address) *(sender_sp-1);
150       // Note: frame::sender_sp_offset is only valid for compiled frame
151       intptr_t** saved_fp_addr = (intptr_t**) (sender_sp - frame::sender_sp_offset);
152       saved_fp = *saved_fp_addr;
153 
154       // Repair the sender sp if this is a method with scalarized inline type args
155       sender_sp = repair_sender_sp(sender_sp, saved_fp_addr);
156       sender_unextended_sp = sender_sp;
157     }
158     if (Continuation::is_return_barrier_entry(sender_pc)) {
159       // sender_pc might be invalid so check that the frame
160       // actually belongs to a Continuation.
161       if (!Continuation::is_frame_in_continuation(thread, *this)) {
162         return false;
163       }
164       // If our sender_pc is the return barrier, then our "real" sender is the continuation entry
165       frame s = Continuation::continuation_bottom_sender(thread, *this, sender_sp);
166       sender_sp = s.sp();
167       sender_pc = s.pc();
168     }
169 
170     // If the potential sender is the interpreter then we can do some more checking
171     if (Interpreter::contains(sender_pc)) {
172 
173       // ebp is always saved in a recognizable place in any code we generate. However
174       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved ebp
175       // is really a frame pointer.
176 
177       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
178         return false;
179       }
180 
181       // construct the potential sender
182 
183       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
184 
185       return sender.is_interpreted_frame_valid(thread);
186 
187     }
188 
189     // We must always be able to find a recognizable pc
190     CodeBlob* sender_blob = CodeCache::find_blob(sender_pc);
191     if (sender_pc == nullptr ||  sender_blob == nullptr) {
192       return false;
193     }
194 
195     // Could just be some random pointer within the codeBlob
196     if (!sender_blob->code_contains(sender_pc)) {
197       return false;
198     }
199 
200     // We should never be able to see an adapter if the current frame is something from code cache
201     if (sender_blob->is_adapter_blob()) {
202       return false;
203     }
204 
205     // Could be the call_stub
206     if (StubRoutines::returns_to_call_stub(sender_pc)) {
207       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
208         return false;
209       }
210 
211       // construct the potential sender
212 
213       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
214 
215       // Validate the JavaCallWrapper an entry frame must have
216       address jcw = (address)sender.entry_frame_call_wrapper();
217 
218       return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
219     } else if (sender_blob->is_upcall_stub()) {
220       return false;
221     }
222 
223     nmethod* nm = sender_blob->as_nmethod_or_null();
224     if (nm != nullptr) {
225         if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
226             nm->method()->is_method_handle_intrinsic()) {
227             return false;
228         }
229     }
230 
231     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
232     // because the return address counts against the callee's frame.
233 
234     if (sender_blob->frame_size() <= 0) {
235       assert(!sender_blob->is_nmethod(), "should count return address at least");
236       return false;
237     }
238 
239     // We should never be able to see anything here except an nmethod. If something in the
240     // code cache (current frame) is called by an entity within the code cache that entity
241     // should not be anything but the call stub (already covered), the interpreter (already covered)
242     // or an nmethod.
243 
244     if (!sender_blob->is_nmethod()) {
245         return false;
246     }
247 
248     // Could put some more validation for the potential non-interpreted sender
249     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
250 
251     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
252 
253     // We've validated the potential sender that would be created
254     return true;
255   }
256 
257   // Must be native-compiled frame. Since sender will try and use fp to find
258   // linkages it must be safe
259 
260   if (!fp_safe) {
261     return false;
262   }
263 
264   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
265 
266   if ( (address) this->fp()[return_addr_offset] == nullptr) return false;
267 
268 
269   // could try and do some more potential verification of native frame if we could think of some...
270 
271   return true;
272 
273 }
274 
275 
276 void frame::patch_pc(Thread* thread, address pc) {
277   assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
278   address* pc_addr = &(((address*) sp())[-1]);
279 
280   if (TracePcPatching) {
281     tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
282                   p2i(pc_addr), p2i(*pc_addr), p2i(pc));
283   }
284   // Either the return address is the original one or we are going to
285   // patch in the same address that's already there.
286 
287   assert(!Continuation::is_return_barrier_entry(*pc_addr), "return barrier");
288 
289   assert(_pc == *pc_addr || pc == *pc_addr || *pc_addr == nullptr, "");
290   DEBUG_ONLY(address old_pc = _pc;)
291   *pc_addr = pc;
292   _pc = pc; // must be set before call to get_deopt_original_pc
293   address original_pc = get_deopt_original_pc();
294   if (original_pc != nullptr) {
295     assert(original_pc == old_pc, "expected original PC to be stored before patching");
296     _deopt_state = is_deoptimized;
297     _pc = original_pc;
298   } else {
299     _deopt_state = not_deoptimized;
300   }
301   assert(!is_compiled_frame() || !_cb->as_nmethod()->is_deopt_entry(_pc), "must be");
302 
303 #ifdef ASSERT
304   {
305     frame f(this->sp(), this->unextended_sp(), this->fp(), pc);
306     assert(f.is_deoptimized_frame() == this->is_deoptimized_frame() && f.pc() == this->pc() && f.raw_pc() == this->raw_pc(),
307       "must be (f.is_deoptimized_frame(): %d this->is_deoptimized_frame(): %d "
308       "f.pc(): " INTPTR_FORMAT " this->pc(): " INTPTR_FORMAT " f.raw_pc(): " INTPTR_FORMAT " this->raw_pc(): " INTPTR_FORMAT ")",
309       f.is_deoptimized_frame(), this->is_deoptimized_frame(), p2i(f.pc()), p2i(this->pc()), p2i(f.raw_pc()), p2i(this->raw_pc()));
310   }
311 #endif
312 }
313 
314 intptr_t* frame::entry_frame_argument_at(int offset) const {
315   // convert offset to index to deal with tsi
316   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
317   // Entry frame's arguments are always in relation to unextended_sp()
318   return &unextended_sp()[index];
319 }
320 
321 // locals
322 
323 void frame::interpreter_frame_set_locals(intptr_t* locs)  {
324   assert(is_interpreted_frame(), "interpreted frame expected");
325   // set relativized locals
326   ptr_at_put(interpreter_frame_locals_offset, (intptr_t) (locs - fp()));
327 }
328 
329 // sender_sp
330 
331 intptr_t* frame::interpreter_frame_sender_sp() const {
332   assert(is_interpreted_frame(), "interpreted frame expected");
333   return (intptr_t*) at(interpreter_frame_sender_sp_offset);
334 }
335 
336 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
337   assert(is_interpreted_frame(), "interpreted frame expected");
338   ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
339 }
340 
341 
342 // monitor elements
343 
344 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
345   return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
346 }
347 
348 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
349   BasicObjectLock* result = (BasicObjectLock*) at_relative(interpreter_frame_monitor_block_top_offset);
350   // make sure the pointer points inside the frame
351   assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
352   assert((intptr_t*) result < fp(),  "monitor end should be strictly below the frame pointer: result: " INTPTR_FORMAT " fp: " INTPTR_FORMAT, p2i(result), p2i(fp()));
353   return result;
354 }
355 
356 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
357   assert(is_interpreted_frame(), "interpreted frame expected");
358   // set relativized monitor_block_top
359   ptr_at_put(interpreter_frame_monitor_block_top_offset, (intptr_t*)value - fp());
360   assert(at_absolute(interpreter_frame_monitor_block_top_offset) <= interpreter_frame_monitor_block_top_offset, "");
361 }
362 
363 // Used by template based interpreter deoptimization
364 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
365   assert(is_interpreted_frame(), "interpreted frame expected");
366   // set relativized last_sp
367   ptr_at_put(interpreter_frame_last_sp_offset, sp != nullptr ? (sp - fp()) : 0);
368 }
369 
370 frame frame::sender_for_entry_frame(RegisterMap* map) const {
371   assert(map != nullptr, "map must be set");
372   // Java frame called from C; skip all C frames and return top C
373   // frame of that chunk as the sender
374   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
375   assert(!entry_frame_is_first(), "next Java fp must be non zero");
376   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
377   // Since we are walking the stack now this nested anchor is obviously walkable
378   // even if it wasn't when it was stacked.
379   jfa->make_walkable();
380   map->clear();
381   assert(map->include_argument_oops(), "should be set by clear");
382   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
383 
384   return fr;
385 }
386 
387 UpcallStub::FrameData* UpcallStub::frame_data_for_frame(const frame& frame) const {
388   assert(frame.is_upcall_stub_frame(), "wrong frame");
389   // need unextended_sp here, since normal sp is wrong for interpreter callees
390   return reinterpret_cast<UpcallStub::FrameData*>(
391     reinterpret_cast<address>(frame.unextended_sp()) + in_bytes(_frame_data_offset));
392 }
393 
394 bool frame::upcall_stub_frame_is_first() const {
395   assert(is_upcall_stub_frame(), "must be optimzed entry frame");
396   UpcallStub* blob = _cb->as_upcall_stub();
397   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
398   return jfa->last_Java_sp() == nullptr;
399 }
400 
401 frame frame::sender_for_upcall_stub_frame(RegisterMap* map) const {
402   assert(map != nullptr, "map must be set");
403   UpcallStub* blob = _cb->as_upcall_stub();
404   // Java frame called from C; skip all C frames and return top C
405   // frame of that chunk as the sender
406   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
407   assert(!upcall_stub_frame_is_first(), "must have a frame anchor to go back to");
408   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
409   // Since we are walking the stack now this nested anchor is obviously walkable
410   // even if it wasn't when it was stacked.
411   jfa->make_walkable();
412   map->clear();
413   assert(map->include_argument_oops(), "should be set by clear");
414   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
415 
416   return fr;
417 }
418 
419 #if defined(ASSERT)
420 static address get_register_address_in_stub(const frame& stub_fr, VMReg reg) {
421   RegisterMap map(nullptr,
422                   RegisterMap::UpdateMap::include,
423                   RegisterMap::ProcessFrames::skip,
424                   RegisterMap::WalkContinuation::skip);
425   stub_fr.oop_map()->update_register_map(&stub_fr, &map);
426   return map.location(reg, stub_fr.sp());
427 }
428 #endif
429 
430 JavaThread** frame::saved_thread_address(const frame& f) {
431   CodeBlob* cb = f.cb();
432   assert(cb != nullptr && cb->is_runtime_stub(), "invalid frame");
433 
434   JavaThread** thread_addr;
435 #ifdef COMPILER1
436   if (cb == Runtime1::blob_for(C1StubId::monitorenter_id) ||
437       cb == Runtime1::blob_for(C1StubId::monitorenter_nofpu_id)) {
438     thread_addr = (JavaThread**)(f.sp() + Runtime1::runtime_blob_current_thread_offset(f));
439   } else
440 #endif
441   {
442     // c2 only saves rbp in the stub frame so nothing to do.
443     thread_addr = nullptr;
444   }
445   assert(get_register_address_in_stub(f, SharedRuntime::thread_register()) == (address)thread_addr, "wrong thread address");
446   return thread_addr;
447 }
448 
449 //------------------------------------------------------------------------------
450 // frame::verify_deopt_original_pc
451 //
452 // Verifies the calculated original PC of a deoptimization PC for the
453 // given unextended SP.
454 #ifdef ASSERT
455 void frame::verify_deopt_original_pc(nmethod* nm, intptr_t* unextended_sp) {
456   frame fr;
457 
458   // This is ugly but it's better than to change {get,set}_original_pc
459   // to take an SP value as argument.  And it's only a debugging
460   // method anyway.
461   fr._unextended_sp = unextended_sp;
462 
463   address original_pc = nm->get_original_pc(&fr);
464   assert(nm->insts_contains_inclusive(original_pc),
465          "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());
466 }
467 #endif
468 
469 //------------------------------------------------------------------------------
470 // frame::adjust_unextended_sp
471 #ifdef ASSERT
472 void frame::adjust_unextended_sp() {
473   // On x86, sites calling method handle intrinsics and lambda forms are treated
474   // as any other call site. Therefore, no special action is needed when we are
475   // returning to any of these call sites.
476 
477   if (_cb != nullptr) {
478     nmethod* sender_nm = _cb->as_nmethod_or_null();
479     if (sender_nm != nullptr) {
480       // If the sender PC is a deoptimization point, get the original PC.
481       if (sender_nm->is_deopt_entry(_pc) ||
482           sender_nm->is_deopt_mh_entry(_pc)) {
483         verify_deopt_original_pc(sender_nm, _unextended_sp);
484       }
485     }
486   }
487 }
488 #endif
489 
490 //------------------------------------------------------------------------------
491 // frame::sender_for_interpreter_frame
492 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
493   // SP is the raw SP from the sender after adapter or interpreter
494   // extension.
495   intptr_t* sender_sp = this->sender_sp();
496 
497   // This is the sp before any possible extension (adapter/locals).
498   intptr_t* unextended_sp = interpreter_frame_sender_sp();
499   intptr_t* sender_fp = link();
500 
501 #if COMPILER2_OR_JVMCI
502   if (map->update_map()) {
503     update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
504   }
505 #endif // COMPILER2_OR_JVMCI
506 
507   address sender_pc = this->sender_pc();
508 
509   if (Continuation::is_return_barrier_entry(sender_pc)) {
510     if (map->walk_cont()) { // about to walk into an h-stack
511       return Continuation::top_frame(*this, map);
512     } else {
513       return Continuation::continuation_bottom_sender(map->thread(), *this, sender_sp);
514     }
515   }
516 
517   return frame(sender_sp, unextended_sp, sender_fp, sender_pc);
518 }
519 
520 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
521   assert(is_interpreted_frame(), "Not an interpreted frame");
522   // These are reasonable sanity checks
523   if (fp() == nullptr || (intptr_t(fp()) & (wordSize-1)) != 0) {
524     return false;
525   }
526   if (sp() == nullptr || (intptr_t(sp()) & (wordSize-1)) != 0) {
527     return false;
528   }
529   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
530     return false;
531   }
532   // These are hacks to keep us out of trouble.
533   // The problem with these is that they mask other problems
534   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
535     return false;
536   }
537 
538   // do some validation of frame elements
539   // first the method
540 
541   Method* m = safe_interpreter_frame_method();
542 
543   // validate the method we'd find in this potential sender
544   if (!Method::is_valid_method(m)) return false;
545 
546   // stack frames shouldn't be much larger than max_stack elements
547   // this test requires the use the unextended_sp which is the sp as seen by
548   // the current frame, and not sp which is the "raw" pc which could point
549   // further because of local variables of the callee method inserted after
550   // method arguments
551   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
552     return false;
553   }
554 
555   // validate bci/bcp
556 
557   address bcp = interpreter_frame_bcp();
558   if (m->validate_bci_from_bcp(bcp) < 0) {
559     return false;
560   }
561 
562   // validate ConstantPoolCache*
563   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
564   if (MetaspaceObj::is_valid(cp) == false) return false;
565 
566   // validate locals
567 
568   address locals =  (address)interpreter_frame_locals();
569   return thread->is_in_stack_range_incl(locals, (address)fp());
570 }
571 
572 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
573   assert(is_interpreted_frame(), "interpreted frame expected");
574   Method* method = interpreter_frame_method();
575   BasicType type = method->result_type();
576 
577   intptr_t* tos_addr;
578   if (method->is_native()) {
579     // Prior to calling into the runtime to report the method_exit the possible
580     // return value is pushed to the native stack. If the result is a jfloat/jdouble
581     // then ST0 is saved before EAX/EDX. See the note in generate_native_result
582     tos_addr = (intptr_t*)sp();
583     if (type == T_FLOAT || type == T_DOUBLE) {
584     // QQQ seems like this code is equivalent on the two platforms
585 #ifdef AMD64
586       // This is times two because we do a push(ltos) after pushing XMM0
587       // and that takes two interpreter stack slots.
588       tos_addr += 2 * Interpreter::stackElementWords;
589 #else
590       tos_addr += 2;
591 #endif // AMD64
592     }
593   } else {
594     tos_addr = (intptr_t*)interpreter_frame_tos_address();
595   }
596 
597   switch (type) {
598     case T_OBJECT  :
599     case T_ARRAY   : {
600       oop obj;
601       if (method->is_native()) {
602         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
603       } else {
604         oop* obj_p = (oop*)tos_addr;
605         obj = (obj_p == nullptr) ? (oop)nullptr : *obj_p;
606       }
607       assert(Universe::is_in_heap_or_null(obj), "sanity check");
608       *oop_result = obj;
609       break;
610     }
611     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
612     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
613     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
614     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
615     case T_INT     : value_result->i = *(jint*)tos_addr; break;
616     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
617     case T_FLOAT   : {
618 #ifdef AMD64
619         value_result->f = *(jfloat*)tos_addr;
620 #else
621       if (method->is_native()) {
622         jdouble d = *(jdouble*)tos_addr;  // Result was in ST0 so need to convert to jfloat
623         value_result->f = (jfloat)d;
624       } else {
625         value_result->f = *(jfloat*)tos_addr;
626       }
627 #endif // AMD64
628       break;
629     }
630     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
631     case T_VOID    : /* Nothing to do */ break;
632     default        : ShouldNotReachHere();
633   }
634 
635   return type;
636 }
637 
638 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
639   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
640   return &interpreter_frame_tos_address()[index];
641 }
642 
643 #ifndef PRODUCT
644 
645 #define DESCRIBE_FP_OFFSET(name) \
646   values.describe(frame_no, fp() + frame::name##_offset, #name, 1)
647 
648 void frame::describe_pd(FrameValues& values, int frame_no) {
649   if (is_interpreted_frame()) {
650     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
651     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
652     DESCRIBE_FP_OFFSET(interpreter_frame_method);
653     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
654     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
655     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
656     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
657     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
658     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
659 #ifdef AMD64
660   } else if (is_entry_frame()) {
661     // This could be more descriptive if we use the enum in
662     // stubGenerator to map to real names but it's most important to
663     // claim these frame slots so the error checking works.
664     for (int i = 0; i < entry_frame_after_call_words; i++) {
665       values.describe(frame_no, fp() - i, err_msg("call_stub word fp - %d", i));
666     }
667 #endif // AMD64
668   }
669 
670   if (is_java_frame() || Continuation::is_continuation_enterSpecial(*this)) {
671     intptr_t* ret_pc_loc;
672     intptr_t* fp_loc;
673     if (is_interpreted_frame()) {
674       ret_pc_loc = fp() + return_addr_offset;
675       fp_loc = fp();
676     } else {
677       ret_pc_loc = real_fp() - return_addr_offset;
678       fp_loc = real_fp() - sender_sp_offset;
679     }
680     address ret_pc = *(address*)ret_pc_loc;
681     values.describe(frame_no, ret_pc_loc,
682       Continuation::is_return_barrier_entry(ret_pc) ? "return address (return barrier)" : "return address");
683     values.describe(-1, fp_loc, "saved fp", 0); // "unowned" as value belongs to sender
684   }
685 }
686 
687 #endif // !PRODUCT
688 
689 intptr_t *frame::initial_deoptimization_info() {
690   // used to reset the saved FP
691   return fp();
692 }
693 
694 #ifndef PRODUCT
695 // This is a generic constructor which is only used by pns() in debug.cpp.
696 frame::frame(void* sp, void* fp, void* pc) {
697   init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
698 }
699 
700 #endif
701 
702 // Check for a method with scalarized inline type arguments that needs
703 // a stack repair and return the repaired sender stack pointer.
704 intptr_t* frame::repair_sender_sp(intptr_t* sender_sp, intptr_t** saved_fp_addr) const {
705   nmethod* nm = _cb->as_nmethod_or_null();
706   if (nm != nullptr && nm->needs_stack_repair()) {
707     // The stack increment resides just below the saved rbp on the stack
708     // and does not account for the return address.
709     intptr_t* real_frame_size_addr = (intptr_t*) (saved_fp_addr - 1);
710     int real_frame_size = ((*real_frame_size_addr) + wordSize) / wordSize;
711     assert(real_frame_size >= _cb->frame_size() && real_frame_size <= 1000000, "invalid frame size");
712     sender_sp = unextended_sp() + real_frame_size;
713   }
714   return sender_sp;
715 }
716 
717 void JavaFrameAnchor::make_walkable() {
718   // last frame set?
719   if (last_Java_sp() == nullptr) return;
720   // already walkable?
721   if (walkable()) return;
722   vmassert(last_Java_pc() == nullptr, "already walkable");
723   _last_Java_pc = (address)_last_Java_sp[-1];
724   vmassert(walkable(), "something went wrong");
725 }