From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 35FBC3858D33; Mon, 19 Feb 2024 15:01:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 35FBC3858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1708354902; bh=TPhbFNESqeLvXuer9mHXDc9TPZXBLRHrvbJn87BGD2I=; h=From:To:Subject:Date:From; b=imtSFFZ0eDx84rqE4jdMncrzX0zr+16zqkGjHRfbH7IJHig1TFhSicgLI+sRg3kbK BLGb8odY0wzlf3JFmIlCGy5R/68x2m1vXQKTc3DOFe0UkEhQh7uzmcux73PTXmxbiE nm0/nE8ExnfX014080+2tUMqtWds7N/QQSguqsBg= From: "thierry at lelegard dot fr" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/113994] New: Probable C++ code generation bug with -O2 on s390x platform Date: Mon, 19 Feb 2024 15:01:41 +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.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: thierry at lelegard dot fr 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=3D113994 Bug ID: 113994 Summary: Probable C++ code generation bug with -O2 on s390x platform Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: thierry at lelegard dot fr Target Milestone: --- - GCC version: 13.2.0 - Operating system: Ubuntu 23.10 - Platform: IBM s390x (emulated on Qemu 8.2.1) On s390x platform, with "g++ -O2", the following C++ sample test code aborts with std::out_of_range exception in the instantiation of template std::string::substr(). The parameters are valid (see below) and no exception should be thrown. The problem does not occur either: 1. when the std::cout trace line is uncommented (probably break some incorr= ect optimization) 2. with -O1 or -O0 3. with clang 4. on other CPU architectures For information, the original project (https://tsduck.io/) was tested on 16 different operating systems, 8 CPU architectures and 3 compilers (details h= ere: https://tsduck.io/doxy/building.html). The problem occurs on s390x only. Demonstration: ------------------------------------------------------------ $ g++ -O2 test.cpp -o test $ ./test terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 3) > this->size() (which i= s 3) Aborted (core dumped) $ $ g++ -O1 test.cpp -o test $ ./test ok $ $ clang++ -O2 test.cpp -o test $ ./test ok $ ------------------------------------------------------------ Source code: ------------------------------------------------------------ #include #include void f(const std::string& path, size_t& index, std::string& next) { size_t end =3D path.find(']'); if (end >=3D path.size()) { return; } else if (end =3D=3D 1) { index =3D std::string::npos; } while (++end < path.size() && path[end] =3D=3D u'.') { } // If uncommented, display "end =3D 3" and there is no exception. // std::cout << "end =3D " << end << std::endl; next =3D path.substr(end); } int main(int argc, char* argv[]) { size_t index =3D 0; std::string next; f("[0]", index, next); std::cout << "ok" << std::endl; } ------------------------------------------------------------ At the point of exception, end =3D=3D path.size() =3D=3D 3. This is case is= permitted by the C++ standard and should return the empty string. By the way, the exception message "__pos (which is 3) > this->size() (which is 3)" is inconsistent because "3 > 3" is false. The problem is not in the source code if the C++ library but most probably = in the generated code for the instantiation of the template class std::basic_string. If the code was in the C++ library, the problem should be also present with the trace line, with other forms of optimization in the compilation of the application or with clang. Using gdb on the core file demonstrates that: - the exception is thrown in the generated code for the instantiation of std::__cxx11::basic_string::substr - the __pos parameter of substr() is 3 (get the substring starting at index= 3) - the string is "[0]" (length is 3) ------------------------------------------------------------ $ gdb ./test core GNU gdb (Ubuntu 14.0.50.20230907-0ubuntu1) 14.0.50.20230907-git .... Core was generated by `./test'. Program terminated with signal SIGABRT, Aborted. #0 __pthread_kill_implementation (threadid=3D, signo=3Dsigno@entry=3D6, no_tid=3Dno_tid@entry=3D0) at pthread_kill.c:44 44 pthread_kill.c: No such file or directory. (gdb) bt #0 __pthread_kill_implementation (threadid=3D, signo=3Dsigno@entry=3D6, no_tid=3Dno_tid@entry=3D0) at pthread_kill.c:44 #1 0x000003ff89d9e106 in __pthread_kill_internal (signo=3D6, threadid=3D) at pthread_kill.c:78 #2 0x000003ff89d4aa90 in __GI_raise (sig=3Dsig@entry=3D6) at ../sysdeps/posix/raise.c:26 #3 0x000003ff89d2a4ec in __GI_abort () at abort.c:79 #4 0x000003ff8a0d28f2 in __gnu_cxx::__verbose_terminate_handler() () from /lib/s390x-linux-gnu/libstdc++.so.6 #5 0x000003ff8a0cfc4e in ?? () from /lib/s390x-linux-gnu/libstdc++.so.6 #6 0x000003ff8a0cfcd8 in std::terminate() () from /lib/s390x-linux-gnu/libstdc++.so.6 #7 0x000003ff8a0cffe6 in __cxa_throw () from /lib/s390x-linux-gnu/libstdc++.so.6 #8 0x000003ff8a1053de in std::__throw_out_of_range_fmt(char const*, ...) () from /lib/s390x-linux-gnu/libstdc++.so.6 #9 0x000002aa3f8013ac in std::__cxx11::basic_string, std::allocator >::_M_check ( __s=3D0x2aa3f8013f0 "basic_string::substr", __pos=3D3, this=3D0x3ffe997= 9cf8) at /usr/include/c++/13/bits/basic_string.h:379 #10 std::__cxx11::basic_string, std::allocator >::substr (__n=3D18446744073709551615, __pos=3D3,=20 this=3D0x3ffe9979cf8) at /usr/include/c++/13/bits/basic_string.h:3153 #11 f (path=3D"[0]", index=3D@0x3ffe9979cd0: 0, next=3D"") at test.cpp:17 #12 0x000002aa3f800e48 in main (argc=3D, argv=3D) at test.cpp:24 (gdb) f 9 #9 0x000002aa3f8013ac in std::__cxx11::basic_string, std::allocator >::_M_check ( __s=3D0x2aa3f8013f0 "basic_string::substr", __pos=3D3, this=3D0x3ffe997= 9cf8) at /usr/include/c++/13/bits/basic_string.h:379 379 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > " (gdb) p *this $2 =3D "[0]" (gdb)=20 ------------------------------------------------------------ Compilers and OS versions: ------------------------------------------------------------ $ gcc --version gcc (Ubuntu 13.2.0-4ubuntu3) 13.2.0 Copyright (C) 2023 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ $ gcc -v Using built-in specs. COLLECT_GCC=3Dgcc COLLECT_LTO_WRAPPER=3D/usr/libexec/gcc/s390x-linux-gnu/13/lto-wrapper Target: s390x-linux-gnu Configured with: ../src/configure -v --with-pkgversion=3D'Ubuntu 13.2.0-4ub= untu3' --with-bugurl=3Dfile:///usr/share/doc/gcc-13/README.Bugs --enable-languages=3Dc,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=3D/usr --with-gcc-major-version-only --program-suffix=3D-13 --program-prefix=3Ds390x-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=3D/usr/libexec --without-included-gettext --enable-threads=3Dp= osix --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-libquadmath --disable-libquadmath-support --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=3Drelea= se --with-target-system-zlib=3Dauto --enable-objc-gc=3Dauto --enable-multiarch --disable-werror --with-arch=3Dz13 --with-tune=3Dz16 --enable-s390-excess-float-precision --with-long-double-128 --enable-multil= ib --enable-checking=3Drelease --build=3Ds390x-linux-gnu --host=3Ds390x-linux-= gnu --target=3Ds390x-linux-gnu --with-build-config=3Dbootstrap-lto-lean --enable-link-serialization=3D2 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 13.2.0 (Ubuntu 13.2.0-4ubuntu3)=20 $ $ clang --version Ubuntu clang version 16.0.6 (15) Target: s390x-unknown-linux-gnu Thread model: posix InstalledDir: /usr/bin $=20 $ uname -a Linux vms390x 6.5.0-17-generic #17-Ubuntu SMP Thu Jan 11 13:28:22 UTC 2024 s390x s390x s390x GNU/Linux $=20 $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 23.10 Release: 23.10 Codename: mantic ------------------------------------------------------------=