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