From patchwork Wed Sep 4 17:12:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1980952 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=RNUg+Zjf; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WzTb40f7jz1yZ9 for ; Thu, 5 Sep 2024 03:13:04 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 59A72385DDDA for ; Wed, 4 Sep 2024 17:13:02 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 940F63858406 for ; Wed, 4 Sep 2024 17:12:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 940F63858406 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 940F63858406 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725469961; cv=none; b=g0p1z5yh37rYguwWMyPEUbZLe7BR0e1c4zfOWvEus9TmWZ+ey1EPa1q9jDyR163xwEB0O/Nr4UB50vKyiP67frwkyQYcjjQf99yUlOKCQh8rEYI9x5aetATeYBuFkRpMjUjFfsUKCT7Kb1/bIyikQYema65NOjIP1zIdEXh94Wo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725469961; c=relaxed/simple; bh=mw3vU/rshIdCYHEZDZJLpFqCuy9XeQUZTbMoZQol2HU=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=kRw4llkERIEhDmTAvE8yaBhEqQv0PuZjpDRYlt7r0F/jf/sLf994ZnQfIyYsGilXIfrsPJsZY9uLYNSAsO1qxWLOBa+6Aud3nK1RLntZD/RSEFTplSC8vX37VHb/315D8k1+RG/9k4aWwf9LvrmA+cqR/xvKf2S9JGjh4JI720w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725469960; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=SYOYvw8+pQ47n3PGaM+e+IwghjcOYb6obSrT8Q2Hvrc=; b=RNUg+ZjfiGElCtxte9uNEfjoY1Sw5M7P4pQ+8v7jIvpTx5eXdlX4tyRIpeduiAdGVSSpUR 9EILMRL9tKsJgHpHgIuMQO60Mj6e5qk9Xfg0AHX2e44zOPOuIsf6flVHCO5Zskr9XqK+7l mmc2wpO8m2jSqdgFDIYgVALZkiPi1Y8= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-423-gjxv2VbFMyOKXO7hECB3vQ-1; Wed, 04 Sep 2024 13:12:39 -0400 X-MC-Unique: gjxv2VbFMyOKXO7hECB3vQ-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 169AB195394A for ; Wed, 4 Sep 2024 17:12:37 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.29]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 157FB1955F45; Wed, 4 Sep 2024 17:12:35 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 484HCXJl3188977 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 4 Sep 2024 19:12:33 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 484HCX9D3188976; Wed, 4 Sep 2024 19:12:33 +0200 Date: Wed, 4 Sep 2024 19:12:33 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++, v2: Fix get_member_function_from_ptrfunc with -fsanitize=bounds [PR116449] Message-ID: References: <62e7d11b-f731-423f-8e19-7c08c4c131a6@redhat.com> MIME-Version: 1.0 In-Reply-To: <62e7d11b-f731-423f-8e19-7c08c4c131a6@redhat.com> X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org On Wed, Sep 04, 2024 at 12:34:04PM -0400, Jason Merrill wrote: > > So, one possibility would be to call save_expr unconditionally in > > get_member_function_from_ptrfunc as well. > > > > Or build a TARGET_EXPR (force_target_expr or similar). > > Yes. I don't have a strong preference between the two. Here is a patch that uses save_expr but uses it still conditionally, doesn't make sense to use it for the common case of just decls, there is nothing to unshare in that case. Passed the test, ok if it passes full bootstrap/regtest? 2024-09-04 Jakub Jelinek PR c++/116449 * typeck.cc (get_member_function_from_ptrfunc): Use save_expr on instance_ptr and function even if it doesn't have side-effects, as long as it isn't a decl. * g++.dg/ubsan/pr116449.C: New test. Jakub --- gcc/cp/typeck.cc.jj 2024-09-02 17:07:30.115098114 +0200 +++ gcc/cp/typeck.cc 2024-09-04 19:08:24.127490242 +0200 @@ -4188,10 +4188,21 @@ get_member_function_from_ptrfunc (tree * if (!nonvirtual && is_dummy_object (instance_ptr)) nonvirtual = true; - if (TREE_SIDE_EFFECTS (instance_ptr)) - instance_ptr = instance_save_expr = save_expr (instance_ptr); + /* Use save_expr even when instance_ptr doesn't have side-effects, + unless it is a simple decl (save_expr won't do anything on + constants), so that we don't ubsan instrument the expression + multiple times. See PR116449. */ + if (TREE_SIDE_EFFECTS (instance_ptr) || !DECL_P (instance_ptr)) + { + instance_save_expr = save_expr (instance_ptr); + if (instance_save_expr == instance_ptr) + instance_save_expr = NULL_TREE; + else + instance_ptr = instance_save_expr; + } - if (TREE_SIDE_EFFECTS (function)) + /* See above comment. */ + if (TREE_SIDE_EFFECTS (function) || !DECL_P (function)) function = save_expr (function); /* Start by extracting all the information from the PMF itself. */ --- gcc/testsuite/g++.dg/ubsan/pr116449.C.jj 2024-09-04 18:58:46.106764285 +0200 +++ gcc/testsuite/g++.dg/ubsan/pr116449.C 2024-09-04 18:58:46.106764285 +0200 @@ -0,0 +1,14 @@ +// PR c++/116449 +// { dg-do compile } +// { dg-options "-O2 -Wall -fsanitize=undefined" } + +struct C { void foo (int); void bar (); int c[16]; }; +typedef void (C::*P) (); +struct D { P d; }; +static D e[1] = { { &C::bar } }; + +void +C::foo (int x) +{ + (this->*e[c[x]].d) (); +}