Re: -fno-exceptions, ffi, lua_error, and C++ exceptions

  • From: Dan Eloff <dan.eloff@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 27 Aug 2012 18:24:42 -0500

Thanks, that clarifies things a lot! So I either need to make sure I
don't use RAII anywhere around lua api calls or enable exception
handling and put try/except stuff in my extern "C" functions used by
lua ffi. Or do I need to do that? If lua_error is really raising a c++
exception then I don't want to catch that. I would guess it's safe for
the ffi invoked functions to raise c++ exceptions on x64 posix.

Cheers,
Dan

On Mon, Aug 27, 2012 at 6:08 PM, Mike Pall <mike-1208@xxxxxxxxxx> wrote:
> Dan Eloff wrote:
>> Sorry to flog the exceptions horse on this list again, but I've been
>> unable to find out what happens when embedding LuaJIT in a C++ program
>> compiled with exception support disabled (posix x64 with gcc with
>> -fno-exceptions)
>
> All this option does on x64 is to help eliminate some control flow
> paths and omit the exception tables. The unwind info is still there
> (it's mandatory on x64), but it gets smaller. LuaJIT depends on
> the unwind info, because it throws native exceptions on x64.
>
>> Some things I do:
>>
>> Call lua_error in c++ code. I *think* the places I do this don't have
>> leakable resources on the stack, but I'm curious if the stack would
>> actually be unwound and destructors called. Afaik this is the LuaJIT
>> behavior on posix x64 when exceptions are enabled, does this change
>> when compiling with -fno-exceptions?
>
> If you tell the C++ compiler to disable exceptions, then you also
> allow it to assume that no callee ever throws an exception. Which
> in turn allows the C++ compiler to omit the exception tables.
>
> Alas, lua_error() does in fact throw a C++ exception on x64. The
> stack will be unwound (unwind info is ok), but destructors will
> NOT be called (exception tables are missing).
>
> So if you disable exceptions, you need to make absolutely sure you
> don't use RAII in any code path leading up to lua_error(). Note
> that plenty of lua*() functions can potentially throw an error.
>
>> Call extern "C" functions via ffi, which call c++ code. Here disabled
>> exceptions seems to be good because these are not allowed to raise c++
>> exceptions in LuaJIT.
>
> Ok, but you don't gain or lose anything here in practice.
>
>> Anything else I should be aware of?
>
> C++ exceptions are all-or-nothing: you either need to enable or
> disable exceptions in *all* libraries or frameworks you plan to
> use. Or make 100% sure they don't leak any exceptions.
>
> --Mike
>

Other related posts: