1 /*
2 * Copyright (c) 2008, 2026, 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 "compiler/oopMap.hpp"
26 #include "interpreter/interpreter.hpp"
27 #include "memory/resourceArea.hpp"
28 #include "memory/universe.hpp"
29 #include "oops/markWord.hpp"
30 #include "oops/method.hpp"
31 #include "oops/oop.inline.hpp"
32 #include "prims/methodHandles.hpp"
33 #include "runtime/frame.inline.hpp"
34 #include "runtime/handles.inline.hpp"
35 #include "runtime/javaCalls.hpp"
36 #include "runtime/monitorChunk.hpp"
37 #include "runtime/os.inline.hpp"
38 #include "runtime/signature.hpp"
39 #include "runtime/stubCodeGenerator.hpp"
40 #include "runtime/stubRoutines.hpp"
41 #include "vmreg_arm.inline.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
51
52
53 // Profiling/safepoint support
54
55 bool frame::safe_for_sender(JavaThread *thread) {
56 address sp = (address)_sp;
57 address fp = (address)_fp;
58 address unextended_sp = (address)_unextended_sp;
59
60 // consider stack guards when trying to determine "safe" stack pointers
61 // sp must be within the usable part of the stack (not in guards)
62 if (!thread->is_in_usable_stack(sp)) {
63 return false;
64 }
65
66 if (!thread->is_in_stack_range_incl(unextended_sp, sp)) {
67 return false;
68 }
69
70 // We know sp/unextended_sp are safe. Only fp is questionable here.
71
72 bool fp_safe = thread->is_in_stack_range_incl(fp, sp);
73
74 if (_cb != nullptr ) {
75
76 // First check if frame is complete and tester is reliable
77 // Unfortunately we can only check frame complete for runtime stubs and nmethod
78 // other generic buffer blobs are more problematic so we just assume they are
79 // ok. adapter blobs never have a frame complete and are never ok.
80
81 if (!_cb->is_frame_complete_at(_pc)) {
82 if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
83 return false;
84 }
85 }
86
87 // Could just be some random pointer within the codeBlob
88 if (!_cb->code_contains(_pc)) {
89 return false;
90 }
91
92 // Entry frame checks
93 if (is_entry_frame()) {
94 // an entry frame must have a valid fp.
95 return fp_safe && is_entry_frame_valid(thread);
96 }
97
98 intptr_t* sender_sp = nullptr;
99 address sender_pc = nullptr;
100
101 if (is_interpreted_frame()) {
102 // fp must be safe
103 if (!fp_safe) {
104 return false;
105 }
106
107 sender_pc = (address) this->fp()[return_addr_offset];
108 sender_sp = (intptr_t*) addr_at(sender_sp_offset);
109
110 } else {
111 // must be some sort of compiled/runtime frame
112 // fp does not have to be safe (although it could be check for c1?)
113
114 sender_sp = _unextended_sp + _cb->frame_size();
115 // Is sender_sp safe?
116 if (!thread->is_in_full_stack_checked((address)sender_sp)) {
117 return false;
118 }
119 // With our calling conventions, the return_address should
120 // end up being the word on the stack
121 sender_pc = (address) *(sender_sp - sender_sp_offset + return_addr_offset);
122 }
123
124 // We must always be able to find a recognizable pc
125 CodeBlob* sender_blob = CodeCache::find_blob(sender_pc);
126 if (sender_pc == nullptr || sender_blob == nullptr) {
127 return false;
128 }
129
130
131 // If the potential sender is the interpreter then we can do some more checking
132 if (Interpreter::contains(sender_pc)) {
133
134 // FP is always saved in a recognizable place in any code we generate. However
135 // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved FP
136 // is really a frame pointer.
137
138 intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset + link_offset);
139 if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
140 return false;
141 }
142
143 // construct the potential sender
144
145 frame sender(sender_sp, saved_fp, sender_pc);
146
147 return sender.is_interpreted_frame_valid(thread);
148 }
149
150 // Could just be some random pointer within the codeBlob
151 if (!sender_blob->code_contains(sender_pc)) {
152 return false;
153 }
154
155 // We should never be able to see an adapter if the current frame is something from code cache
156 if (sender_blob->is_adapter_blob()) {
157 return false;
158 }
159
160 // Could be the call_stub
161 if (StubRoutines::returns_to_call_stub(sender_pc)) {
162 intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset + link_offset);
163 if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
164 return false;
165 }
166
167 // construct the potential sender
168
169 frame sender(sender_sp, saved_fp, sender_pc);
170
171 // Validate the JavaCallWrapper an entry frame must have
172 address jcw = (address)sender.entry_frame_call_wrapper();
173
174 return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
175 }
176
177 // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
178 // because the return address counts against the callee's frame.
179
180 if (sender_blob->frame_size() <= 0) {
181 assert(!sender_blob->is_nmethod(), "should count return address at least");
182 return false;
183 }
184
185 // We should never be able to see anything here except an nmethod. If something in the
186 // code cache (current frame) is called by an entity within the code cache that entity
187 // should not be anything but the call stub (already covered), the interpreter (already covered)
188 // or an nmethod.
189
190 if (!sender_blob->is_nmethod()) {
191 return false;
192 }
193
194 // Could put some more validation for the potential non-interpreted sender
195 // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
196
197 // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
198
199 // We've validated the potential sender that would be created
200 return true;
201 }
202
203 // Must be native-compiled frame. Since sender will try and use fp to find
204 // linkages it must be safe
205
206 if (!fp_safe) {
207 return false;
208 }
209
210 // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
211
212 if ((address) this->fp()[return_addr_offset] == nullptr) return false;
213
214
215 // could try and do some more potential verification of native frame if we could think of some...
216
217 return true;
218 }
219
220
221 void frame::patch_pc(Thread* thread, address pc) {
222 assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
223 address* pc_addr = &((address *)sp())[-sender_sp_offset+return_addr_offset];
224 if (TracePcPatching) {
225 tty->print_cr("patch_pc at address" INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "] ",
226 p2i(pc_addr), p2i(*pc_addr), p2i(pc));
227 }
228 DEBUG_ONLY(address old_pc = _pc;)
229 *pc_addr = pc;
230 _pc = pc; // must be set before call to get_deopt_original_pc
231 address original_pc = get_deopt_original_pc();
232 if (original_pc != nullptr) {
233 assert(original_pc == old_pc, "expected original PC to be stored before patching");
234 _deopt_state = is_deoptimized;
235 // leave _pc as is
236 } else {
237 _deopt_state = not_deoptimized;
238 _pc = pc;
239 }
240 }
241
242 bool frame::is_interpreted_frame() const {
243 return Interpreter::contains(pc());
244 }
245
246 intptr_t* frame::entry_frame_argument_at(int offset) const {
247 assert(is_entry_frame(), "entry frame expected");
248 // convert offset to index to deal with tsi
249 int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
250 // Entry frame's arguments are always in relation to unextended_sp()
251 return &unextended_sp()[index];
252 }
253
254 // locals
255
256 void frame::interpreter_frame_set_locals(intptr_t* locs) {
257 assert(is_interpreted_frame(), "interpreted frame expected");
258 // set relativized locals
259 ptr_at_put(interpreter_frame_locals_offset, (intptr_t) (locs - fp()));
260 }
261
262 // sender_sp
263
264 intptr_t* frame::interpreter_frame_sender_sp() const {
265 assert(is_interpreted_frame(), "interpreted frame expected");
266 return (intptr_t*) at(interpreter_frame_sender_sp_offset);
267 }
268
269 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
270 assert(is_interpreted_frame(), "interpreted frame expected");
271 ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
272 }
273
274
275 // monitor elements
276
277 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
278 return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
279 }
280
281 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
282 BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
283 // make sure the pointer points inside the frame
284 assert((intptr_t) fp() > (intptr_t) result, "result must < than frame pointer");
285 assert((intptr_t) sp() <= (intptr_t) result, "result must >= than stack pointer");
286 return result;
287 }
288
289 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
290 *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
291 }
292
293
294 // Used by template based interpreter deoptimization
295 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
296 *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
297 }
298
299
300 frame frame::sender_for_entry_frame(RegisterMap* map) const {
301 assert(map != nullptr, "map must be set");
302 // Java frame called from C; skip all C frames and return top C
303 // frame of that chunk as the sender
304 JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
305 assert(!entry_frame_is_first(), "next Java fp must be non zero");
306 assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
307 map->clear();
308 assert(map->include_argument_oops(), "should be set by clear");
309 if (jfa->last_Java_pc() != nullptr) {
310 frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
311 return fr;
312 }
313 frame fr(jfa->last_Java_sp(), jfa->last_Java_fp());
314 return fr;
315 }
316
317 UpcallStub::FrameData* UpcallStub::frame_data_for_frame(const frame& frame) const {
318 ShouldNotCallThis();
319 return nullptr;
320 }
321
322 bool frame::upcall_stub_frame_is_first() const {
323 ShouldNotCallThis();
324 return false;
325 }
326
327 JavaThread** frame::saved_thread_address(const frame& f) {
328 Unimplemented();
329 return nullptr;
330 }
331
332 //------------------------------------------------------------------------------
333 // frame::update_map_with_saved_link
334 void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {
335 // see x86 for comments
336 map->set_location(FP->as_VMReg(), (address) link_addr);
337 }
338
339 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
340 // SP is the raw SP from the sender after adapter or interpreter
341 // extension.
342 intptr_t* sender_sp = this->sender_sp();
343
344 // This is the sp before any possible extension (adapter/locals).
345 intptr_t* unextended_sp = interpreter_frame_sender_sp();
346
347 #ifdef COMPILER2
348 if (map->update_map()) {
349 update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
350 }
351 #endif // COMPILER2
352
353 return frame(sender_sp, unextended_sp, link(), sender_pc());
354 }
355
356 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
357 assert(is_interpreted_frame(), "Not an interpreted frame");
358 // These are reasonable sanity checks
359 if (fp() == nullptr || (intptr_t(fp()) & (wordSize-1)) != 0) {
360 return false;
361 }
362 if (sp() == nullptr || (intptr_t(sp()) & (wordSize-1)) != 0) {
363 return false;
364 }
365 if (fp() + interpreter_frame_initial_sp_offset < sp()) {
366 return false;
367 }
368 // These are hacks to keep us out of trouble.
369 // The problem with these is that they mask other problems
370 if (fp() <= sp()) { // this attempts to deal with unsigned comparison above
371 return false;
372 }
373 // do some validation of frame elements
374
375 // first the method
376
377 Method* m = safe_interpreter_frame_method();
378
379 // validate the method we'd find in this potential sender
380 if (!Method::is_valid_method(m)) return false;
381
382 // stack frames shouldn't be much larger than max_stack elements
383
384 if (fp() - sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
385 return false;
386 }
387
388 // validate bci/bcp
389
390 address bcp = interpreter_frame_bcp();
391 if (m->validate_bci_from_bcp(bcp) < 0) {
392 return false;
393 }
394
395 // validate ConstantPoolCache*
396 ConstantPoolCache* cp = *interpreter_frame_cache_addr();
397 if (MetaspaceObj::is_valid(cp) == false) return false;
398
399 // validate locals
400
401 address locals = (address)interpreter_frame_locals();
402 return thread->is_in_stack_range_incl(locals, (address)fp());
403 }
404
405 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
406 assert(is_interpreted_frame(), "interpreted frame expected");
407 Method* method = interpreter_frame_method();
408 BasicType type = method->result_type();
409
410 intptr_t* res_addr;
411 if (method->is_native()) {
412 // Prior to calling into the runtime to report the method_exit both of
413 // the possible return value registers are saved.
414 // Return value registers are pushed to the native stack
415 res_addr = (intptr_t*)sp();
416 #ifdef __ABI_HARD__
417 // FP result is pushed onto a stack along with integer result registers
418 if (type == T_FLOAT || type == T_DOUBLE) {
419 res_addr += 2;
420 }
421 #endif // __ABI_HARD__
422 } else {
423 res_addr = (intptr_t*)interpreter_frame_tos_address();
424 }
425
426 switch (type) {
427 case T_OBJECT :
428 case T_ARRAY : {
429 oop obj;
430 if (method->is_native()) {
431 obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
432 } else {
433 obj = *(oop*)res_addr;
434 }
435 assert(Universe::is_in_heap_or_null(obj), "sanity check");
436 *oop_result = obj;
437 break;
438 }
439 case T_BOOLEAN : value_result->z = *(jboolean*)res_addr; break;
440 case T_BYTE : value_result->b = *(jbyte*)res_addr; break;
441 case T_CHAR : value_result->c = *(jchar*)res_addr; break;
442 case T_SHORT : value_result->s = *(jshort*)res_addr; break;
443 case T_INT : value_result->i = *(jint*)res_addr; break;
444 case T_LONG : value_result->j = *(jlong*)res_addr; break;
445 case T_FLOAT : value_result->f = *(jfloat*)res_addr; break;
446 case T_DOUBLE : value_result->d = *(jdouble*)res_addr; break;
447 case T_VOID : /* Nothing to do */ break;
448 default : ShouldNotReachHere();
449 }
450
451 return type;
452 }
453
454 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
455 int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
456 return &interpreter_frame_tos_address()[index];
457 }
458
459 #ifndef PRODUCT
460
461 #define DESCRIBE_FP_OFFSET(name) \
462 values.describe(frame_no, fp() + frame::name##_offset, #name)
463
464 void frame::describe_pd(FrameValues& values, int frame_no) {
465 if (is_interpreted_frame()) {
466 DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
467 DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
468 DESCRIBE_FP_OFFSET(interpreter_frame_method);
469 DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
470 DESCRIBE_FP_OFFSET(interpreter_frame_cache);
471 DESCRIBE_FP_OFFSET(interpreter_frame_locals);
472 DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
473 DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
474 }
475 }
476
477 // This is a generic constructor which is only used by pns() in debug.cpp.
478 frame::frame(void* sp, void* fp, void* pc) {
479 init((intptr_t*)sp, (intptr_t*)sp, (intptr_t*)fp, (address)pc);
480 }
481
482 #endif
483
484 intptr_t *frame::initial_deoptimization_info() {
485 // used to reset the saved FP
486 return fp();
487 }
488
489 intptr_t* frame::real_fp() const {
490 if (is_entry_frame()) {
491 // Work-around: FP (currently) does not conform to the ABI for entry
492 // frames (see generate_call_stub). Might be worth fixing as another CR.
493 // Following code assumes (and asserts) this has not yet been fixed.
494 assert(frame::entry_frame_call_wrapper_offset == 0, "adjust this code");
495 intptr_t* new_fp = fp();
496 new_fp += 5; // saved R0,R1,R2,R4,R10
497 #ifndef __SOFTFP__
498 new_fp += 8*2; // saved D8..D15
499 #endif
500 return new_fp;
501 }
502 if (_cb != nullptr) {
503 // use the frame size if valid
504 int size = _cb->frame_size();
505 if (size > 0) {
506 return unextended_sp() + size;
507 }
508 }
509 // else rely on fp()
510 assert(! is_compiled_frame(), "unknown compiled frame size");
511 return fp();
512 }