1 /* 2 * Copyright (c) 2008, 2018, 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 "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 "runtime/frame.inline.hpp" 33 #include "runtime/handles.inline.hpp" 34 #include "runtime/javaCalls.hpp" 35 #include "runtime/monitorChunk.hpp" 36 #include "runtime/os.inline.hpp" 37 #include "runtime/signature.hpp" 38 #include "runtime/stubCodeGenerator.hpp" 39 #include "runtime/stubRoutines.hpp" 40 #include "vmreg_arm.inline.hpp" 41 #ifdef COMPILER1 42 #include "c1/c1_Runtime1.hpp" 43 #include "runtime/vframeArray.hpp" 44 #endif 45 #include "prims/methodHandles.hpp" 46 47 #ifdef ASSERT 48 void RegisterMap::check_location_valid() { 49 } 50 #endif 51 52 53 // Profiling/safepoint support 54 55 bool frame::safe_for_sender(JavaThread *thread) { 56 address sp = (address)_sp; 57 address fp = (address)_fp; 58 address unextended_sp = (address)_unextended_sp; 59 60 static size_t stack_guard_size = os::uses_stack_guard_pages() ? 61 (JavaThread::stack_red_zone_size() + JavaThread::stack_yellow_zone_size()) : 0; 62 size_t usable_stack_size = thread->stack_size() - stack_guard_size; 63 64 // sp must be within the usable part of the stack (not in guards) 65 bool sp_safe = (sp != NULL && 66 (sp <= thread->stack_base()) && 67 (sp >= thread->stack_base() - usable_stack_size)); 68 69 if (!sp_safe) { 70 return false; 71 } 72 73 bool unextended_sp_safe = (unextended_sp != NULL && 74 (unextended_sp <= thread->stack_base()) && 75 (unextended_sp >= sp)); 76 if (!unextended_sp_safe) { 77 return false; 78 } 79 80 // We know sp/unextended_sp are safe. Only fp is questionable here. 81 82 bool fp_safe = (fp != NULL && 83 (fp <= thread->stack_base()) && 84 fp >= sp); 85 86 if (_cb != NULL ) { 87 88 // First check if frame is complete and tester is reliable 89 // Unfortunately we can only check frame complete for runtime stubs and nmethod 90 // other generic buffer blobs are more problematic so we just assume they are 91 // ok. adapter blobs never have a frame complete and are never ok. 92 93 if (!_cb->is_frame_complete_at(_pc)) { 94 if (_cb->is_compiled() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) { 95 return false; 96 } 97 } 98 99 // Could just be some random pointer within the codeBlob 100 if (!_cb->code_contains(_pc)) { 101 return false; 102 } 103 104 // Entry frame checks 105 if (is_entry_frame()) { 106 // an entry frame must have a valid fp. 107 return fp_safe && is_entry_frame_valid(thread); 108 } 109 110 intptr_t* sender_sp = NULL; 111 address sender_pc = NULL; 112 113 if (is_interpreted_frame()) { 114 // fp must be safe 115 if (!fp_safe) { 116 return false; 117 } 118 119 sender_pc = (address) this->fp()[return_addr_offset]; 120 sender_sp = (intptr_t*) addr_at(sender_sp_offset); 121 122 } else { 123 // must be some sort of compiled/runtime frame 124 // fp does not have to be safe (although it could be check for c1?) 125 126 sender_sp = _unextended_sp + _cb->frame_size(); 127 // Is sender_sp safe? 128 if ((address)sender_sp >= thread->stack_base()) { 129 return false; 130 } 131 // With our calling conventions, the return_address should 132 // end up being the word on the stack 133 sender_pc = (address) *(sender_sp - sender_sp_offset + return_addr_offset); 134 } 135 136 // We must always be able to find a recognizable pc 137 CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc); 138 if (sender_pc == NULL || sender_blob == NULL) { 139 return false; 140 } 141 142 143 // If the potential sender is the interpreter then we can do some more checking 144 if (Interpreter::contains(sender_pc)) { 145 146 // FP is always saved in a recognizable place in any code we generate. However 147 // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved FP 148 // is really a frame pointer. 149 150 intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset + link_offset); 151 bool saved_fp_safe = ((address)saved_fp <= thread->stack_base()) && (saved_fp > sender_sp); 152 153 if (!saved_fp_safe) { 154 return false; 155 } 156 157 // construct the potential sender 158 159 frame sender(sender_sp, saved_fp, sender_pc); 160 161 return sender.is_interpreted_frame_valid(thread); 162 } 163 164 if (sender_blob->is_zombie() || sender_blob->is_unloaded()) { 165 return false; 166 } 167 168 // Could just be some random pointer within the codeBlob 169 if (!sender_blob->code_contains(sender_pc)) { 170 return false; 171 } 172 173 // We should never be able to see an adapter if the current frame is something from code cache 174 if (sender_blob->is_adapter_blob()) { 175 return false; 176 } 177 178 // Could be the call_stub 179 if (StubRoutines::returns_to_call_stub(sender_pc)) { 180 intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset + link_offset); 181 bool saved_fp_safe = ((address)saved_fp <= thread->stack_base()) && (saved_fp >= sender_sp); 182 183 if (!saved_fp_safe) { 184 return false; 185 } 186 187 // construct the potential sender 188 189 frame sender(sender_sp, saved_fp, sender_pc); 190 191 // Validate the JavaCallWrapper an entry frame must have 192 address jcw = (address)sender.entry_frame_call_wrapper(); 193 194 bool jcw_safe = (jcw <= thread->stack_base()) && (jcw > (address)sender.fp()); 195 196 return jcw_safe; 197 } 198 199 // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size 200 // because the return address counts against the callee's frame. 201 202 if (sender_blob->frame_size() <= 0) { 203 assert(!sender_blob->is_compiled(), "should count return address at least"); 204 return false; 205 } 206 207 // We should never be able to see anything here except an nmethod. If something in the 208 // code cache (current frame) is called by an entity within the code cache that entity 209 // should not be anything but the call stub (already covered), the interpreter (already covered) 210 // or an nmethod. 211 212 if (!sender_blob->is_compiled()) { 213 return false; 214 } 215 216 // Could put some more validation for the potential non-interpreted sender 217 // frame we'd create by calling sender if I could think of any. Wait for next crash in forte... 218 219 // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb 220 221 // We've validated the potential sender that would be created 222 return true; 223 } 224 225 // Must be native-compiled frame. Since sender will try and use fp to find 226 // linkages it must be safe 227 228 if (!fp_safe) { 229 return false; 230 } 231 232 // Will the pc we fetch be non-zero (which we'll find at the oldest frame) 233 234 if ((address) this->fp()[return_addr_offset] == NULL) return false; 235 236 237 // could try and do some more potential verification of native frame if we could think of some... 238 239 return true; 240 } 241 242 243 void frame::patch_pc(Thread* thread, address pc) { 244 address* pc_addr = &((address *)sp())[-sender_sp_offset+return_addr_offset]; 245 if (TracePcPatching) { 246 tty->print_cr("patch_pc at address" INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "] ", 247 p2i(pc_addr), p2i(*pc_addr), p2i(pc)); 248 } 249 *pc_addr = pc; 250 _cb = CodeCache::find_blob(pc); 251 address original_pc = CompiledMethod::get_deopt_original_pc(this); 252 if (original_pc != NULL) { 253 assert(original_pc == _pc, "expected original PC to be stored before patching"); 254 _deopt_state = is_deoptimized; 255 // leave _pc as is 256 } else { 257 _deopt_state = not_deoptimized; 258 _pc = pc; 259 } 260 } 261 262 bool frame::is_interpreted_frame() const { 263 return Interpreter::contains(pc()); 264 } 265 266 int frame::frame_size(RegisterMap* map) const { 267 frame sender = this->sender(map); 268 return sender.sp() - sp(); 269 } 270 271 intptr_t* frame::entry_frame_argument_at(int offset) const { 272 assert(is_entry_frame(), "entry frame expected"); 273 // convert offset to index to deal with tsi 274 int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize); 275 // Entry frame's arguments are always in relation to unextended_sp() 276 return &unextended_sp()[index]; 277 } 278 279 // sender_sp 280 intptr_t* frame::interpreter_frame_sender_sp() const { 281 assert(is_interpreted_frame(), "interpreted frame expected"); 282 return (intptr_t*) at(interpreter_frame_sender_sp_offset); 283 } 284 285 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) { 286 assert(is_interpreted_frame(), "interpreted frame expected"); 287 ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp); 288 } 289 290 291 // monitor elements 292 293 BasicObjectLock* frame::interpreter_frame_monitor_begin() const { 294 return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset); 295 } 296 297 BasicObjectLock* frame::interpreter_frame_monitor_end() const { 298 BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset); 299 // make sure the pointer points inside the frame 300 assert((intptr_t) fp() > (intptr_t) result, "result must < than frame pointer"); 301 assert((intptr_t) sp() <= (intptr_t) result, "result must >= than stack pointer"); 302 return result; 303 } 304 305 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) { 306 *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value; 307 } 308 309 310 // Used by template based interpreter deoptimization 311 void frame::interpreter_frame_set_last_sp(intptr_t* sp) { 312 *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp; 313 } 314 315 316 frame frame::sender_for_entry_frame(RegisterMap* map) const { 317 assert(map != NULL, "map must be set"); 318 // Java frame called from C; skip all C frames and return top C 319 // frame of that chunk as the sender 320 JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor(); 321 assert(!entry_frame_is_first(), "next Java fp must be non zero"); 322 assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack"); 323 map->clear(); 324 assert(map->include_argument_oops(), "should be set by clear"); 325 if (jfa->last_Java_pc() != NULL) { 326 frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc()); 327 return fr; 328 } 329 frame fr(jfa->last_Java_sp(), jfa->last_Java_fp()); 330 return fr; 331 } 332 333 //------------------------------------------------------------------------------ 334 // frame::verify_deopt_original_pc 335 // 336 // Verifies the calculated original PC of a deoptimization PC for the 337 // given unextended SP. The unextended SP might also be the saved SP 338 // for MethodHandle call sites. 339 #ifdef ASSERT 340 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp, bool is_method_handle_return) { 341 frame fr; 342 343 // This is ugly but it's better than to change {get,set}_original_pc 344 // to take an SP value as argument. And it's only a debugging 345 // method anyway. 346 fr._unextended_sp = unextended_sp; 347 348 address original_pc = nm->get_original_pc(&fr); 349 assert(nm->insts_contains_inclusive(original_pc), 350 "original PC must be in the main code section of the the compiled method (or must be immediately following it)"); 351 assert(nm->is_method_handle_return(original_pc) == is_method_handle_return, "must be"); 352 } 353 #endif 354 355 //------------------------------------------------------------------------------ 356 // frame::adjust_unextended_sp 357 void frame::adjust_unextended_sp() { 358 // same as on x86 359 360 // If we are returning to a compiled MethodHandle call site, the 361 // saved_fp will in fact be a saved value of the unextended SP. The 362 // simplest way to tell whether we are returning to such a call site 363 // is as follows: 364 365 CompiledMethod* sender_cm = (_cb == NULL) ? NULL : _cb->as_compiled_method_or_null(); 366 if (sender_cm != NULL) { 367 // If the sender PC is a deoptimization point, get the original 368 // PC. For MethodHandle call site the unextended_sp is stored in 369 // saved_fp. 370 if (sender_cm->is_deopt_mh_entry(_pc)) { 371 DEBUG_ONLY(verify_deopt_mh_original_pc(sender_cm, _fp)); 372 _unextended_sp = _fp; 373 } 374 else if (sender_cm->is_deopt_entry(_pc)) { 375 DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp)); 376 } 377 else if (sender_cm->is_method_handle_return(_pc)) { 378 _unextended_sp = _fp; 379 } 380 } 381 } 382 383 //------------------------------------------------------------------------------ 384 // frame::update_map_with_saved_link 385 void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) { 386 // see x86 for comments 387 map->set_location(FP->as_VMReg(), (address) link_addr); 388 } 389 390 frame frame::sender_for_interpreter_frame(RegisterMap* map) const { 391 // SP is the raw SP from the sender after adapter or interpreter 392 // extension. 393 intptr_t* sender_sp = this->sender_sp(); 394 395 // This is the sp before any possible extension (adapter/locals). 396 intptr_t* unextended_sp = interpreter_frame_sender_sp(); 397 398 #ifdef COMPILER2 399 if (map->update_map()) { 400 update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset)); 401 } 402 #endif // COMPILER2 403 404 return frame(sender_sp, unextended_sp, link(), sender_pc()); 405 } 406 407 frame frame::sender_for_compiled_frame(RegisterMap* map) const { 408 assert(map != NULL, "map must be set"); 409 410 // frame owned by optimizing compiler 411 assert(_cb->frame_size() >= 0, "must have non-zero frame size"); 412 intptr_t* sender_sp = unextended_sp() + _cb->frame_size(); 413 intptr_t* unextended_sp = sender_sp; 414 415 address sender_pc = (address) *(sender_sp - sender_sp_offset + return_addr_offset); 416 417 // This is the saved value of FP which may or may not really be an FP. 418 // It is only an FP if the sender is an interpreter frame (or C1?). 419 intptr_t** saved_fp_addr = (intptr_t**) (sender_sp - sender_sp_offset + link_offset); 420 421 if (map->update_map()) { 422 // Tell GC to use argument oopmaps for some runtime stubs that need it. 423 // For C1, the runtime stub might not have oop maps, so set this flag 424 // outside of update_register_map. 425 map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread())); 426 if (_cb->oop_maps() != NULL) { 427 OopMapSet::update_register_map(this, map); 428 } 429 430 // Since the prolog does the save and restore of FP there is no oopmap 431 // for it so we must fill in its location as if there was an oopmap entry 432 // since if our caller was compiled code there could be live jvm state in it. 433 update_map_with_saved_link(map, saved_fp_addr); 434 } 435 436 assert(sender_sp != sp(), "must have changed"); 437 return frame(sender_sp, unextended_sp, *saved_fp_addr, sender_pc); 438 } 439 440 frame frame::sender(RegisterMap* map) const { 441 // Default is we done have to follow them. The sender_for_xxx will 442 // update it accordingly 443 map->set_include_argument_oops(false); 444 445 if (is_entry_frame()) return sender_for_entry_frame(map); 446 if (is_interpreted_frame()) return sender_for_interpreter_frame(map); 447 assert(_cb == CodeCache::find_blob(pc()),"Must be the same"); 448 449 if (_cb != NULL) { 450 return sender_for_compiled_frame(map); 451 } 452 453 assert(false, "should not be called for a C frame"); 454 return frame(); 455 } 456 457 bool frame::is_interpreted_frame_valid(JavaThread* thread) const { 458 assert(is_interpreted_frame(), "Not an interpreted frame"); 459 // These are reasonable sanity checks 460 if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) { 461 return false; 462 } 463 if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) { 464 return false; 465 } 466 if (fp() + interpreter_frame_initial_sp_offset < sp()) { 467 return false; 468 } 469 // These are hacks to keep us out of trouble. 470 // The problem with these is that they mask other problems 471 if (fp() <= sp()) { // this attempts to deal with unsigned comparison above 472 return false; 473 } 474 // do some validation of frame elements 475 476 // first the method 477 478 Method* m = *interpreter_frame_method_addr(); 479 480 // validate the method we'd find in this potential sender 481 if (!Method::is_valid_method(m)) return false; 482 483 // stack frames shouldn't be much larger than max_stack elements 484 485 if (fp() - sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) { 486 return false; 487 } 488 489 // validate bci/bcp 490 491 address bcp = interpreter_frame_bcp(); 492 if (m->validate_bci_from_bcp(bcp) < 0) { 493 return false; 494 } 495 496 // validate ConstantPoolCache* 497 ConstantPoolCache* cp = *interpreter_frame_cache_addr(); 498 if (MetaspaceObj::is_valid(cp) == false) return false; 499 500 // validate locals 501 502 address locals = (address) *interpreter_frame_locals_addr(); 503 504 if (locals > thread->stack_base() || locals < (address) fp()) return false; 505 506 // We'd have to be pretty unlucky to be mislead at this point 507 508 return true; 509 } 510 511 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) { 512 assert(is_interpreted_frame(), "interpreted frame expected"); 513 Method* method = interpreter_frame_method(); 514 BasicType type = method->result_type(); 515 516 intptr_t* res_addr; 517 if (method->is_native()) { 518 // Prior to calling into the runtime to report the method_exit both of 519 // the possible return value registers are saved. 520 // Return value registers are pushed to the native stack 521 res_addr = (intptr_t*)sp(); 522 #ifdef __ABI_HARD__ 523 // FP result is pushed onto a stack along with integer result registers 524 if (type == T_FLOAT || type == T_DOUBLE) { 525 res_addr += 2; 526 } 527 #endif // __ABI_HARD__ 528 } else { 529 res_addr = (intptr_t*)interpreter_frame_tos_address(); 530 } 531 532 switch (type) { 533 case T_OBJECT : 534 case T_ARRAY : { 535 oop obj; 536 if (method->is_native()) { 537 obj = cast_to_oop(at(interpreter_frame_oop_temp_offset)); 538 } else { 539 obj = *(oop*)res_addr; 540 } 541 assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check"); 542 *oop_result = obj; 543 break; 544 } 545 case T_BOOLEAN : value_result->z = *(jboolean*)res_addr; break; 546 case T_BYTE : value_result->b = *(jbyte*)res_addr; break; 547 case T_CHAR : value_result->c = *(jchar*)res_addr; break; 548 case T_SHORT : value_result->s = *(jshort*)res_addr; break; 549 case T_INT : value_result->i = *(jint*)res_addr; break; 550 case T_LONG : value_result->j = *(jlong*)res_addr; break; 551 case T_FLOAT : value_result->f = *(jfloat*)res_addr; break; 552 case T_DOUBLE : value_result->d = *(jdouble*)res_addr; break; 553 case T_VOID : /* Nothing to do */ break; 554 default : ShouldNotReachHere(); 555 } 556 557 return type; 558 } 559 560 561 intptr_t* frame::interpreter_frame_tos_at(jint offset) const { 562 int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize); 563 return &interpreter_frame_tos_address()[index]; 564 } 565 566 #ifndef PRODUCT 567 568 #define DESCRIBE_FP_OFFSET(name) \ 569 values.describe(frame_no, fp() + frame::name##_offset, #name) 570 571 void frame::describe_pd(FrameValues& values, int frame_no) { 572 if (is_interpreted_frame()) { 573 DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp); 574 DESCRIBE_FP_OFFSET(interpreter_frame_last_sp); 575 DESCRIBE_FP_OFFSET(interpreter_frame_method); 576 DESCRIBE_FP_OFFSET(interpreter_frame_mdp); 577 DESCRIBE_FP_OFFSET(interpreter_frame_cache); 578 DESCRIBE_FP_OFFSET(interpreter_frame_locals); 579 DESCRIBE_FP_OFFSET(interpreter_frame_bcp); 580 DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp); 581 } 582 } 583 584 // This is a generic constructor which is only used by pns() in debug.cpp. 585 frame::frame(void* sp, void* fp, void* pc) { 586 init((intptr_t*)sp, (intptr_t*)fp, (address)pc); 587 } 588 589 void frame::pd_ps() {} 590 #endif 591 592 intptr_t *frame::initial_deoptimization_info() { 593 // used to reset the saved FP 594 return fp(); 595 } 596 597 intptr_t* frame::real_fp() const { 598 if (is_entry_frame()) { 599 // Work-around: FP (currently) does not conform to the ABI for entry 600 // frames (see generate_call_stub). Might be worth fixing as another CR. 601 // Following code assumes (and asserts) this has not yet been fixed. 602 assert(frame::entry_frame_call_wrapper_offset == 0, "adjust this code"); 603 intptr_t* new_fp = fp(); 604 new_fp += 5; // saved R0,R1,R2,R4,R10 605 #ifndef __SOFTFP__ 606 new_fp += 8*2; // saved D8..D15 607 #endif 608 return new_fp; 609 } 610 if (_cb != NULL) { 611 // use the frame size if valid 612 int size = _cb->frame_size(); 613 if (size > 0) { 614 return unextended_sp() + size; 615 } 616 } 617 // else rely on fp() 618 assert(! is_compiled_frame(), "unknown compiled frame size"); 619 return fp(); 620 }