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 96242388E683 for ; Tue, 28 Jun 2022 14:29:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 96242388E683 Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-582-z6BP0TXyOgaBkYpcKJ8j8Q-1; Tue, 28 Jun 2022 10:29:11 -0400 X-MC-Unique: z6BP0TXyOgaBkYpcKJ8j8Q-1 Received: by mail-wr1-f70.google.com with SMTP id z11-20020a056000110b00b0021b9c009d09so1860750wrw.17 for ; Tue, 28 Jun 2022 07:29:11 -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=0KbI6WJDKpyBR2iiltQgrxfA6YkXZxEBrO1sPgoPseA=; b=h937T2Ywe/LyGcrRsvzDWSTNkOyouRwQh5LcMqn0TUgrnqX9DucWU0j2F0cXRgUM8i qbw/nWk+e6DuhTZmbeHacxNoWAe+iXQstrOKJBbQlxelxP3WXpL8w9ZLJYB7M8BN/Or7 9MWdZRt1pSEErppW815J93OHLp59y65PsYgCbJ1sA1YAXXZE7/j/f47VQyZJXzXJ6+Fg aRCbLVtDGYrujQBJ36d16Ux6a9JqplEzONiEB47lWADtZlnUN97sgP7hSsY91GDTI9yc ArwIvGk7tgJhMFSscNEBMsTcV+QTYAdNQTMWafR7fC4OWKZh9/+5RZ2PyWvOnyaj4U/U WEGA== X-Gm-Message-State: AJIora8Ay81uEd4ykLoLpILDkJY82w8CEzRweKWcGZctgDPhObEkDDKk kOBXAy5nwXJMsdjd9Z2gGWaVmd8w3RE/QnTdnzF4gIGR61LbXKsvTDClyjnk5txCaWh/dmBCrmT 1ataKkN0WNbSu9m+SMbLjoFF2nAWs8Ru/8sP2P9lNVUDzkGaOYjHxOioJphBQHQo2snuYrExZ8Q == X-Received: by 2002:adf:f751:0:b0:21a:3ab8:1991 with SMTP id z17-20020adff751000000b0021a3ab81991mr18848731wrp.711.1656426550049; Tue, 28 Jun 2022 07:29:10 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uu3eONFZ3VNw9oia1E/CRHGaiYztf+Q16BHeeXaxQeuoKiRm1xTsj2NXBlJeKDtp7d5a05dA== X-Received: by 2002:adf:f751:0:b0:21a:3ab8:1991 with SMTP id z17-20020adff751000000b0021a3ab81991mr18848704wrp.711.1656426549667; Tue, 28 Jun 2022 07:29:09 -0700 (PDT) Received: from localhost (15.72.115.87.dyn.plus.net. [87.115.72.15]) by smtp.gmail.com with ESMTPSA id m22-20020a05600c3b1600b00397402ae674sm23098789wms.11.2022.06.28.07.29.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jun 2022 07:29:09 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Pedro Alves , Tom Tromey Subject: [PATCHv4 5/6] gdbsupport: add checked_static_cast Date: Tue, 28 Jun 2022 15:28:56 +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=-10.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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, 28 Jun 2022 14:29:14 -0000 Pedro, This patch is based on the idea you put forward in your feedback. In terms of naming I went with gdb::checked_static_cast in the end as I feel the new function is really replacing static_cast. The use of dynamic_cast is really an implementation detail. Let me know what you think. Thanks, Andrew --- This commit was inspired by these mailing list posts: https://sourceware.org/pipermail/gdb-patches/2022-June/190323.html https://sourceware.org/pipermail/gdb-patches/2022-April/188098.html The idea is to add a new function gdb::checked_static_cast, which can, in some cases, be used as a drop-in replacement for static_cast. And so, if I previously wrote this: BaseClass *base = get_base_class_pointer (); DerivedClass *derived = static_cast (base); I can now write: BaseClass *base = get_base_class_pointer (); DerivedClass *derived = gdb::checked_static_cast (base); The requirement is that BaseClass and DerivedClass must be polymorphic. The benefit of making this change is that, when GDB is built in developer mode, a run-time check will be made to ensure that `base` really is of type DerivedClass before the cast is performed. If `base` is not of type DerivedClass then GDB will assert. In a non-developer build gdb::checked_static_cast is equivalent to a static_cast, and there should be no performance difference. This commit adds the support function, but does not make use of this function, a use will be added in the next commit. Co-Authored-By: Pedro Alves Co-Authored-By: Tom Tromey --- gdbsupport/gdb-checked-static-cast.h | 68 ++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 gdbsupport/gdb-checked-static-cast.h diff --git a/gdbsupport/gdb-checked-static-cast.h b/gdbsupport/gdb-checked-static-cast.h new file mode 100644 index 00000000000..62378e458f8 --- /dev/null +++ b/gdbsupport/gdb-checked-static-cast.h @@ -0,0 +1,68 @@ +/* Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef COMMON_GDB_CHECKED_DYNAMIC_CAST_H +#define COMMON_GDB_CHECKED_DYNAMIC_CAST_H + +#include "gdbsupport/traits.h" + +namespace gdb +{ + +/* This function can be used in place of static_cast when casting between + pointers of polymorphic types. The benefit of using this call is that, + when compiling in developer mode, dynamic_cast will be used to validate + the cast. This use of dynamic_cast is why this function will only + work for polymorphic types. + + In non-developer (i.e. production) builds, the dynamic_cast is replaced + with a static_cast which is usually significantly faster. */ + +template +T +checked_static_cast (V *v) +{ + /* We only support casting to pointer types. */ + static_assert (std::is_pointer::value, "target must be a pointer type"); + + /* Check for polymorphic types explicitly in case we're in release mode. */ + static_assert (std::is_polymorphic::value, "types must be polymorphic"); + + /* Figure out the type that T points to. */ + using T_no_P = typename std::remove_pointer::type; + + /* In developer mode this cast uses dynamic_cast to confirm at run-time + that the cast from V* to T is valid. However, we can catch some + mistakes at compile time, this assert prevents anything other than + downcasts, or casts to same type. */ + static_assert (std::is_base_of::value + || std::is_base_of::value, + "types must be related"); + +#ifdef DEVELOPMENT + T result = dynamic_cast (v); + gdb_assert (result != nullptr); +#else + T result = static_cast (v); +#endif + + return result; +} + +} + +#endif /* COMMON_GDB_CHECKED_DYNAMIC_CAST_H */ -- 2.25.4