From patchwork Fri Sep 6 18:38:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1982040 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=hJ8qSB7m; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4X0lPr2ZXBz1y1D for ; Sat, 7 Sep 2024 04:39:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2ABF8384A474 for ; Fri, 6 Sep 2024 18:39:26 +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 3C6623858C56 for ; Fri, 6 Sep 2024 18:39:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3C6623858C56 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 3C6623858C56 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=1725647945; cv=none; b=fZXTp3LRtSo50jp+sihId2RsNgjjNM2AmUHLODn0/ejx+Ve7mEiEa7/B9G2HZVadLH7w9U7qfrNwnEX11yh2U9Jw67gTYMpSBNz+FpteN4O+9fsDSS/O5BpPw+1IuzzWzUeUC7eywo3uM4sG42xWFQDuBYs43GDIh4psKbZx9sU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725647945; c=relaxed/simple; bh=czZwA9Aw20N/cCe7zEui7zr+SqxPG/AM8SLnt9Kb13I=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=O9aro1MGzD7uC50LEqvyPTIAbwYlTdVnJaxh3aWWr5ssHpjWZYuEpkEdjVfkd7PhbLMoEPn67/2HPKUju8adF5CvFQ1V7d4i3V1SGIVxocd8rzqqskcfjFpkEJF1hoV1Zp6rH7NXrEF4wupdDzjt6yWrGOc5IXozSHoSJutnDIQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725647944; 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=leqVJmxxSmwIeh4NnA2MVSDX3yJ2eNk+gajJONa4siU=; b=hJ8qSB7mWCWOciLKrjrBDbi+x2CPpz9S+SZkH9timDdAp3CuCnsxu4ltVoIKeyQyPtunE1 vYtgHZ/1xiMtzNGcclEtt6BLNNwR4Go6CRKFD1uR/yz0nGXd8O/Q79OaznM+uhbN4xZ9AR MHHFsBEaltG3FRmOOtcFnMD1WlZLUwk= Received: from mx-prod-mc-05.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-117-1PEZO6TeOLKELAfHrF-uRw-1; Fri, 06 Sep 2024 14:39:00 -0400 X-MC-Unique: 1PEZO6TeOLKELAfHrF-uRw-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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E37821956080; Fri, 6 Sep 2024 18:38:58 +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 06E591955D47; Fri, 6 Sep 2024 18:38:57 +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 486IctSg100159 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 6 Sep 2024 20:38:55 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 486IcstX100158; Fri, 6 Sep 2024 20:38:54 +0200 Date: Fri, 6 Sep 2024 20:38:54 +0200 From: Jakub Jelinek To: Jason Merrill , Franz Sirl Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++, v3: Fix get_member_function_from_ptrfunc with -fsanitize=bounds [PR116449] Message-ID: References: <62e7d11b-f731-423f-8e19-7c08c4c131a6@redhat.com> <45da584d-6cb4-4792-a29a-361f45b7d314@lauterbach.com> MIME-Version: 1.0 In-Reply-To: <45da584d-6cb4-4792-a29a-361f45b7d314@lauterbach.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 10:31:48PM +0200, Franz Sirl wrote: > Hmm, it just occured to me, how about adding !NONVIRTUAL here? When > NONVIRTUAL is true, there is no conditional stmt at all, or? Yeah, that makes sense, the problem doesn't happen in that case. Here is an adjusted patch, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-09-06 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,23 @@ 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) + || (!nonvirtual && !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) + || (!nonvirtual && !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) (); +}