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 AD9D13858410 for ; Fri, 17 Mar 2023 12:59:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AD9D13858410 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679057948; 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=s9iwVxBOS+VdPHhVGS0AL0np6L4MBSrGYL7kpB2qRuw=; b=NAsrvLXqIOqZQOgrWLU+7xLwEFYlYZ9ehpmMW0EjrkqQ1vPzbzjvr9PahAyRJSSdTgVdsT Kyudts7FXbiTYUg0pJ7q3CWgqmkZzQFKVfdxCcczcNH6cgHlnZGR2+8d4TMlryz2m50dul /mMGerN3df+ANqlDbrFDtsOK9TppM/Q= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-515-BBAYgjJSOWGOz9DfcHYlmQ-1; Fri, 17 Mar 2023 08:59:07 -0400 X-MC-Unique: BBAYgjJSOWGOz9DfcHYlmQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E6F873C025D2; Fri, 17 Mar 2023 12:59:06 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A4ABB6B590; Fri, 17 Mar 2023 12:59:06 +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 32HCx43p922705 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 17 Mar 2023 13:59:04 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 32HCx31U922704; Fri, 17 Mar 2023 13:59:03 +0100 Date: Fri, 17 Mar 2023 13:59:02 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org, aldyh@redhat.com, amacleod@redhat.com Subject: Re: [PATCH] tree-optimization/109170 - bogus use-after-free with __builtin_expect Message-ID: Reply-To: Jakub Jelinek References: <20230317121833.16A961346F@imap2.suse-dmz.suse.de> MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.7 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_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, Mar 17, 2023 at 12:53:48PM +0000, Richard Biener wrote: > On Fri, 17 Mar 2023, Jakub Jelinek wrote: > > > On Fri, Mar 17, 2023 at 01:18:32PM +0100, Richard Biener wrote: > > > The following adds a missing range-op for __builtin_expect which > > > helps -Wuse-after-free to detect the case a realloc original > > > pointer is used when the result was NULL. > > > > > > Bootstrap and regtest running on x86_64-unknown-linux-gnu, OK? > > > > > > PR tree-optimization/109170 > > > * gimple-range-op.cc (cfn_expect): New. > > > (gimple_range_op_handler::maybe_builtin_call): Handle > > > __builtin_expect. > > > > > > * gcc.dg/Wuse-after-free-pr109170.c: New testcase. > > > > Shouldn't that be something we handle generically for all > > ERF_RETURNS_ARG calls (and not just for irange, but for any > > supported ranges)? > > > > Though, admittedly __builtin_expect probably doesn't set that > > and all the other current builtins with ERF_RETURNS_ARG return > > pointers I think. > > Looking at builtin_fnspec we're indeed missing BUILT_IN_EXPECT, > but we could indeed use gimple_call_fnspec and look for a > returned argument. If it's not the first handling this > generically is going to be interesting wrt op?_range though, > so we'd need a range operator for each case (returns arg 1, > returns arg 2, more args are not supported?). Currently I think fnspec supports 1-4, but nothing actually uses anything but 1 or none; I could be wrong. Anyway, I think it is fine to implement __builtin_expect this way for now, ERF_RETURNS_ARG will be more important for pointers, especially if we propagate something more than just maybe be/can't be/must be null. Don't you need to handle BUILT_IN_EXPECT_WITH_PROBABILITY the same though? > all returns-arg builtins return the first arg, but eventually > modref or the fortran frontend will end up with calls returning > sth else. > > If a float arg is returned it also needs to be a frange operator, > right? Jakub