src/cpu/x86/vm/frame_x86.cpp

Print this page
rev 4773 : 8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>
   1 /*
   2  * Copyright (c) 1997, 2012, 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 "oops/markOop.hpp"
  29 #include "oops/method.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "prims/methodHandles.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/signature.hpp"
  37 #include "runtime/stubCodeGenerator.hpp"
  38 #include "runtime/stubRoutines.hpp"
  39 #include "vmreg_x86.inline.hpp"
  40 #ifdef COMPILER1
  41 #include "c1/c1_Runtime1.hpp"
  42 #include "runtime/vframeArray.hpp"
  43 #endif
  44 
  45 #ifdef ASSERT
  46 void RegisterMap::check_location_valid() {
  47 }
  48 #endif
  49 
  50 
  51 // Profiling/safepoint support
  52 
  53 bool frame::safe_for_sender(JavaThread *thread) {
  54   address   sp = (address)_sp;
  55   address   fp = (address)_fp;
  56   address   unextended_sp = (address)_unextended_sp;
  57   // sp must be within the stack
  58   bool sp_safe = (sp <= thread->stack_base()) &&
  59                  (sp >= thread->stack_base() - thread->stack_size());






  60 
  61   if (!sp_safe) {
  62     return false;
  63   }
  64 
  65   // unextended sp must be within the stack and above or equal sp
  66   bool unextended_sp_safe = (unextended_sp <= thread->stack_base()) &&
  67                             (unextended_sp >= sp);
  68 
  69   if (!unextended_sp_safe) {
  70     return false;
  71   }
  72 
  73   // an fp must be within the stack and above (but not equal) sp
  74   bool fp_safe = (fp <= thread->stack_base()) && (fp > sp);

  75 
  76   // We know sp/unextended_sp are safe only fp is questionable here
  77 
  78   // If the current frame is known to the code cache then we can attempt to
  79   // to construct the sender and do some validation of it. This goes a long way
  80   // toward eliminating issues when we get in frame construction code
  81 
  82   if (_cb != NULL ) {
  83 
  84     // First check if frame is complete and tester is reliable
  85     // Unfortunately we can only check frame complete for runtime stubs and nmethod
  86     // other generic buffer blobs are more problematic so we just assume they are
  87     // ok. adapter blobs never have a frame complete and are never ok.
  88 







  89     if (!_cb->is_frame_complete_at(_pc)) {
  90       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
  91         return false;
  92       }
  93     }
  94 
  95     // Could just be some random pointer within the codeBlob
  96     if (!_cb->code_contains(_pc)) {
  97       return false;
  98     }
  99 
 100     // Entry frame checks
 101     if (is_entry_frame()) {
 102       // an entry frame must have a valid fp.
 103 
 104       if (!fp_safe) return false;
 105 
 106       // Validate the JavaCallWrapper an entry frame must have
 107 
 108       address jcw = (address)entry_frame_call_wrapper();
 109 
 110       bool jcw_safe = (jcw <= thread->stack_base()) && ( jcw > fp);
 111 
 112       return jcw_safe;
 113 
 114     }
 115 
 116     intptr_t* sender_sp = NULL;
 117     address   sender_pc = NULL;
 118 
 119     if (is_interpreted_frame()) {
 120       // fp must be safe
 121       if (!fp_safe) {
 122         return false;
 123       }
 124 
 125       sender_pc = (address) this->fp()[return_addr_offset];
 126       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
 127 
 128     } else {
 129       // must be some sort of compiled/runtime frame
 130       // fp does not have to be safe (although it could be check for c1?)
 131 
 132       sender_sp = _unextended_sp + _cb->frame_size();
 133       // On Intel the return_address is always the word on the stack
 134       sender_pc = (address) *(sender_sp-1);
 135     }
 136 
 137     // We must always be able to find a recognizable pc
 138     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
 139     if (sender_pc == NULL ||  sender_blob == NULL) {
 140       return false;
 141     }
 142 
 143 
 144     // If the potential sender is the interpreter then we can do some more checking
 145     if (Interpreter::contains(sender_pc)) {
 146 
 147       // ebp is always saved in a recognizable place in any code we generate. However
 148       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved ebp
 149       // is really a frame pointer.
 150 
 151       intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset);
 152       bool saved_fp_safe = ((address)saved_fp <= thread->stack_base()) && (saved_fp > sender_sp);
 153 
 154       if (!saved_fp_safe) {
 155         return false;
 156       }
 157 
 158       // construct the potential sender
 159 
 160       frame sender(sender_sp, saved_fp, sender_pc);
 161 
 162       return sender.is_interpreted_frame_valid(thread);
 163 
 164     }
 165 











 166     // Could just be some random pointer within the codeBlob
 167     if (!sender_blob->code_contains(sender_pc)) {
 168       return false;
 169     }
 170 
 171     // We should never be able to see an adapter if the current frame is something from code cache
 172     if (sender_blob->is_adapter_blob()) {
 173       return false;
 174     }
 175 
 176     // Could be the call_stub
 177 
 178     if (StubRoutines::returns_to_call_stub(sender_pc)) {
 179       intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset);
 180       bool saved_fp_safe = ((address)saved_fp <= thread->stack_base()) && (saved_fp > sender_sp);
 181 
 182       if (!saved_fp_safe) {
 183         return false;
 184       }
 185 
 186       // construct the potential sender
 187 
 188       frame sender(sender_sp, saved_fp, sender_pc);
 189 
 190       // Validate the JavaCallWrapper an entry frame must have
 191       address jcw = (address)sender.entry_frame_call_wrapper();
 192 
 193       bool jcw_safe = (jcw <= thread->stack_base()) && ( jcw > (address)sender.fp());
 194 
 195       return jcw_safe;
 196     }
 197 
 198     // If the frame size is 0 something is bad because every nmethod has a non-zero frame size









 199     // because the return address counts against the callee's frame.
 200 
 201     if (sender_blob->frame_size() == 0) {
 202       assert(!sender_blob->is_nmethod(), "should count return address at least");
 203       return false;
 204     }
 205 
 206     // We should never be able to see anything here except an nmethod. If something in the
 207     // code cache (current frame) is called by an entity within the code cache that entity
 208     // should not be anything but the call stub (already covered), the interpreter (already covered)
 209     // or an nmethod.
 210 
 211     assert(sender_blob->is_nmethod(), "Impossible call chain");


 212 
 213     // Could put some more validation for the potential non-interpreted sender
 214     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
 215 
 216     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
 217 
 218     // We've validated the potential sender that would be created
 219     return true;
 220   }
 221 
 222   // Must be native-compiled frame. Since sender will try and use fp to find
 223   // linkages it must be safe
 224 
 225   if (!fp_safe) {
 226     return false;
 227   }
 228 
 229   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
 230 
 231   if ( (address) this->fp()[return_addr_offset] == NULL) return false;


   1 /*
   2  * Copyright (c) 1997, 2013, 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 "oops/markOop.hpp"
  29 #include "oops/method.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "prims/methodHandles.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.hpp"
  37 #include "runtime/signature.hpp"
  38 #include "runtime/stubCodeGenerator.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "vmreg_x86.inline.hpp"
  41 #ifdef COMPILER1
  42 #include "c1/c1_Runtime1.hpp"
  43 #include "runtime/vframeArray.hpp"
  44 #endif
  45 
  46 #ifdef ASSERT
  47 void RegisterMap::check_location_valid() {
  48 }
  49 #endif
  50 
  51 
  52 // Profiling/safepoint support
  53 
  54 bool frame::safe_for_sender(JavaThread *thread) {
  55   address   sp = (address)_sp;
  56   address   fp = (address)_fp;
  57   address   unextended_sp = (address)_unextended_sp;
  58 
  59   // consider stack guards when trying to determine "safe" stack pointers
  60   static size_t stack_guard_size = os::uses_stack_guard_pages() ? (StackYellowPages + StackRedPages) * os::vm_page_size() : 0;
  61   size_t usable_stack_size = thread->stack_size() - stack_guard_size;
  62 
  63   // sp must be within the usable part of the stack (not in guards)
  64   bool sp_safe = (sp < thread->stack_base()) &&
  65                  (sp >= thread->stack_base() - usable_stack_size);
  66 
  67 
  68   if (!sp_safe) {
  69     return false;
  70   }
  71 
  72   // unextended sp must be within the stack and above or equal sp
  73   bool unextended_sp_safe = (unextended_sp < thread->stack_base()) &&
  74                             (unextended_sp >= sp);
  75 
  76   if (!unextended_sp_safe) {
  77     return false;
  78   }
  79 
  80   // an fp must be within the stack and above (but not equal) sp
  81   // second evaluation on fp+ is added to handle situation where fp is -1
  82   bool fp_safe = (fp < thread->stack_base() && (fp > sp) && (((fp + (return_addr_offset * sizeof(void*))) < thread->stack_base())));
  83 
  84   // We know sp/unextended_sp are safe only fp is questionable here
  85 
  86   // If the current frame is known to the code cache then we can attempt to
  87   // to construct the sender and do some validation of it. This goes a long way
  88   // toward eliminating issues when we get in frame construction code
  89 
  90   if (_cb != NULL ) {
  91 
  92     // First check if frame is complete and tester is reliable
  93     // Unfortunately we can only check frame complete for runtime stubs and nmethod
  94     // other generic buffer blobs are more problematic so we just assume they are
  95     // ok. adapter blobs never have a frame complete and are never ok.
  96 
  97     // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
  98 
  99     if (!Interpreter::contains(_pc) && _cb->frame_size() <= 0) {
 100       //assert(0, "Invalid frame_size");
 101       return false;
 102     }
 103 
 104     if (!_cb->is_frame_complete_at(_pc)) {
 105       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
 106         return false;
 107       }
 108     }
 109 
 110     // Could just be some random pointer within the codeBlob
 111     if (!_cb->code_contains(_pc)) {
 112       return false;
 113     }
 114 
 115     // Entry frame checks
 116     if (is_entry_frame()) {
 117       // an entry frame must have a valid fp.
 118 
 119       if (!fp_safe) return false;
 120 
 121       // Validate the JavaCallWrapper an entry frame must have
 122 
 123       address jcw = (address)entry_frame_call_wrapper();
 124 
 125       bool jcw_safe = (jcw < thread->stack_base()) && ( jcw > fp);
 126 
 127       return jcw_safe;
 128 
 129     }
 130 
 131     intptr_t* sender_sp = NULL;
 132     address   sender_pc = NULL;
 133 
 134     if (is_interpreted_frame()) {
 135       // fp must be safe
 136       if (!fp_safe) {
 137         return false;
 138       }
 139 
 140       sender_pc = (address) this->fp()[return_addr_offset];
 141       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
 142 
 143     } else {
 144       // must be some sort of compiled/runtime frame
 145       // fp does not have to be safe (although it could be check for c1?)
 146 
 147       sender_sp = _unextended_sp + _cb->frame_size();
 148       // On Intel the return_address is always the word on the stack
 149       sender_pc = (address) *(sender_sp-1);
 150     }
 151 






 152 
 153     // If the potential sender is the interpreter then we can do some more checking
 154     if (Interpreter::contains(sender_pc)) {
 155 
 156       // ebp is always saved in a recognizable place in any code we generate. However
 157       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved ebp
 158       // is really a frame pointer.
 159 
 160       intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset);
 161       bool saved_fp_safe = ((address)saved_fp < thread->stack_base()) && (saved_fp > sender_sp);
 162 
 163       if (!saved_fp_safe) {
 164         return false;
 165       }
 166 
 167       // construct the potential sender
 168 
 169       frame sender(sender_sp, saved_fp, sender_pc);
 170 
 171       return sender.is_interpreted_frame_valid(thread);
 172 
 173     }
 174 
 175     // We must always be able to find a recognizable pc
 176     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
 177     if (sender_pc == NULL ||  sender_blob == NULL) {
 178       return false;
 179     }
 180 
 181     // Could be a zombie method
 182     if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
 183       return false;
 184     }
 185 
 186     // Could just be some random pointer within the codeBlob
 187     if (!sender_blob->code_contains(sender_pc)) {
 188       return false;
 189     }
 190 
 191     // We should never be able to see an adapter if the current frame is something from code cache
 192     if (sender_blob->is_adapter_blob()) {
 193       return false;
 194     }
 195 
 196     // Could be the call_stub

 197     if (StubRoutines::returns_to_call_stub(sender_pc)) {
 198       intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset);
 199       bool saved_fp_safe = ((address)saved_fp < thread->stack_base()) && (saved_fp > sender_sp);
 200 
 201       if (!saved_fp_safe) {
 202         return false;
 203       }
 204 
 205       // construct the potential sender
 206 
 207       frame sender(sender_sp, saved_fp, sender_pc);
 208 
 209       // Validate the JavaCallWrapper an entry frame must have
 210       address jcw = (address)sender.entry_frame_call_wrapper();
 211 
 212       bool jcw_safe = (jcw < thread->stack_base()) && ( jcw > (address)sender.fp());
 213 
 214       return jcw_safe;
 215     }
 216 
 217     if (sender_blob->is_nmethod()) {
 218         nmethod* nm = sender_blob->as_nmethod_or_null();
 219         if (nm != NULL) {
 220             if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc)) {
 221                 return false;
 222             }
 223         }
 224     }
 225 
 226     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
 227     // because the return address counts against the callee's frame.
 228 
 229     if (sender_blob->frame_size() <= 0) {
 230       assert(!sender_blob->is_nmethod(), "should count return address at least");
 231       return false;
 232     }
 233 
 234     // We should never be able to see anything here except an nmethod. If something in the
 235     // code cache (current frame) is called by an entity within the code cache that entity
 236     // should not be anything but the call stub (already covered), the interpreter (already covered)
 237     // or an nmethod.
 238 
 239     if (!sender_blob->is_nmethod()) {
 240         return false;
 241     }
 242 
 243     // Could put some more validation for the potential non-interpreted sender
 244     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
 245 
 246     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
 247 
 248     // We've validated the potential sender that would be created
 249     return true;
 250   }
 251 
 252   // Must be native-compiled frame. Since sender will try and use fp to find
 253   // linkages it must be safe
 254 
 255   if (!fp_safe) {
 256     return false;
 257   }
 258 
 259   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
 260 
 261   if ( (address) this->fp()[return_addr_offset] == NULL) return false;