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 29081395A051 for ; Tue, 31 May 2022 14:31:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 29081395A051 Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-520-QKzcH33dNoOv1hAFZDj9UQ-1; Tue, 31 May 2022 10:31:02 -0400 X-MC-Unique: QKzcH33dNoOv1hAFZDj9UQ-1 Received: by mail-wr1-f69.google.com with SMTP id e7-20020adfa747000000b0020fe61b0c62so2115012wrd.22 for ; Tue, 31 May 2022 07:31:02 -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=C8Iig6eTmJ+coJh52bq6ObutMCBIpyYQljgO/0etvbs=; b=xQANp5bEUVt8K4JFlriTbHGCBcDRh8T5SHhtIcGckwT1lO/WmaYRoh9UmcCh9Dvfdq tSL53xx1RnSyz7frgv436jh94TQ6ZQFSghv1Ntfm4vDaDVz/kQIrBQx5qYUIOFo6IR7x a2+wk3AuxjgN2Sopp0mqmpb5taEkR+9J9+D5DI4tba2sg5UnrQ7aKPk+pzlRNYomB7A4 E512IIi72sJxmKhL+3NaZH0JoR2/CkdcmzVIBPn36N5sscLCvkUk0zP5jah4Y58TZApf QuALZCEYwSvCOEZ6IRWq/Wz/wg8aKbVaisr8arV+IyzCf2GXCNX0pE//tuvKj89zoryc k/uw== X-Gm-Message-State: AOAM53225y/w0FZCKvOUwP1LZD5ZSxeGvzUK8S/LPtl4DJGxdTcvyr65 MBgJyYSbXC9k20Jz2nulcQHC3bT+lnIyyDg/7N8L4lHN9hWtjUF89OMqOM6zlM33AVACjfEb5yQ N7H0VRtU87+HIykv9rtwFdR/V3aqkKPAlIa1QXdC6YZxq4Liwt0EkHXLY8eH6gJ3hejquR9E9Zw == X-Received: by 2002:a05:600c:1d99:b0:397:482b:429 with SMTP id p25-20020a05600c1d9900b00397482b0429mr23785556wms.130.1654007460751; Tue, 31 May 2022 07:31:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3Za3uco4Fra+6pBaGDXpEN6f2jxo5Ka3FdBcgH26VlLSsU5Qc4O0aPm5kbqGTByWguuj8fA== X-Received: by 2002:a05:600c:1d99:b0:397:482b:429 with SMTP id p25-20020a05600c1d9900b00397482b0429mr23785538wms.130.1654007460494; Tue, 31 May 2022 07:31:00 -0700 (PDT) Received: from localhost (host109-152-215-36.range109-152.btcentralplus.com. [109.152.215.36]) by smtp.gmail.com with ESMTPSA id f14-20020a0560001a8e00b0020c5253d90asm12121711wry.86.2022.05.31.07.30.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 07:31:00 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 4/5] gdb: ensure the cast in gdbarch_tdep is valid Date: Tue, 31 May 2022 15:30:49 +0100 Message-Id: <65f89b8748dfec7fd62681df2b1efcfa2c25026f.1654007211.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.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_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: Tue, 31 May 2022 14:31:05 -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 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h index b2c91db0c4f..ea507d70ec9 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. @@ -157,6 +163,7 @@ static inline TDepType * gdbarch_tdep (struct gdbarch *gdbarch) { struct gdbarch_tdep *tdep = gdbarch_tdep_1 (gdbarch); + gdb_assert (dynamic_cast (tdep) != nullptr); return static_cast (tdep); } -- 2.25.4