From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id B8F6438582AE; Wed, 20 Dec 2023 23:41:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B8F6438582AE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1703115698; bh=eo0z73mWin7GjPFl/SSAj+UVL9WcJH/bPdoiLQEdVeE=; h=From:To:Subject:Date:From; b=n9foM4NfuJ5JCfxsecrnzNTsJ2x/te6YeUr7vFpNF0XPj7N5HxYIVAp+bquK0k8ia PBWdfvYiC7PqjlAozTkh7QO0JRxkTfSfYJKBKGwnlPP6SMGMKxT1wLZaIC3ZvN86Fd yIIoNeo5bTm/lYI1N4AS2N2uIqrU5Uu/2iTGlUeo= From: "andysem at mail dot ru" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/113099] New: locale without RTTI uses dynamic_cast before gcc 13.2 or has ODR violation since gcc 13.2 Date: Wed, 20 Dec 2023 23:41:37 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 11.4.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: andysem at mail dot ru X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D113099 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 class __attribute__((__visibility__("default"))) my_codecvt final : public std::codecvt< wchar_t, char, std::mbstate_t > { public: explicit my_codecvt(std::size_t refs =3D 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 =3D std::use_facet< std::codecvt< wchar_t, char, std::mbstate_t > >(loc); (void)fac; } ``` ``` g++ -std=3Dc++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 const& std::use_facet >(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=3Dg++ COLLECT_LTO_WRAPPER=3D/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper OFFLOAD_TARGET_NAMES=3Dnvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=3D1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion=3D'Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=3Dfile:///usr/share/doc/gcc-11/README.= Bugs --enable-languages=3Dc,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix= =3D/usr --with-gcc-major-version-only --program-suffix=3D-11 --program-prefix=3Dx86_64-linux-gnu- --enable-shared --enable-linker-build-= id --libexecdir=3D/usr/lib --without-included-gettext --enable-threads=3Dposix --libdir=3D/usr/lib --enable-nls --enable-bootstrap --enable-clocale=3Dgnu --enable-libstdcxx-debug --enable-libstdcxx-time=3Dyes --with-default-libstdcxx-abi=3Dnew --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-= zlib --enable-libphobos-checking=3Drelease --with-target-system-zlib=3Dauto --enable-objc-gc=3Dauto --enable-multiarch --disable-werror --enable-cet --with-arch-32=3Di686 --with-abi=3Dm64 --with-multilib-list=3Dm32,m64,mx32 --enable-multilib --with-tune=3Dgeneric --enable-offload-targets=3Dnvptx-none=3D/build/gcc-11-XeT9lY/gcc-11-11.4.0/= debian/tmp-nvptx/usr,amdgcn-amdhsa=3D/build/gcc-11-XeT9lY/gcc-11-11.4.0/deb= ian/tmp-gcn/usr --without-cuda-driver --enable-checking=3Drelease --build=3Dx86_64-linux-gnu --host=3Dx86_64-linux-gnu --target=3Dx86_64-linux-gnu --with-build-config=3Dbootstrap-lto-lean --enable-link-serialization=3D2 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)=20 ``` This was apparently fixed by accident in gcc 13.2 by this commit: https://github.com/gcc-mirror/gcc/commit/b3ac43a3c05744d62a963d656bed782fc8= 67ad79 The commit introduces shortcuts that use static_casts for the standard face= ts, which allows to avoid the crash, but that still retains an ODR violation, w= here the explicitly instantiated __try_use_facet templates in libstdc++ library = use dynamic_cast (even if unreachable) and the template definition that is visi= ble to user's code uses static_cast. The problematic code is here: https://github.com/gcc-mirror/gcc/blob/d7e9ae4fa94afd5517536b4dfc7d6be0b3e8= c2c3/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 t= he __try_use_facet template definition is different. It doesn't seem like the commit I mentioned above intended to fix the origi= nal 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 t= he 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.=