1 /* 2 * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2016, 2019, SAP SE. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "interpreter/interpreter.hpp" 28 #include "memory/resourceArea.hpp" 29 #include "memory/universe.hpp" 30 #include "oops/markWord.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_s390.inline.hpp" 41 #ifdef COMPILER1 42 #include "c1/c1_Runtime1.hpp" 43 #include "runtime/vframeArray.hpp" 44 #endif 45 46 // Major contributions by Aha, AS. 47 48 #ifdef ASSERT 49 void RegisterMap::check_location_valid() { 50 } 51 #endif // ASSERT 52 53 54 // Profiling/safepoint support 55 56 bool frame::safe_for_sender(JavaThread *thread) { 57 bool safe = false; 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 69 bool unextended_sp_safe = (unextended_sp < thread->stack_base()); 70 71 if (!unextended_sp_safe) { 72 return false; 73 } 74 75 // An fp must be within the stack and above (but not equal) sp. 76 bool fp_safe = (fp < thread->stack_base()) && (fp > sp); 77 // An interpreter fp must be within the stack and above (but not equal) sp. 78 // Moreover, it must be at least the size of the z_ijava_state structure. 79 bool fp_interp_safe = (fp < thread->stack_base()) && (fp > sp) && 80 ((fp - sp) >= z_ijava_state_size); 81 82 // We know sp/unextended_sp are safe, only fp is questionable here 83 84 // If the current frame is known to the code cache then we can attempt to 85 // to construct the sender and do some validation of it. This goes a long way 86 // toward eliminating issues when we get in frame construction code 87 88 if (_cb != NULL ) { 89 // Entry frame checks 90 if (is_entry_frame()) { 91 // An entry frame must have a valid fp. 92 return fp_safe && is_entry_frame_valid(thread); 93 } 94 95 // Now check if the frame is complete and the test is 96 // reliable. Unfortunately we can only check frame completeness for 97 // runtime stubs. Other generic buffer blobs are more 98 // problematic so we just assume they are OK. Adapter blobs never have a 99 // complete frame and are never OK. nmethods should be OK on s390. 100 if (!_cb->is_frame_complete_at(_pc)) { 101 if (_cb->is_adapter_blob() || _cb->is_runtime_stub()) { 102 return false; 103 } 104 } 105 106 // Could just be some random pointer within the codeBlob. 107 if (!_cb->code_contains(_pc)) { 108 return false; 109 } 110 111 if (is_interpreted_frame() && !fp_interp_safe) { 112 return false; 113 } 114 115 z_abi_160* sender_abi = (z_abi_160*) fp; 116 intptr_t* sender_sp = (intptr_t*) sender_abi->callers_sp; 117 address sender_pc = (address) sender_abi->return_pc; 118 119 // We must always be able to find a recognizable pc. 120 CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc); 121 if (sender_blob == NULL) { 122 return false; 123 } 124 125 // Could be a zombie method 126 if (sender_blob->is_zombie() || sender_blob->is_unloaded()) { 127 return false; 128 } 129 130 // It should be safe to construct the sender though it might not be valid. 131 132 frame sender(sender_sp, sender_pc); 133 134 // Do we have a valid fp? 135 address sender_fp = (address) sender.fp(); 136 137 // sender_fp must be within the stack and above (but not 138 // equal) current frame's fp. 139 if (sender_fp >= thread->stack_base() || sender_fp <= fp) { 140 return false; 141 } 142 143 // If the potential sender is the interpreter then we can do some more checking. 144 if (Interpreter::contains(sender_pc)) { 145 return sender.is_interpreted_frame_valid(thread); 146 } 147 148 // Could just be some random pointer within the codeBlob. 149 if (!sender.cb()->code_contains(sender_pc)) { 150 return false; 151 } 152 153 // We should never be able to see an adapter if the current frame is something from code cache. 154 if (sender_blob->is_adapter_blob()) { 155 return false; 156 } 157 158 if (sender.is_entry_frame()) { 159 return sender.is_entry_frame_valid(thread); 160 } 161 162 // Frame size is always greater than zero. If the sender frame size is zero or less, 163 // something is really weird and we better give up. 164 if (sender_blob->frame_size() <= 0) { 165 return false; 166 } 167 168 return true; 169 } 170 171 // Must be native-compiled frame. Since sender will try and use fp to find 172 // linkages it must be safe 173 174 if (!fp_safe) { 175 return false; 176 } 177 178 return true; 179 } 180 181 bool frame::is_interpreted_frame() const { 182 return Interpreter::contains(pc()); 183 } 184 185 // sender_sp 186 187 intptr_t* frame::interpreter_frame_sender_sp() const { 188 return sender_sp(); 189 } 190 191 frame frame::sender_for_entry_frame(RegisterMap *map) const { 192 assert(map != NULL, "map must be set"); 193 // Java frame called from C. Skip all C frames and return top C 194 // frame of that chunk as the sender. 195 JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor(); 196 197 assert(!entry_frame_is_first(), "next Java sp must be non zero"); 198 assert(jfa->last_Java_sp() > _sp, "must be above this frame on stack"); 199 200 map->clear(); 201 202 assert(map->include_argument_oops(), "should be set by clear"); 203 204 if (jfa->last_Java_pc() != NULL) { 205 frame fr(jfa->last_Java_sp(), jfa->last_Java_pc()); 206 return fr; 207 } 208 // Last_java_pc is not set if we come here from compiled code. 209 frame fr(jfa->last_Java_sp()); 210 return fr; 211 } 212 213 frame frame::sender_for_interpreter_frame(RegisterMap *map) const { 214 // Pass callers sender_sp as unextended_sp. 215 return frame(sender_sp(), sender_pc(), (intptr_t*)(ijava_state()->sender_sp)); 216 } 217 218 frame frame::sender_for_compiled_frame(RegisterMap *map) const { 219 assert(map != NULL, "map must be set"); 220 // Frame owned by compiler. 221 222 address pc = *compiled_sender_pc_addr(_cb); 223 frame caller(compiled_sender_sp(_cb), pc); 224 225 // Now adjust the map. 226 227 // Get the rest. 228 if (map->update_map()) { 229 // Tell GC to use argument oopmaps for some runtime stubs that need it. 230 map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread())); 231 if (_cb->oop_maps() != NULL) { 232 OopMapSet::update_register_map(this, map); 233 } 234 } 235 236 return caller; 237 } 238 239 intptr_t* frame::compiled_sender_sp(CodeBlob* cb) const { 240 return sender_sp(); 241 } 242 243 address* frame::compiled_sender_pc_addr(CodeBlob* cb) const { 244 return sender_pc_addr(); 245 } 246 247 frame frame::sender(RegisterMap* map) const { 248 // Default is we don't have to follow them. The sender_for_xxx will 249 // update it accordingly. 250 map->set_include_argument_oops(false); 251 252 if (is_entry_frame()) { 253 return sender_for_entry_frame(map); 254 } 255 if (is_interpreted_frame()) { 256 return sender_for_interpreter_frame(map); 257 } 258 assert(_cb == CodeCache::find_blob(pc()),"Must be the same"); 259 if (_cb != NULL) { 260 return sender_for_compiled_frame(map); 261 } 262 // Must be native-compiled frame, i.e. the marshaling code for native 263 // methods that exists in the core system. 264 return frame(sender_sp(), sender_pc()); 265 } 266 267 void frame::patch_pc(Thread* thread, address pc) { 268 if (TracePcPatching) { 269 tty->print_cr("patch_pc at address " PTR_FORMAT " [" PTR_FORMAT " -> " PTR_FORMAT "] ", 270 p2i(&((address*) _sp)[-1]), p2i(((address*) _sp)[-1]), p2i(pc)); 271 } 272 own_abi()->return_pc = (uint64_t)pc; 273 _cb = CodeCache::find_blob(pc); 274 address original_pc = CompiledMethod::get_deopt_original_pc(this); 275 if (original_pc != NULL) { 276 assert(original_pc == _pc, "expected original to be stored before patching"); 277 _deopt_state = is_deoptimized; 278 // Leave _pc as is. 279 } else { 280 _deopt_state = not_deoptimized; 281 _pc = pc; 282 } 283 } 284 285 bool frame::is_interpreted_frame_valid(JavaThread* thread) const { 286 // Is there anything to do? 287 assert(is_interpreted_frame(), "Not an interpreted frame"); 288 return true; 289 } 290 291 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) { 292 assert(is_interpreted_frame(), "interpreted frame expected"); 293 Method* method = interpreter_frame_method(); 294 BasicType type = method->result_type(); 295 296 if (method->is_native()) { 297 address lresult = (address)&(ijava_state()->lresult); 298 address fresult = (address)&(ijava_state()->fresult); 299 300 switch (type) { 301 case T_OBJECT: 302 case T_ARRAY: { 303 *oop_result = (oop) (void*) ijava_state()->oop_tmp; 304 break; 305 } 306 // We use std/stfd to store the values. 307 case T_BOOLEAN : value_result->z = (jboolean) *(unsigned long*)lresult; break; 308 case T_INT : value_result->i = (jint) *(long*)lresult; break; 309 case T_CHAR : value_result->c = (jchar) *(unsigned long*)lresult; break; 310 case T_SHORT : value_result->s = (jshort) *(long*)lresult; break; 311 case T_BYTE : value_result->z = (jbyte) *(long*)lresult; break; 312 case T_LONG : value_result->j = (jlong) *(long*)lresult; break; 313 case T_FLOAT : value_result->f = (jfloat) *(float*)fresult; break; 314 case T_DOUBLE : value_result->d = (jdouble) *(double*)fresult; break; 315 case T_VOID : break; // Nothing to do. 316 default : ShouldNotReachHere(); 317 } 318 } else { 319 intptr_t* tos_addr = interpreter_frame_tos_address(); 320 switch (type) { 321 case T_OBJECT: 322 case T_ARRAY: { 323 oop obj = *(oop*)tos_addr; 324 assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check"); 325 *oop_result = obj; 326 break; 327 } 328 case T_BOOLEAN : value_result->z = (jboolean) *(jint*)tos_addr; break; 329 case T_BYTE : value_result->b = (jbyte) *(jint*)tos_addr; break; 330 case T_CHAR : value_result->c = (jchar) *(jint*)tos_addr; break; 331 case T_SHORT : value_result->s = (jshort) *(jint*)tos_addr; break; 332 case T_INT : value_result->i = *(jint*)tos_addr; break; 333 case T_LONG : value_result->j = *(jlong*)tos_addr; break; 334 case T_FLOAT : value_result->f = *(jfloat*)tos_addr; break; 335 case T_DOUBLE : value_result->d = *(jdouble*)tos_addr; break; 336 case T_VOID : break; // Nothing to do. 337 default : ShouldNotReachHere(); 338 } 339 } 340 341 return type; 342 } 343 344 345 // Dump all frames starting a given C stack-pointer. 346 // Use max_frames to limit the number of traced frames. 347 void frame::back_trace(outputStream* st, intptr_t* start_sp, intptr_t* top_pc, unsigned long flags, int max_frames) { 348 349 static char buf[ 150 ]; 350 351 bool print_outgoing_arguments = flags & 0x1; 352 bool print_istate_pointers = flags & 0x2; 353 int num = 0; 354 355 intptr_t* current_sp = (intptr_t*) start_sp; 356 int last_num_jargs = 0; 357 int frame_type = 0; 358 int last_frame_type = 0; 359 360 while (current_sp) { 361 intptr_t* current_fp = (intptr_t*) *current_sp; 362 address current_pc = (num == 0) 363 ? (address) top_pc 364 : (address) *((intptr_t*)(((address) current_sp) + _z_abi(return_pc))); 365 366 if ((intptr_t*) current_fp != 0 && (intptr_t*) current_fp <= current_sp) { 367 st->print_cr("ERROR: corrupt stack"); 368 return; 369 } 370 371 st->print("#%-3d ", num); 372 const char* type_name = " "; 373 const char* function_name = NULL; 374 375 // Detect current frame's frame_type, default to 'C frame'. 376 frame_type = 0; 377 378 CodeBlob* blob = NULL; 379 380 if (Interpreter::contains(current_pc)) { 381 frame_type = 1; 382 } else if (StubRoutines::contains(current_pc)) { 383 if (StubRoutines::returns_to_call_stub(current_pc)) { 384 frame_type = 2; 385 } else { 386 frame_type = 4; 387 type_name = "stu"; 388 StubCodeDesc* desc = StubCodeDesc::desc_for (current_pc); 389 if (desc) { 390 function_name = desc->name(); 391 } else { 392 function_name = "unknown stub"; 393 } 394 } 395 } else if (CodeCache::contains(current_pc)) { 396 blob = CodeCache::find_blob_unsafe(current_pc); 397 if (blob) { 398 if (blob->is_nmethod()) { 399 frame_type = 3; 400 } else if (blob->is_deoptimization_stub()) { 401 frame_type = 4; 402 type_name = "deo"; 403 function_name = "deoptimization blob"; 404 } else if (blob->is_uncommon_trap_stub()) { 405 frame_type = 4; 406 type_name = "uct"; 407 function_name = "uncommon trap blob"; 408 } else if (blob->is_exception_stub()) { 409 frame_type = 4; 410 type_name = "exc"; 411 function_name = "exception blob"; 412 } else if (blob->is_safepoint_stub()) { 413 frame_type = 4; 414 type_name = "saf"; 415 function_name = "safepoint blob"; 416 } else if (blob->is_runtime_stub()) { 417 frame_type = 4; 418 type_name = "run"; 419 function_name = ((RuntimeStub *)blob)->name(); 420 } else if (blob->is_method_handles_adapter_blob()) { 421 frame_type = 4; 422 type_name = "mha"; 423 function_name = "method handles adapter blob"; 424 } else { 425 frame_type = 4; 426 type_name = "blo"; 427 function_name = "unknown code blob"; 428 } 429 } else { 430 frame_type = 4; 431 type_name = "blo"; 432 function_name = "unknown code blob"; 433 } 434 } 435 436 st->print("sp=" PTR_FORMAT " ", p2i(current_sp)); 437 438 if (frame_type == 0) { 439 current_pc = (address) *((intptr_t*)(((address) current_sp) + _z_abi(gpr14))); 440 } 441 442 st->print("pc=" PTR_FORMAT " ", p2i(current_pc)); 443 st->print(" "); 444 445 switch (frame_type) { 446 case 0: // C frame: 447 { 448 st->print(" "); 449 if (current_pc == 0) { 450 st->print("? "); 451 } else { 452 // name 453 int func_offset; 454 char demangled_name[256]; 455 int demangled_name_len = 256; 456 if (os::dll_address_to_function_name(current_pc, demangled_name, demangled_name_len, &func_offset)) { 457 demangled_name[demangled_name_len-1] = '\0'; 458 st->print(func_offset == -1 ? "%s " : "%s+0x%x", demangled_name, func_offset); 459 } else { 460 st->print("? "); 461 } 462 } 463 } 464 break; 465 466 case 1: // interpreter frame: 467 { 468 st->print(" i "); 469 470 if (last_frame_type != 1) last_num_jargs = 8; 471 472 // name 473 Method* method = *(Method**)((address)current_fp + _z_ijava_state_neg(method)); 474 if (method) { 475 ResourceMark rm; 476 if (method->is_synchronized()) st->print("synchronized "); 477 if (method->is_static()) st->print("static "); 478 if (method->is_native()) st->print("native "); 479 method->name_and_sig_as_C_string(buf, sizeof(buf)); 480 st->print("%s ", buf); 481 } 482 else 483 st->print("? "); 484 485 intptr_t* tos = (intptr_t*) *(intptr_t*)((address)current_fp + _z_ijava_state_neg(esp)); 486 if (print_istate_pointers) { 487 st->cr(); 488 st->print(" "); 489 st->print("ts=" PTR_FORMAT " ", p2i(tos)); 490 } 491 492 // Dump some Java stack slots. 493 if (print_outgoing_arguments) { 494 if (method->is_native()) { 495 #ifdef ASSERT 496 intptr_t* cargs = (intptr_t*) (((address)current_sp) + _z_abi(carg_1)); 497 for (int i = 0; i < last_num_jargs; i++) { 498 // Cargs is not prepushed. 499 st->cr(); 500 st->print(" "); 501 st->print(PTR_FORMAT, *(cargs)); 502 cargs++; 503 } 504 #endif /* ASSERT */ 505 } 506 else { 507 if (tos) { 508 for (int i = 0; i < last_num_jargs; i++) { 509 // tos+0 is prepushed, ignore. 510 tos++; 511 if (tos >= (intptr_t *)((address)current_fp + _z_ijava_state_neg(monitors))) 512 break; 513 st->cr(); 514 st->print(" "); 515 st->print(PTR_FORMAT " %+.3e %+.3le", *(tos), *(float*)(tos), *(double*)(tos)); 516 } 517 } 518 } 519 last_num_jargs = method->size_of_parameters(); 520 } 521 } 522 break; 523 524 case 2: // entry frame: 525 { 526 st->print("v2i "); 527 528 // name 529 st->print("call stub"); 530 } 531 break; 532 533 case 3: // compiled frame: 534 { 535 st->print(" c "); 536 537 // name 538 Method* method = ((nmethod *)blob)->method(); 539 if (method) { 540 ResourceMark rm; 541 method->name_and_sig_as_C_string(buf, sizeof(buf)); 542 st->print("%s ", buf); 543 } 544 else 545 st->print("? "); 546 } 547 break; 548 549 case 4: // named frames 550 { 551 st->print("%s ", type_name); 552 553 // name 554 if (function_name) 555 st->print("%s", function_name); 556 } 557 break; 558 559 default: 560 break; 561 } 562 563 st->cr(); 564 st->flush(); 565 566 current_sp = current_fp; 567 last_frame_type = frame_type; 568 num++; 569 // Check for maximum # of frames, and stop when reached. 570 if (max_frames > 0 && --max_frames == 0) 571 break; 572 } 573 574 } 575 576 // Convenience function for calls from the debugger. 577 578 extern "C" void bt(intptr_t* start_sp,intptr_t* top_pc) { 579 frame::back_trace(tty,start_sp, top_pc, 0); 580 } 581 582 extern "C" void bt_full(intptr_t* start_sp,intptr_t* top_pc) { 583 frame::back_trace(tty,start_sp, top_pc, (unsigned long)(long)-1); 584 } 585 586 587 // Function for tracing a limited number of frames. 588 // Use this one if you only need to see the "top of stack" frames. 589 extern "C" void bt_max(intptr_t *start_sp, intptr_t *top_pc, int max_frames) { 590 frame::back_trace(tty, start_sp, top_pc, 0, max_frames); 591 } 592 593 #if !defined(PRODUCT) 594 595 #define DESCRIBE_ADDRESS(name) \ 596 values.describe(frame_no, (intptr_t*)&ijava_state()->name, #name); 597 598 void frame::describe_pd(FrameValues& values, int frame_no) { 599 if (is_interpreted_frame()) { 600 // Describe z_ijava_state elements. 601 DESCRIBE_ADDRESS(method); 602 DESCRIBE_ADDRESS(locals); 603 DESCRIBE_ADDRESS(monitors); 604 DESCRIBE_ADDRESS(cpoolCache); 605 DESCRIBE_ADDRESS(bcp); 606 DESCRIBE_ADDRESS(mdx); 607 DESCRIBE_ADDRESS(esp); 608 DESCRIBE_ADDRESS(sender_sp); 609 DESCRIBE_ADDRESS(top_frame_sp); 610 DESCRIBE_ADDRESS(oop_tmp); 611 DESCRIBE_ADDRESS(lresult); 612 DESCRIBE_ADDRESS(fresult); 613 } 614 } 615 616 617 void frame::pd_ps() {} 618 #endif // !PRODUCT 619 620 intptr_t *frame::initial_deoptimization_info() { 621 // Used to reset the saved FP. 622 return fp(); 623 }