Skip to content

8346775: Improve os::current_stack_pointer #22864

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

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

TheShermanTanker
Copy link
Contributor

@TheShermanTanker TheShermanTanker commented Dec 23, 2024

os::current_stack_pointer has many varying implementations, some of which are erroneous. Additionally, this does not play well with Link Time Optimization, which often inlines the method into the caller, causing various issues such as the method return the frame pointer of the caller rather than the stack pointer, or the address of a dummy local, for instance. Improve os::current_stack_pointer to work correctly with Link Time Optimization and be more robust to errors, so that it returns the accurate value of the stack pointer, documented below

Current ways used to load the stack pointer:
BSD/Zero:

address os::current_stack_pointer() {
  address dummy = (address) &dummy;
  return dummy;
}

BSD/x86:

address os::current_stack_pointer() {
#if defined(__clang__) || defined(__llvm__)
  void *esp;
  __asm__("mov %%" SPELL_REG_SP ", %0":"=r"(esp));
  return (address) esp;
#else
  register void *esp __asm__ (SPELL_REG_SP);
  return (address) esp;
#endif
}

BSD/ARM64:

address os::current_stack_pointer() {
#if defined(__clang__) || defined(__llvm__)
  void *sp;
  __asm__("mov %0, " SPELL_REG_SP : "=r"(sp));
  return (address) sp;
#else
  register void *sp __asm__ (SPELL_REG_SP);
  return (address) sp;
#endif
}

AIX/PPC:

address os::current_stack_pointer() {
  return (address)__builtin_frame_address(0);
}

Windows/ARM64:

PRAGMA_DISABLE_MSVC_WARNING(4172)
// Returns an estimate of the current stack pointer. Result must be guaranteed
// to point into the calling threads stack, and be no lower than the current
// stack pointer.
address os::current_stack_pointer() {
  int dummy;
  address sp = (address)&dummy;
  return sp;
}

Windows/x86:

// Returns the current stack pointer. Accurate value needed for
// os::verify_stack_alignment().
address os::current_stack_pointer() {
  typedef address get_sp_func();
  get_sp_func* func = CAST_TO_FN_PTR(get_sp_func*,
                                     StubRoutines::x86::get_previous_sp_entry());
  return (*func)();
}

Linux/Zero:

address os::current_stack_pointer() {
  // return the address of the current function
  return (address)__builtin_frame_address(0);
}

Linux/x86:

address os::current_stack_pointer() {
  return (address)__builtin_frame_address(0);
}

Linux/ARM64:

NOINLINE address os::current_stack_pointer() {
  return (address)__builtin_frame_address(0);
}

Linux/ARM:

address os::current_stack_pointer() {
  register address sp __asm__ (SPELL_REG_SP);
  return sp;
}

Linux/RISC-V:

NOINLINE address os::current_stack_pointer() {
  return (address)__builtin_frame_address(0);
}

Linux/s390x:

address os::current_stack_pointer() {
  intptr_t* csp;

  // Inline assembly for `z_lgr regno(csp), Z_SP' (Z_SP = Z_R15):
  __asm__ __volatile__ ("lgr %0, 15":"=r"(csp):);

  assert(((uint64_t)csp & (frame::alignment_in_bytes-1)) == 0, "SP must be aligned");
  return (address) csp;
}

Linux/PPC:

address os::current_stack_pointer() {
  return (address)__builtin_frame_address(0);
}

The assembly below indicates how to load the stack pointer in assembly for each supported architecture (Created via __builtin_stack_address with gcc):

void *current_stack_pointer() noexcept {
    return reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(__builtin_stack_address()) + sizeof (void *));
}

x64:

lea     rax, [rsp+8]
mov     rax, rsp
ret

x86:

lea     eax, [esp+4]
mov     eax, esp
ret

ARM64:

add     x0, sp, 8
mov     x0, sp
ret

ARM:

add     r0, sp, #4
mov     r0, sp
bx      lr

RISC-V:

addi    a0,sp,8
mv      a0,sp
ret

s390x:

la      %r2,8(%r15)
lgr     %r2,%r15
br      %r14

PPC:

addi 3,1,8
mr 3,1
blr

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8346775: Improve os::current_stack_pointer (Enhancement - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/22864/head:pull/22864
$ git checkout pull/22864

Update a local copy of the PR:
$ git checkout pull/22864
$ git pull https://git.openjdk.org/jdk.git pull/22864/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 22864

View PR using the GUI difftool:
$ git pr show -t 22864

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/22864.diff

@TheShermanTanker TheShermanTanker marked this pull request as draft December 23, 2024 12:37
@bridgekeeper
Copy link

bridgekeeper bot commented Dec 23, 2024

👋 Welcome back jwaters! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Dec 23, 2024

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot changed the title 8346775 8346775: Improve os::current_stack_pointer Dec 23, 2024
@openjdk
Copy link

openjdk bot commented Dec 23, 2024

@TheShermanTanker The following label will be automatically applied to this pull request:

  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 17, 2025

@TheShermanTanker This pull request has been inactive for more than 8 weeks and will be automatically closed if another 8 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 14, 2025

@TheShermanTanker This pull request has been inactive for more than 16 weeks and will now be automatically closed. If you would like to continue working on this pull request in the future, feel free to reopen it! This can be done using the /open pull request command.

@bridgekeeper bridgekeeper bot closed this Apr 14, 2025
@TheShermanTanker
Copy link
Contributor Author

No.

/open

@openjdk openjdk bot reopened this Apr 14, 2025
@openjdk
Copy link

openjdk bot commented Apr 14, 2025

@TheShermanTanker This pull request is now open

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 9, 2025

@TheShermanTanker This pull request has been inactive for more than 8 weeks and will be automatically closed if another 8 weeks passes without any activity. To avoid this, simply issue a /touch or /keepalive command to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@TheShermanTanker
Copy link
Contributor Author

/touch

@openjdk
Copy link

openjdk bot commented Jun 10, 2025

@TheShermanTanker The pull request is being re-evaluated and the inactivity timeout has been reset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

1 participant