On Fri, 4 Dec 2026 20:05:39 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 two additional 
> commits since the last revision:
> 
>  - Disable red zone in every call to `report_fatal_stack_overflow()`
>    
>    The double negation in setting `disable_red_zone` to false makes it
>    confusing to ascertain whether the value is correct.  It seems simpler
>    (and semantically valid) to instead unguard the red zone prior to
>    calling the unrecoverable handler.
>  - Address PR comments
>    
>    1. Make `disable_stack_reserved_zone()` and
>       `disable_stack_yellow_reserved_zone()` return a boolean based on
>       their success.  This way, if the unguarding step fails, we abort
>       early instead of possibly running into an infinite loop, especially
>       on Windows.
>    
>    2. Since the signal handlers on (Linux and macOS) Zero builds don't
>       handle stack overflows, the test is skipped on Zero builds.

Thanks Dean for reviewing the changes.

> Have you tried testing a Zero build? Codex thinks the new tests will 
> fail/crash.

Thanks, I had no idea what a Zero build was, but you're right, the Linux and 
macOS Zero build ports don't handle stack overflows in their signal handlers, 
so the tests did indeed fail.  I've added a precondition for the failing tests.

> Codex thinks that it should be a fatal error here if 
> disable_stack_yellow_reserved_zone() failed, because continuing execution 
> below will lead to an infinite trap loop.

That is correct as well.  I temporarily swiched `os::unguard_memory()` in 
os_windows.cpp to be a NOP and the native overflow does indeed loop 
indefinitely.  I've updated the code so that we check whether the unguard 
operations on both yellow and red zones worked, and if not, to stop early.

> Codex thinks the test could pass with a false positive if one path ran twice 
> instead of both paths running once each.

This one is a bit tricky.  This is a best-efforts check, so yes, it's possible 
that `EXCEPTION_STACK_OVERFLOW` has already occurred before we even started the 
test, in which case, we'd only test `EXCEPTION_ACCESS_OVERFLOW` followed by 
`EXCEPTION_ACCESS_OVERFLOW`.  I don't think we would ever run into an 
`EXCEPTION_STACK_OVERFLOW` followed by another `EXCEPTION_STACK_OVERFLOW`, 
since (as I understand) the first one will change all page permissions that 
make another `EXCEPTION_STACK_OVERFLOW` impossible.  Nevertheless, perhaps the 
alternative is to add a test-specific vectored exception handler that counts 
the number of stack overflow versus access violation exceptions, but I'm not 
sure if both Windows/x64 and Windows/ARM64 would handle it the same way.  Let 
me know what you think.

I did make one other change, which was to drop the `disable_red_zone` argument 
to `report_fatal_stack_overflow()`, since I myself got confused when to set it, 
often because of the double negation.  I think it doesn't change the semantics, 
but let me know please if you disagree.  Thanks!

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

PR Comment: https://git.openjdk.org/jdk/pull/34043#issuecomment-7422997599

Reply via email to