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

  • From: Mike Pall <mike-1208@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 28 Aug 2012 01:08:19 +0200

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: