Stefan de Bruijn via llvm-dev
2016-Sep-06 08:44 UTC
[llvm-dev] LLVM MCJIT SEH Exception handling
Hi, I apologize if I'm posting this to the wrong list; if this is the case, please let me know. For some time now, I've been trying to get SEH exception handling to work in LLVM MCJIT (x64). While reading up on LLVM 3.8, I decided to pick it up again, because a lot has changed which simplifies things greatly. As a toy project, I'm attempting to catch an exception that's thrown in a Windows x64 C++ library call from within an LLVM x64 MCJIT instance. The version I'm working with is LLVM 3.8.1, and the code I've created is based on the documentation http://llvm.org/docs/ExceptionHandling.html . I've also been reading up on the Exception toy/example code that's shipped with LLVM, as well as several mail discussions, the CGException class from clang and of course the ever useful 'clang --emit-llvm'. My aim here is to create a minimum working test case. My first attempt was to simply compile a piece of C++ code with an external function call and run that through LLC. The code is pretty straight-forward: -- extern "C++" void Test2(); int Foo() { try { Test2(); return 1; } catch (...) { return 0; } } -- My first attempt was to run LLC to simply create the code for me. Basically the steps I took were: - clang -std=c++14 -O3 -emit-llvm -S -fexceptions -fms-compatibility -fcxx-exceptions test.cpp - llc -march=cpp -o test_exceptions.cpp test.ll Unfortunately this won't work; LLC doesn't seem to support this and will exit with an error: 'LLVM ERROR: Bad constant'. My second attempt was to simply create the CPP code myself and emit the same IR code that clang has so gracefully provided me. The complete code of a self-contained minimum test case can be found at http://stackoverflow.com/questions/39224636/llvm-mcjit-seh-exception-handling . However, when I try to run that code, the exception seems to corrupt the stack and the Visual Studio debugger will abort the execution with "Stack cookie instrumentation code detected a stack-based buffer overrun.". Currently I'm a bit lost what to try next. At this point I'm not even sure if this behavior is to be expected or not. I'm by no means an expert on SEH exception handling, so any help or advice would be very much appreciated. Kind regards, Stefan. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160906/e059abd7/attachment-0001.html>
Reid Kleckner via llvm-dev
2016-Sep-07 16:37 UTC
[llvm-dev] LLVM MCJIT SEH Exception handling
Someone needs to implement https://llvm.org/bugs/show_bug.cgi?id=24233 before MCJIT can support EH on Win64. The llc command probably failed because the Cpp backend was unmaintained. It was removed from the last release. On Tue, Sep 6, 2016 at 1:44 AM, Stefan de Bruijn via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi, > > > > I apologize if I'm posting this to the wrong list; if this is the case, > please let me know. > > > > For some time now, I've been trying to get SEH exception handling to work > in LLVM MCJIT (x64). While reading up on LLVM 3.8, I decided to pick it up > again, because a lot has changed which simplifies things greatly. > > > > As a toy project, I'm attempting to catch an exception that's thrown in a > Windows x64 C++ library call from within an LLVM x64 MCJIT instance. The > version I'm working with is LLVM 3.8.1, and the code I've created is based > on the documentation http://llvm.org/docs/ExceptionHandling.html . I've > also been reading up on the Exception toy/example code that's shipped with > LLVM, as well as several mail discussions, the CGException class from clang > and of course the ever useful 'clang --emit-llvm'. My aim here is to create > a minimum working test case. > > > > My first attempt was to simply compile a piece of C++ code with an > external function call and run that through LLC. The code is pretty > straight-forward: > > > > -- > > extern "C++" void Test2(); > > > > int Foo() > > { > > try > > { > > Test2(); > > return 1; > > } > > catch (...) > > { > > return 0; > > } > > } > > -- > > > > My first attempt was to run LLC to simply create the code for me. > Basically the steps I took were: > > > > - clang -std=c++14 -O3 -emit-llvm -S -fexceptions -fms-compatibility > -fcxx-exceptions test.cpp > > - llc -march=cpp -o test_exceptions.cpp test.ll > > > > Unfortunately this won't work; LLC doesn't seem to support this and will > exit with an error: 'LLVM ERROR: Bad constant'. > > > > My second attempt was to simply create the CPP code myself and emit the > same IR code that clang has so gracefully provided me. The complete code of > a self-contained minimum test case can be found at > http://stackoverflow.com/questions/39224636/llvm-mcjit- > seh-exception-handling . However, when I try to run that code, the > exception seems to corrupt the stack and the Visual Studio debugger will > abort the execution with "Stack cookie instrumentation code detected a > stack-based buffer overrun.". > > > > Currently I'm a bit lost what to try next. At this point I’m not even sure > if this behavior is to be expected or not. I’m by no means an expert on SEH > exception handling, so any help or advice would be very much appreciated. > > > > Kind regards, > > Stefan. > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160907/1de03e28/attachment.html>
Stefan Gränitz via llvm-dev
2016-Sep-08 17:31 UTC
[llvm-dev] LLVM MCJIT SEH Exception handling
For catching the exception you may use VEH as a fallback on Windows as discussed earlier this year: http://lists.llvm.org/pipermail/llvm-dev/2016-June/100902.html https://msdn.microsoft.com/en-us/library/windows/desktop/ms681411(v=vs.85).aspx So far that worked for me. The problem was that I could not manage to escape from this handler and resume execution, because setjmp/longjmp didn't to work (fails with an exception and re-enters the VEH handler). However, there seems to be some magic behind, as it sometimes works with trivial examples. Thanks Reid for pointing to the bug. It explains most of it. Am 07.09.16 um 18:37 schrieb Reid Kleckner via llvm-dev:> Someone needs to implement https://llvm.org/bugs/show_bug.cgi?id=24233 > before MCJIT can support EH on Win64. > > The llc command probably failed because the Cpp backend was > unmaintained. It was removed from the last release. > > On Tue, Sep 6, 2016 at 1:44 AM, Stefan de Bruijn via llvm-dev > <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote: > > Hi, > > > > I apologize if I'm posting this to the wrong list; if this is the > case, please let me know. > > > > For some time now, I've been trying to get SEH exception handling > to work in LLVM MCJIT (x64). While reading up on LLVM 3.8, I > decided to pick it up again, because a lot has changed which > simplifies things greatly. > > > > As a toy project, I'm attempting to catch an exception that's > thrown in a Windows x64 C++ library call from within an LLVM x64 > MCJIT instance. The version I'm working with is LLVM 3.8.1, and > the code I've created is based on the documentation > http://llvm.org/docs/ExceptionHandling.html > <http://llvm.org/docs/ExceptionHandling.html> . I've also been > reading up on the Exception toy/example code that's shipped with > LLVM, as well as several mail discussions, the CGException class > from clang and of course the ever useful 'clang --emit-llvm'. My > aim here is to create a minimum working test case. > > > > My first attempt was to simply compile a piece of C++ code with an > external function call and run that through LLC. The code is > pretty straight-forward: > > > > -- > > extern "C++" void Test2(); > > > > int Foo() > > { > > try > > { > > Test2(); > > return 1; > > } > > catch (...) > > { > > return 0; > > } > > } > > -- > > > > My first attempt was to run LLC to simply create the code for me. > Basically the steps I took were: > > > > - clang -std=c++14 -O3 -emit-llvm -S -fexceptions > -fms-compatibility -fcxx-exceptions test.cpp > > - llc -march=cpp -o test_exceptions.cpp test.ll > > > > Unfortunately this won't work; LLC doesn't seem to support this > and will exit with an error: 'LLVM ERROR: Bad constant'. > > > > My second attempt was to simply create the CPP code myself and > emit the same IR code that clang has so gracefully provided me. > The complete code of a self-contained minimum test case can be > found at > http://stackoverflow.com/questions/39224636/llvm-mcjit-seh-exception-handling > <http://stackoverflow.com/questions/39224636/llvm-mcjit-seh-exception-handling> > . However, when I try to run that code, the exception seems to > corrupt the stack and the Visual Studio debugger will abort the > execution with "Stack cookie instrumentation code detected a > stack-based buffer overrun.". > > > > Currently I'm a bit lost what to try next. At this point I’m not > even sure if this behavior is to be expected or not. I’m by no > means an expert on SEH exception handling, so any help or advice > would be very much appreciated. > > > > Kind regards, > > Stefan. > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org> > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > <http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev> > > > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev-- https://about.me/stefan.graenitz -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160908/2e784cc9/attachment.html>