From patchwork Fri Jun 9 16:17:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 70833 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 759483856628 for ; Fri, 9 Jun 2023 16:18:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 759483856628 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686327487; bh=ptm2YTb9Q+UR5CbEVBDdhVrhHdBTbWXrm1mnQJ6xWaI=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=C6gxn7ZMUTSM7/WQradGGRh+R4koUey2x1WIvrSUrSbHp3iZle+oZLYYNF9sWUJVX yPCl0nolznnEPiY5sB1EodmGm1b/zomosYQwfvWOGT+VfYx5mC+P9XRSQlZNFeYUra W6zjPpTsa2TpyB+/H4ma7wk+JN7MPnnQc6zgp13k= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) by sourceware.org (Postfix) with ESMTPS id 254B13858D3C for ; Fri, 9 Jun 2023 16:17:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 254B13858D3C Received: by mail-io1-xd2f.google.com with SMTP id ca18e2360f4ac-77aec3ce39aso2542139f.0 for ; Fri, 09 Jun 2023 09:17:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686327452; x=1688919452; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ptm2YTb9Q+UR5CbEVBDdhVrhHdBTbWXrm1mnQJ6xWaI=; b=iYhKQGmCGw0e1gw/2BToj8fBI4FW6f+WM3n92ELZgD+Z24/YP33cFWEnpxe3VyD37m vUz/0QEwdQR+v0wMcmLKUGiJIktgJSrNkkBvUQtPNDAUVb4tLA5EEDluLO8Rn0NLw+6h WJkGWLIhlfHmHcr/mb8iD29fiipos9nRMaVJKL6Fq51nC04dKZHOFcHKEGxsT1D0p4Tj ddP5ZO/VBWke3U6y6KIJzFR9wVp6vMX1SoO11VtiSAGIvWb+CIMV2I5ZIK2oybCCU3Nm i9E3+DBg3T9173MwS848yIh+K96nMZW3Owuf6emlBXHkH7oYbsfd438pAcDgMTU8HnJ0 gvYQ== X-Gm-Message-State: AC+VfDw43moyJU+eFWK18rWDg8h98mRB2Sjc/lWkIOtRyZboi8NHoNb6 60OEkxWvO8MB1cPFK2+aodct6QxMXkRvaHqIKfLoig== X-Google-Smtp-Source: ACHHUZ6kE4ru+GHMGjy9qjRt5aIRHkiVxHoBhUH7R2OzWJF9IaZOFoaEZBT4c+C0kldOSoWf4YWyMw== X-Received: by 2002:a6b:e515:0:b0:774:91f6:9530 with SMTP id y21-20020a6be515000000b0077491f69530mr2184186ioc.9.1686327452274; Fri, 09 Jun 2023 09:17:32 -0700 (PDT) Received: from localhost.localdomain (75-166-136-83.hlrn.qwest.net. [75.166.136.83]) by smtp.gmail.com with ESMTPSA id c8-20020a5e8f08000000b007749c05ab14sm1130367iok.2.2023.06.09.09.17.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jun 2023 09:17:31 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Simon Marchi , Tom Tromey Subject: [PATCH] Remove unchecked casts to mi_interp Date: Fri, 9 Jun 2023 10:17:20 -0600 Message-Id: <20230609161720.3677538-1-tromey@adacore.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Simon noticed a crash that could be caused via new Python gdb.execute_mi function. Looking into this, I found a few unchecked casts to mi_interp, like: - struct mi_interp *mi = (struct mi_interp *) command_interp (); This patch replaces all such casts with safer variants. For -gdb-exit and mi_load_progress, I chose to have the functions simply not generate any output. It didn't seem useful to do so. Some casts I eliminated by adding a parameter to a function. Then, in mi_execute_command, I changed the code to use gdb::checked_static_cast. This is appropriate because this particular overload can only be called by the MI interpreter. There does not seem to be a very good way to test -gdb-exit. Regression tested on x86-64 Fedora 36. --- gdb/mi/mi-interp.c | 9 --------- gdb/mi/mi-interp.h | 9 +++++++++ gdb/mi/mi-main.c | 48 +++++++++++++++++++++++++++++----------------- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c index a8de62ba630..0e51c884a65 100644 --- a/gdb/mi/mi-interp.c +++ b/gdb/mi/mi-interp.c @@ -72,15 +72,6 @@ display_mi_prompt (struct mi_interp *mi) ui->prompt_state = PROMPTED; } -/* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and - returns NULL otherwise. */ - -static struct mi_interp * -as_mi_interp (struct interp *interp) -{ - return dynamic_cast (interp); -} - void mi_interp::on_command_error () { diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h index ce003242801..6326ec7d524 100644 --- a/gdb/mi/mi-interp.h +++ b/gdb/mi/mi-interp.h @@ -107,4 +107,13 @@ class mi_interp final : public interp void mi_output_solib_attribs (ui_out *uiout, struct so_list *solib); +/* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and + returns NULL otherwise. */ + +static inline struct mi_interp * +as_mi_interp (struct interp *interp) +{ + return dynamic_cast (interp); +} + #endif /* MI_MI_INTERP_H */ diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index 7503ffd2f18..6868852ca8a 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -144,14 +144,21 @@ static void print_diff (struct ui_file *file, struct mi_timestamp *start, void mi_cmd_gdb_exit (const char *command, const char *const *argv, int argc) { - struct mi_interp *mi = (struct mi_interp *) current_interpreter (); - - /* We have to print everything right here because we never return. */ - if (current_token) - gdb_puts (current_token, mi->raw_stdout); - gdb_puts ("^exit\n", mi->raw_stdout); - mi_out_put (current_uiout, mi->raw_stdout); - gdb_flush (mi->raw_stdout); + struct mi_interp *mi = as_mi_interp (current_interpreter ()); + + /* If the current interpreter is not an MI interpreter, then just + don't bother printing anything. This case can arise from using + the Python gdb.execute_mi function -- but here the result does + not matter, as gdb is about to exit anyway. */ + if (mi != nullptr) + { + /* We have to print everything right here because we never return. */ + if (current_token) + gdb_puts (current_token, mi->raw_stdout); + gdb_puts ("^exit\n", mi->raw_stdout); + mi_out_put (current_uiout, mi->raw_stdout); + gdb_flush (mi->raw_stdout); + } /* FIXME: The function called is not yet a formal libgdb function. */ quit_force (NULL, FROM_TTY); } @@ -1802,10 +1809,9 @@ mi_cmd_remove_inferior (const char *command, const char *const *argv, int argc) prompt, display error). */ static void -captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) +captured_mi_execute_command (struct mi_interp *mi, struct ui_out *uiout, + struct mi_parse *context) { - struct mi_interp *mi = (struct mi_interp *) command_interp (); - if (do_timings) current_command_ts = context->cmd_start; @@ -1890,10 +1896,9 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) /* Print a gdb exception to the MI output stream. */ static void -mi_print_exception (const char *token, const struct gdb_exception &exception) +mi_print_exception (struct mi_interp *mi, const char *token, + const struct gdb_exception &exception) { - struct mi_interp *mi = (struct mi_interp *) current_interpreter (); - gdb_puts (token, mi->raw_stdout); gdb_puts ("^error,msg=\"", mi->raw_stdout); if (exception.message == NULL) @@ -1925,13 +1930,15 @@ mi_execute_command (const char *cmd, int from_tty) target_log_command (cmd); + struct mi_interp *mi + = gdb::checked_static_cast (command_interp ()); try { command = mi_parse::make (cmd, &token); } catch (const gdb_exception &exception) { - mi_print_exception (token, exception); + mi_print_exception (mi, token, exception); xfree (token); } @@ -1947,7 +1954,7 @@ mi_execute_command (const char *cmd, int from_tty) try { - captured_mi_execute_command (current_uiout, command.get ()); + captured_mi_execute_command (mi, current_uiout, command.get ()); } catch (const gdb_exception &result) { @@ -1960,7 +1967,7 @@ mi_execute_command (const char *cmd, int from_tty) /* The command execution failed and error() was called somewhere. */ - mi_print_exception (command->token, result); + mi_print_exception (mi, command->token, result); mi_out_rewind (current_uiout); /* Throw to a higher level catch for SIGTERM sent to GDB. */ @@ -2189,7 +2196,12 @@ mi_load_progress (const char *section_name, static steady_clock::time_point last_update; static char *previous_sect_name = NULL; int new_section; - struct mi_interp *mi = (struct mi_interp *) current_interpreter (); + struct mi_interp *mi = as_mi_interp (current_interpreter ()); + + /* If the current interpreter is not an MI interpreter, then just + don't bother printing anything. */ + if (mi == nullptr) + return; /* This function is called through deprecated_show_load_progress which means uiout may not be correct. Fix it for the duration