[llvm-dev] Windows: How to catch C++ exceptions in runtime-compiled code?

Stefan Gränitz via llvm-dev llvm-dev at lists.llvm.org
Mon Jun 13 09:28:50 PDT 2016


Hi Igor! Thanks for sharing your experiences.

> [...] or use another way to deal with exceptions. One of the possible workarounds is VEH
Good point, with VEH I can actually catch exceptions and use sjlj to
continue on a valid instruction! It allows us to implement a safe return
from exceptional code. For the moment that's an acceptable workaround!
Thanks!

> You can look through OpenJDK as an example.
Btw. just found a compact VEH implementation in
llvm::CrashRecoveryContext. And it works out if the box! Great!

See
http://llvm.org/docs/doxygen/html/CrashRecoveryContext_8cpp_source.html#l00144

> The problem is that Windows checks memory region of exception handler for MEM_EXECUTE_OPTION_EXECUTE_DISPATCH_ENABLE flag, that is not set by default and can't be set easily. [...] So you need to enable this flag for your memory-generated code. As far as I know it is impossible if permanent DEP is enabled.
Yes, this doesn't sound like a good option for us right now, but I think
it's useful to keep it in mind.. so I tried the following and it still
doesn't work:
* disable DEP via "bcdedit /set {current} nx AlwaysOff" (described in
http://www.thewindowsclub.com/disable-data-execution-prevention)
* reboot
* set MEM_EXECUTE_OPTION_EXECUTE_DISPATCH_ENABLE flag programmatically
via NtSetInformationProcess (based on
https://github.com/Sh1ft0x0EF/metahook/blob/master/sys_launcher.cpp#L65)
* runtime-compiled catch handler is still not considered

I have no clue what's missing here. Any ideas?

Best,
Stefan

--
https://about.me/stefan.graenitz

Am 10.06.2016 um 18:13 schrieb Timur Doumler via llvm-dev:
> Hey Igor,
> 
> Thanks a lot for your e-mail! I am Cc’ing Stefan Gränitz who is the one
> actually working on this project, hope this will be helpful.
> 
> Cheers, Timur
> 
>> On 10 Jun 2016, at 15:07, Igor Minin <igorm6387 at gmail.com
>> <mailto:igorm6387 at gmail.com>> wrote:
>>
>> Hi, Timur. I may be wrong, but it seems that I faced similar problem before. 
>>
>> The problem is that Windows checks memory region of exception handler for MEM_EXECUTE_OPTION_EXECUTE_DISPATCH_ENABLE flag, that is not set by default and can't be set easily.
>>
>> Before Windows passes control to your handler it does the following checks:
>>
>> KiUserExceptionDispatcher ->RtlDispatchException -> RtlIsValidHandler (FAIL)
>>
>> So you need to enable this flag for your memory-generated code. As far as I know it is impossible if permanent DEP is enabled. So you can either run your process without DEP (not an option in the most cases), or use another way to deal with exceptions. One of the possible workarounds is VEH. You can look through OpenJDK as an example.
> 
> 
> 
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
> 



More information about the llvm-dev mailing list