On Thu, 26 Nov 2026 00:19:16 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 ...
>
> Ashay Rane has updated the pull request incrementally with one additional
> commit since the last revision:
>
> Extend the test to run on Linux, macOS, and on Windows
>
> - To maintain compatibility with `handle_stack_overflow()` in
> os_posix.cpp, the handler for `EXCEPTION_STACK_OVERFLOW` does not
> check for `_thread_in_vm` or `_thread_in_native`. All non-Java
> executions continue execution.
>
> - TestWindowsStackOverflow (renamed to TestStackOverflowExceptions) is
> now a cross-platform test (it runs on Linux, Windows, and macOS).
>
> - That test checks the following scenarios:
>
> - Unrecoverable overflow handling (after touching a red zone page)
>
> - Recoverable overflow handling from _Java_ threads (interpreter, C1,
> and C2)
>
> - Recoverable overflow handling from _native_ threads (interpreter,
> C1, and C2)
>
> - Back-to-back recoverable overflow handling (to exercise both code
> paths on Windows)
>
> - Since this patch no longer restricts `StackLimit` growth to just
> Java-thread execution (since a native thread, too, could potentially
> bang past `StackLimit`), I've updated TestWindowsStackLimit to
> exercise both Java- and native-thread execution.
test/hotspot/jtreg/runtime/StackGuardPages/TestStackOverflowExceptions.java
line 141:
> 139: "native");
> 140: output.shouldNotHaveExitValue(0);
> 141: output.shouldContain("An irrecoverable stack overflow has
> occurred.");
TODO: This assumes that there is at least one red page, which is perhaps a
valid assumption, since we need some way to catch runaway stack accesses.
Nevertheless, add a condition around these statements so that zero red pages,
whether intentional or not, does not cause a test failure.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/34043#discussion_r4702502540