From 1c84050610e778010a2ce3a25d48fceee87af6cc Mon Sep 17 00:00:00 2001 From: Ron Pressler Date: Thu, 19 Jan 2023 15:34:01 +0000 Subject: [PATCH] 8298400: Virtual thread instability when stack overflows Co-authored-by: Fei Yang Co-authored-by: Richard Reingruber Reviewed-by: dlong, pchilanomate --- src/hotspot/cpu/aarch64/sharedRuntime_aarch64.cpp | 9 +++++++++ src/hotspot/cpu/ppc/sharedRuntime_ppc.cpp | 13 +++++++++++++ src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp | 9 +++++++++ src/hotspot/cpu/x86/sharedRuntime_x86_64.cpp | 9 +++++++++ .../share/runtime/continuationFreezeThaw.cpp | 4 ++-- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/hotspot/cpu/aarch64/sharedRuntime_aarch64.cpp b/src/hotspot/cpu/aarch64/sharedRuntime_aarch64.cpp index 7f4fcae1f9316..296c1905cc788 100644 --- a/src/hotspot/cpu/aarch64/sharedRuntime_aarch64.cpp +++ b/src/hotspot/cpu/aarch64/sharedRuntime_aarch64.cpp @@ -1218,6 +1218,15 @@ static void gen_continuation_yield(MacroAssembler* masm, __ bind(pinned); // pinned -- return to caller + // handle pending exception thrown by freeze + __ ldr(rscratch1, Address(rthread, in_bytes(Thread::pending_exception_offset()))); + Label ok; + __ cbz(rscratch1, ok); + __ leave(); + __ lea(rscratch1, RuntimeAddress(StubRoutines::forward_exception_entry())); + __ br(rscratch1); + __ bind(ok); + __ leave(); __ ret(lr); diff --git a/src/hotspot/cpu/ppc/sharedRuntime_ppc.cpp b/src/hotspot/cpu/ppc/sharedRuntime_ppc.cpp index 2cd3323f181c9..27e2bae6ab708 100644 --- a/src/hotspot/cpu/ppc/sharedRuntime_ppc.cpp +++ b/src/hotspot/cpu/ppc/sharedRuntime_ppc.cpp @@ -1990,6 +1990,19 @@ static void gen_continuation_yield(MacroAssembler* masm, __ bind(L_pinned); // pinned -- return to caller + // handle pending exception thrown by freeze + Label ok; + __ ld(tmp, in_bytes(JavaThread::pending_exception_offset()), R16_thread); + __ cmpdi(CCR0, tmp, 0); + __ beq(CCR0, ok); + __ pop_frame(); + __ ld(R0, _abi0(lr), R1_SP); // Return pc + __ mtlr(R0); + __ load_const_optimized(tmp, StubRoutines::forward_exception_entry(), R0); + __ mtctr(tmp); + __ bctr(); + __ bind(ok); + // Pop frame and return __ pop_frame(); __ ld(R0, _abi0(lr), R1_SP); // Return pc diff --git a/src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp b/src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp index d2992b9012b0f..56899a3a23428 100644 --- a/src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp +++ b/src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp @@ -1095,6 +1095,15 @@ static void gen_continuation_yield(MacroAssembler* masm, __ bind(pinned); // pinned -- return to caller + // handle pending exception thrown by freeze + __ ld(t0, Address(xthread, in_bytes(Thread::pending_exception_offset()))); + Label ok; + __ beqz(t0, ok); + __ leave(); + __ la(t0, RuntimeAddress(StubRoutines::forward_exception_entry())); + __ jr(t0); + __ bind(ok); + __ leave(); __ ret(); diff --git a/src/hotspot/cpu/x86/sharedRuntime_x86_64.cpp b/src/hotspot/cpu/x86/sharedRuntime_x86_64.cpp index 4ebdaa04aaeaf..8a4a7aa22b119 100644 --- a/src/hotspot/cpu/x86/sharedRuntime_x86_64.cpp +++ b/src/hotspot/cpu/x86/sharedRuntime_x86_64.cpp @@ -1587,6 +1587,15 @@ static void gen_continuation_yield(MacroAssembler* masm, __ bind(L_pinned); // Pinned, return to caller + + // handle pending exception thrown by freeze + __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), NULL_WORD); + Label ok; + __ jcc(Assembler::equal, ok); + __ leave(); + __ jump(RuntimeAddress(StubRoutines::forward_exception_entry())); + __ bind(ok); + __ leave(); __ ret(0); } diff --git a/src/hotspot/share/runtime/continuationFreezeThaw.cpp b/src/hotspot/share/runtime/continuationFreezeThaw.cpp index 95b189887bed7..436da04f8ee0d 100644 --- a/src/hotspot/share/runtime/continuationFreezeThaw.cpp +++ b/src/hotspot/share/runtime/continuationFreezeThaw.cpp @@ -274,7 +274,7 @@ class Config { static bool stack_overflow_check(JavaThread* thread, int size, address sp) { const int page_size = os::vm_page_size(); if (size > page_size) { - if (sp - size < thread->stack_overflow_state()->stack_overflow_limit()) { + if (sp - size < thread->stack_overflow_state()->shadow_zone_safe_limit()) { return false; } } @@ -1259,7 +1259,7 @@ NOINLINE void FreezeBase::finish_freeze(const frame& f, const frame& top) { inline bool FreezeBase::stack_overflow() { // detect stack overflow in recursive native code JavaThread* t = !_preempt ? _thread : JavaThread::current(); assert(t == JavaThread::current(), ""); - if (os::current_stack_pointer() < t->stack_overflow_state()->stack_overflow_limit()) { + if (os::current_stack_pointer() < t->stack_overflow_state()->shadow_zone_safe_limit()) { if (!_preempt) { ContinuationWrapper::SafepointOp so(t, _cont); // could also call _cont.done() instead Exceptions::_throw_msg(t, __FILE__, __LINE__, vmSymbols::java_lang_StackOverflowError(), "Stack overflow while freezing");