1 /*
   2  * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2017 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/method.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "runtime/frame.inline.hpp"
  34 #include "runtime/handles.inline.hpp"
  35 #include "runtime/javaCalls.hpp"
  36 #include "runtime/jniHandles.inline.hpp"
  37 #include "runtime/monitorChunk.hpp"
  38 #include "runtime/os.inline.hpp"
  39 #include "runtime/signature.hpp"
  40 #include "runtime/stubCodeGenerator.hpp"
  41 #include "runtime/stubRoutines.hpp"
  42 #ifdef COMPILER1
  43 #include "c1/c1_Runtime1.hpp"
  44 #include "runtime/vframeArray.hpp"
  45 #endif
  46 
  47 #ifdef ASSERT
  48 void RegisterMap::check_location_valid() {
  49 }
  50 #endif // ASSERT
  51 
  52 bool frame::safe_for_sender(JavaThread *thread) {
  53   bool safe = false;
  54   address sp = (address)_sp;
  55   address fp = (address)_fp;
  56   address unextended_sp = (address)_unextended_sp;
  57 
  58   // consider stack guards when trying to determine "safe" stack pointers
  59   // sp must be within the usable part of the stack (not in guards)
  60   if (!thread->is_in_usable_stack(sp)) {
  61     return false;
  62   }
  63 
  64   // Unextended sp must be within the stack
  65   if (!thread->is_in_full_stack(unextended_sp)) {
  66     return false;
  67   }
  68 
  69   // An fp must be within the stack and above (but not equal) sp.
  70   bool fp_safe = thread->is_in_stack_range_excl(fp, sp);
  71   // An interpreter fp must be within the stack and above (but not equal) sp.
  72   // Moreover, it must be at least the size of the ijava_state structure.
  73   bool fp_interp_safe = fp_safe && ((fp - sp) >= ijava_state_size);
  74 
  75   // We know sp/unextended_sp are safe, only fp is questionable here
  76 
  77   // If the current frame is known to the code cache then we can attempt to
  78   // to construct the sender and do some validation of it. This goes a long way
  79   // toward eliminating issues when we get in frame construction code
  80 
  81   if (_cb != NULL ){
  82     // Entry frame checks
  83     if (is_entry_frame()) {
  84       // An entry frame must have a valid fp.
  85       return fp_safe && is_entry_frame_valid(thread);
  86     }
  87 
  88     // Now check if the frame is complete and the test is
  89     // reliable. Unfortunately we can only check frame completeness for
  90     // runtime stubs and nmethods. Other generic buffer blobs are more
  91     // problematic so we just assume they are OK. Adapter blobs never have a
  92     // complete frame and are never OK
  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     if (is_interpreted_frame() && !fp_interp_safe) {
 105       return false;
 106     }
 107 
 108     abi_minframe* sender_abi = (abi_minframe*) fp;
 109     intptr_t* sender_sp = (intptr_t*) fp;
 110     address   sender_pc = (address) sender_abi->lr;;
 111 
 112     // We must always be able to find a recognizable pc.
 113     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
 114     if (sender_blob == NULL) {
 115       return false;
 116     }
 117 
 118     // Could be a zombie method
 119     if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
 120       return false;
 121     }
 122 
 123     // It should be safe to construct the sender though it might not be valid.
 124 
 125     frame sender(sender_sp, sender_pc);
 126 
 127     // Do we have a valid fp?
 128     address sender_fp = (address) sender.fp();
 129 
 130     // sender_fp must be within the stack and above (but not
 131     // equal) current frame's fp.
 132     if (!thread->is_in_stack_range_excl(sender_fp, fp)) {
 133         return false;
 134     }
 135 
 136     // If the potential sender is the interpreter then we can do some more checking.
 137     if (Interpreter::contains(sender_pc)) {
 138       return sender.is_interpreted_frame_valid(thread);
 139     }
 140 
 141     // Could just be some random pointer within the codeBlob.
 142     if (!sender.cb()->code_contains(sender_pc)) {
 143       return false;
 144     }
 145 
 146     // We should never be able to see an adapter if the current frame is something from code cache.
 147     if (sender_blob->is_adapter_blob()) {
 148       return false;
 149     }
 150 
 151     if (sender.is_entry_frame()) {
 152       return sender.is_entry_frame_valid(thread);
 153     }
 154 
 155     // Frame size is always greater than zero. If the sender frame size is zero or less,
 156     // something is really weird and we better give up.
 157     if (sender_blob->frame_size() <= 0) {
 158       return false;
 159     }
 160 
 161     return true;
 162   }
 163 
 164   // Must be native-compiled frame. Since sender will try and use fp to find
 165   // linkages it must be safe
 166 
 167   if (!fp_safe) {
 168     return false;
 169   }
 170 
 171   return true;
 172 }
 173 
 174 bool frame::is_interpreted_frame() const  {
 175   return Interpreter::contains(pc());
 176 }
 177 
 178 frame frame::sender_for_entry_frame(RegisterMap *map) const {
 179   assert(map != NULL, "map must be set");
 180   // Java frame called from C; skip all C frames and return top C
 181   // frame of that chunk as the sender.
 182   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
 183   assert(!entry_frame_is_first(), "next Java fp must be non zero");
 184   assert(jfa->last_Java_sp() > _sp, "must be above this frame on stack");
 185   map->clear();
 186   assert(map->include_argument_oops(), "should be set by clear");
 187 
 188   if (jfa->last_Java_pc() != NULL) {
 189     frame fr(jfa->last_Java_sp(), jfa->last_Java_pc());
 190     return fr;
 191   }
 192   // Last_java_pc is not set, if we come here from compiled code. The
 193   // constructor retrieves the PC from the stack.
 194   frame fr(jfa->last_Java_sp());
 195   return fr;
 196 }
 197 
 198 frame frame::sender_for_interpreter_frame(RegisterMap *map) const {
 199   // Pass callers initial_caller_sp as unextended_sp.
 200   return frame(sender_sp(), sender_pc(), (intptr_t*)get_ijava_state()->sender_sp);
 201 }
 202 
 203 frame frame::sender_for_compiled_frame(RegisterMap *map) const {
 204   assert(map != NULL, "map must be set");
 205 
 206   // Frame owned by compiler.
 207   address pc = *compiled_sender_pc_addr(_cb);
 208   frame caller(compiled_sender_sp(_cb), pc);
 209 
 210   // Now adjust the map.
 211 
 212   // Get the rest.
 213   if (map->update_map()) {
 214     // Tell GC to use argument oopmaps for some runtime stubs that need it.
 215     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
 216     if (_cb->oop_maps() != NULL) {
 217       OopMapSet::update_register_map(this, map);
 218     }
 219   }
 220 
 221   return caller;
 222 }
 223 
 224 intptr_t* frame::compiled_sender_sp(CodeBlob* cb) const {
 225   return sender_sp();
 226 }
 227 
 228 address* frame::compiled_sender_pc_addr(CodeBlob* cb) const {
 229   return sender_pc_addr();
 230 }
 231 
 232 frame frame::sender(RegisterMap* map) const {
 233   // Default is we do have to follow them. The sender_for_xxx will
 234   // update it accordingly.
 235   map->set_include_argument_oops(false);
 236 
 237   if (is_entry_frame())       return sender_for_entry_frame(map);
 238   if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
 239   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
 240 
 241   if (_cb != NULL) {
 242     return sender_for_compiled_frame(map);
 243   }
 244   // Must be native-compiled frame, i.e. the marshaling code for native
 245   // methods that exists in the core system.
 246   return frame(sender_sp(), sender_pc());
 247 }
 248 
 249 void frame::patch_pc(Thread* thread, address pc) {
 250   if (TracePcPatching) {
 251     tty->print_cr("patch_pc at address " PTR_FORMAT " [" PTR_FORMAT " -> " PTR_FORMAT "]",
 252                   p2i(&((address*) _sp)[-1]), p2i(((address*) _sp)[-1]), p2i(pc));
 253   }
 254   own_abi()->lr = (uint64_t)pc;
 255   _cb = CodeCache::find_blob(pc);
 256   if (_cb != NULL && _cb->is_nmethod() && ((nmethod*)_cb)->is_deopt_pc(_pc)) {
 257     address orig = (((nmethod*)_cb)->get_original_pc(this));
 258     assert(orig == _pc, "expected original to be stored before patching");
 259     _deopt_state = is_deoptimized;
 260     // Leave _pc as is.
 261   } else {
 262     _deopt_state = not_deoptimized;
 263     _pc = pc;
 264   }
 265 }
 266 
 267 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
 268   // Is there anything to do?
 269   assert(is_interpreted_frame(), "Not an interpreted frame");
 270   return true;
 271 }
 272 
 273 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
 274   assert(is_interpreted_frame(), "interpreted frame expected");
 275   Method* method = interpreter_frame_method();
 276   BasicType type = method->result_type();
 277 
 278   if (method->is_native()) {
 279     // Prior to calling into the runtime to notify the method exit the possible
 280     // result value is saved into the interpreter frame.
 281     address lresult = (address)&(get_ijava_state()->lresult);
 282     address fresult = (address)&(get_ijava_state()->fresult);
 283 
 284     switch (method->result_type()) {
 285       case T_OBJECT:
 286       case T_ARRAY: {
 287         *oop_result = JNIHandles::resolve(*(jobject*)lresult);
 288         break;
 289       }
 290       // We use std/stfd to store the values.
 291       case T_BOOLEAN : value_result->z = (jboolean) *(unsigned long*)lresult; break;
 292       case T_INT     : value_result->i = (jint)     *(long*)lresult;          break;
 293       case T_CHAR    : value_result->c = (jchar)    *(unsigned long*)lresult; break;
 294       case T_SHORT   : value_result->s = (jshort)   *(long*)lresult;          break;
 295       case T_BYTE    : value_result->z = (jbyte)    *(long*)lresult;          break;
 296       case T_LONG    : value_result->j = (jlong)    *(long*)lresult;          break;
 297       case T_FLOAT   : value_result->f = (jfloat)   *(double*)fresult;        break;
 298       case T_DOUBLE  : value_result->d = (jdouble)  *(double*)fresult;        break;
 299       case T_VOID    : /* Nothing to do */ break;
 300       default        : ShouldNotReachHere();
 301     }
 302   } else {
 303     intptr_t* tos_addr = interpreter_frame_tos_address();
 304     switch (method->result_type()) {
 305       case T_OBJECT:
 306       case T_ARRAY: {
 307         oop obj = *(oop*)tos_addr;
 308         assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 309         *oop_result = obj;
 310       }
 311       case T_BOOLEAN : value_result->z = (jboolean) *(jint*)tos_addr; break;
 312       case T_BYTE    : value_result->b = (jbyte) *(jint*)tos_addr; break;
 313       case T_CHAR    : value_result->c = (jchar) *(jint*)tos_addr; break;
 314       case T_SHORT   : value_result->s = (jshort) *(jint*)tos_addr; break;
 315       case T_INT     : value_result->i = *(jint*)tos_addr; break;
 316       case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
 317       case T_FLOAT   : value_result->f = *(jfloat*)tos_addr; break;
 318       case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
 319       case T_VOID    : /* Nothing to do */ break;
 320       default        : ShouldNotReachHere();
 321     }
 322   }
 323   return type;
 324 }
 325 
 326 #ifndef PRODUCT
 327 
 328 void frame::describe_pd(FrameValues& values, int frame_no) {
 329   if (is_interpreted_frame()) {
 330 #define DESCRIBE_ADDRESS(name) \
 331   values.describe(frame_no, (intptr_t*)&(get_ijava_state()->name), #name);
 332 
 333       DESCRIBE_ADDRESS(method);
 334       DESCRIBE_ADDRESS(mirror);
 335       DESCRIBE_ADDRESS(locals);
 336       DESCRIBE_ADDRESS(monitors);
 337       DESCRIBE_ADDRESS(cpoolCache);
 338       DESCRIBE_ADDRESS(bcp);
 339       DESCRIBE_ADDRESS(esp);
 340       DESCRIBE_ADDRESS(mdx);
 341       DESCRIBE_ADDRESS(top_frame_sp);
 342       DESCRIBE_ADDRESS(sender_sp);
 343       DESCRIBE_ADDRESS(oop_tmp);
 344       DESCRIBE_ADDRESS(lresult);
 345       DESCRIBE_ADDRESS(fresult);
 346   }
 347 }
 348 #endif
 349 
 350 intptr_t *frame::initial_deoptimization_info() {
 351   // unused... but returns fp() to minimize changes introduced by 7087445
 352   return fp();
 353 }
 354 
 355 #ifndef PRODUCT
 356 // This is a generic constructor which is only used by pns() in debug.cpp.
 357 frame::frame(void* sp, void* fp, void* pc) : _sp((intptr_t*)sp), _unextended_sp((intptr_t*)sp) {
 358   find_codeblob_and_set_pc_and_deopt_state((address)pc); // also sets _fp and adjusts _unextended_sp
 359 }
 360 
 361 void frame::pd_ps() {}
 362 #endif