Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/cpu/arm/frame_arm.cpp
40930 views
1
/*
2
* Copyright (c) 2008, 2021, 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 "compiler/oopMap.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/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
#include "prims/methodHandles.hpp"
47
48
#ifdef ASSERT
49
void RegisterMap::check_location_valid() {
50
}
51
#endif
52
53
54
// Profiling/safepoint support
55
56
bool frame::safe_for_sender(JavaThread *thread) {
57
address sp = (address)_sp;
58
address fp = (address)_fp;
59
address unextended_sp = (address)_unextended_sp;
60
61
// consider stack guards when trying to determine "safe" stack pointers
62
// sp must be within the usable part of the stack (not in guards)
63
if (!thread->is_in_usable_stack(sp)) {
64
return false;
65
}
66
67
if (!thread->is_in_stack_range_incl(unextended_sp, sp)) {
68
return false;
69
}
70
71
// We know sp/unextended_sp are safe. Only fp is questionable here.
72
73
bool fp_safe = thread->is_in_stack_range_incl(fp, sp);
74
75
if (_cb != NULL ) {
76
77
// First check if frame is complete and tester is reliable
78
// Unfortunately we can only check frame complete for runtime stubs and nmethod
79
// other generic buffer blobs are more problematic so we just assume they are
80
// ok. adapter blobs never have a frame complete and are never ok.
81
82
if (!_cb->is_frame_complete_at(_pc)) {
83
if (_cb->is_compiled() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
84
return false;
85
}
86
}
87
88
// Could just be some random pointer within the codeBlob
89
if (!_cb->code_contains(_pc)) {
90
return false;
91
}
92
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
intptr_t* sender_sp = NULL;
100
address sender_pc = NULL;
101
102
if (is_interpreted_frame()) {
103
// fp must be safe
104
if (!fp_safe) {
105
return false;
106
}
107
108
sender_pc = (address) this->fp()[return_addr_offset];
109
sender_sp = (intptr_t*) addr_at(sender_sp_offset);
110
111
} else {
112
// must be some sort of compiled/runtime frame
113
// fp does not have to be safe (although it could be check for c1?)
114
115
sender_sp = _unextended_sp + _cb->frame_size();
116
// Is sender_sp safe?
117
if (!thread->is_in_full_stack_checked((address)sender_sp)) {
118
return false;
119
}
120
// With our calling conventions, the return_address should
121
// end up being the word on the stack
122
sender_pc = (address) *(sender_sp - sender_sp_offset + return_addr_offset);
123
}
124
125
// We must always be able to find a recognizable pc
126
CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
127
if (sender_pc == NULL || sender_blob == NULL) {
128
return false;
129
}
130
131
132
// If the potential sender is the interpreter then we can do some more checking
133
if (Interpreter::contains(sender_pc)) {
134
135
// FP is always saved in a recognizable place in any code we generate. However
136
// only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved FP
137
// is really a frame pointer.
138
139
intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset + link_offset);
140
if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
141
return false;
142
}
143
144
// construct the potential sender
145
146
frame sender(sender_sp, saved_fp, sender_pc);
147
148
return sender.is_interpreted_frame_valid(thread);
149
}
150
151
if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
152
return false;
153
}
154
155
// Could just be some random pointer within the codeBlob
156
if (!sender_blob->code_contains(sender_pc)) {
157
return false;
158
}
159
160
// We should never be able to see an adapter if the current frame is something from code cache
161
if (sender_blob->is_adapter_blob()) {
162
return false;
163
}
164
165
// Could be the call_stub
166
if (StubRoutines::returns_to_call_stub(sender_pc)) {
167
intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset + link_offset);
168
if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
169
return false;
170
}
171
172
// construct the potential sender
173
174
frame sender(sender_sp, saved_fp, sender_pc);
175
176
// Validate the JavaCallWrapper an entry frame must have
177
address jcw = (address)sender.entry_frame_call_wrapper();
178
179
return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
180
}
181
182
// If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
183
// because the return address counts against the callee's frame.
184
185
if (sender_blob->frame_size() <= 0) {
186
assert(!sender_blob->is_compiled(), "should count return address at least");
187
return false;
188
}
189
190
// We should never be able to see anything here except an nmethod. If something in the
191
// code cache (current frame) is called by an entity within the code cache that entity
192
// should not be anything but the call stub (already covered), the interpreter (already covered)
193
// or an nmethod.
194
195
if (!sender_blob->is_compiled()) {
196
return false;
197
}
198
199
// Could put some more validation for the potential non-interpreted sender
200
// frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
201
202
// One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
203
204
// We've validated the potential sender that would be created
205
return true;
206
}
207
208
// Must be native-compiled frame. Since sender will try and use fp to find
209
// linkages it must be safe
210
211
if (!fp_safe) {
212
return false;
213
}
214
215
// Will the pc we fetch be non-zero (which we'll find at the oldest frame)
216
217
if ((address) this->fp()[return_addr_offset] == NULL) return false;
218
219
220
// could try and do some more potential verification of native frame if we could think of some...
221
222
return true;
223
}
224
225
226
void frame::patch_pc(Thread* thread, address pc) {
227
assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
228
address* pc_addr = &((address *)sp())[-sender_sp_offset+return_addr_offset];
229
if (TracePcPatching) {
230
tty->print_cr("patch_pc at address" INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "] ",
231
p2i(pc_addr), p2i(*pc_addr), p2i(pc));
232
}
233
*pc_addr = pc;
234
address original_pc = CompiledMethod::get_deopt_original_pc(this);
235
if (original_pc != NULL) {
236
assert(original_pc == _pc, "expected original PC to be stored before patching");
237
_deopt_state = is_deoptimized;
238
// leave _pc as is
239
} else {
240
_deopt_state = not_deoptimized;
241
_pc = pc;
242
}
243
}
244
245
bool frame::is_interpreted_frame() const {
246
return Interpreter::contains(pc());
247
}
248
249
int frame::frame_size(RegisterMap* map) const {
250
frame sender = this->sender(map);
251
return sender.sp() - sp();
252
}
253
254
intptr_t* frame::entry_frame_argument_at(int offset) const {
255
assert(is_entry_frame(), "entry frame expected");
256
// convert offset to index to deal with tsi
257
int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
258
// Entry frame's arguments are always in relation to unextended_sp()
259
return &unextended_sp()[index];
260
}
261
262
// sender_sp
263
intptr_t* frame::interpreter_frame_sender_sp() const {
264
assert(is_interpreted_frame(), "interpreted frame expected");
265
return (intptr_t*) at(interpreter_frame_sender_sp_offset);
266
}
267
268
void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
269
assert(is_interpreted_frame(), "interpreted frame expected");
270
ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
271
}
272
273
274
// monitor elements
275
276
BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
277
return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
278
}
279
280
BasicObjectLock* frame::interpreter_frame_monitor_end() const {
281
BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
282
// make sure the pointer points inside the frame
283
assert((intptr_t) fp() > (intptr_t) result, "result must < than frame pointer");
284
assert((intptr_t) sp() <= (intptr_t) result, "result must >= than stack pointer");
285
return result;
286
}
287
288
void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
289
*((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
290
}
291
292
293
// Used by template based interpreter deoptimization
294
void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
295
*((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
296
}
297
298
299
frame frame::sender_for_entry_frame(RegisterMap* map) const {
300
assert(map != NULL, "map must be set");
301
// Java frame called from C; skip all C frames and return top C
302
// frame of that chunk as the sender
303
JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
304
assert(!entry_frame_is_first(), "next Java fp must be non zero");
305
assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
306
map->clear();
307
assert(map->include_argument_oops(), "should be set by clear");
308
if (jfa->last_Java_pc() != NULL) {
309
frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
310
return fr;
311
}
312
frame fr(jfa->last_Java_sp(), jfa->last_Java_fp());
313
return fr;
314
}
315
316
//------------------------------------------------------------------------------
317
// frame::verify_deopt_original_pc
318
//
319
// Verifies the calculated original PC of a deoptimization PC for the
320
// given unextended SP. The unextended SP might also be the saved SP
321
// for MethodHandle call sites.
322
#ifdef ASSERT
323
void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp, bool is_method_handle_return) {
324
frame fr;
325
326
// This is ugly but it's better than to change {get,set}_original_pc
327
// to take an SP value as argument. And it's only a debugging
328
// method anyway.
329
fr._unextended_sp = unextended_sp;
330
331
address original_pc = nm->get_original_pc(&fr);
332
assert(nm->insts_contains_inclusive(original_pc),
333
"original PC must be in the main code section of the the compiled method (or must be immediately following it)");
334
assert(nm->is_method_handle_return(original_pc) == is_method_handle_return, "must be");
335
}
336
#endif
337
338
//------------------------------------------------------------------------------
339
// frame::adjust_unextended_sp
340
void frame::adjust_unextended_sp() {
341
// same as on x86
342
343
// If we are returning to a compiled MethodHandle call site, the
344
// saved_fp will in fact be a saved value of the unextended SP. The
345
// simplest way to tell whether we are returning to such a call site
346
// is as follows:
347
348
CompiledMethod* sender_cm = (_cb == NULL) ? NULL : _cb->as_compiled_method_or_null();
349
if (sender_cm != NULL) {
350
// If the sender PC is a deoptimization point, get the original
351
// PC. For MethodHandle call site the unextended_sp is stored in
352
// saved_fp.
353
if (sender_cm->is_deopt_mh_entry(_pc)) {
354
DEBUG_ONLY(verify_deopt_mh_original_pc(sender_cm, _fp));
355
_unextended_sp = _fp;
356
}
357
else if (sender_cm->is_deopt_entry(_pc)) {
358
DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp));
359
}
360
else if (sender_cm->is_method_handle_return(_pc)) {
361
_unextended_sp = _fp;
362
}
363
}
364
}
365
366
//------------------------------------------------------------------------------
367
// frame::update_map_with_saved_link
368
void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {
369
// see x86 for comments
370
map->set_location(FP->as_VMReg(), (address) link_addr);
371
}
372
373
frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
374
// SP is the raw SP from the sender after adapter or interpreter
375
// extension.
376
intptr_t* sender_sp = this->sender_sp();
377
378
// This is the sp before any possible extension (adapter/locals).
379
intptr_t* unextended_sp = interpreter_frame_sender_sp();
380
381
#ifdef COMPILER2
382
if (map->update_map()) {
383
update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
384
}
385
#endif // COMPILER2
386
387
return frame(sender_sp, unextended_sp, link(), sender_pc());
388
}
389
390
frame frame::sender_for_compiled_frame(RegisterMap* map) const {
391
assert(map != NULL, "map must be set");
392
393
// frame owned by optimizing compiler
394
assert(_cb->frame_size() >= 0, "must have non-zero frame size");
395
intptr_t* sender_sp = unextended_sp() + _cb->frame_size();
396
intptr_t* unextended_sp = sender_sp;
397
398
address sender_pc = (address) *(sender_sp - sender_sp_offset + return_addr_offset);
399
400
// This is the saved value of FP which may or may not really be an FP.
401
// It is only an FP if the sender is an interpreter frame (or C1?).
402
intptr_t** saved_fp_addr = (intptr_t**) (sender_sp - sender_sp_offset + link_offset);
403
404
if (map->update_map()) {
405
// Tell GC to use argument oopmaps for some runtime stubs that need it.
406
// For C1, the runtime stub might not have oop maps, so set this flag
407
// outside of update_register_map.
408
map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
409
if (_cb->oop_maps() != NULL) {
410
OopMapSet::update_register_map(this, map);
411
}
412
413
// Since the prolog does the save and restore of FP there is no oopmap
414
// for it so we must fill in its location as if there was an oopmap entry
415
// since if our caller was compiled code there could be live jvm state in it.
416
update_map_with_saved_link(map, saved_fp_addr);
417
}
418
419
assert(sender_sp != sp(), "must have changed");
420
return frame(sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
421
}
422
423
frame frame::sender(RegisterMap* map) const {
424
// Default is we done have to follow them. The sender_for_xxx will
425
// update it accordingly
426
map->set_include_argument_oops(false);
427
428
if (is_entry_frame()) return sender_for_entry_frame(map);
429
if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
430
assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
431
432
if (_cb != NULL) {
433
return sender_for_compiled_frame(map);
434
}
435
436
assert(false, "should not be called for a C frame");
437
return frame();
438
}
439
440
bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
441
assert(is_interpreted_frame(), "Not an interpreted frame");
442
// These are reasonable sanity checks
443
if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
444
return false;
445
}
446
if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
447
return false;
448
}
449
if (fp() + interpreter_frame_initial_sp_offset < sp()) {
450
return false;
451
}
452
// These are hacks to keep us out of trouble.
453
// The problem with these is that they mask other problems
454
if (fp() <= sp()) { // this attempts to deal with unsigned comparison above
455
return false;
456
}
457
// do some validation of frame elements
458
459
// first the method
460
461
Method* m = *interpreter_frame_method_addr();
462
463
// validate the method we'd find in this potential sender
464
if (!Method::is_valid_method(m)) return false;
465
466
// stack frames shouldn't be much larger than max_stack elements
467
468
if (fp() - sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
469
return false;
470
}
471
472
// validate bci/bcp
473
474
address bcp = interpreter_frame_bcp();
475
if (m->validate_bci_from_bcp(bcp) < 0) {
476
return false;
477
}
478
479
// validate ConstantPoolCache*
480
ConstantPoolCache* cp = *interpreter_frame_cache_addr();
481
if (MetaspaceObj::is_valid(cp) == false) return false;
482
483
// validate locals
484
485
address locals = (address) *interpreter_frame_locals_addr();
486
return thread->is_in_stack_range_incl(locals, (address)fp());
487
}
488
489
BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
490
assert(is_interpreted_frame(), "interpreted frame expected");
491
Method* method = interpreter_frame_method();
492
BasicType type = method->result_type();
493
494
intptr_t* res_addr;
495
if (method->is_native()) {
496
// Prior to calling into the runtime to report the method_exit both of
497
// the possible return value registers are saved.
498
// Return value registers are pushed to the native stack
499
res_addr = (intptr_t*)sp();
500
#ifdef __ABI_HARD__
501
// FP result is pushed onto a stack along with integer result registers
502
if (type == T_FLOAT || type == T_DOUBLE) {
503
res_addr += 2;
504
}
505
#endif // __ABI_HARD__
506
} else {
507
res_addr = (intptr_t*)interpreter_frame_tos_address();
508
}
509
510
switch (type) {
511
case T_OBJECT :
512
case T_ARRAY : {
513
oop obj;
514
if (method->is_native()) {
515
obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
516
} else {
517
obj = *(oop*)res_addr;
518
}
519
assert(Universe::is_in_heap_or_null(obj), "sanity check");
520
*oop_result = obj;
521
break;
522
}
523
case T_BOOLEAN : value_result->z = *(jboolean*)res_addr; break;
524
case T_BYTE : value_result->b = *(jbyte*)res_addr; break;
525
case T_CHAR : value_result->c = *(jchar*)res_addr; break;
526
case T_SHORT : value_result->s = *(jshort*)res_addr; break;
527
case T_INT : value_result->i = *(jint*)res_addr; break;
528
case T_LONG : value_result->j = *(jlong*)res_addr; break;
529
case T_FLOAT : value_result->f = *(jfloat*)res_addr; break;
530
case T_DOUBLE : value_result->d = *(jdouble*)res_addr; break;
531
case T_VOID : /* Nothing to do */ break;
532
default : ShouldNotReachHere();
533
}
534
535
return type;
536
}
537
538
539
intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
540
int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
541
return &interpreter_frame_tos_address()[index];
542
}
543
544
#ifndef PRODUCT
545
546
#define DESCRIBE_FP_OFFSET(name) \
547
values.describe(frame_no, fp() + frame::name##_offset, #name)
548
549
void frame::describe_pd(FrameValues& values, int frame_no) {
550
if (is_interpreted_frame()) {
551
DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
552
DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
553
DESCRIBE_FP_OFFSET(interpreter_frame_method);
554
DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
555
DESCRIBE_FP_OFFSET(interpreter_frame_cache);
556
DESCRIBE_FP_OFFSET(interpreter_frame_locals);
557
DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
558
DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
559
}
560
}
561
562
// This is a generic constructor which is only used by pns() in debug.cpp.
563
frame::frame(void* sp, void* fp, void* pc) {
564
init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
565
}
566
567
void frame::pd_ps() {}
568
#endif
569
570
intptr_t *frame::initial_deoptimization_info() {
571
// used to reset the saved FP
572
return fp();
573
}
574
575
intptr_t* frame::real_fp() const {
576
if (is_entry_frame()) {
577
// Work-around: FP (currently) does not conform to the ABI for entry
578
// frames (see generate_call_stub). Might be worth fixing as another CR.
579
// Following code assumes (and asserts) this has not yet been fixed.
580
assert(frame::entry_frame_call_wrapper_offset == 0, "adjust this code");
581
intptr_t* new_fp = fp();
582
new_fp += 5; // saved R0,R1,R2,R4,R10
583
#ifndef __SOFTFP__
584
new_fp += 8*2; // saved D8..D15
585
#endif
586
return new_fp;
587
}
588
if (_cb != NULL) {
589
// use the frame size if valid
590
int size = _cb->frame_size();
591
if (size > 0) {
592
return unextended_sp() + size;
593
}
594
}
595
// else rely on fp()
596
assert(! is_compiled_frame(), "unknown compiled frame size");
597
return fp();
598
}
599
600