From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 4A6D53858410 for ; Fri, 17 Mar 2023 12:53:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4A6D53858410 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 0D48D21A50; Fri, 17 Mar 2023 12:53:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1679057629; h=from:from:reply-to: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=KH4RTAuhwccfrW/btAN/00QNYZ6uEalHIB4nMzYSHwE=; b=xRkxArISGVcPiV8hB3hlXpc8eHUQUAW04SCguxB4YUVj9XZXNRJygYnfj8vFs7+hsNYci+ 2s4e7NV0J/FfY8KeVkX2rFwZAqkhqLoTG+H0TUp6dYn28dJ83tlUcLZ/qkFXaH6wWGnDO5 RBmm5av+NQZ2NHMJu3ea7baNSg3bK/M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1679057629; h=from:from:reply-to: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=KH4RTAuhwccfrW/btAN/00QNYZ6uEalHIB4nMzYSHwE=; b=34uvlT/GgN9hRH/sputRRLIn/Kh3wF54B4RhzEz0+oqSF46T/ti3EkH/IKpKaeuegxzNxH 7ZLFCRY1SIThs9BA== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id CCD472C141; Fri, 17 Mar 2023 12:53:48 +0000 (UTC) Date: Fri, 17 Mar 2023 12:53:48 +0000 (UTC) From: Richard Biener To: Jakub Jelinek 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 In-Reply-To: Message-ID: References: <20230317121833.16A961346F@imap2.suse-dmz.suse.de> User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,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, 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 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? Richard. > So, I think LGTM, but give Andrew/Aldy a chance to chime in. > > > diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc > > index a5d625387e7..f3d6e29e58d 100644 > > --- a/gcc/gimple-range-op.cc > > +++ b/gcc/gimple-range-op.cc > > @@ -309,6 +309,25 @@ public: > > } > > } op_cfn_constant_p; > > > > +// Implement range operator for integral CFN_BUILT_IN_EXPECT. > > +class cfn_expect : public range_operator > > +{ > > +public: > > + using range_operator::fold_range; > > + virtual bool fold_range (irange &r, tree, const irange &lh, > > + const irange &, relation_trio) const > > + { > > + r = lh; > > + return true; > > + } > > + virtual bool op1_range (irange &r, tree, const irange &lhs, > > + const irange &, relation_trio) const > > + { > > + r = lhs; > > + return true; > > + } > > +} op_cfn_expect; > > + > > // Implement range operator for CFN_BUILT_IN_SIGNBIT. > > class cfn_signbit : public range_operator_float > > { > > @@ -966,6 +985,12 @@ gimple_range_op_handler::maybe_builtin_call () > > m_int = &op_cfn_parity; > > break; > > > > + case CFN_BUILT_IN_EXPECT: > > + m_valid = true; > > + m_op1 = gimple_call_arg (call, 0); > > + m_int = &op_cfn_expect; > > + break; > > + > > default: > > break; > > } > > diff --git a/gcc/testsuite/gcc.dg/Wuse-after-free-pr109170.c b/gcc/testsuite/gcc.dg/Wuse-after-free-pr109170.c > > new file mode 100644 > > index 00000000000..fa7dc66d66c > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/Wuse-after-free-pr109170.c > > @@ -0,0 +1,15 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-O2 -Wuse-after-free" } */ > > + > > +unsigned long bufmax = 0; > > +unsigned long __open_catalog_bufmax; > > +void *realloc(void *, __SIZE_TYPE__); > > +void free(void *); > > + > > +void __open_catalog(char *buf) > > +{ > > + char *old_buf = buf; > > + buf = realloc (buf, bufmax); > > + if (__builtin_expect ((buf == ((void *)0)), 0)) > > + free (old_buf); /* { dg-bogus "used after" } */ > > +} > > -- > > 2.35.3 > > Jakub > > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; HRB 36809 (AG Nuernberg)