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