From patchwork Wed Oct 27 08:25:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jay Feldblum X-Patchwork-Id: 46698 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B17643858014 for ; Wed, 27 Oct 2021 08:26:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B17643858014 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1635323200; bh=wC6lKRa5RBPZYvBD0nxx0mnQiyT05+PzQhRvrcx5PTI=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=KYsgaz5J0gzrKOzWSx4qrd/b//LbDsgWfZR/OeGrRJXWIBxddTcvK2XsnbBNNGHOy SCP9VwM2uAfgcbP17xE8ppz2sKH7E6BZMZLeZgU9+4gxQV4bi7zNSNc8X117nw0R74 VC2s4APwcIIcJrsnvY9maCqMOcolgM5U3W56X0io= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ua1-x929.google.com (mail-ua1-x929.google.com [IPv6:2607:f8b0:4864:20::929]) by sourceware.org (Postfix) with ESMTPS id A429F3858405; Wed, 27 Oct 2021 08:26:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A429F3858405 Received: by mail-ua1-x929.google.com with SMTP id v20so3366519uaj.9; Wed, 27 Oct 2021 01:26:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=wC6lKRa5RBPZYvBD0nxx0mnQiyT05+PzQhRvrcx5PTI=; b=2yhOH/vsgTHCnjm/+zNivzwm4xNwrwb4Puk1nZ5NjBI0hz2+4OgQCZwWHpclj9uoVA zLP30s77f8U3kuD0dDpRTPmveXmXlcZLjkAF2VkenNgjdgls7rgJbSz4Cj7Gul9GoqF3 qu1iR0QPpukn93LxJo9hNXftVfEDbQA69lh3c9meNUQCe/D/DQOa7JSouKfJraqd1lF3 X2ZLAlE093UgU5Z4jGZY2IQteqBLqfXj1RkogXzYtPcEiyP6WRWQlMYT+9qZrO8KEypk v+MWjLmGxrTHAm0+Ar6l/Cif0KCsjfKIroCSpkDVI8On0k971lGvhgE6nSpPwzZkrJik C1Qw== X-Gm-Message-State: AOAM531ObPocrSC3maImMAQod3VJX9ssxizWnI7EZ6NbMewqYQCA9/nE 9ilNOmIT04HWgUy7aF8JM6DrjgiTWzKQSqqY/OCJXMt+6mU= X-Google-Smtp-Source: ABdhPJwj0a3nZXH5vA/rGgkRzHBfd26GnQFlo3XznhvHduoLs+l/1F9CdeactOjNoFLOVLqBzATauOZ9kt95pP+ySgQ= X-Received: by 2002:a05:6130:38e:: with SMTP id az14mr19835312uab.100.1635323168922; Wed, 27 Oct 2021 01:26:08 -0700 (PDT) MIME-Version: 1.0 Date: Wed, 27 Oct 2021 01:25:58 -0700 Message-ID: Subject: [PATCH] print extended assertion failures to stderr To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Spam-Status: No, score=-8.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_LOTSOFHASH, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jay Feldblum via Gcc-patches From: Jay Feldblum Reply-To: Jay Feldblum Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" From: yfeldblum The stdout stream is reserved for output intentionally produced by the application. Assertion failures and other forms of logging must be emitted to stderr, not to stdout. It is common for testing and monitoring infrastructure to scan stderr for errors, such as for assertion failures, and to collect or retain them for analysis or observation. It is a norm that assertion failures match this expectation in practice. While `__builtin_fprintf` is available as a builtin, there is no equivalent builtin for `stderr`. The only option in practice is to use the macro `stderr`, which requires `#include `. It is desired not to add such an include to `bits/c++config` so the solution is to write and export a function which may be called by `bits/c++config`. This is expected to be API-compatible and ABI-compatible with caveats. Code compiled against an earlier libstdc++ will work when linked into a later libstdc++ but the stream to which assertion failures are logged is anybody's guess, and in practice will be determined by the link line and the choice of linker. This fix targets builds for which all C++ code is built against a libstdc++ with the fix. Alternatives: * This, which is the smallest change. * This, but also defining symbols `std::__stdin` and `std::__stdout` for completeness. * Define a symbol like `std::__printf_stderr` which prints any message with any formatting to stderr, just as `std::printf` does to stdout, and call that from `std::__replacement_assert` instead of calling `__builtin_printf`. * Move `std::__replacement_assert` into libstdc++.so and no longer mark it as weak. This allows an application with some parts built against a previous libstdc++ to guarantee that the fix will be applied at least to the parts that are built against a libstdc++ containing the fix. libstdc++-v3/ChangeLog: include/bits/c++config (__glibcxx_assert): print to stderr. --- libstdc++-v3/include/bits/c++config | 8 ++++-- libstdc++-v3/src/c++98/Makefile.am | 1 + libstdc++-v3/src/c++98/Makefile.in | 2 +- libstdc++-v3/src/c++98/stdio.cc | 39 +++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/src/c++98/stdio.cc +} diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config index a64958096718126a49e8767694e913ed96108df2..d821ba09d88dc3e42ff1807200cfece71cc18bd9 100644 --- a/libstdc++-v3/include/bits/c++config +++ b/libstdc++-v3/include/bits/c++config @@ -523,6 +523,10 @@ namespace std # ifdef _GLIBCXX_VERBOSE_ASSERT namespace std { + // Avoid the use of stderr, because we're trying to keep the + // include out of the mix. + extern "C++" void* __stderr() _GLIBCXX_NOEXCEPT; + // Avoid the use of assert, because we're trying to keep the // include out of the mix. extern "C++" _GLIBCXX_NORETURN @@ -531,8 +535,8 @@ namespace std const char* __function, const char* __condition) _GLIBCXX_NOEXCEPT { - __builtin_printf("%s:%d: %s: Assertion '%s' failed.\n", __file, __line, - __function, __condition); + __builtin_fprintf(__stderr(), "%s:%d: %s: Assertion '%s' failed.\n", + __file, __line, __function, __condition); __builtin_abort(); } } diff --git a/libstdc++-v3/src/c++98/Makefile.am b/libstdc++-v3/src/c++98/Makefile.am index b48b57a2945780bb48496d3b5e76de4be61f836e..4032f914ea20344f51f2f219c5575d2a3858c44c 100644 --- a/libstdc++-v3/src/c++98/Makefile.am +++ b/libstdc++-v3/src/c++98/Makefile.am @@ -136,6 +136,7 @@ sources = \ math_stubs_float.cc \ math_stubs_long_double.cc \ stdexcept.cc \ + stdio.cc \ strstream.cc \ tree.cc \ istream.cc \ diff --git a/libstdc++-v3/src/c++98/Makefile.in b/libstdc++-v3/src/c++98/Makefile.in index f9ebb0ff4f4cb86cde7070b5ba6b8bf6a20515b3..e8aeb37d864a0ab7711d763fe8fbd3045db6e00d 100644 --- a/libstdc++-v3/src/c++98/Makefile.in +++ b/libstdc++-v3/src/c++98/Makefile.in @@ -142,7 +142,7 @@ am__objects_7 = bitmap_allocator.lo pool_allocator.lo mt_allocator.lo \ list.lo list-aux.lo list-aux-2.lo list_associated.lo \ list_associated-2.lo locale.lo locale_init.lo locale_facets.lo \ localename.lo math_stubs_float.lo math_stubs_long_double.lo \ - stdexcept.lo strstream.lo tree.lo istream.lo istream-string.lo \ + stdexcept.lo stdio.lo strstream.lo tree.lo istream.lo istream-string.lo \ streambuf.lo valarray.lo $(am__objects_1) $(am__objects_3) \ $(am__objects_6) am_libc__98convenience_la_OBJECTS = $(am__objects_7) diff --git a/libstdc++-v3/src/c++98/stdio.cc b/libstdc++-v3/src/c++98/stdio.cc new file mode 100644 index 0000000000000000000000000000000000000000..d0acb9117e1728f66f1a72ae3a9f471af72034ef --- /dev/null +++ b/libstdc++-v3/src/c++98/stdio.cc @@ -0,0 +1,39 @@ +// Portability symbols for -*- C++ -*- + +// Copyright (C) 2021-2021 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// . + +#include +#include + +namespace std { + +// The name stderr is specified to be a macro and is specified to be defined in +// C++ header cstdio or, equivalently, C header stdio.h. That means it cannot be +// used by code which intentionally avoids library includes, in particular, by +// bits/c++config. And it cannot be declared or even aliased in such headers +// since that would not be portable across libc implementations. +extern "C++" void* __stderr() _GLIBCXX_NOEXCEPT { + return stderr; +} +