1 /*
  2  * Copyright (c) 1997, 2021, 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   address   sp = (address)_sp;
 59   address   fp = (address)_fp;
 60   address   unextended_sp = (address)_unextended_sp;
 61 
 62   // consider stack guards when trying to determine "safe" stack pointers
 63   // sp must be within the usable part of the stack (not in guards)
 64   if (!thread->is_in_usable_stack(sp)) {
 65     return false;
 66   }
 67 
 68   // unextended sp must be within the stack and above or equal sp
 69   if (!thread->is_in_stack_range_incl(unextended_sp, sp)) {
 70     return false;
 71   }
 72 
 73   // an fp must be within the stack and above (but not equal) sp
 74   // second evaluation on fp+ is added to handle situation where fp is -1
 75   bool fp_safe = thread->is_in_stack_range_excl(fp, sp) &&
 76                  thread->is_in_full_stack_checked(fp + (return_addr_offset * sizeof(void*)));
 77 
 78   // We know sp/unextended_sp are safe only fp is questionable here
 79 
 80   // If the current frame is known to the code cache then we can attempt to
 81   // construct the sender and do some validation of it. This goes a long way
 82   // toward eliminating issues when we get in frame construction code
 83 
 84   if (_cb != NULL ) {
 85 
 86     // First check if frame is complete and tester is reliable
 87     // Unfortunately we can only check frame complete for runtime stubs and nmethod
 88     // other generic buffer blobs are more problematic so we just assume they are
 89     // ok. adapter blobs never have a frame complete and are never ok.
 90 
 91     if (!_cb->is_frame_complete_at(_pc)) {
 92       if (_cb->is_compiled() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
 93         return false;
 94       }
 95     }
 96 
 97     // Could just be some random pointer within the codeBlob
 98     if (!_cb->code_contains(_pc)) {
 99       return false;
100     }
101 
102     // Entry frame checks
103     if (is_entry_frame()) {
104       // an entry frame must have a valid fp.
105       return fp_safe && is_entry_frame_valid(thread);
106     } else if (is_optimized_entry_frame()) {
107       return fp_safe;
108     }
109 
110     intptr_t* sender_sp = NULL;
111     intptr_t* sender_unextended_sp = NULL;
112     address   sender_pc = NULL;
113     intptr_t* saved_fp =  NULL;
114 
115     if (is_interpreted_frame()) {
116       // fp must be safe
117       if (!fp_safe) {
118         return false;
119       }
120 
121       sender_pc = (address) this->fp()[return_addr_offset];
122       // for interpreted frames, the value below is the sender "raw" sp,
123       // which can be different from the sender unextended sp (the sp seen
124       // by the sender) because of current frame local variables
125       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
126       sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
127       saved_fp = (intptr_t*) this->fp()[link_offset];
128 
129     } else {
130       // must be some sort of compiled/runtime frame
131       // fp does not have to be safe (although it could be check for c1?)
132 
133       // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
134       if (_cb->frame_size() <= 0) {
135         return false;
136       }
137 
138       sender_sp = _unextended_sp + _cb->frame_size();
139       // Is sender_sp safe?
140       if (!thread->is_in_full_stack_checked((address)sender_sp)) {
141         return false;
142       }
143       sender_unextended_sp = sender_sp;
144       // On Intel the return_address is always the word on the stack
145       sender_pc = (address) *(sender_sp-1);
146       // Note: frame::sender_sp_offset is only valid for compiled frame
147       saved_fp = (intptr_t*) *(sender_sp - frame::sender_sp_offset);
148     }
149 
150     if (Continuation::is_return_barrier_entry(sender_pc)) {	
151       Continuation::fix_continuation_bottom_sender(thread, *this, &sender_pc, &sender_sp);	
152     }
153 
154     // If the potential sender is the interpreter then we can do some more checking
155     if (Interpreter::contains(sender_pc)) {
156 
157       // ebp is always saved in a recognizable place in any code we generate. However
158       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved ebp
159       // is really a frame pointer.
160 
161       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
162         return false;
163       }
164 
165       // construct the potential sender
166 
167       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
168 
169       return sender.is_interpreted_frame_valid(thread);
170 
171     }
172 
173     // We must always be able to find a recognizable pc
174     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
175     if (sender_pc == NULL ||  sender_blob == NULL) {
176       return false;
177     }
178 
179     // Could be a zombie method
180     if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
181       return false;
182     }
183 
184     // Could just be some random pointer within the codeBlob
185     if (!sender_blob->code_contains(sender_pc)) {
186       return false;
187     }
188 
189     // We should never be able to see an adapter if the current frame is something from code cache
190     if (sender_blob->is_adapter_blob()) {
191       return false;
192     }
193 
194     // Could be the call_stub
195     if (StubRoutines::returns_to_call_stub(sender_pc)) {
196       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
197         return false;
198       }
199 
200       // construct the potential sender
201 
202       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
203 
204       // Validate the JavaCallWrapper an entry frame must have
205       address jcw = (address)sender.entry_frame_call_wrapper();
206 
207       return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
208     } else if (sender_blob->is_optimized_entry_blob()) {
209       return false;
210     }
211 
212     CompiledMethod* nm = sender_blob->as_compiled_method_or_null();
213     if (nm != NULL) {
214         if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
215             nm->method()->is_method_handle_intrinsic()) {
216             return false;
217         }
218     }
219 
220     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
221     // because the return address counts against the callee's frame.
222 
223     if (sender_blob->frame_size() <= 0) {
224       assert(!sender_blob->is_compiled(), "should count return address at least");
225       return false;
226     }
227 
228     // We should never be able to see anything here except an nmethod. If something in the
229     // code cache (current frame) is called by an entity within the code cache that entity
230     // should not be anything but the call stub (already covered), the interpreter (already covered)
231     // or an nmethod.
232 
233     if (!sender_blob->is_compiled()) {
234         return false;
235     }
236 
237     // Could put some more validation for the potential non-interpreted sender
238     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
239 
240     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
241 
242     // We've validated the potential sender that would be created
243     return true;
244   }
245 
246   // Must be native-compiled frame. Since sender will try and use fp to find
247   // linkages it must be safe
248 
249   if (!fp_safe) {
250     return false;
251   }
252 
253   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
254 
255   if ( (address) this->fp()[return_addr_offset] == NULL) return false;
256 
257 
258   // could try and do some more potential verification of native frame if we could think of some...
259 
260   return true;
261 
262 }
263 
264 
265 void frame::patch_pc(Thread* thread, address pc) {
266   assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
267   address* pc_addr = &(((address*) sp())[-1]);
268 
269   if (TracePcPatching) {
270     tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
271                   p2i(pc_addr), p2i(*pc_addr), p2i(pc));
272   }
273   // Either the return address is the original one or we are going to
274   // patch in the same address that's already there.
275 
276   assert(!Continuation::is_return_barrier_entry(*pc_addr), "return barrier");
277 
278   assert(_pc == *pc_addr || pc == *pc_addr || *pc_addr == 0, "must be (pc: " INTPTR_FORMAT " _pc: " INTPTR_FORMAT " pc_addr: " INTPTR_FORMAT " *pc_addr: " INTPTR_FORMAT  " sp: " INTPTR_FORMAT ")", p2i(pc), p2i(_pc), p2i(pc_addr), p2i(*pc_addr), p2i(sp()));
279   DEBUG_ONLY(address old_pc = _pc;)
280   *pc_addr = pc;
281   _pc = pc; // must be set before call to get_deopt_original_pc
282   address original_pc = CompiledMethod::get_deopt_original_pc(this);
283   if (original_pc != NULL) {
284     assert(original_pc == old_pc, "expected original PC to be stored before patching");
285     _deopt_state = is_deoptimized;
286     _pc = original_pc;
287   } else {
288     _deopt_state = not_deoptimized;
289   }
290   assert (!is_compiled_frame() || !_cb->as_compiled_method()->is_deopt_entry(_pc), "must be");
291 
292 #ifdef ASSERT
293   {
294     frame f(this->sp(), this->unextended_sp(), this->fp(), pc);
295     assert(f.is_deoptimized_frame() == this->is_deoptimized_frame() && f.pc() == this->pc() && f.raw_pc() == this->raw_pc(), 
296       "must be (f.is_deoptimized_frame(): %d this->is_deoptimized_frame(): %d "
297       "f.pc(): " INTPTR_FORMAT " this->pc(): " INTPTR_FORMAT " f.raw_pc(): " INTPTR_FORMAT " this->raw_pc(): " INTPTR_FORMAT ")", 
298       f.is_deoptimized_frame(), this->is_deoptimized_frame(), p2i(f.pc()), p2i(this->pc()), p2i(f.raw_pc()), p2i(this->raw_pc()));
299   }
300 #endif
301 }
302 
303 intptr_t* frame::entry_frame_argument_at(int offset) const {
304   // convert offset to index to deal with tsi
305   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
306   // Entry frame's arguments are always in relation to unextended_sp()
307   return &unextended_sp()[index];
308 }
309 
310 // sender_sp
311 
312 intptr_t* frame::interpreter_frame_sender_sp() const {
313   assert(is_interpreted_frame(), "interpreted frame expected");
314   return (intptr_t*) at(interpreter_frame_sender_sp_offset);
315 }
316 
317 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
318   assert(is_interpreted_frame(), "interpreted frame expected");
319   ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
320 }
321 
322 
323 // monitor elements
324 
325 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
326   return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
327 }
328 
329 template BasicObjectLock* frame::interpreter_frame_monitor_end<true>() const;
330 template BasicObjectLock* frame::interpreter_frame_monitor_end<false>() const;
331 
332 template <bool relative>
333 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
334   BasicObjectLock* result = (BasicObjectLock*) at<relative>(interpreter_frame_monitor_block_top_offset);
335   // make sure the pointer points inside the frame
336   assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
337   assert((intptr_t*) result < fp(),  "monitor end should be strictly below the frame pointer: result: " INTPTR_FORMAT " fp: " INTPTR_FORMAT, p2i(result), p2i(fp()));
338   return result;
339 }
340 
341 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
342   *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
343 }
344 
345 // Used by template based interpreter deoptimization
346 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
347     *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
348 }
349 
350 frame frame::sender_for_entry_frame(RegisterMap* map) const {
351   assert(map != NULL, "map must be set");
352   // Java frame called from C; skip all C frames and return top C
353   // frame of that chunk as the sender
354   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
355   assert(!entry_frame_is_first(), "next Java fp must be non zero");
356   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
357   // Since we are walking the stack now this nested anchor is obviously walkable
358   // even if it wasn't when it was stacked.
359   if (!jfa->walkable()) {
360     // Capture _last_Java_pc (if needed) and mark anchor walkable.
361     jfa->capture_last_Java_pc();
362   }
363   map->clear();
364   assert(map->include_argument_oops(), "should be set by clear");
365   vmassert(jfa->last_Java_pc() != NULL, "not walkable");
366   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
367 
368   return fr;
369 }
370 
371 OptimizedEntryBlob::FrameData* OptimizedEntryBlob::frame_data_for_frame(const frame& frame) const {
372   assert(frame.is_optimized_entry_frame(), "wrong frame");
373   // need unextended_sp here, since normal sp is wrong for interpreter callees
374   return reinterpret_cast<OptimizedEntryBlob::FrameData*>(
375     reinterpret_cast<char*>(frame.unextended_sp()) + in_bytes(_frame_data_offset));
376 }
377 
378 bool frame::optimized_entry_frame_is_first() const {
379   assert(is_optimized_entry_frame(), "must be optimzed entry frame");
380   OptimizedEntryBlob* blob = _cb->as_optimized_entry_blob();
381   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
382   return jfa->last_Java_sp() == NULL;
383 }
384 
385 frame frame::sender_for_optimized_entry_frame(RegisterMap* map) const {
386   assert(map != NULL, "map must be set");
387   OptimizedEntryBlob* blob = _cb->as_optimized_entry_blob();
388   // Java frame called from C; skip all C frames and return top C
389   // frame of that chunk as the sender
390   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
391   assert(!optimized_entry_frame_is_first(), "must have a frame anchor to go back to");
392   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
393   // Since we are walking the stack now this nested anchor is obviously walkable
394   // even if it wasn't when it was stacked.
395   if (!jfa->walkable()) {
396     // Capture _last_Java_pc (if needed) and mark anchor walkable.
397     jfa->capture_last_Java_pc();
398   }
399   map->clear();
400   assert(map->include_argument_oops(), "should be set by clear");
401   vmassert(jfa->last_Java_pc() != NULL, "not walkable");
402   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
403 
404   return fr;
405 }
406 
407 //------------------------------------------------------------------------------
408 // frame::verify_deopt_original_pc
409 //
410 // Verifies the calculated original PC of a deoptimization PC for the
411 // given unextended SP.
412 #ifdef ASSERT
413 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
414   frame fr;
415 
416   // This is ugly but it's better than to change {get,set}_original_pc
417   // to take an SP value as argument.  And it's only a debugging
418   // method anyway.
419   fr._unextended_sp = unextended_sp;
420 
421   address original_pc = nm->get_original_pc(&fr);
422   assert(nm->insts_contains_inclusive(original_pc),
423          "original PC must be in the main code section of the 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());
424 }
425 #endif
426 
427 //------------------------------------------------------------------------------
428 // frame::adjust_unextended_sp
429 #ifdef ASSERT
430 void frame::adjust_unextended_sp() {
431   // On x86, sites calling method handle intrinsics and lambda forms are treated
432   // as any other call site. Therefore, no special action is needed when we are
433   // returning to any of these call sites.
434 
435   if (_cb != NULL) {
436     CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
437     if (sender_cm != NULL) {
438       // If the sender PC is a deoptimization point, get the original PC.
439       if (sender_cm->is_deopt_entry(_pc) ||
440           sender_cm->is_deopt_mh_entry(_pc)) {
441         verify_deopt_original_pc(sender_cm, _unextended_sp);
442       }
443     }
444   }
445 }
446 #endif
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       Continuation::fix_continuation_bottom_sender(map->thread(), *this, &sender_pc, &unextended_sp);
472     }
473   }
474 
475   return frame(sender_sp, unextended_sp, sender_fp, sender_pc);
476 }
477 
478 
479 //------------------------------------------------------------------------------
480 // frame::sender
481 
482 frame frame::sender(RegisterMap* map) const {
483   frame result = sender_raw(map);
484 
485   if (map->process_frames() && !map->in_cont()) {
486     StackWatermarkSet::on_iteration(map->thread(), result);
487   }
488 
489   return result;
490 }
491 
492 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
493   assert(is_interpreted_frame(), "Not an interpreted frame");
494   // These are reasonable sanity checks
495   if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
496     return false;
497   }
498   if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
499     return false;
500   }
501   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
502     return false;
503   }
504   // These are hacks to keep us out of trouble.
505   // The problem with these is that they mask other problems
506   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
507     return false;
508   }
509 
510   // do some validation of frame elements
511   // first the method
512 
513   Method* m = *interpreter_frame_method_addr();
514 
515   // validate the method we'd find in this potential sender
516   if (!Method::is_valid_method(m)) return false;
517 
518   // stack frames shouldn't be much larger than max_stack elements
519   // this test requires the use the unextended_sp which is the sp as seen by
520   // the current frame, and not sp which is the "raw" pc which could point
521   // further because of local variables of the callee method inserted after
522   // method arguments
523   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
524     return false;
525   }
526 
527   // validate bci/bcp
528 
529   address bcp = interpreter_frame_bcp();
530   if (m->validate_bci_from_bcp(bcp) < 0) {
531     return false;
532   }
533 
534   // validate ConstantPoolCache*
535   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
536   if (MetaspaceObj::is_valid(cp) == false) return false;
537 
538   // validate locals
539 
540   address locals =  (address) *interpreter_frame_locals_addr();
541   return thread->is_in_stack_range_incl(locals, (address)fp());
542 }
543 
544 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
545   assert(is_interpreted_frame(), "interpreted frame expected");
546   Method* method = interpreter_frame_method();
547   BasicType type = method->result_type();
548 
549   intptr_t* tos_addr;
550   if (method->is_native()) {
551     // Prior to calling into the runtime to report the method_exit the possible
552     // return value is pushed to the native stack. If the result is a jfloat/jdouble
553     // then ST0 is saved before EAX/EDX. See the note in generate_native_result
554     tos_addr = (intptr_t*)sp();
555     if (type == T_FLOAT || type == T_DOUBLE) {
556     // QQQ seems like this code is equivalent on the two platforms
557 #ifdef AMD64
558       // This is times two because we do a push(ltos) after pushing XMM0
559       // and that takes two interpreter stack slots.
560       tos_addr += 2 * Interpreter::stackElementWords;
561 #else
562       tos_addr += 2;
563 #endif // AMD64
564     }
565   } else {
566     tos_addr = (intptr_t*)interpreter_frame_tos_address();
567   }
568 
569   switch (type) {
570     case T_OBJECT  :
571     case T_ARRAY   : {
572       oop obj;
573       if (method->is_native()) {
574         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
575       } else {
576         oop* obj_p = (oop*)tos_addr;
577         obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
578       }
579       assert(Universe::is_in_heap_or_null(obj), "sanity check");
580       *oop_result = obj;
581       break;
582     }
583     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
584     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
585     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
586     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
587     case T_INT     : value_result->i = *(jint*)tos_addr; break;
588     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
589     case T_FLOAT   : {
590 #ifdef AMD64
591         value_result->f = *(jfloat*)tos_addr;
592 #else
593       if (method->is_native()) {
594         jdouble d = *(jdouble*)tos_addr;  // Result was in ST0 so need to convert to jfloat
595         value_result->f = (jfloat)d;
596       } else {
597         value_result->f = *(jfloat*)tos_addr;
598       }
599 #endif // AMD64
600       break;
601     }
602     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
603     case T_VOID    : /* Nothing to do */ break;
604     default        : ShouldNotReachHere();
605   }
606 
607   return type;
608 }
609 
610 template intptr_t* frame::interpreter_frame_tos_at<false>(jint offset) const;
611 template intptr_t* frame::interpreter_frame_tos_at<true >(jint offset) const;
612 
613 template <bool relative>
614 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
615   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
616   return &interpreter_frame_tos_address<relative>()[index];
617 }
618 
619 #ifndef PRODUCT
620 
621 #define DESCRIBE_FP_OFFSET(name) \
622   values.describe(frame_no, fp() + frame::name##_offset, #name, 1)
623 
624 void frame::describe_pd(FrameValues& values, int frame_no) {
625   if (is_interpreted_frame()) {
626     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
627     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
628     DESCRIBE_FP_OFFSET(interpreter_frame_method);
629     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
630     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
631     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
632     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
633     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
634     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
635 #ifdef AMD64
636   } else if (is_entry_frame()) {
637     // This could be more descriptive if we use the enum in
638     // stubGenerator to map to real names but it's most important to
639     // claim these frame slots so the error checking works.
640     for (int i = 0; i < entry_frame_after_call_words; i++) {
641       values.describe(frame_no, fp() - i, err_msg("call_stub word fp - %d", i));
642     }
643 #endif // AMD64
644   }
645 
646   if (is_java_frame()) {
647     address ret_pc = *(address*)(real_fp() - return_addr_offset);
648     values.describe(frame_no, real_fp() - return_addr_offset, Continuation::is_return_barrier_entry(ret_pc) ? "return address (return barrier)" : "return address");
649     values.describe(-1, real_fp() - sender_sp_offset, "saved fp", 2);
650   }
651 }
652 
653 void frame::describe_top_pd(FrameValues& values) {
654   address ret_pc_callee = *(address*)(sp() - return_addr_offset);
655   values.describe(-1, sp() - return_addr_offset, Continuation::is_return_barrier_entry(ret_pc_callee) ? "return address (return barrier)" : "return address");
656   values.describe(-1, sp() - sender_sp_offset, "saved fp", 2);
657 }
658 #endif // !PRODUCT
659 
660 intptr_t *frame::initial_deoptimization_info() {
661   // used to reset the saved FP
662   return fp();
663 }
664 
665 #ifndef PRODUCT
666 // This is a generic constructor which is only used by pns() in debug.cpp.
667 frame::frame(void* sp, void* fp, void* pc) {
668   init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
669 }
670 
671 #endif
672 
673 void JavaFrameAnchor::make_walkable(JavaThread* thread) {
674   // last frame set?
675   if (last_Java_sp() == NULL) return;
676   // already walkable?
677   if (walkable()) return;
678   vmassert(Thread::current() == (Thread*)thread, "not current thread");
679   vmassert(last_Java_sp() != NULL, "not called from Java code?");
680   vmassert(last_Java_pc() == NULL, "already walkable");
681   capture_last_Java_pc();
682   vmassert(walkable(), "something went wrong");
683 }
684 
685 void JavaFrameAnchor::capture_last_Java_pc() {
686   vmassert(_last_Java_sp != NULL, "no last frame set");
687   vmassert(_last_Java_pc == NULL, "already walkable");
688   _last_Java_pc = (address)_last_Java_sp[-1];
689 }