From patchwork Mon Oct 28 04:47:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 99690 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 DDA553858D38 for ; Mon, 28 Oct 2024 04:46:48 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from EUR03-VI1-obe.outbound.protection.outlook.com (mail-vi1eur03olkn2041.outbound.protection.outlook.com [40.92.57.41]) by sourceware.org (Postfix) with ESMTPS id 747663858D20 for ; Mon, 28 Oct 2024 04:46:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 747663858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=hotmail.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=hotmail.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 747663858D20 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=40.92.57.41 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1730090784; cv=pass; b=kXqmMKgULxYvh1SUZqgcz9bMPD+GZqRXvWe6yYjQdhKeOv/NSl4vmUB417SkPRQeXg9auuNxlTowJ8fYpFs2FarkWWYxRK32lGtzcSuDrf1cqW5JKooCGnQLZ1ExPiiVqbB34zICTwlIhHd0yfmPWgB68Ap6vcZ50xVxC+ZNKKo= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1730090784; c=relaxed/simple; bh=qyLXPFUg8ndE0nVMLV9EYkK8blTWvS/fiiw7Hoje4H8=; h=DKIM-Signature:Message-ID:Date:To:From:Subject:MIME-Version; b=ZhcZMU2K4uGOdlalV1/6wv70HCXJW8q9DTtmPpQ7t99BMNr4bBw2E+iowTICvkAQQIfCLJQesbupurmbM+IrW7cVppDC1BKSMAIvtup21DkCkmFl9Utkpw9+7g1ggltQ/0c5YiD4S/wMOrVIswf+e3sNUlnIIbR+3nkEIYMmB2Y= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=n2KsxQdHuHS1eNuQhxCfcZoJZCVh/0Yyl8sIBSFB5MTcmzgaH9FK/dPwr592kGYr9rocC2HWdaGRwMXif8+tOfYaJ58ZWKC8RnfUcGkWKoiTTR8dAzk3Gb/57gTLPGxObuIKq6vQ9ApIExoateFhIW7evwqOPZNICsBztnfzrO7en2sQ+1AfeEZAaN2yqgEVQCa8MI9ZqHhh4H4jYDe4IrkBDC4WzaKpb1Cmd1p2o3vJz2rLR8YWmJ4rBFWvRtvGbbS3rhW+itH545vspe5G7iH3v06+jkYfuMVd+CKcxfs9l3rpkvFARLLlHN5a0pnX7d3WnzxecgFQSF0RZ51V+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=GxkTz4SNIUKWzaq5MnlS7JvahZH/c6aYPOFKgWb3t88=; b=w7rG3Hvw7U/U9wc50LVtv8D3ESYZQ/bE/hHq3aKlYo6CobzlKEdEkke/JwUXvENuYDwcfUsTDQ/0Jk0uHUjrIvd7FkDztbPH6sNG6C5pqaBGGPoxI34S9DAS/HadavSyJ7Bp7naxT91rWSJOtCTgqJM7TrFU1FWW+ykdtf+wiZFy0U5sPepRcjqv5cbfJX17qnu8HTM/NnWgtWpZPch8LYLJxXtMr7wTjApj0Btqpwzw62hNkLcI8yWpjsIA7t90LTp6l13Sd1cQZqbT3apNV8y64LbVhBGax44VfBs6kHVd6Tl/r8jQb1jJeNglFwVDdSeJ5LK6CgDuhJhivgB9NQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=HOTMAIL.DE; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GxkTz4SNIUKWzaq5MnlS7JvahZH/c6aYPOFKgWb3t88=; b=lGoJXdZ7hXokKLiMp4shp1vUBU3SLV+7rF9M0/jEyix24TpTYneMlLjOzTicK20wL7MUGmMN+eHiu7GRfcdiyDFzkQHaRY63dGLmP/Se7djaayhH3zuZ1+kdvBR4PvCccxqb9wfJpLfkBYIFusofj8EJYzcZvs+nN7nZiyM4YRpeg7gzpma5MUDFLqz1OUknw4a024S0KZwe2iwF2TNtEVDQszAthNNwmXK/5eMgJ2IXGOryuOJcA4vvUcVabeWyQZj/ZTOaIk0S6gCdAKN1+d7O8VK2Q1eUMphVWd5v7eY8jrhY25clhShZECyynmh5Cx5TQm7FcvZrZ1KD/PpadQ== Received: from DU2PR08MB10263.eurprd08.prod.outlook.com (2603:10a6:10:491::6) by GV1PR08MB7379.eurprd08.prod.outlook.com (2603:10a6:150:23::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.24; Mon, 28 Oct 2024 04:46:18 +0000 Received: from DU2PR08MB10263.eurprd08.prod.outlook.com ([fe80::c2a3:fed5:607f:20c8]) by DU2PR08MB10263.eurprd08.prod.outlook.com ([fe80::c2a3:fed5:607f:20c8%5]) with mapi id 15.20.8093.014; Mon, 28 Oct 2024 04:46:18 +0000 Message-ID: Date: Mon, 28 Oct 2024 05:47:03 +0100 User-Agent: Mozilla Thunderbird Content-Language: en-US To: "gdb-patches@sourceware.org" , Andrew Burgess From: Bernd Edlinger Subject: [PATCH v4] Fix signal unsafe call inside a signal X-ClientProxiedBy: FR3P281CA0014.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:1d::7) To DU2PR08MB10263.eurprd08.prod.outlook.com (2603:10a6:10:491::6) X-Microsoft-Original-Message-ID: <37f60e71-da68-4b69-b90d-1f1f054e54d5@hotmail.de> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PR08MB10263:EE_|GV1PR08MB7379:EE_ X-MS-Office365-Filtering-Correlation-Id: 68b648f7-edfa-4aec-7278-08dcf70b75bb X-Microsoft-Antispam: BCL:0; ARA:14566002|7092599003|461199028|15080799006|5072599009|19110799003|8060799006|6090799003|3412199025|4302099013|440099028|1602099012|10035399004; X-Microsoft-Antispam-Message-Info: =?utf-8?q?wiAg7NMx68s6cPiOtal34by8DjNSkXT?= =?utf-8?q?+efTLwoiYvlpAHFTUAEwnH3W88OwayaLEsBJ9T530XuVEfYNdUP8pW3KD55HoL1yF?= =?utf-8?q?rK5ybzEoQrynfoy43XFgrS1kd/zTQnIdBJVA2AYFYoU7H4UmAxXdzpCgacsQUJAAK?= =?utf-8?q?u8Dt5Kq3jO0T3ErCpZf92s8HVA8etnxrAnkDpo0a6X5PLc2++hTqXLdcbE7gVu1tx?= =?utf-8?q?XWZb2bh2ZcUtPjmGrk1lNtB5u6nGu15gXHdV9LOTcaOLh1kI/hTlpxqaTlX+WBqYx?= =?utf-8?q?7jN8pRx1G8n6X8bETuDbJmr8w0Xu9+KSv3p9+MaPNY6SktNxEQCjuq9+lN9AYFk74?= =?utf-8?q?uk/QONM7fd4kR8rG7BAOxLVbnZqY8uwsrP8a2TUCoeEhstXvTxrqVqA/zDgtx0nN6?= =?utf-8?q?1ST0MRLO5chYsVKV+2mXEO35dLQOZypJpE3yeECjhQH2g/9ZfBOOcwJyoDJuej9Dc?= =?utf-8?q?0Qn1TjZ7CowH1itV9rtJoaoV8WGk5cgsSZAclWyZXT9eFtjiRGiiEb7etEVa5+6T/?= =?utf-8?q?0qnu1YIviEYNLxIv4h2yPHpblFQdcq5PW6/8oww415Y79AGZDXJG1bCP5jE/+2baZ?= =?utf-8?q?1uiaU+gn7RKUfQToeou56abOOp1POlA6+YE2dWvqRu69nVYHAQ6JnEvxtGrDqTqeV?= =?utf-8?q?kdufA2NU5NrJmFM9ebzzSCThMhVt6GxhIkzKckNGzlTrb51T1pVgjDvMrFp8GWVJI?= =?utf-8?q?T3pxYY7BtAwgXB48VZsOUpp4BhuiUVcZyQjbnliVEzu9rYmnLqULOU/TKcULLU9vX?= =?utf-8?q?GF/qoBhGeKG9Zl+pYzHM1s3y3YCFmwlXLxCRWwGzHabkfjdCYL8OQwpwIQOJeCZWA?= =?utf-8?q?zOEw4RAbUSIW9/nWpP+iS/7Ms54b236lllbB8322PKMC+glIXBu5o2xouejEzqcBn?= =?utf-8?q?AvqwMvAt1Qe+I1FM8srK+ZiEj6pS9TNYa93cx+p+g=3D=3D?= X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?nLNpsLDKdQ2iS6SaBz4ho/MoKKMh?= =?utf-8?q?LYfeQASog05BzklYA3eeJ+tPuFkCRBWceflXCWR0+IlN3C7Yxop3GqIUeTthp6LZn?= =?utf-8?q?SDE6HSJSaUSfKC5ucvhfhoEnhQTPV+sBQhumvPVQhC3aeKHH4o0FIjj9EsYEe30L5?= =?utf-8?q?rR1Iy8+7baR+Qx6DimYdYiudGVL1bWxq5nCwIgo/U84RarjvuM5vpTgrAM7xuBKk1?= =?utf-8?q?nwGSH3ozia0bp7SS+xrUtMWctRjYROzijYPlUvcJhWZwlPZ/4a+d9NnQoZlSc/nzT?= =?utf-8?q?b8KMKsGNVKueiXCLgD261RfFILOUXhLxEnyAkJ22Br6gjk1dOz3nx65I/vAvvkeRT?= =?utf-8?q?RboaSFsF8BYnHnZlwDY0qcmOLQReovqDcsRQpYcp/kQ+YleSpK7SDcjMTOfvYeOxH?= =?utf-8?q?KTP33VvtL7o3i6CJBKo+7fF52XSLt2vv1G2txtbe/F83BzqM9BAjc6jc6IkcCTCrT?= =?utf-8?q?DbuukICzi9LTjotzPM2Qz5COpQm5KAj2/v+LOppyHPi0TnlT2KOcO8sR1/MOBRRlL?= =?utf-8?q?oBqZO7QiIasdqwy3r1J6QQalIs5z6bBzcJn5iF4/vyh5VqrlLRHMwxH/9UsLHxbAh?= =?utf-8?q?m+RbPvZs8RjYSoAAP+Uv4rk6WDIvps2gBXNSbGbUY+qkmMR/p9IuxJKrnUnGoy0tM?= =?utf-8?q?pycPBtPu1CbFbdIZ/SLjY41qEKdCwsHhVJFKOOrmc43cSRBeSMhhtyJzta+1Q/fpd?= =?utf-8?q?lk71Yklrj4ps2gsARWFj05e7mOQ+nSGpHupdDv3/4WJThBk2LFAhgA41ERq3catA3?= =?utf-8?q?PP7NHSWXGsYMWBL5S/NYg30XFiSclFYiQ9jiC4qnNRBxiCltEgKfJ+Xgbrh1O9aUH?= =?utf-8?q?CUMEIPE/4HzNQJBME/No44rwWcGC1mnSKDiVW4VIgSw5YtRtnxhI5/Pci9445Vlnz?= =?utf-8?q?7VusdHM3WTeadWYxobRC0NinYe1wM1dsRlLt/dyZpKEyJYbzcExeR2EOjL+WcR0Uc?= =?utf-8?q?c3T6cOUjzUYMtPRBHad78PnST9KcutrWml6F+u6BwUL+yf92REL5qCHvb7khDuc4a?= =?utf-8?q?vxM5xwbUsZU0owRV6LCdNz0VE6rC6dzXpWNDoyjtOtW1o9o9rlzscVVn9SgZtyyvO?= =?utf-8?q?fMAqohSFTe/szujtUCWqAPVYg2VLZ5JG3QzoKmJckA3f1C7z4exNaBYxORgaUWunW?= =?utf-8?q?OnQ24T1HLwveztY9IOSxtlh1CHCQlMAJuzuhyagt+PZYbj1I9w4Wo5GyRx63HxG9S?= =?utf-8?q?FGu4Xpsvbi4/YNA2I?= X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-37dd7.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: 68b648f7-edfa-4aec-7278-08dcf70b75bb X-MS-Exchange-CrossTenant-AuthSource: DU2PR08MB10263.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Oct 2024 04:46:17.4388 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV1PR08MB7379 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org It can easily happen that the signal handler function `handle_fatal_signal` uses various signal unsafe functions. The problematic functions are `_` and `strsignal` which can be pre-computed after the `setlocale` call is done. Unfortunately when compiled with --disable-libbacktrace a different code path is used, that calls the glibc function `backtrace` which calls `malloc` and `free` and is therefore also signal unsafe, that is probably unfixable, so there is no attempt to fix anything in this code path. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31713#c9 --- gdb/bt-utils.c | 19 +++++++++-- gdb/bt-utils.h | 2 ++ gdb/event-top.c | 84 ++++++++++++++++++++++++++++++++++++++++++++----- gdb/main.c | 2 ++ 4 files changed, 98 insertions(+), 9 deletions(-) v2: moved initalization of language specific string to an init function v3: addressed review comments v4: improved commit message, addressed review commments diff --git a/gdb/bt-utils.c b/gdb/bt-utils.c index f658ce0d4bc..fb006c29178 100644 --- a/gdb/bt-utils.c +++ b/gdb/bt-utils.c @@ -145,8 +145,23 @@ gdb_internal_backtrace_1 () #else #error "unexpected internal backtrace policy" #endif + +static const char *str_backtrace = "----- Backtrace -----\n"; +static const char *str_backtrace_unavailable = "Backtrace unavailable\n"; + #endif /* GDB_PRINT_INTERNAL_BACKTRACE */ +/* Initialize language specific strings. */ + +void +gdb_internal_backtrace_init_str () +{ +#ifdef GDB_PRINT_INTERNAL_BACKTRACE + str_backtrace = _("----- Backtrace -----\n"); + str_backtrace_unavailable = _("Backtrace unavailable\n"); +#endif +} + /* See bt-utils.h. */ void @@ -161,12 +176,12 @@ gdb_internal_backtrace () gdb_stderr->write_async_safe (msg, strlen (msg)); }; - sig_write (_("----- Backtrace -----\n")); + sig_write (str_backtrace); if (gdb_stderr->fd () > -1) gdb_internal_backtrace_1 (); else - sig_write (_("Backtrace unavailable\n")); + sig_write (str_backtrace_unavailable); sig_write ("---------------------\n"); #endif diff --git a/gdb/bt-utils.h b/gdb/bt-utils.h index ec2d14a5484..6fe7c8c4f3b 100644 --- a/gdb/bt-utils.h +++ b/gdb/bt-utils.h @@ -71,4 +71,6 @@ extern void gdb_internal_backtrace (); extern void gdb_internal_backtrace_set_cmd (const char *args, int from_tty, cmd_list_element *c); +extern void gdb_internal_backtrace_init_str (); + #endif /* BT_UTILS_H */ diff --git a/gdb/event-top.c b/gdb/event-top.c index d3cf144958a..e7556f71453 100644 --- a/gdb/event-top.c +++ b/gdb/event-top.c @@ -908,6 +908,51 @@ unblock_signal (int sig) return false; } +/* Signal safe language specific strings. */ + +#ifdef GDB_PRINT_INTERNAL_BACKTRACE +static const char *str_fatal_signal; +static const char *str_sigsegv; +#ifdef SIGFPE +static const char *str_sigfpe; +#endif +#ifdef SIGBUS +static const char *str_sigbus; +#endif +#ifdef SIGABRT +static const char *str_sigabrt; +#endif +static const char *str_unknown_signal; +static const char *str_fatal_error_detected_gdb_will_now_terminate; +static const char *str_this_is_a_bug; +static const char *str_for_instructions_see; + +/* Initialize language specific strings. */ + +static void +init_str_handle_fatal_signal () +{ + str_fatal_signal = _("Fatal signal: "); + str_sigsegv = strsignal (SIGSEGV); +#ifdef SIGFPE + str_sigfpe = strsignal (SIGFPE); +#endif +#ifdef SIGBUS + str_sigbus = strsignal (SIGBUS); +#endif +#ifdef SIGABRT + str_sigabrt = strsignal (SIGABRT); +#endif + str_unknown_signal = _("Unknown signal"); + str_fatal_error_detected_gdb_will_now_terminate = + _("A fatal error internal to GDB has been detected, " + "further\ndebugging is not possible. GDB will now " + "terminate.\n\n"); + str_this_is_a_bug = _("This is a bug, please report it."); + str_for_instructions_see = _(" For instructions, see:\n"); +} +#endif + /* Called to handle fatal signals. SIG is the signal number. */ [[noreturn]] static void @@ -926,19 +971,40 @@ handle_fatal_signal (int sig) if (bt_on_fatal_signal) { sig_write ("\n\n"); - sig_write (_("Fatal signal: ")); - sig_write (strsignal (sig)); + sig_write (str_fatal_signal); + switch (sig) + { + case SIGSEGV: + sig_write (str_sigsegv); + break; +#ifdef SIGFPE + case SIGFPE: + sig_write (str_sigfpe); + break; +#endif +#ifdef SIGBUS + case SIGBUS: + sig_write (str_sigbus); + break; +#endif +#ifdef SIGABRT + case SIGABRT: + sig_write (str_sigabrt); + break; +#endif + default: + sig_write (str_unknown_signal); + break; + } sig_write ("\n"); gdb_internal_backtrace (); - sig_write (_("A fatal error internal to GDB has been detected, " - "further\ndebugging is not possible. GDB will now " - "terminate.\n\n")); - sig_write (_("This is a bug, please report it.")); + sig_write (str_fatal_error_detected_gdb_will_now_terminate); + sig_write (str_this_is_a_bug); if (REPORT_BUGS_TO[0] != '\0') { - sig_write (_(" For instructions, see:\n")); + sig_write (str_for_instructions_see); sig_write (REPORT_BUGS_TO); sig_write ("."); } @@ -1066,6 +1132,10 @@ gdb_init_signals (void) create_async_signal_handler (async_sigtstp_handler, NULL, "sigtstp"); #endif +#ifdef GDB_PRINT_INTERNAL_BACKTRACE + init_str_handle_fatal_signal (); +#endif + #ifdef SIGFPE signal (SIGFPE, handle_fatal_signal); #endif diff --git a/gdb/main.c b/gdb/main.c index e4a40c51023..4370e95ada4 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -58,6 +58,7 @@ #include "observable.h" #include "serial.h" #include "cli-out.h" +#include "bt-utils.h" /* The selected interpreter. */ std::string interpreter_p; @@ -676,6 +677,7 @@ captured_main_1 (struct captured_main_args *context) /* Note: `error' cannot be called before this point, because the caller will crash when trying to print the exception. */ main_ui = new ui (stdin, stdout, stderr); + gdb_internal_backtrace_init_str (); current_ui = main_ui; gdb_stdtarg = gdb_stderr;