On Thu, 22 Oct 2026 00:22:23 GMT, Ashay Rane <[email protected]> wrote:

>> This patch rewrites the code in the Windows port of HotSpot for detecting,
>> handling, and reporting stack overflows.  This patch is motivated by 
>> documented
>> failures in JDK-8152598, JDK-8156538, JDK-8157521, JDK-8157924, JDK-8335362, 
>> and
>> in JDK-8067946.
>> 
>> The key result of this patch is that in addition to preventing the crashes 
>> that
>> can be reproduced using the information in the above JBS issues, HotSpot on
>> Windows now supports far-access stack bangs in compiled code while continuing
>> to leverage Windows' incremental stack growth based on guard pages, ensuring
>> that we don't sacrifice performance that would have been lost to page-by-page
>> stack bangs.  The Red and Yellow page counts now change the actual page
>> permissions and HotSpot Guard Zone size, thus impacting the page that 
>> triggers
>> the stack overflow error after stack banging.  This patch also makes 
>> HotSpot's
>> semantics for stack overflows for native and VM threads on Windows to match 
>> with
>> those for Linux and macOS, in turn paving the way for supporting Reserved 
>> Stack
>> pages on Windows.
>> 
>> This is a non-trivial change, so I have documented the key ideas 
>> [here](https://gist.github.com/raneashay/82a0e2b32b3c051e92c5686bc0905743).  
>> The
>> commits in this patch are organized (so far) to match the section headings in
>> the document:
>> 
>> - ed824007: [Section 
>> 4.1](https://gist.github.com/raneashay/82a0e2b32b3c051e92c5686bc0905743#41-growing-the-stack-on-demand)
>> - f765e79d: [Section 
>> 4.2](https://gist.github.com/raneashay/82a0e2b32b3c051e92c5686bc0905743#42-matching-hotspots-guard-zone-with-windows-stack-guarantee)
>> - 9d694f5c: [Section 
>> 4.3](https://gist.github.com/raneashay/82a0e2b32b3c051e92c5686bc0905743#43-handling-windows-stack-overflow-exceptions)
>> - edf58c4f: [Section 
>> 4.4](https://gist.github.com/raneashay/82a0e2b32b3c051e92c5686bc0905743#44-handling-subsequent-stack-overflows)
>> - ff9b65f6: [Section 
>> 4.5.1](https://gist.github.com/raneashay/82a0e2b32b3c051e92c5686bc0905743#44-handling-subsequent-stack-overflows)
>> - b3e146e9: [Section 
>> 4.5.2](https://gist.github.com/raneashay/82a0e2b32b3c051e92c5686bc0905743#44-handling-subsequent-stack-overflows)
>> - 8189a507: [Section 
>> 4.6](https://gist.github.com/raneashay/82a0e2b32b3c051e92c5686bc0905743#46-validation)
>> 
>> I've validated this patch by running through all tier 1, 2, and 3 JDK, 
>> HotSpot,
>> and LangTools tests on Windows/x64 and Windows/ARM64 in FastDebug config.
>> 
>> ---------
>> - [x] I confirm that I make this contributio...
>
> Ashay Rane has updated the pull request with a new target base due to a merge 
> or a rebase. The incremental webrev excludes the unrelated changes brought in 
> by the merge/rebase. The pull request contains 17 additional commits since 
> the last revision:
> 
>  - Merge branch 'master' into JDK-8067946-setthreadstackguarantee
>  - [Section 4.6] Add supporting tests
>    
>    - TestWindowsStackOverflow.java tests both recoverable and unrecoverable
>      overflows.
>    
>    - In particular, since recoverable overflow handling has two entry
>      points (first overflow using EXCEPTION_STACK_OVERFLOW and subsequent
>      overflows using EXCEPTION_ACCESS_VIOLATION), this test induces
>      multiple recoverable stack overflows.
>    
>    - The unrecoverable overflow test triggers a separate JVM execution that
>      executes native code that bangs into the red zone.  The triggering JVM
>      then checks the return code and the output message.
>    
>    - TestWindowsStackPages.java makes the older TestWindowsStackYellowPages
>      test more broad, by checking the default and configurable values for
>      both Red and Yellow pages.
>  - [Section 4.5.2] Match default stack size across OSes on AArch64
>    
>    Both Linux and macOS on AArch64 use a 2MB stack.  Without this patch,
>    the SOEDeeplyNestedBlockTest.java test fails on Windows/ARM64.
>  - [Section 4.5.1] Include the allocated frame in the stack bang
>  - [Section 4.4] Handle Subsequent Stack Overflows
>    
>    Update handler for EXCEPTION_ACCESS_VIOLATION to report fatal versus
>    recoverable overflow errors depending on red zone or yellow zone memory
>    access
>  - [Section 4.3] Handle Windows' Stack Overflow Exceptions
>    
>    - Change protection of guard zone to use PAGE_NOACCESS instead of
>      PAGE_GUARD
>    
>    - Update handler for EXCEPTION_STACK_OVERFLOW to detect nested overflow
>      exceptions
>    
>    - Protect the red zone with PAGE_NOACCESS to detect runaway stack usage
>    
>    - If thread is executing Java code, report a StackOverflowError
>      exception; for native or VM threads, continue execution
>  - [Section 4.2] Match HotSpot's Guard Zone with Windows' Stack Guarantee
>    
>    - Set the (logical) yellow zone size to be the same for Windows as all
>      other OSes
>    
>    - Request the stack guarantee based on the red page count and the
>      logical yellow page count
>    
>    - If the actual stack guarantee is larger than the requested value, then
>      expand the yellow zone (see `stack_yellow_reserved_zone_size()`)
>    
>    - Update the TestWind...

I've cleaned up (and pushed) the implementation, trimming a few useless 
changes.  I have validated the implementation on x64 and ARM64 boxes, using 
Windows 11 (x64 and ARM64), Windows 10 (x64 only), and Windows Server 2025 (x64 
only).

The recently pushed commits are organized to match section headings in the 
[accompanying 
document](https://gist.github.com/raneashay/82a0e2b32b3c051e92c5686bc0905743); 
hopefully that will make it easier to review the changes.

To repeat a previous comment, despite my best effort, I haven't been able to 
get rid of `EXCEPTION_STACK_OVERFLOW` (and just use 
`EXCEPTION_ACCESS_VIOLATION`).  This is indeed not ideal, but I think the 
revised implementation fixes several problems that exist in the current 
implementation.

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

PR Comment: https://git.openjdk.org/jdk/pull/32365#issuecomment-6329913829

Reply via email to