On Tue, 24 Nov 2026 14:55:06 GMT, Ashay Rane <[email protected]> wrote:

> Prior to this patch, HotSpot on Windows used `PAGE_GUARD` protection
> (which creates so-called Guard Pages) for Yellow and Red pages, but
> guard pages provide a one-shot alarm that is handled by the OS and not
> by HotSpot's signal handler, so HotSpot doesn't have control over how
> Yellow and Red page accesses are handled.  Still, stack overflow
> handling works, although by coincidence and not under all conditions,
> since HotSpot's Yellow zone boundary happens to match Windows' default
> terminal stack growth page.  The end result is that changing HotSpot's
> Yellow or Red page count doesn't have the intended effect on stack
> handling.
> 
> As a specific example, when Windows delivers `EXCEPTION_STACK_OVERFLOW`
> to HotSpot, both Yellow and Red pages end up being writeable, so the Red
> zone protection is effectively nullified.  Continued stack usage
> eventually terminates the JVM, but that's because those accesses land in
> the so-called bottom page (which again, just happens to match the lone
> Red page in HotSpot).  Unless specified, Windows marks this bottom page
> as non-readable and non-writeable, whereas in HotSpot, we may need to
> use the Red zone pages, so HotSpot cannot rely on default Windows
> behavior.  Put another way, if the Windows' defaults were to change,
> HotSpot's stack overflow handling will break.
> 
> This patch brings stack overflow handling under HotSpot's control,
> primarily by switching to `PAGE_NOACCESS` protection for Yellow and Red
> pages.  HotSpot now responds to the `EXCEPTION_STACK_OVERFLOW` exception
> (triggered by Windows running out of stack pages to commit) and to the
> `EXCEPTION_ACCESS_VIOLATION` exception (triggered by accesses to Red and
> Yellow pages).  This patch includes some ancillary fixes, like detecting
> nested stack overflow exceptions and unexpected thread states, in
> addition to overall simplification of the code.
> 
> In the interest of limiting the diff size, this patch does not address
> the coincidence that HotSpot's existing Yellow pages align with Windows'
> terminal growth page.  Fixing that requires dynamically inflating
> HotSpot's Yellow zone size depending on Windows' terminal growth page,
> which is a function of the thread stack guarantee.  A subsequent patch
> will query and set the Windows thread stack guarantee at run-time to
> adjust the yellow zone size.
> 
> I validated this patch by running through tier 1 through 3 tests in the
> JDK, HotSpot, and LangTools test groups on both Windows/x64 and
> Windows/ARM64.
> 
> ---------
> - [x] I confirm that I make this...

Thanks for tackling this Ashay!

I've started taking a look at this but it is a very complex area - given we've 
done it this way since day one, and have known it doesn't work as intended for 
just on 12 years now. I can't claim any expertise on this code so my review is 
fairly superficial. The real test of this will be putting it into operation and 
seeing that it operates exactly as expected. To that end what common tests do 
we have (if any?) that check that accessing a red or yellow page produces the 
expected result on all platforms? Can we verify that stackoverflow is now 
handled consistently on all OS?

I have a few minor comments below. Thanks

src/hotspot/os/windows/os_windows.cpp line 2715:

> 2713: static void report_fatal_stack_overflow(JavaThread* thread,
> 2714:     struct _EXCEPTION_POINTERS* exceptionInfo, address pc,
> 2715:     const char* message, bool disable_red_zone) {

Suggestion:

static void report_fatal_stack_overflow(JavaThread* thread,
                                        struct _EXCEPTION_POINTERS* 
exceptionInfo, address pc,
                                        const char* message, bool 
disable_red_zone) {

src/hotspot/os/windows/os_windows.cpp line 2727:

> 2725: 
> 2726: static LONG handle_stack_overflow_in_yellow_zone(JavaThread* thread,
> 2727:     struct _EXCEPTION_POINTERS* exceptionInfo, address pc) {

Suggestion:

static LONG handle_stack_overflow_in_yellow_zone(JavaThread* thread,
                                                 struct _EXCEPTION_POINTERS* 
exceptionInfo, address pc) {

src/hotspot/os/windows/os_windows.cpp line 2758:

> 2756:   }
> 2757: 
> 2758:   // The thread is in an unexpected state, so terminate out of abundant 
> caution.

I think `_thread_blocked` can be handled the same as `_thread_in_vm`. Though 
per the original code I think all we need to distinguish is `in java` or not.

src/hotspot/os/windows/os_windows.cpp line 2814:

> 2812:     if (exception_code == EXCEPTION_STACK_OVERFLOW) {
> 2813:       // After reporting a stack overflow exception, Windows makes the 
> terminal
> 2814:       // growth guard page, the stack guaratee pages, and the bottom 
> page both

Suggestion:

      // growth guard page, the stack guarantee pages, and the bottom page both

test/hotspot/jtreg/runtime/StackGuardPages/TestWindowsStackOverflow.java line 
28:

> 26:  * @bug 8339569
> 27:  * @summary Verifies both recoverable and unrecoverable Windows stack 
> overflow handling
> 28:  * @requires os.family == "windows" & (os.arch == "amd64" | os.arch == 
> "aarch64")

Do we need the arch check? What else is there?

test/hotspot/jtreg/runtime/StackGuardPages/TestWindowsStackOverflow.java line 
29:

> 27:  * @summary Verifies both recoverable and unrecoverable Windows stack 
> overflow handling
> 28:  * @requires os.family == "windows" & (os.arch == "amd64" | os.arch == 
> "aarch64")
> 29:  * @requires !jdk.static

What is the issue with the static build? Perhaps add a `@comment` to explain.

test/hotspot/jtreg/runtime/StackGuardPages/TestWindowsStackOverflow.java line 
33:

> 31:  * @run main/native ${test.main.class} fatal
> 32:  * @run main/othervm -Xcomp -XX:TieredStopAtLevel=1 ${test.main.class} 
> recoverable
> 33:  * @run main/othervm -Xcomp -XX:-TieredCompilation ${test.main.class} 
> recoverable

If you are using -Xcomp explicitly then it would be better to split out into a 
separate @test block that is flagless, else this at best will waste resource in 
higher tiers and at worst won't work correctly with some flag combinations.

test/hotspot/jtreg/runtime/StackGuardPages/TestWindowsStackOverflow.java line 
73:

> 71:         // HotSpot should have terminated with an unrecoverable overflow.
> 72:         String msg = "Unexpected return from native red-zone bang, 
> expected termination";
> 73:         throw new AssertionError(msg);

Just use `RuntimeException` as this really isn't an assertion.

test/hotspot/jtreg/runtime/StackGuardPages/TestWindowsStackOverflow.java line 
89:

> 87:                 caughtCount += 1;
> 88:             }
> 89:         }

How does this test both kinds? Once we get the first SOE each loop iteration 
should act exactly the same. Or does the first SOE cause something to change 
for the subsequent ones?

-------------

PR Review: https://git.openjdk.org/jdk/pull/34043#pullrequestreview-6048425795
PR Review Comment: https://git.openjdk.org/jdk/pull/34043#discussion_r4687423338
PR Review Comment: https://git.openjdk.org/jdk/pull/34043#discussion_r4687480038
PR Review Comment: https://git.openjdk.org/jdk/pull/34043#discussion_r4687508926
PR Review Comment: https://git.openjdk.org/jdk/pull/34043#discussion_r4687513543
PR Review Comment: https://git.openjdk.org/jdk/pull/34043#discussion_r4687560870
PR Review Comment: https://git.openjdk.org/jdk/pull/34043#discussion_r4687567015
PR Review Comment: https://git.openjdk.org/jdk/pull/34043#discussion_r4687606450
PR Review Comment: https://git.openjdk.org/jdk/pull/34043#discussion_r4687628227
PR Review Comment: https://git.openjdk.org/jdk/pull/34043#discussion_r4687652235

Reply via email to