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.129.124]) by sourceware.org (Postfix) with ESMTPS id 01C1738293EC for ; Fri, 10 Jun 2022 13:08:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 01C1738293EC Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-489-5ElnvWMfMEqrBMlYoNR00Q-1; Fri, 10 Jun 2022 09:08:29 -0400 X-MC-Unique: 5ElnvWMfMEqrBMlYoNR00Q-1 Received: by mail-wr1-f71.google.com with SMTP id i10-20020a5d55ca000000b002103d76ffcaso6226393wrw.17 for ; Fri, 10 Jun 2022 06:08:29 -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=bvsadRdY66MH9TjrAVYXmBp3IVHbPoKu6tweFDcqbXT9cPW6tWu+XBRR5iENiaY5br G1DKn4FWQ4ueJ2PXNlILlwqozdlPLASIjf8toCpoHEG8kXf2l7a3JDb9cTHOqed8q6U0 BmryEBg9Hp5F3W2TnlWpy3hxW9l+hN8XScfQAFz7gz/34sqw/FptbLKWov6LrZJDA5qY sI1Ug/ZoFAJ+/kBnhPEO1HW2WSXxChFm/3G1oR1Duo5bMU5WaD5IdufpL08CR8kAvzes E0dXUTdTVeBIjGAIv5mafMGtkdL8HVnipCff8kB8MKqWIRIfUfECbOmfpo81Ix+5f5E5 PLlg== X-Gm-Message-State: AOAM531oM+vPVZNGFMbYBgCs/IIMfUiov8TfHUu75LBdEPVNUVvFVO1s 9PPvMAop3xTO6aLONjWjW9rJh3AQuMt3kMU0Ktsa9VdH0mU42R8I3dvm3iPCL86FXCOiP+3HdLr RgBF2G8HCSYWLFuMGRDtoTpBl+BNMavhoCqyK6PORoGRjoEqrGt9crrLq7kXDPXoT1/F3RmDnWQ == X-Received: by 2002:a5d:5885:0:b0:218:3d12:e0eb with SMTP id n5-20020a5d5885000000b002183d12e0ebmr27894074wrf.510.1654866507775; Fri, 10 Jun 2022 06:08:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx5qenPi4VKls0SJ5n9qONlzIpzYMsKYwgIy5Xr+/nHObRX9otVRTfaC/fF7FgJTwtB0p7MBQ== X-Received: by 2002:a5d:5885:0:b0:218:3d12:e0eb with SMTP id n5-20020a5d5885000000b002183d12e0ebmr27894041wrf.510.1654866507442; Fri, 10 Jun 2022 06:08:27 -0700 (PDT) Received: from localhost (host109-152-215-36.range109-152.btcentralplus.com. [109.152.215.36]) by smtp.gmail.com with ESMTPSA id w12-20020a7bc74c000000b003973d425a7fsm3601278wmk.41.2022.06.10.06.08.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jun 2022 06:08:27 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 5/6] gdb: ensure the cast in gdbarch_tdep is valid Date: Fri, 10 Jun 2022 14:08:13 +0100 Message-Id: <957b89f452fffb7c88b9bfae65625cddc93e6d00.1654866188.git.aburgess@redhat.com> 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=-10.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, 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: Fri, 10 Jun 2022 13:08:32 -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