https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113099

            Bug ID: 113099
           Summary: locale without RTTI uses dynamic_cast before gcc 13.2
                    or has ODR violation since gcc 13.2
           Product: gcc
           Version: 11.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andysem at mail dot ru
  Target Milestone: ---

Consider this test case:

```
#include <locale>

class __attribute__((__visibility__("default"))) my_codecvt final :
    public std::codecvt< wchar_t, char, std::mbstate_t >
{
public:
    explicit my_codecvt(std::size_t refs = 0) :
        std::codecvt< wchar_t, char, std::mbstate_t >(refs)
    {
    }

protected:
    bool do_always_noconv() const noexcept override { return false; }

    int do_encoding() const noexcept override { return 0; }
    std::codecvt_base::result do_in(std::mbstate_t&, const char*,
        const char*, const char*&, wchar_t*, wchar_t*, wchar_t*&)
        const override
    { return ok; }
    std::codecvt_base::result do_out(std::mbstate_t&, const wchar_t*,
        const wchar_t*, const wchar_t*&, char*, char*, char*&)
        const override
    { return ok; }
    std::codecvt_base::result do_unshift(std::mbstate_t&, char*,
        char*, char*&) const override
    { return ok; }
    int do_length(std::mbstate_t&, const char*, const char*,
        std::size_t) const override
    { return 0; }
    int do_max_length() const noexcept override { return 0; }
};

int main()
{
    std::locale loc(std::locale(), new my_codecvt());
    auto const& fac = std::use_facet<
        std::codecvt< wchar_t, char, std::mbstate_t > >(loc);
    (void)fac;
}
```

```
g++ -std=c++17 -fno-rtti -o locale_no_rtti locale_no_rtti.cpp
```

When compiled with RTTI disabled, with the command line above, this code
crashes with the following backtrace:

```
#0  0x00007ffff7caccd1 in __dynamic_cast () from
/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x00007ffff7d5307f in std::codecvt<wchar_t, char, __mbstate_t> const&
std::use_facet<std::codecvt<wchar_t, char, __mbstate_t> >(std::locale const&)
() from /lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x000055555555539b in main ()
```

This reproduces on gcc 11.4 and 12 at least.

```
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-11
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib
--enable-libphobos-checking=release --with-target-system-zlib=auto
--enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet
--with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr
--without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
--with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) 
```

This was apparently fixed by accident in gcc 13.2 by this commit:

https://github.com/gcc-mirror/gcc/commit/b3ac43a3c05744d62a963d656bed782fc867ad79

The commit introduces shortcuts that use static_casts for the standard facets,
which allows to avoid the crash, but that still retains an ODR violation, where
the explicitly instantiated __try_use_facet templates in libstdc++ library use
dynamic_cast (even if unreachable) and the template definition that is visible
to user's code uses static_cast. The problematic code is here:

https://github.com/gcc-mirror/gcc/blob/d7e9ae4fa94afd5517536b4dfc7d6be0b3e8c2c3/libstdc%2B%2B-v3/include/bits/locale_classes.tcc#L142-L146

When libstdc++ is compiled, RTTI is enabled and __cpp_rtti is defined, but when
user's code is compiled with RTTI disabled, that macro is not defined, so the
__try_use_facet template definition is different.

It doesn't seem like the commit I mentioned above intended to fix the original
issue with dynamic_cast anyway, so I thought it was worth creating this bug
report, even though the original test case passes on the latest gcc.

I think, the code should be modified so that __cpp_rtti is only tested in the
code that is instantiated by the user but not libstdc++. libstdc++ should
export two different functions - one that could use dynamic_cast and another
one that doesn't - and the selection of which one to call should happen in the
user-visible code based on the __cpp_rtti macro.

Reply via email to