public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Xinliang David Li <davidxl@google.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Cc: Jason Merrill <jason@redhat.com>,
	Richard Guenther <richard.guenther@gmail.com>
Subject: New option to turn off stack reuse for temporaries
Date: Wed, 20 Jun 2012 23:44:00 -0000	[thread overview]
Message-ID: <CAAkRFZK_27tveOTLioz-YwfF_4w4qtTJkRFjno7r8risRVQsnQ@mail.gmail.com> (raw)

One of the most common runtime errors we have seen in gcc-4_7 is
caused by dangling references to temporaries whole life time have
ended

e.g,

 const A& a = foo();

or
foo (A());// where temp's address is saved and used after foo.

Of course this is user error according to the standard, triaging of
bugs like this is pretty time consuming to triage. This patch tries to
introduce an option to disable stack reuse for temporaries, which can
be used to debugging purpose.

Is this good for trunk?

thanks,

David

2012-06-20  Xinliang David Li  <davidxl@google.com>

        * common.opt: -ftemp-reuse-stack option.
        * gimplify.c (gimplify_target_expr): Check new flag.



Index: doc/invoke.texi
===================================================================
--- doc/invoke.texi     (revision 188362)
+++ doc/invoke.texi     (working copy)
@@ -1003,6 +1003,7 @@ See S/390 and zSeries Options.
 -fstack-limit-register=@var{reg}  -fstack-limit-symbol=@var{sym} @gol
 -fno-stack-limit -fsplit-stack @gol
 -fleading-underscore  -ftls-model=@var{model} @gol
+-ftemp-stack-reuse @gol
 -ftrapv  -fwrapv  -fbounds-check @gol
 -fvisibility -fstrict-volatile-bitfields}
 @end table
@@ -19500,6 +19501,10 @@ indices used to access arrays are within
 currently only supported by the Java and Fortran front ends, where
 this option defaults to true and false respectively.

+@item -ftemp-stack-reuse
+@opindex ftemp_stack_reuse
+This option enables stack space reuse for temporaries. The default is on.
+
 @item -ftrapv
 @opindex ftrapv
 This option generates traps for signed overflow on addition, subtraction,
Index: gimplify.c
===================================================================
--- gimplify.c  (revision 188362)
+++ gimplify.c  (working copy)
@@ -5487,7 +5487,8 @@ gimplify_target_expr (tree *expr_p, gimp
       /* Add a clobber for the temporary going out of scope, like
         gimplify_bind_expr.  */
       if (gimplify_ctxp->in_cleanup_point_expr
-         && needs_to_live_in_memory (temp))
+         && needs_to_live_in_memory (temp)
+         && flag_temp_stack_reuse)
        {
          tree clobber = build_constructor (TREE_TYPE (temp), NULL);
          TREE_THIS_VOLATILE (clobber) = true;
Index: common.opt
===================================================================
--- common.opt  (revision 188362)
+++ common.opt  (working copy)
@@ -1322,6 +1322,10 @@ fif-conversion2
 Common Report Var(flag_if_conversion2) Optimization
 Perform conversion of conditional jumps to conditional execution

+ftemp-stack-reuse
+Common Report Var(flag_temp_stack_reuse) Init(1)
+Enable stack reuse for compiler generated temps
+
 ftree-loop-if-convert
 Common Report Var(flag_tree_loop_if_convert) Init(-1) Optimization
 Convert conditional jumps in innermost loops to branchless equivalents

             reply	other threads:[~2012-06-20 22:51 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-20 23:44 Xinliang David Li [this message]
2012-06-21  5:28 ` Jason Merrill
2012-06-21  6:06   ` Xinliang David Li
2012-06-21  6:27     ` Jason Merrill
2012-06-21  9:32     ` Richard Guenther
2012-06-21 16:41       ` Michael Matz
2012-06-22  8:46         ` Richard Guenther
2012-06-21 18:19       ` Jason Merrill
2012-06-21 18:44       ` Xinliang David Li
2012-06-22  8:50         ` Richard Guenther
2012-06-22  9:39           ` Jason Merrill
2012-06-22  9:51             ` Richard Guenther
2012-06-22 16:09               ` Xinliang David Li
2012-06-25 16:29                 ` Xinliang David Li
2012-06-26  8:42                   ` Richard Guenther
2012-06-26 15:29                     ` Jason Merrill
2012-06-26 17:12                       ` Michael Matz
2012-06-26 17:19                         ` Jakub Jelinek
2012-06-26 20:12                         ` Mike Stump
2012-06-27  3:03                           ` Eric Botcazou
2012-06-29  8:18                     ` Xinliang David Li
2012-07-02 23:30                       ` Xinliang David Li
2012-07-04 15:01                         ` Xinliang David Li
2012-07-09 16:31                           ` Xinliang David Li
2012-07-09 22:53                         ` Jason Merrill
2012-12-02 12:32 ` Olivier Ballereau
2012-12-03  1:03   ` Xinliang David Li
2012-06-22 21:09 Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAAkRFZK_27tveOTLioz-YwfF_4w4qtTJkRFjno7r8risRVQsnQ@mail.gmail.com \
    --to=davidxl@google.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=richard.guenther@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).