RFR(S): Use Vectored Exception Handling on Windows

Ludovic Henry luhenry at microsoft.com
Fri Jun 19 16:30:33 UTC 2020


Perfect, thank you.

-----Original Message-----
From: hotspot-runtime-dev <hotspot-runtime-dev-bounces at openjdk.java.net> On Behalf Of Ioi Lam
Sent: Friday, June 19, 2020 8:54 AM
To: hotspot-runtime-dev at openjdk.java.net
Subject: Re: RFR(S): Use Vectored Exception Handling on Windows

Hi Ludovic,

Thanks for the proposal. I've created

https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbugs.openjdk.java.net%2Fbrowse%2FJDK-8247941&data=02%7C01%7Cluhenry%40microsoft.com%7C174f628cdfc94277cf5008d814695fd5%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637281790111818398&sdata=OE%2BTD7YLYD3QmtO2WNWER7FWuqTkUEyHRbvRVInLIjc%3D&reserved=0 Use Vectored Exception 
Handling on Windows

Thanks
- Ioi


On 6/19/20 8:22 AM, Ludovic Henry wrote:
> Hello,
>
> First, some context and definitions:
> - when talking about exception here, I'm talking about Win32 exception which are equivalent to signals on Linux and other Unix, I am _not_ talking about Java exceptions.
> - an explanation of an _exception filter_ can be found at https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcpp%2Fcpp%2Fwriting-an-exception-filter%3Fview%3Dvs-2019&data=02%7C01%7Cluhenry%40microsoft.com%7C174f628cdfc94277cf5008d814695fd5%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637281790111818398&sdata=FAxeoVnq59b8FlVGr5galbErytEWXdcjUMQxBACrR5M%3D&reserved=0. There is only a limited concept of that in Java with type-based exception filter (ex: `try { ... } catch (IOException ioe) { ... } catch (Throwable t) { ... }`).
> - in Win32, there exist two exception handling mechanism:
>    - Structured Exception Handling: the historical one, based on `__try {} __except (...) {}`
>    - Vectored Exception Handling: introduced in Windows XP / Windows Server 2003, much more similar to signals on Linux
>
> These exception handling mechanisms are used to catch any exceptions like Access Violation, Stack Overflow, Divide by Zero, Overflow, and more. These exceptions are equivalent to signal on Linux and are then core to many mechanisms in the OpenJDK.
>
> Today, the OpenJDK uses Structured Exception Handling to catch such exceptions, creating several requirements. First, all code that might trigger an exception on purpose (like a Access Violation / SIGSEGV in the arraycopy stub), needs to be wrapped up in a __try / __except. Because it's not feasible to wrap every single instance of such code, these __try / __except are put at the top-level most function of any thread started by the runtime. Second, for code generated by Hotspot, `RtlAddFunctionTable` is used to simulate the use of __try / __except for a specific code area. This function needs platform specific code with the generation of  a trampoline that calls the exception filter declared in the runtime. It's also meant to be used as a one to one mapping with try / catch in user code, and not as a "catch all the exceptions in this code area". Third, Structured Exception Handling expects to be able to unwind the stack. However, because Hotspot doesn't guarantee the usage of the platform-specific ABI internally, the platform-specific unwinder might break. Hotspot's usage of `RtlAddFunctionTable` for the code cache relies on the assumption that Structured Exception Handling never tries to unwind the stack (which it would fail to do because of the different ABI) before calling the registered exception filter.
>
> Discussing that with Windows Kernel maintainers, this approach is highly discouraged, considered brittle, and the better solution is Vectored Exception Handling. Vectored Exception Handling is conceptually much more similar to signal / sigaction on Linux and other Unix systems. It will catch all exceptions happening across the process, and no __try / __except will be required. It also removes the requirement to call `RtlAddFunctionTable`.  The exception filter then behaves like a signal handler with the possibility to modify the registers at will, modifying the PC to step over an instruction after an expected Access Violation for example. Vectored Exception Handling is also already used for AOT code.
>
> The changes can be found at https://nam06.safelinks.protection.outlook.com/?url=http:%2F%2Fcr.openjdk.java.net%2F~burban%2Fludovic_vecexc%2F&data=02%7C01%7Cluhenry%40microsoft.com%7C174f628cdfc94277cf5008d814695fd5%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637281790111828393&sdata=%2BFqGQdJc0TdbsQapmaKDIetCCq%2BjSusntiC7hxPA8Cs%3D&reserved=0. As I am not an author, I have not created a corresponding bug in JBS.
>
> Thank you, and looking forward for your feedback!
>
> --
> Ludovic
>
>




More information about the hotspot-runtime-dev mailing list