Path: blob/master/src/hotspot/cpu/ppc/frame_ppc.cpp
64440 views
/*1* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2012, 2022 SAP SE. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#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/jniHandles.inline.hpp"37#include "runtime/monitorChunk.hpp"38#include "runtime/os.inline.hpp"39#include "runtime/signature.hpp"40#include "runtime/stackWatermarkSet.hpp"41#include "runtime/stubCodeGenerator.hpp"42#include "runtime/stubRoutines.hpp"43#ifdef COMPILER144#include "c1/c1_Runtime1.hpp"45#include "runtime/vframeArray.hpp"46#endif4748#ifdef ASSERT49void RegisterMap::check_location_valid() {50}51#endif // ASSERT5253bool frame::safe_for_sender(JavaThread *thread) {54address sp = (address)_sp;55address fp = (address)_fp;56address unextended_sp = (address)_unextended_sp;5758// consider stack guards when trying to determine "safe" stack pointers59// sp must be within the usable part of the stack (not in guards)60if (!thread->is_in_usable_stack(sp)) {61return false;62}6364// Unextended sp must be within the stack65if (!thread->is_in_full_stack_checked(unextended_sp)) {66return false;67}6869// An fp must be within the stack and above (but not equal) sp.70bool fp_safe = thread->is_in_stack_range_excl(fp, sp);71// An interpreter fp must be fp_safe.72// Moreover, it must be at a distance at least the size of the ijava_state structure.73bool fp_interp_safe = fp_safe && ((fp - sp) >= ijava_state_size);7475// We know sp/unextended_sp are safe, only fp is questionable here7677// If the current frame is known to the code cache then we can attempt to78// construct the sender and do some validation of it. This goes a long way79// toward eliminating issues when we get in frame construction code8081if (_cb != NULL ){8283// First check if the frame is complete and the test is reliable.84// Unfortunately we can only check frame completeness for runtime stubs85// and nmethods. Other generic buffer blobs are more problematic86// so we just assume they are OK.87// Adapter blobs never have a complete frame and are never OK88if (!_cb->is_frame_complete_at(_pc)) {89if (_cb->is_compiled() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {90return false;91}92}9394// Could just be some random pointer within the codeBlob.95if (!_cb->code_contains(_pc)) {96return false;97}9899// Entry frame checks100if (is_entry_frame()) {101// An entry frame must have a valid fp.102return fp_safe && is_entry_frame_valid(thread);103}104105if (is_interpreted_frame() && !fp_interp_safe) {106return false;107}108109// At this point, there still is a chance that fp_safe is false.110// In particular, (fp == NULL) might be true. So let's check and111// bail out before we actually dereference from fp.112if (!fp_safe) {113return false;114}115116abi_minframe* sender_abi = (abi_minframe*) fp;117intptr_t* sender_sp = (intptr_t*) fp;118address sender_pc = (address) sender_abi->lr;;119120// We must always be able to find a recognizable pc.121CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);122if (sender_blob == NULL) {123return false;124}125126// Could be a zombie method127if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {128return false;129}130131// It should be safe to construct the sender though it might not be valid.132133frame sender(sender_sp, sender_pc);134135// Do we have a valid fp?136address sender_fp = (address) sender.fp();137138// sender_fp must be within the stack and above (but not139// equal) current frame's fp.140if (!thread->is_in_stack_range_excl(sender_fp, fp)) {141return false;142}143144// If the potential sender is the interpreter then we can do some more checking.145if (Interpreter::contains(sender_pc)) {146return sender.is_interpreted_frame_valid(thread);147}148149// Could just be some random pointer within the codeBlob.150if (!sender.cb()->code_contains(sender_pc)) {151return false;152}153154// We should never be able to see an adapter if the current frame is something from code cache.155if (sender_blob->is_adapter_blob()) {156return false;157}158159if (sender.is_entry_frame()) {160return sender.is_entry_frame_valid(thread);161}162163// Frame size is always greater than zero. If the sender frame size is zero or less,164// something is really weird and we better give up.165if (sender_blob->frame_size() <= 0) {166return false;167}168169return true;170}171172// Must be native-compiled frame. Since sender will try and use fp to find173// linkages it must be safe174175if (!fp_safe) {176return false;177}178179return true;180}181182bool frame::is_interpreted_frame() const {183return Interpreter::contains(pc());184}185186frame frame::sender_for_entry_frame(RegisterMap *map) const {187assert(map != NULL, "map must be set");188// Java frame called from C; skip all C frames and return top C189// frame of that chunk as the sender.190JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();191assert(!entry_frame_is_first(), "next Java fp must be non zero");192assert(jfa->last_Java_sp() > _sp, "must be above this frame on stack");193map->clear();194assert(map->include_argument_oops(), "should be set by clear");195196if (jfa->last_Java_pc() != NULL) {197frame fr(jfa->last_Java_sp(), jfa->last_Java_pc());198return fr;199}200// Last_java_pc is not set, if we come here from compiled code. The201// constructor retrieves the PC from the stack.202frame fr(jfa->last_Java_sp());203return fr;204}205206OptimizedEntryBlob::FrameData* OptimizedEntryBlob::frame_data_for_frame(const frame& frame) const {207ShouldNotCallThis();208return nullptr;209}210211bool frame::optimized_entry_frame_is_first() const {212ShouldNotCallThis();213return false;214}215216frame frame::sender_for_interpreter_frame(RegisterMap *map) const {217// Pass callers initial_caller_sp as unextended_sp.218return frame(sender_sp(), sender_pc(), (intptr_t*)get_ijava_state()->sender_sp);219}220221frame frame::sender_for_compiled_frame(RegisterMap *map) const {222assert(map != NULL, "map must be set");223224// Frame owned by compiler.225address pc = *compiled_sender_pc_addr(_cb);226frame caller(compiled_sender_sp(_cb), pc);227228// Now adjust the map.229230// Get the rest.231if (map->update_map()) {232// Tell GC to use argument oopmaps for some runtime stubs that need it.233map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));234if (_cb->oop_maps() != NULL) {235OopMapSet::update_register_map(this, map);236}237}238239return caller;240}241242intptr_t* frame::compiled_sender_sp(CodeBlob* cb) const {243return sender_sp();244}245246address* frame::compiled_sender_pc_addr(CodeBlob* cb) const {247return sender_pc_addr();248}249250frame frame::sender_raw(RegisterMap* map) const {251// Default is we do have to follow them. The sender_for_xxx will252// update it accordingly.253map->set_include_argument_oops(false);254255if (is_entry_frame()) return sender_for_entry_frame(map);256if (is_interpreted_frame()) return sender_for_interpreter_frame(map);257assert(_cb == CodeCache::find_blob(pc()),"Must be the same");258259if (_cb != NULL) {260return sender_for_compiled_frame(map);261}262// Must be native-compiled frame, i.e. the marshaling code for native263// methods that exists in the core system.264return frame(sender_sp(), sender_pc());265}266267frame frame::sender(RegisterMap* map) const {268frame result = sender_raw(map);269270if (map->process_frames()) {271StackWatermarkSet::on_iteration(map->thread(), result);272}273274return result;275}276277void frame::patch_pc(Thread* thread, address pc) {278assert(_cb == CodeCache::find_blob(pc), "unexpected pc");279if (TracePcPatching) {280tty->print_cr("patch_pc at address " PTR_FORMAT " [" PTR_FORMAT " -> " PTR_FORMAT "]",281p2i(&((address*) _sp)[-1]), p2i(((address*) _sp)[-1]), p2i(pc));282}283own_abi()->lr = (uint64_t)pc;284if (_cb != NULL && _cb->is_nmethod() && ((nmethod*)_cb)->is_deopt_pc(_pc)) {285address orig = (((nmethod*)_cb)->get_original_pc(this));286assert(orig == _pc, "expected original to be stored before patching");287_deopt_state = is_deoptimized;288// Leave _pc as is.289} else {290_deopt_state = not_deoptimized;291_pc = pc;292}293}294295bool frame::is_interpreted_frame_valid(JavaThread* thread) const {296assert(is_interpreted_frame(), "Not an interpreted frame");297// These are reasonable sanity checks298if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {299return false;300}301if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {302return false;303}304int min_frame_slots = (abi_minframe_size + ijava_state_size) / sizeof(intptr_t);305if (fp() - min_frame_slots < sp()) {306return false;307}308// These are hacks to keep us out of trouble.309// The problem with these is that they mask other problems310if (fp() <= sp()) { // this attempts to deal with unsigned comparison above311return false;312}313314// do some validation of frame elements315316// first the method317318Method* m = *interpreter_frame_method_addr();319320// validate the method we'd find in this potential sender321if (!Method::is_valid_method(m)) return false;322323// stack frames shouldn't be much larger than max_stack elements324// this test requires the use of unextended_sp which is the sp as seen by325// the current frame, and not sp which is the "raw" pc which could point326// further because of local variables of the callee method inserted after327// method arguments328if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {329return false;330}331332// validate bci/bcx333334address bcp = interpreter_frame_bcp();335if (m->validate_bci_from_bcp(bcp) < 0) {336return false;337}338339// validate constantPoolCache*340ConstantPoolCache* cp = *interpreter_frame_cache_addr();341if (MetaspaceObj::is_valid(cp) == false) return false;342343// validate locals344345address locals = (address) *interpreter_frame_locals_addr();346return thread->is_in_stack_range_incl(locals, (address)fp());347}348349BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {350assert(is_interpreted_frame(), "interpreted frame expected");351Method* method = interpreter_frame_method();352BasicType type = method->result_type();353354if (method->is_native()) {355// Prior to calling into the runtime to notify the method exit the possible356// result value is saved into the interpreter frame.357address lresult = (address)&(get_ijava_state()->lresult);358address fresult = (address)&(get_ijava_state()->fresult);359360switch (method->result_type()) {361case T_OBJECT:362case T_ARRAY: {363*oop_result = JNIHandles::resolve(*(jobject*)lresult);364break;365}366// We use std/stfd to store the values.367case T_BOOLEAN : value_result->z = (jboolean) *(unsigned long*)lresult; break;368case T_INT : value_result->i = (jint) *(long*)lresult; break;369case T_CHAR : value_result->c = (jchar) *(unsigned long*)lresult; break;370case T_SHORT : value_result->s = (jshort) *(long*)lresult; break;371case T_BYTE : value_result->z = (jbyte) *(long*)lresult; break;372case T_LONG : value_result->j = (jlong) *(long*)lresult; break;373case T_FLOAT : value_result->f = (jfloat) *(double*)fresult; break;374case T_DOUBLE : value_result->d = (jdouble) *(double*)fresult; break;375case T_VOID : /* Nothing to do */ break;376default : ShouldNotReachHere();377}378} else {379intptr_t* tos_addr = interpreter_frame_tos_address();380switch (method->result_type()) {381case T_OBJECT:382case T_ARRAY: {383oop obj = *(oop*)tos_addr;384assert(Universe::is_in_heap_or_null(obj), "sanity check");385*oop_result = obj;386}387case T_BOOLEAN : value_result->z = (jboolean) *(jint*)tos_addr; break;388case T_BYTE : value_result->b = (jbyte) *(jint*)tos_addr; break;389case T_CHAR : value_result->c = (jchar) *(jint*)tos_addr; break;390case T_SHORT : value_result->s = (jshort) *(jint*)tos_addr; break;391case T_INT : value_result->i = *(jint*)tos_addr; break;392case T_LONG : value_result->j = *(jlong*)tos_addr; break;393case T_FLOAT : value_result->f = *(jfloat*)tos_addr; break;394case T_DOUBLE : value_result->d = *(jdouble*)tos_addr; break;395case T_VOID : /* Nothing to do */ break;396default : ShouldNotReachHere();397}398}399return type;400}401402#ifndef PRODUCT403404void frame::describe_pd(FrameValues& values, int frame_no) {405if (is_interpreted_frame()) {406#define DESCRIBE_ADDRESS(name) \407values.describe(frame_no, (intptr_t*)&(get_ijava_state()->name), #name);408409DESCRIBE_ADDRESS(method);410DESCRIBE_ADDRESS(mirror);411DESCRIBE_ADDRESS(locals);412DESCRIBE_ADDRESS(monitors);413DESCRIBE_ADDRESS(cpoolCache);414DESCRIBE_ADDRESS(bcp);415DESCRIBE_ADDRESS(esp);416DESCRIBE_ADDRESS(mdx);417DESCRIBE_ADDRESS(top_frame_sp);418DESCRIBE_ADDRESS(sender_sp);419DESCRIBE_ADDRESS(oop_tmp);420DESCRIBE_ADDRESS(lresult);421DESCRIBE_ADDRESS(fresult);422}423}424#endif425426intptr_t *frame::initial_deoptimization_info() {427// unused... but returns fp() to minimize changes introduced by 7087445428return fp();429}430431#ifndef PRODUCT432// This is a generic constructor which is only used by pns() in debug.cpp.433frame::frame(void* sp, void* fp, void* pc) : _sp((intptr_t*)sp), _unextended_sp((intptr_t*)sp) {434find_codeblob_and_set_pc_and_deopt_state((address)pc); // also sets _fp and adjusts _unextended_sp435}436437void frame::pd_ps() {}438#endif439440441