Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8268178: Extract sender frame parsing to CodeBlob::FrameParser #4337

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/hotspot/cpu/aarch64/codeBlob_aarch64.cpp
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, Datadog, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#include "precompiled.hpp"
#include "code/codeBlob.hpp"
#include "code/compiledMethod.hpp"
#include "interpreter/interpreter.hpp"
#include "runtime/frame.hpp"

bool CodeBlob::FrameParser::sender_frame(JavaThread *thread, address pc, intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, bool fp_safe,
address* sender_pc, intptr_t** sender_sp, intptr_t** sender_unextended_sp, intptr_t*** saved_fp) {
// must be some sort of compiled/runtime frame
// fp does not have to be safe (although it could be check for c1?)

assert(sender_pc != NULL, "invariant");
assert(sender_sp != NULL, "invariant");

// check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
if (_cb->frame_size() <= 0) {
return false;
}

*sender_sp = unextended_sp + _cb->frame_size();
// Is sender_sp safe?
if (thread != NULL && !thread->is_in_full_stack_checked((address)*sender_sp)) {
return false;
}
*sender_pc = (address)*((*sender_sp) - frame::return_addr_offset);

if (sender_unextended_sp) *sender_unextended_sp = *sender_sp;
// Note: frame::sender_sp_offset is only valid for compiled frame
if (saved_fp) *saved_fp = (intptr_t**)((*sender_sp) - frame::sender_sp_offset);

return true;
}

bool InterpreterBlob::FrameParser::sender_frame(JavaThread *thread, address pc, intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, bool fp_safe,
address* sender_pc, intptr_t** sender_sp, intptr_t** sender_unextended_sp, intptr_t*** saved_fp) {

assert(sender_pc != NULL, "invariant");
assert(sender_sp != NULL, "invariant");

// fp must be safe
if (!fp_safe) {
return false;
}

*sender_pc = (address)*(fp + frame::return_addr_offset);
// for interpreted frames, the value below is the sender "raw" sp,
// which can be different from the sender unextended sp (the sp seen
// by the sender) because of current frame local variables
*sender_sp = fp + frame::sender_sp_offset;

if (sender_unextended_sp) *sender_unextended_sp = (intptr_t*)*(fp + frame::interpreter_frame_sender_sp_offset);
if (saved_fp) *saved_fp = (intptr_t**)(fp + frame::link_offset);

return true;
}
75 changes: 25 additions & 50 deletions src/hotspot/cpu/aarch64/frame_aarch64.cpp
Expand Up @@ -56,6 +56,8 @@ void RegisterMap::check_location_valid() {
// Profiling/safepoint support

bool frame::safe_for_sender(JavaThread *thread) {
ResourceMark rm;

address sp = (address)_sp;
address fp = (address)_fp;
address unextended_sp = (address)_unextended_sp;
Expand Down Expand Up @@ -117,60 +119,30 @@ bool frame::safe_for_sender(JavaThread *thread) {
return fp_safe && is_entry_frame_valid(thread);
}

intptr_t* sender_sp = NULL;
intptr_t* sender_unextended_sp = NULL;
address sender_pc = NULL;
intptr_t* saved_fp = NULL;

if (is_interpreted_frame()) {
// fp must be safe
if (!fp_safe) {
return false;
}

sender_pc = (address) this->fp()[return_addr_offset];
// for interpreted frames, the value below is the sender "raw" sp,
// which can be different from the sender unextended sp (the sp seen
// by the sender) because of current frame local variables
sender_sp = (intptr_t*) addr_at(sender_sp_offset);
sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
saved_fp = (intptr_t*) this->fp()[link_offset];

} else {
// must be some sort of compiled/runtime frame
// fp does not have to be safe (although it could be check for c1?)

// check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
if (_cb->frame_size() <= 0) {
return false;
}

sender_sp = _unextended_sp + _cb->frame_size();
// Is sender_sp safe?
if (!thread->is_in_full_stack_checked((address)sender_sp)) {
return false;
}
sender_unextended_sp = sender_sp;
sender_pc = (address) *(sender_sp-1);
// Note: frame::sender_sp_offset is only valid for compiled frame
saved_fp = (intptr_t*) *(sender_sp - frame::sender_sp_offset);
intptr_t* sender_sp = NULL;
intptr_t* sender_unextended_sp = NULL;
address sender_pc = NULL;
intptr_t** saved_fp = NULL;
if (!_cb->frame_parser()->sender_frame(
thread, _pc, (intptr_t*)sp, (intptr_t*)unextended_sp, (intptr_t*)fp, fp_safe,
&sender_pc, &sender_sp, &sender_unextended_sp, &saved_fp)) {
return false;
}


// If the potential sender is the interpreter then we can do some more checking
if (Interpreter::contains(sender_pc)) {

// fp is always saved in a recognizable place in any code we generate. However
// only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved fp
// is really a frame pointer.

if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
if (!thread->is_in_stack_range_excl((address)*saved_fp, (address)sender_sp)) {
return false;
}

// construct the potential sender

frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
frame sender(sender_sp, sender_unextended_sp, *saved_fp, sender_pc);

return sender.is_interpreted_frame_valid(thread);

Expand Down Expand Up @@ -199,13 +171,13 @@ bool frame::safe_for_sender(JavaThread *thread) {

// Could be the call_stub
if (StubRoutines::returns_to_call_stub(sender_pc)) {
if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
if (!thread->is_in_stack_range_excl((address)*saved_fp, (address)sender_sp)) {
return false;
}

// construct the potential sender

frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
frame sender(sender_sp, sender_unextended_sp, *saved_fp, sender_pc);

// Validate the JavaCallWrapper an entry frame must have
address jcw = (address)sender.entry_frame_call_wrapper();
Expand Down Expand Up @@ -453,18 +425,21 @@ frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
//------------------------------------------------------------------------------
// frame::sender_for_compiled_frame
frame frame::sender_for_compiled_frame(RegisterMap* map) const {
ResourceMark rm;

// we cannot rely upon the last fp having been saved to the thread
// in C2 code but it will have been pushed onto the stack. so we
// have to find it relative to the unextended sp

assert(_cb->frame_size() >= 0, "must have non-zero frame size");
intptr_t* l_sender_sp = unextended_sp() + _cb->frame_size();
intptr_t* unextended_sp = l_sender_sp;

// the return_address is always the word on the stack
address sender_pc = (address) *(l_sender_sp-1);

intptr_t** saved_fp_addr = (intptr_t**) (l_sender_sp - frame::sender_sp_offset);
intptr_t* l_sender_sp;
intptr_t* l_unextended_sp;
address l_sender_pc;
intptr_t** l_saved_fp;
_cb->frame_parser()->sender_frame(
NULL, pc(), sp(), unextended_sp(), fp(), true,
&l_sender_pc, &l_sender_sp, &l_unextended_sp, &l_saved_fp);

// assert (sender_sp() == l_sender_sp, "should be");
// assert (*saved_fp_addr == link(), "should be");
Expand All @@ -482,10 +457,10 @@ frame frame::sender_for_compiled_frame(RegisterMap* map) const {
// oopmap for it so we must fill in its location as if there was
// an oopmap entry since if our caller was compiled code there
// could be live jvm state in it.
update_map_with_saved_link(map, saved_fp_addr);
update_map_with_saved_link(map, l_saved_fp);
}

return frame(l_sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
return frame(l_sender_sp, l_unextended_sp, *l_saved_fp, l_sender_pc);
}

//------------------------------------------------------------------------------
Expand Down
70 changes: 70 additions & 0 deletions src/hotspot/cpu/arm/codeBlob_arm.cpp
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, Datadog, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#include "precompiled.hpp"
#include "code/codeBlob.hpp"
#include "code/compiledMethod.hpp"
#include "interpreter/interpreter.hpp"
#include "runtime/frame.hpp"

bool CodeBlob::FrameParser::sender_frame(JavaThread *thread, address pc, intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, bool fp_safe,
address* sender_pc, intptr_t** sender_sp, intptr_t** sender_unextended_sp, intptr_t*** saved_fp) {
// must be some sort of compiled/runtime frame
// fp does not have to be safe (although it could be check for c1?)

assert(sender_pc != NULL, "invariant");
assert(sender_sp != NULL, "invariant");

*sender_sp = unextended_sp + _cb->frame_size();
// Is sender_sp safe?
if (thread != NULL && !thread->is_in_full_stack_checked((address)*sender_sp)) {
return false;
}
// With our calling conventions, the return_address should
// end up being the word on the stack
*sender_pc = (address)*((*sender_sp) - frame::sender_sp_offset + frame::return_addr_offset);

if (sender_unextended_sp) *sender_unextended_sp = *sender_sp;
if (saved_fp) *saved_fp = (intptr_t**)((*sender_sp) - frame::sender_sp_offset + frame::link_offset);

return true;
}

bool InterpreterBlob::FrameParser::sender_frame(JavaThread *thread, address pc, intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, bool fp_safe,
address* sender_pc, intptr_t** sender_sp, intptr_t** sender_unextended_sp, intptr_t*** saved_fp) {

assert(sender_pc != NULL, "invariant");
assert(sender_sp != NULL, "invariant");

// fp must be safe
if (!fp_safe) {
return false;
}

*sender_pc = (address)*(fp + frame::return_addr_offset);
*sender_sp = fp + frame::sender_sp_offset;

return true;
}
51 changes: 19 additions & 32 deletions src/hotspot/cpu/arm/frame_arm.cpp
Expand Up @@ -54,6 +54,8 @@ void RegisterMap::check_location_valid() {
// Profiling/safepoint support

bool frame::safe_for_sender(JavaThread *thread) {
ResourceMark rm;

address sp = (address)_sp;
address fp = (address)_fp;
address unextended_sp = (address)_unextended_sp;
Expand Down Expand Up @@ -98,28 +100,10 @@ bool frame::safe_for_sender(JavaThread *thread) {

intptr_t* sender_sp = NULL;
address sender_pc = NULL;

if (is_interpreted_frame()) {
// fp must be safe
if (!fp_safe) {
return false;
}

sender_pc = (address) this->fp()[return_addr_offset];
sender_sp = (intptr_t*) addr_at(sender_sp_offset);

} else {
// must be some sort of compiled/runtime frame
// fp does not have to be safe (although it could be check for c1?)

sender_sp = _unextended_sp + _cb->frame_size();
// Is sender_sp safe?
if (!thread->is_in_full_stack_checked((address)sender_sp)) {
return false;
}
// With our calling conventions, the return_address should
// end up being the word on the stack
sender_pc = (address) *(sender_sp - sender_sp_offset + return_addr_offset);
if (!_cb->frame_parser()->sender_frame(
thread, _pc, (intptr_t*)sp, (intptr_t*)unextended_sp, (intptr_t*)fp, fp_safe,
&sender_pc, &sender_sp, NULL, NULL)) {
return false;
}

// We must always be able to find a recognizable pc
Expand Down Expand Up @@ -388,18 +372,21 @@ frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
}

frame frame::sender_for_compiled_frame(RegisterMap* map) const {
ResourceMark rm;

assert(map != NULL, "map must be set");

// frame owned by optimizing compiler
assert(_cb->frame_size() >= 0, "must have non-zero frame size");
intptr_t* sender_sp = unextended_sp() + _cb->frame_size();
intptr_t* unextended_sp = sender_sp;

address sender_pc = (address) *(sender_sp - sender_sp_offset + return_addr_offset);
assert(_cb->frame_size() >= 0, "must have non-zero frame size");

// This is the saved value of FP which may or may not really be an FP.
// It is only an FP if the sender is an interpreter frame (or C1?).
intptr_t** saved_fp_addr = (intptr_t**) (sender_sp - sender_sp_offset + link_offset);
intptr_t* l_sender_sp;
intptr_t* l_unextended_sp;
address l_sender_pc;
intptr_t** l_saved_fp;
_cb->frame_parser()->sender_frame(
NULL, pc(), sp(), unextended_sp(), fp(), true,
&l_sender_pc, &l_sender_sp, &l_unextended_sp, &l_saved_fp);

if (map->update_map()) {
// Tell GC to use argument oopmaps for some runtime stubs that need it.
Expand All @@ -413,11 +400,11 @@ frame frame::sender_for_compiled_frame(RegisterMap* map) const {
// Since the prolog does the save and restore of FP there is no oopmap
// for it so we must fill in its location as if there was an oopmap entry
// since if our caller was compiled code there could be live jvm state in it.
update_map_with_saved_link(map, saved_fp_addr);
update_map_with_saved_link(map, l_saved_fp);
}

assert(sender_sp != sp(), "must have changed");
return frame(sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
assert(l_sender_sp != sp(), "must have changed");
return frame(l_sender_sp, l_unextended_sp, *l_saved_fp, l_sender_pc);
}

frame frame::sender(RegisterMap* map) const {
Expand Down