Geoff Levner via llvm-dev
2018-Jun-14 10:44 UTC
[llvm-dev] runStaticConstructorsDestructors() causes crash on exit
Greetings, LLVM wizards. I am using clang to compile a C++ module, and an ExecutionEngine (MCJIT) to execute a function it defines. That works (or pretends to). However, if I call the module's constructors first: exec_engine->runStaticConstructorsDestructors(false); exec_engine->runFunctionAsMain(function, argvec, NULL); execution still works, but my program crashes when it exits, in __run_exit_handlers(). I can't tell from gdb what exit handler is crashing, but no calls are made to atexit() or on_exit(); all exit handlers are installed via __cxa_atexit(). This may or may not be meaningful, but I am forced to compile with -fno-use-cxa-atexit, otherwise clang complains that __dso_handle could not be resolved. Any guidance would be MUCH appreciated. I am at a loss... Also, if you know of a better place to look for help than this mailing list, that would be appreciated, too. Thanks, Geoff -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180614/7dca7262/attachment.html>
Alex Denisov via llvm-dev
2018-Jun-15 09:23 UTC
[llvm-dev] runStaticConstructorsDestructors() causes crash on exit
Hi Geoff, I hit the same problem some time ago. The problem that atexit handlers are registered from the JITted code, and the handlers point to the memory allocated by JIT. When the host program exits it calls the atexit handlers, but the memory is already deallocated by JIT. That's basically the reason of the crash. From what I see ExecutionEngine and MCJIT do not provide any helpful API for this case. I can only recommend switching to Orc APIs in this case: there you can use custom symbol resolver and redirect atexit functions to your own function(s). That function would record atexit handlers, which you can call manually after running your program. Orc APIs even provide a class for that: orc::LocalCXXRuntimeOverrides. I think this is the right place to ask such questions. Otherwise, feel free to ping me on #llvm (AlexDenisov) if you need some hints on how to JIT native code using Orc APIs. I hope it helps. Cheers, Alex.> On 14. Jun 2018, at 12:44, Geoff Levner via llvm-dev <llvm-dev at lists.llvm.org> wrote: > > Greetings, LLVM wizards. > > I am using clang to compile a C++ module, and an ExecutionEngine (MCJIT) to execute a function it defines. That works (or pretends to). However, if I call the module's constructors first: > > exec_engine->runStaticConstructorsDestructors(false); > exec_engine->runFunctionAsMain(function, argvec, NULL); > > execution still works, but my program crashes when it exits, in __run_exit_handlers(). I can't tell from gdb what exit handler is crashing, but no calls are made to atexit() or on_exit(); all exit handlers are installed via __cxa_atexit(). > > This may or may not be meaningful, but I am forced to compile with -fno-use-cxa-atexit, otherwise clang complains that __dso_handle could not be resolved. > > Any guidance would be MUCH appreciated. I am at a loss... Also, if you know of a better place to look for help than this mailing list, that would be appreciated, too. > > Thanks, > Geoff > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev-- AlexDenisov Software Engineer, https://lowlevelbits.org -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 488 bytes Desc: Message signed with OpenPGP URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180615/6d7e0255/attachment.sig>
Geoff Levner via llvm-dev
2018-Jun-15 09:29 UTC
[llvm-dev] runStaticConstructorsDestructors() causes crash on exit
Wow, now that is an answer. Thank you very much! On Fri, 15 Jun 2018 at 11:23, Alex Denisov <1101.debian at gmail.com> wrote:> Hi Geoff, > > I hit the same problem some time ago. > The problem that atexit handlers are registered from the JITted code, and > the handlers point to the memory allocated by JIT. > When the host program exits it calls the atexit handlers, but the memory > is already deallocated by JIT. > That's basically the reason of the crash. > > From what I see ExecutionEngine and MCJIT do not provide any helpful API > for this case. > I can only recommend switching to Orc APIs in this case: there you can use > custom symbol resolver and redirect atexit functions to your own > function(s). > That function would record atexit handlers, which you can call manually > after running your program. > Orc APIs even provide a class for that: orc::LocalCXXRuntimeOverrides. > > I think this is the right place to ask such questions. > Otherwise, feel free to ping me on #llvm (AlexDenisov) if you need some > hints on how to JIT native code using Orc APIs. > > I hope it helps. > > Cheers, > Alex. > > > On 14. Jun 2018, at 12:44, Geoff Levner via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > > > > Greetings, LLVM wizards. > > > > I am using clang to compile a C++ module, and an ExecutionEngine (MCJIT) > to execute a function it defines. That works (or pretends to). However, if > I call the module's constructors first: > > > > exec_engine->runStaticConstructorsDestructors(false); > > exec_engine->runFunctionAsMain(function, argvec, NULL); > > > > execution still works, but my program crashes when it exits, in > __run_exit_handlers(). I can't tell from gdb what exit handler is crashing, > but no calls are made to atexit() or on_exit(); all exit handlers are > installed via __cxa_atexit(). > > > > This may or may not be meaningful, but I am forced to compile with > -fno-use-cxa-atexit, otherwise clang complains that __dso_handle could not > be resolved. > > > > Any guidance would be MUCH appreciated. I am at a loss... Also, if you > know of a better place to look for help than this mailing list, that would > be appreciated, too. > > > > Thanks, > > Geoff >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180615/6e9554f7/attachment.html>
Geoff Levner via llvm-dev
2018-Jun-19 15:54 UTC
[llvm-dev] runStaticConstructorsDestructors() causes crash on exit
On Alex's advice I am switching from MCJIT to the Orc API to compile and execute functions. Starting from the new clang-interpreter example in the source code (top of the tree!), I am able to execute my functions all right... as long as there are no constructors and destructors to call. My question: is there a simple way, with the Orc API, to run a module's constructors and destructors? I see how OrcMCJITReplacement does it, calling getConstructors() and getDestructors() when a module is added, and running them later using a layer, but is there maybe a simpler way that I am missing? I would prefer to avoid having to invent secret names for constructors, play with their linkage and visibility, and generally get involved in things I don't understand... Thanks, Geoff On Fri, 15 Jun 2018 at 11:23, Alex Denisov <1101.debian at gmail.com> wrote:> Hi Geoff, > > I hit the same problem some time ago. > The problem that atexit handlers are registered from the JITted code, and > the handlers point to the memory allocated by JIT. > When the host program exits it calls the atexit handlers, but the memory > is already deallocated by JIT. > That's basically the reason of the crash. > > From what I see ExecutionEngine and MCJIT do not provide any helpful API > for this case. > I can only recommend switching to Orc APIs in this case: there you can use > custom symbol resolver and redirect atexit functions to your own > function(s). > That function would record atexit handlers, which you can call manually > after running your program. > Orc APIs even provide a class for that: orc::LocalCXXRuntimeOverrides. > > I think this is the right place to ask such questions. > Otherwise, feel free to ping me on #llvm (AlexDenisov) if you need some > hints on how to JIT native code using Orc APIs. > > I hope it helps. > > Cheers, > Alex. > > > On 14. Jun 2018, at 12:44, Geoff Levner via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > > > > Greetings, LLVM wizards. > > > > I am using clang to compile a C++ module, and an ExecutionEngine (MCJIT) > to execute a function it defines. That works (or pretends to). However, if > I call the module's constructors first: > > > > exec_engine->runStaticConstructorsDestructors(false); > > exec_engine->runFunctionAsMain(function, argvec, NULL); > > > > execution still works, but my program crashes when it exits, in > __run_exit_handlers(). I can't tell from gdb what exit handler is crashing, > but no calls are made to atexit() or on_exit(); all exit handlers are > installed via __cxa_atexit(). > > > > This may or may not be meaningful, but I am forced to compile with > -fno-use-cxa-atexit, otherwise clang complains that __dso_handle could not > be resolved. > > > > Any guidance would be MUCH appreciated. I am at a loss... Also, if you > know of a better place to look for help than this mailing list, that would > be appreciated, too. > > > > Thanks, > > Geoff >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180619/ff247091/attachment.html>