From patchwork Mon Jan 11 21:56:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1424844 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=L/YQNqje; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DF6xd4z3Mz9sWr for ; Tue, 12 Jan 2021 08:57:08 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 056DE384A01E; Mon, 11 Jan 2021 21:57:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 056DE384A01E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1610402225; bh=aKlbrGATa1QASsIvJIArAQpUOyFtOkguLqXC5R+nt80=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=L/YQNqjehEMHaUCv7a8C/iUmqL61xB5TQXxF5t+rT3HHM/B3f97rKojzZEv8jmWi7 IrLD4lzEMBQscJtPHcpoVmJw4w7GWeJk+VwvYsa1a9A+DIaebC7Z+xSH+Wxr63e79G DE86Y7rlONL+l5qCW32Uca3Iuz7n6loQAgHO/3FU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 8C46B3857C65 for ; Mon, 11 Jan 2021 21:57:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8C46B3857C65 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-265-xp5xIpPXPsmP2_LbS6sfwQ-1; Mon, 11 Jan 2021 16:57:00 -0500 X-MC-Unique: xp5xIpPXPsmP2_LbS6sfwQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6FE761015C85 for ; Mon, 11 Jan 2021 21:56:59 +0000 (UTC) Received: from t470.redhat.com (ovpn-112-159.phx2.redhat.com [10.3.112.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id E98CC100164C; Mon, 11 Jan 2021 21:56:58 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH] Handle fancy_abort before diagnostic initialization [PR98586] Date: Mon, 11 Jan 2021 16:56:57 -0500 Message-Id: <20210111215657.3542216-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" If fancy_abort is called before the diagnostic subsystem is initialized, internal_error will crash internally in a way that prevents a useful message reaching the user. This can happen with libgccjit in the case of gcc_assert failures that occur outside of the libgccjit mutex that guards the rest of gcc's state, including global_dc (when global_dc may not be initialized yet, or might be in use by another thread). I tried a few approaches to fixing this as noted in PR jit/98586 e.g. using a temporary diagnostic_context and initializing it for the call to internal_error, however the more code that runs, the more chance there is for other errors to occur. The best fix appears to be to simply fall back to a minimal abort implementation that only relies on i18n, as implemented by this patch. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Is there a better way to fix this? If not I plan to push this to master in a few days. gcc/ChangeLog: PR jit/98586 * diagnostic.c (diagnostic_kind_text): Break out this array from... (diagnostic_build_prefix): ...here. (fancy_abort): Detect when diagnostic_initialize has not yet been called and fall back to a minimal implementation of printing the ICE, rather than segfaulting in internal_error. --- gcc/diagnostic.c | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c index 4250bf96c8b..3be7748eb39 100644 --- a/gcc/diagnostic.c +++ b/gcc/diagnostic.c @@ -431,6 +431,13 @@ diagnostic_get_location_text (diagnostic_context *context, line_col, locus_ce); } +static const char *const diagnostic_kind_text[] = { +#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T), +#include "diagnostic.def" +#undef DEFINE_DIAGNOSTIC_KIND + "must-not-happen" +}; + /* Return a malloc'd string describing a location and the severity of the diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for freeing the memory. */ @@ -438,12 +445,6 @@ char * diagnostic_build_prefix (diagnostic_context *context, const diagnostic_info *diagnostic) { - static const char *const diagnostic_kind_text[] = { -#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T), -#include "diagnostic.def" -#undef DEFINE_DIAGNOSTIC_KIND - "must-not-happen" - }; gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND); const char *text = _(diagnostic_kind_text[diagnostic->kind]); @@ -1832,6 +1833,38 @@ error_recursion (diagnostic_context *context) void fancy_abort (const char *file, int line, const char *function) { + /* If fancy_abort is called before the diagnostic subsystem is initialized, + internal_error will crash internally in a way that prevents a + useful message reaching the user. + This can happen with libgccjit in the case of gcc_assert failures + that occur outside of the libgccjit mutex that guards the rest of + gcc's state, including global_dc (when global_dc may not be + initialized yet, or might be in use by another thread). + Handle such cases as gracefully as possible by falling back to a + minimal abort handler that only relies on i18n. */ + if (global_dc->printer == NULL) + { + /* Print the error message. */ + fnotice (stderr, diagnostic_kind_text[DK_ICE]); + fnotice (stderr, "in %s, at %s:%d", function, trim_filename (file), line); + fputc ('\n', stderr); + + /* Attempt to print a backtrace. */ + struct backtrace_state *state + = backtrace_create_state (NULL, 0, bt_err_callback, NULL); + int count = 0; + if (state != NULL) + backtrace_full (state, 2, bt_callback, bt_err_callback, + (void *) &count); + + /* We can't call warn_if_plugins or emergency_dump_function as these + rely on GCC state that might not be initialized, or might be in + use by another thread. */ + + /* Abort the process. */ + real_abort (); + } + internal_error ("in %s, at %s:%d", function, trim_filename (file), line); }