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