llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

The combination of -fno-rtti -fexceptions is very brittle

Open ldionne opened this issue 3 years ago • 4 comments

The combination of -fno-rtti and -fexceptions is not supported very well, and we've seen several issues related to that internally at Apple. This bug report captures an analysis of this issue I did years ago to try to make it visible to the larger community.

Current behaviour

When both these options are specified, we don't generate RTTI for types, except when a type is thrown, at which point we generate some minimal RTTI in the TU where it is thrown (I am not sure whether the "minimal RTTI is exactly the same as "normal RTTI"). Also note that we don't generate RTTI even in the place where a key function exists, and similarly we don't assume that other TUs have a definition of the RTTI when a key function exists.

Problem with the current approach

Let's say some TU a.cpp built with -fno-rtti -fexceptions calls a function (defined in some other TU) that can throw a type E, and tries to catch E. Then, let's say that function is defined in some other TU b.cpp built with -frtti -fexceptions throws a type E. Let's also assume that another TU c.cpp built with -frtti -fexceptions defines the RTTI for E (through a key function, for example). The problem here is that in a.cpp, we'll generate minimal RTTI for E, and in b.cpp we'll use the normal RTTI assumed to be in c.cpp (because there's a key function). Since the two RTTIs don't get de-duplicated, we get a type identity mismatch in a.cpp and b.cpp, and the exception isn't caught.

Here's a minimal working example:

#/usr/bin/env bash

cat <<EOF > a.cpp
struct E { virtual ~E(); };
extern void f();
int main() {
    try {
        f();
    } catch (E const&) { // tries catching E with RTTI from a.o

    }
}
EOF

cat <<EOF > b.cpp
struct E { virtual ~E(); };
extern void f() { throw E{}; } // throws E with RTTI from c.o
EOF

cat <<EOF > c.cpp
struct E { virtual ~E(); };
E::~E() { } // key function
EOF

clang++ a.cpp -fno-rtti -fexceptions -std=c++11 -c -o a.o
clang++ b.cpp -frtti -fexceptions -std=c++11 -c -o b.o
clang++ c.cpp -frtti -fexceptions -std=c++11 -c -o c.o
clang++ b.o c.o -shared -o b.dylib
clang++ a.o b.dylib -o a.exe
nm a.o b.o c.o a.exe b.dylib | c++filt
./a.exe

To map this example to reality, imagine that E is something like std::exception (or a derived class), that b.dylib is libc++.dylib, and that a.exe is a user program built with -fno-rtti -fexceptions. It becomes clear why people are having problems with the feature.

I once discussed this issue with @dexonsmith and we had discussed this potential solution:

  • Add an attribute generate_rtti (name TBD)
  • When you’d normally generate RTTI for a type and currently suppress the RTTI generation based on whether -fno-rtti is present, instead check whether the type has the attribute and still generate full RTTI if it has it.
  • When you try to throw a type with -fno-rtti -fexceptions, you get:
    • a warning if the type doesn’t have the attribute, and minimal RTTI gets generated (like today, for backwards compatibility)
    • the normal behaviour (like when -fno-rtti is not specified) if the type has the attribute. In particular, this means that no RTTI is generated if we can tell it’s somewhere else (e.g. when there’s a key function), and full RTTI (that the linker can dedupe) is generated when we can’t tell for sure.

rdar://58055046

ldionne avatar Sep 12 '23 17:09 ldionne

Thanks for summarizing. The proposed generate_rtti attribute still feels right to me, especially for libraries like libc++.

dexonsmith avatar Sep 12 '23 19:09 dexonsmith

@llvm/issue-subscribers-c-1

The combination of `-fno-rtti` and `-fexceptions` is not supported very well, and we've seen several issues related to that internally at Apple. This bug report captures an analysis of this issue I did years ago to try to make it visible to the larger community.

Current behaviour

When both these options are specified, we don't generate RTTI for types, except when a type is thrown, at which point we generate some minimal RTTI in the TU where it is thrown (I am not sure whether the "minimal RTTI is exactly the same as "normal RTTI"). Also note that we don't generate RTTI even in the place where a key function exists, and similarly we don't assume that other TUs have a definition of the RTTI when a key function exists.

Problem with the current approach

Let's say some TU a.cpp built with -fno-rtti -fexceptions calls a function (defined in some other TU) that can throw a type E, and tries to catch E. Then, let's say that function is defined in some other TU b.cpp built with -frtti -fexceptions throws a type E. Let's also assume that another TU c.cpp built with -frtti -fexceptions defines the RTTI for E (through a key function, for example). The problem here is that in a.cpp, we'll generate minimal RTTI for E, and in b.cpp we'll use the normal RTTI assumed to be in c.cpp (because there's a key function). Since the two RTTIs don't get de-duplicated, we get a type identity mismatch in a.cpp and b.cpp, and the exception isn't caught.

Here's a minimal working example:

#/usr/bin/env bash

cat <<EOF > a.cpp
struct E { virtual ~E(); };
extern void f();
int main() {
    try {
        f();
    } catch (E const&) { // tries catching E with RTTI from a.o

    }
}
EOF

cat <<EOF > b.cpp
struct E { virtual ~E(); };
extern void f() { throw E{}; } // throws E with RTTI from c.o
EOF

cat <<EOF > c.cpp
struct E { virtual ~E(); };
E::~E() { } // key function
EOF

clang++ a.cpp -fno-rtti -fexceptions -std=c++11 -c -o a.o
clang++ b.cpp -frtti -fexceptions -std=c++11 -c -o b.o
clang++ c.cpp -frtti -fexceptions -std=c++11 -c -o c.o
clang++ b.o c.o -shared -o b.dylib
clang++ a.o b.dylib -o a.exe
nm a.o b.o c.o a.exe b.dylib | c++filt
./a.exe

To map this example to reality, imagine that E is something like std::exception (or a derived class), that b.dylib is libc++.dylib, and that a.exe is a user program built with -fno-rtti -fexceptions. It becomes clear why people are having problems with the feature.

I once discussed this issue with @dexonsmith and we had discussed this potential solution:

  • Add an attribute generate_rtti (name TBD)
  • When you’d normally generate RTTI for a type and currently suppress the RTTI generation based on whether -fno-rtti is present, instead check whether the type has the attribute and still generate full RTTI if it has it.
  • When you try to throw a type with -fno-rtti -fexceptions, you get:
    • a warning if the type doesn’t have the attribute, and minimal RTTI gets generated (like today, for backwards compatibility)
    • the normal behaviour (like when -fno-rtti is not specified) if the type has the attribute. In particular, this means that no RTTI is generated if we can tell it’s somewhere else (e.g. when there’s a key function), and full RTTI (that the linker can dedupe) is generated when we can’t tell for sure.

rdar://58055046

llvmbot avatar Sep 15 '23 12:09 llvmbot

The fundamental difference with GCC that seemingly allows it to avoid this problem seems to be that it emits the typeinfo (when compiling c.cpp from the example) as a weak symbol:

        .weak   _ZTI3exc
        .section        .data.rel.ro._ZTI3exc,"awG",@progbits,_ZTI3exc,comdat
        .align 8
        .type   _ZTI3exc, @object
        .size   _ZTI3exc, 16
_ZTI3exc:
        .quad   _ZTVN10__cxxabiv117__class_type_infoE+16
        .quad   _ZTS3exc

I'm testing on FreeBSD where this problem is also apparent. Compiling just that one file (c.cpp) with g++ instead of clang++, and leaving everything else the same, makes the problem disappear; the only really noticable difference in the generated object (that seems like it could be pertinent) is that _ZTI3exc/_ZTS3exc are weak (and the section is marked "comdat" but I assume that makes no difference once linked to an executable/share object).

The FreeBSD dynamic linker doesn't seem to resolve weak symbols in an executable to non-weak symbols in a linked shared library, which the dynamic linker from Glibc does do, so perhaps that contributes to the problem, though I notice that linking using g++ also resolves the issue (even if all source files are compiled using clang++) so perhaps libgcc_s is also comparing the typeinfo using the (mangled) name and not just the address (and libcxxrt is doing the opposite?). That's obviously less efficient of course. Apologies that a lot of this is guesswork on my part.

Perhaps clang could also generate weak typeinfo symbols, at least on platforms where the dynamic linker has this quirk? That seems less drastic than a new attribute, assuming it will solve the problem.

davmac314 avatar Sep 20 '25 05:09 davmac314

Another real-world data point for this: it also hits plain standard-library exceptions on Apple platforms, where the library that throws is the system libc++ and so cannot be rebuilt with a matching RTTI setting.

Setup: macOS 26.7, arm64, a program compiled with -fno-rtti -fexceptions (full LTO, -fvisibility=hidden), headers from Homebrew LLVM 23.1.3's libc++, runtime /usr/lib/libc++.1.dylib + libc++abi.dylib.

Observed (a unit test binary; the same code with RTTI on behaves correctly):

  • std::filesystem::file_size( missing_path ) throws from inside the dylib. None of catch ( std::filesystem::filesystem_error const & ), catch ( std::system_error const & ) or catch ( std::exception const & ) matches; only catch ( ... ) does.
  • std::stoi( "x" ) (invalid_argument thrown by libc++abi): catch ( std::exception const & ) misses it as well.
  • Exceptions thrown by code compiled into the same binary (throw std::runtime_error{...}, etc.) are caught normally, so the failure shows up only for exceptions thrown inside the system library.
  • &typeid( std::filesystem::filesystem_error ) resolves to the executable's own copy; __cxa_current_exception_type() of the thrown object is the symbol in libc++.1.dylib. With RTTI on, both are the dylib's.

Cause, as far as I can tell from reading ItaniumCXXABI.cpp: ShouldUseExternalRTTIDescriptor returns false at the top when -fno-rtti is in effect, so each TU emits its own copy of the type_info of every standard type it throws or catches. On arm64 Darwin that copy is linkonce_odr with hidden visibility and the non-unique name bit set (visible in the IR), while the library's own type_info does not carry the bit, so libc++abi's comparison never treats the two as the same type.

Mixing does not help: linking an -frtti TU with -fno-rtti TUs against the real libc++ (with and without LTO) still defines the type_info locally, and every reference in the -frtti TU binds to the private copy.

The same code works on Linux, where libstdc++ falls back to comparing type names. Emitting the type_info as a weak, default-visibility symbol, as GCC does according to the comment above, would likely address the Darwin case too, but I have not tried that.

psiha avatar Oct 13 '26 23:10 psiha