public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Mikael Pettersson <mikpe@it.uu.se>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] make attribute((returns_twice)) actually work (PR tree-optimization/49243)
Date: Thu, 02 Jun 2011 12:02:00 -0000	[thread overview]
Message-ID: <19943.31710.10474.411273@pilspetsen.it.uu.se> (raw)

GCC has attribute((returns_twice)) which is supposed to allow the safe
use of alternate implementations of setjmp-like functions.  In particular,
a function that calls a setjmp-like function must itself not be inlined,
because that would enable unsafe optimizations.  This works for calls to
"setjmp" (a few alternate spellings are allowed), but not to e.g. "my_setjmp"
even if that function is declared with attribute((returns_twice)).  This
bug affects the entire gcc-4.x series, gcc-3.x worked; see PR49243.

A function that calls "setjmp" is marked non-inlinable because setjmp_call_p
is applied to the function position, and it deduces via special_function_p
that the callee is ECF_RETURNS_TWICE.  But special_function_p only looks at
the name, so setjmp_call_p fails to detect attribute((returns_twice)) callees.

The fix is to have setjmp_call_p also check if the returns_twice attribute
is present, via DECL_IS_RETURNS_TWICE.  It could call flags_from_decl_or_type
instead, but that would perform quite a bit of redundant work for this case.

The test case uses -Winline to check that gcc refuses to inline a function
that calls a returns_twice callee.  This is sufficient to verify the fix, and
avoids the machine-specific code needed in the original runtime test case.

Tested w/o regressions with gcc trunk and 4.6 on x86_64-linux.  The added test
case does fail without the fix and pass with it.

OK for trunk, and perhaps 4.6?

(I don't have svn write access.)

/Mikael

gcc/

2011-06-02  Mikael Pettersson  <mikpe@it.uu.se>

	PR tree-optimization/49243
	* calls.c (setjmp_call_p): Also check if fndecl has the
	returns_twice attribute.

gcc/testsuite/

2011-06-02  Mikael Pettersson  <mikpe@it.uu.se>

	PR tree-optimization/49243
	* gcc.dg/pr49243.c: New.

--- gcc-4.7-20110528/gcc/calls.c.~1~	2011-05-25 13:00:14.000000000 +0200
+++ gcc-4.7-20110528/gcc/calls.c	2011-06-02 12:55:32.000000000 +0200
@@ -554,6 +554,8 @@ special_function_p (const_tree fndecl, i
 int
 setjmp_call_p (const_tree fndecl)
 {
+  if (DECL_IS_RETURNS_TWICE (fndecl))
+    return ECF_RETURNS_TWICE;
   return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
 }
 
--- gcc-4.7-20110528/gcc/testsuite/gcc.dg/pr49243.c.~1~	1970-01-01 01:00:00.000000000 +0100
+++ gcc-4.7-20110528/gcc/testsuite/gcc.dg/pr49243.c	2011-06-02 12:55:32.000000000 +0200
@@ -0,0 +1,25 @@
+/* PR tree-optimization/49243 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Winline" } */
+
+extern unsigned long jb[];
+extern int my_setjmp(unsigned long jb[]) __attribute__((returns_twice));
+extern int decode(const char*);
+
+static inline int wrapper(const char **s_ptr) /* { dg-warning "(inlining failed|function 'wrapper' can never be inlined because it uses setjmp)" } */
+{
+    if (my_setjmp(jb) == 0) {
+	const char *s = *s_ptr;
+	while (decode(s) != 0)
+	    *s_ptr = ++s;
+	return 0;
+    } else
+	return -1;
+}
+
+void parse(const char *data)
+{
+    const char *s = data;
+    if (!(wrapper(&s) == -1 && (s - data) == 1)) /* { dg-warning "called from here" } */
+	__builtin_abort();
+}

             reply	other threads:[~2011-06-02 12:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-02 12:02 Mikael Pettersson [this message]
2011-06-06 11:15 ` Richard Guenther

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=19943.31710.10474.411273@pilspetsen.it.uu.se \
    --to=mikpe@it.uu.se \
    --cc=gcc-patches@gcc.gnu.org \
    /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).