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