From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 69419383F96A for ; Mon, 13 Jun 2022 16:16:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 69419383F96A Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-630-eBdIEMaUMNGNPWHNoNDM0Q-1; Mon, 13 Jun 2022 12:16:14 -0400 X-MC-Unique: eBdIEMaUMNGNPWHNoNDM0Q-1 Received: by mail-qk1-f198.google.com with SMTP id s11-20020a05620a254b00b006a6a23ff939so5343746qko.17 for ; Mon, 13 Jun 2022 09:16:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qeaB6Yf6AWU4E3vySfAYlXvr+Yd6hNS6AvFnQ6GjRto=; b=MRJzbjCp4HsZ0nq3EBKfFJHXaJBd9kaKqPE8+hKV22Kmu/saq/pUwQetw+Tmyp7bgY NPdXJXF6AYRzjUZxWeA30w98pYfx2xbrEWwIXyJSIp5jYIbHSqBd93aVV92ieYtrnlaL uEpCVNrW7YwxF5RChEASyh5TgjFAqvteZH9di0kKcJVn+3x9H1eDKxHuN9z/DPSRpn+V r/nPqRrjEhs+A9mR1FoyOmcWTVOnNr1mbs6SMJMo4axMdj5cCN56LoxulVE/435FB1IC yxHUIVstahiB/lJ6c/LtPhqTjHysjysHXGjEpCW5TK3yuJFzwjH6an4wKaE9WS7Xk5XW vAIQ== X-Gm-Message-State: AOAM533xN3YiCbN+SHgx528eHCUN56YkNaQYrC0X5k6zfbAMtNccorTV Qvp0pYJesWTIinsUD4GoAKkSrjX7ZszF6mltflqsXjUizQ5uP8HJFNmMa1m/9qsqqTp0eWnxp1P ELGi45hzTDjVi4RoFkaqpwOTQy7tr8w06JNoaujEhVR1MuFDkjRzxNl+r7qqpdYCZt1T3HRBlLw == X-Received: by 2002:a05:622a:1c8:b0:304:fc4f:838b with SMTP id t8-20020a05622a01c800b00304fc4f838bmr424267qtw.438.1655136973287; Mon, 13 Jun 2022 09:16:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwkB5XkIp5TCvo34ki33txhxH/1vPW+V5SOjAI6nnQIBo3dGpo6R7G4se32HQnGgIhFQHU4VQ== X-Received: by 2002:a05:622a:1c8:b0:304:fc4f:838b with SMTP id t8-20020a05622a01c800b00304fc4f838bmr424229qtw.438.1655136972852; Mon, 13 Jun 2022 09:16:12 -0700 (PDT) Received: from localhost (host109-154-20-145.range109-154.btcentralplus.com. [109.154.20.145]) by smtp.gmail.com with ESMTPSA id bi13-20020a05620a318d00b006a6dcd92eb3sm6630617qkb.121.2022.06.13.09.16.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jun 2022 09:16:12 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Luis Machado , Andrew Burgess Subject: [PATCHv3 5/6] gdb: ensure the cast in gdbarch_tdep is valid Date: Mon, 13 Jun 2022 17:15:53 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.6 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_NONE, SPF_HELO_NONE, SPF_NONE, 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-List-Received-Date: Mon, 13 Jun 2022 16:16:16 -0000 This commit builds on the previous commit and modifies the gdbarch_tdep function to ensure that the cast being performed is valid. To do this I make use of dynamic_cast to ensure that the generic gdbarch_tdep pointer that we have is of the correct type. The only problem with this approach is that, in order to use dynamic_cast, we need RTTI information, which requires the class to have a vtable, which currently, is not something the various tdep classes have. And so, in this commit, I add a virtual destructor to the gdbarch_tdep class. With this change I can now add an assert in the gdbarch_tdep function. Obviously, this change comes at a cost, creation of the tdep classes is now slightly more expensive (due to vtable initialisation), however, this only happens when a new gdbarch is created, which is not that frequent, so I don't see that as a huge concern. Then, there is an increased cost each time the tdep is accessed. This is much more frequent, but I don't believe the cost is excessive (a vtable pointer comparison), at least, no worse than many of our other asserts. If we consider the motivating example that was discussed in the previous commit; build GDB for all targets on an x86-64 GNU/Linux system, and then attempt to "run" a RISC-V binary using the native x86-64 Linux target. Previously this would trigger an assert while accessing fields within a i386_gdbarch_tdep, like this: ../../src/gdb/i387-tdep.c:596: internal-error: i387_supply_fxsave: Assertion `tdep->st0_regnum >= I386_ST0_REGNUM' failed. But with the changes from this commit in place, we now see an assertion failure like this: ../../src/gdb/gdbarch.h:166: internal-error: gdbarch_tdep: Assertion `dynamic_cast (tdep) != nullptr' failed. On the face of it, this might not seem like much of an improvement, but I think it is. The previous assert was triggered by undefined behaviour. There's no guarantee that we would see an assertion at all, a different combination of native target and binary format might not trigger an assert (and just do the wrong thing), or might crash GDB completely. In contrast, the new assert is based on defined behaviour, we'll always assert if GDB goes wrong, and we assert early, at the point the mistake is being made (casting the result of gdbarch_tdep to the wrong type), rather than at some later point after the incorrect cast has completed. Obviously, when we consider the original example, trying to run a binary of the wrong architecture on a native target, having GDB fail with an assertion is not a real solution. No user action should be able to trigger an assertion failure. In a later commit I will offer a real solution to this architecture mismatch problem. --- gdb/gdbarch.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h index b2c91db0c4f..a47aa8cc8fa 100644 --- a/gdb/gdbarch.h +++ b/gdb/gdbarch.h @@ -58,7 +58,13 @@ struct inferior; #include "regcache.h" -struct gdbarch_tdep {}; +/* The base class for every architecture's tdep sub-class. We include a + virtual destructor so that sub-classes will have RTTI information. */ + +struct gdbarch_tdep +{ + virtual ~gdbarch_tdep() = default; +}; /* The architecture associated with the inferior through the connection to the target. @@ -156,8 +162,9 @@ template static inline TDepType * gdbarch_tdep (struct gdbarch *gdbarch) { - struct gdbarch_tdep *tdep = gdbarch_tdep_1 (gdbarch); - return static_cast (tdep); + TDepType *tdep = dynamic_cast (gdbarch_tdep_1 (gdbarch)); + gdb_assert (tdep != nullptr); + return tdep; } /* Mechanism for co-ordinating the selection of a specific -- 2.25.4