From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 348153858C42; Sun, 12 May 2024 11:40:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 348153858C42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1715514048; bh=uzALGOATC3VNEbt+45AcNG7PHEdjNbbSstIa9FQ3cSo=; h=From:To:Subject:Date:From; b=u+JPx4nWEaAUnQ5oiDZLvmBLRyySDRnXtiqo0ilxEOHnZs4pv73ARzbEzOCWvCB/i HG6zduM+gixut+wEK/njeNZhCwGvjA0lg2RlJdh9q28EmwORfCQ2DOdt1282w/sgQw 8d1CyhDF5isZbXckcreeIWYHaJV8Dp46HAvRnJTc= From: "lobel.krivic at proton dot me" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/115050] New: Segfault when compiled with -O0 Date: Sun, 12 May 2024 11:40:47 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: lobel.krivic at proton dot me 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 attachments.created 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=3D115050 Bug ID: 115050 Summary: Segfault when compiled with -O0 Product: gcc Version: 13.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: lobel.krivic at proton dot me Target Milestone: --- Created attachment 58183 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D58183&action=3Dedit the preprocessed file The following source code: ``` #include #include #include #include /////////////////////////////////////////////////////// class VTable { public: virtual void func(int x) =3D 0; // #1 this line affects behavior virtual ~VTable() =3D default; }; template class Handler final : public VTable { private: static constexpr int S =3D sizeof(void*); std::byte _data[S]; public: template Handler(CArgs&&... args) { static_assert(sizeof(T) <=3D S); ::new (&_data[0]) T(std::forward(args)...); } void func(int) { return; } ~Handler() { static_cast(static_cast(&_data[0]))->~T(); } }; /////////////////////////////////////////////////////// struct FBase { static constexpr int S =3D sizeof(void*) + sizeof(VTable); std::byte _data[S]; ~FBase() { static_cast(static_cast(&_data[0]))->~VTable(); } // #2 this line affects behavior }; struct F : FBase { template F(T&& t) { static_assert(sizeof(Handler>) <=3D S); ::new (&_data[0]) Handler>(std::forward(= t)); } ~F() =3D default; }; /////////////////////////////////////////////////////// struct Testor { Testor() =3D default; ~Testor() =3D default; Testor(const Testor& /*unused*/) { throw std::bad_exception(); } }; /////////////////////////////////////////////////////// #include int main() { try { Testor testor; F f{testor}; } catch (const std::bad_exception& e) { std::cout << " caught! " << e.what() << std::endl; } return 0; } ``` segfaults on runtime when compiled with -O0 but works when compiled with -O1/O2/O3. This happens on all versions of gcc from 11.1 to 14.1 (clang does not have this problem). -Wall and -Wextra do not report anything. Here is the link to the source code: https://godbolt.org/z/5dWKEenMM The complete command line that triggers the bug: `g++ -O0 ./bug.cpp -o ./bug.out && ./bug.out` Bug report that may be related: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D50239 Output of `gcc -v`: Using built-in specs. COLLECT_GCC=3Dgcc COLLECT_LTO_WRAPPER=3D/usr/libexec/gcc/x86_64-pc-linux-gnu/13/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /var/tmp/portage/sys-devel/gcc-13.2.1_p20240113-r1/work/gcc-13-20240113/con= figure --host=3Dx86_64-pc-linux-gnu --build=3Dx86_64-pc-linux-gn u --prefix=3D/usr --bindir=3D/usr/x86_64-pc-linux-gnu/gcc-bin/13 --includedir=3D/usr/lib/gcc/x86_64-pc-linux-gnu/13/include --datadir=3D/usr/share/gcc-data/x86_ 64-pc-linux-gnu/13 --mandir=3D/usr/share/gcc-data/x86_64-pc-linux-gnu/13/man --infodir=3D/usr/share/gcc-data/x86_64-pc-linux-gnu/13/info --with-gxx-incl= ude- dir=3D/usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13 --disable-silent-= rules --disable-dependency-tracking --with-python-dir=3D/share/gcc-data/x86_64-pc -linux-gnu/13/python --enable-languages=3Dc,c++,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --disable-nls --disable-l ibunwind-exceptions --enable-checking=3Drelease --with-bugurl=3Dhttps://bugs.gentoo.org/ --with-pkgversion=3D'Gentoo 13.2.1_p20240113-r1 p12' --with-gcc-major -version-only --enable-libstdcxx-time --enable-lto --disable-libstdcxx-pch --enable-shared --enable-threads=3Dposix --enable-__cxa_atexit --enable-clo= cale =3Dgnu --enable-multilib --with-multilib-list=3Dm32,m64 --disable-fixed-poi= nt --enable-targets=3Dall --enable-libgomp --disable-libssp --disable-libada -= -disa ble-cet --disable-systemtap --disable-valgrind-annotations --disable-vtable-verify --disable-libvtv --without-zstd --without-isl --enable-default-pie -- enable-default-ssp --disable-fixincludes Thread model: posix Supported LTO compression algorithms: zlib gcc version 13.2.1 20240113 (Gentoo 13.2.1_p20240113-r1 p12)=