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