re PR c/38483 (generated aborts lose previous side-effects)
authorJoseph Myers <joseph@codesourcery.com>
Thu, 19 Feb 2009 13:56:00 +0000 (13:56 +0000)
committerJoseph Myers <jsm28@gcc.gnu.org>
Thu, 19 Feb 2009 13:56:00 +0000 (13:56 +0000)
PR c/38483
* builtins.c (gimplify_va_arg_expr): Evaluate the va_list
expression before any __builtin_trap call.
* c-typeck.c (build_function_call): Convert and check function
arguments before generating a call to a trap.  Evaluate the
function arguments before the trap.

testsuite:
* gcc.c-torture/execute/call-trap-1.c,
gcc.c-torture/execute/va-arg-trap-1.c, gcc.dg/call-diag-1.c: New
tests.

From-SVN: r144296

gcc/ChangeLog
gcc/builtins.c
gcc/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.c-torture/execute/call-trap-1.c [new file with mode: 0644]
gcc/testsuite/gcc.c-torture/execute/va-arg-trap-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/call-diag-1.c [new file with mode: 0644]

index 8ca48493c4db9cae642265082eb57ca3c5e23948..da214e8deea2d079965607fbeec9f33a0fdac50e 100644 (file)
@@ -1,3 +1,12 @@
+2009-02-19  Joseph Myers  <joseph@codesourcery.com>
+
+       PR c/38483
+       * builtins.c (gimplify_va_arg_expr): Evaluate the va_list
+       expression before any __builtin_trap call.
+       * c-typeck.c (build_function_call): Convert and check function
+       arguments before generating a call to a trap.  Evaluate the
+       function arguments before the trap.
+
 2009-02-19  Uros Bizjak  <ubizjak@gmail.com>
 
        PR target/39228
index 57cce08abe53a5b02d28349ad594a361b380b8d1..55639cde7280aa455d40cc29fa3dfffa3cd2dff7 100644 (file)
@@ -4999,6 +4999,9 @@ gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
         Call abort to encourage the user to fix the program.  */
       if (warned)
        inform (input_location, "if this code is reached, the program will abort");
+      /* Before the abort, allow the evaluation of the va_list
+        expression to exit or longjmp.  */
+      gimplify_and_add (valist, pre_p);
       t = build_call_expr (implicit_built_in_decls[BUILT_IN_TRAP], 0);
       gimplify_and_add (t, pre_p);
 
index 9ce60cdc9ca44be939225eeeb91e5f3bafdf9e8f..f1d83f3c20cdd8ecf4f2556a4978b39b1e966757 100644 (file)
@@ -2417,6 +2417,16 @@ build_function_call (tree function, tree params)
   /* fntype now gets the type of function pointed to.  */
   fntype = TREE_TYPE (fntype);
 
+  /* Convert the parameters to the types declared in the
+     function prototype, or apply default promotions.  */
+
+  nargs = list_length (params);
+  argarray = (tree *) alloca (nargs * sizeof (tree));
+  nargs = convert_arguments (nargs, argarray, TYPE_ARG_TYPES (fntype), 
+                            params, function, fundecl);
+  if (nargs < 0)
+    return error_mark_node;
+
   /* Check that the function is called through a compatible prototype.
      If it is not, replace the call by a trap, wrapped up in a compound
      expression if necessary.  This has the nice side-effect to prevent
@@ -2430,6 +2440,7 @@ build_function_call (tree function, tree params)
       tree return_type = TREE_TYPE (fntype);
       tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
                                       NULL_TREE);
+      int i;
 
       /* This situation leads to run-time undefined behavior.  We can't,
         therefore, simply error unless we can prove that all possible
@@ -2438,6 +2449,10 @@ build_function_call (tree function, tree params)
        /* We can, however, treat "undefined" any way we please.
           Call abort to encourage the user to fix the program.  */
        inform (input_location, "if this code is reached, the program will abort");
+      /* Before the abort, allow the function arguments to exit or
+        call longjmp.  */
+      for (i = 0; i < nargs; i++)
+       trap = build2 (COMPOUND_EXPR, void_type_node, argarray[i], trap);
 
       if (VOID_TYPE_P (return_type))
        return trap;
@@ -2455,16 +2470,6 @@ build_function_call (tree function, tree params)
        }
     }
 
-  /* Convert the parameters to the types declared in the
-     function prototype, or apply default promotions.  */
-
-  nargs = list_length (params);
-  argarray = (tree *) alloca (nargs * sizeof (tree));
-  nargs = convert_arguments (nargs, argarray, TYPE_ARG_TYPES (fntype), 
-                            params, function, fundecl);
-  if (nargs < 0)
-    return error_mark_node;
-
   /* Check that arguments to builtin functions match the expectations.  */
   if (fundecl
       && DECL_BUILT_IN (fundecl)
index a01fa1fd018977cd48afc97f49dad3300893fc5f..2a4bad1693ecc1dc6b8e0018481acd8c908a0c74 100644 (file)
@@ -1,3 +1,10 @@
+2009-02-19  Joseph Myers  <joseph@codesourcery.com>
+
+       PR c/38483
+       * gcc.c-torture/execute/call-trap-1.c,
+       gcc.c-torture/execute/va-arg-trap-1.c, gcc.dg/call-diag-1.c: New
+       tests.
+
 2009-02-19  Uros Bizjak  <ubizjak@gmail.com>
 
        PR target/39228
diff --git a/gcc/testsuite/gcc.c-torture/execute/call-trap-1.c b/gcc/testsuite/gcc.c-torture/execute/call-trap-1.c
new file mode 100644 (file)
index 0000000..aa226e7
--- /dev/null
@@ -0,0 +1,26 @@
+/* Undefined behavior from a call to a function cast to a different
+   type does not appear until after the function designator and
+   arguments have been evaluated.  PR 38483.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+
+extern void exit (int);
+extern void abort (void);
+
+int
+foo (void)
+{
+  exit (0);
+  return 0;
+}
+
+void
+bar (void)
+{
+}
+
+int
+main (void)
+{
+  ((long (*)(int))bar) (foo ());
+  abort ();
+}
diff --git a/gcc/testsuite/gcc.c-torture/execute/va-arg-trap-1.c b/gcc/testsuite/gcc.c-torture/execute/va-arg-trap-1.c
new file mode 100644 (file)
index 0000000..1e1dae8
--- /dev/null
@@ -0,0 +1,36 @@
+/* Undefined behavior from a call to va_arg with a type other than
+   that of the argument passed (in particular, with a type such as
+   "float" that can never be the type of an argument passed through
+   "...") does not appear until after the va_list expression is
+   evaluated.  PR 38483.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+
+#include <stdarg.h>
+
+extern void exit (int);
+extern void abort (void);
+
+va_list ap;
+float f;
+
+va_list *
+foo (void)
+{
+  exit (0);
+  return &ap;
+}
+
+void
+bar (int i, ...)
+{
+  va_start (ap, i);
+  f = va_arg (*foo (), float);
+  va_end (ap);
+}
+
+int
+main (void)
+{
+  bar (1, 0);
+  abort ();
+}
diff --git a/gcc/testsuite/gcc.dg/call-diag-1.c b/gcc/testsuite/gcc.dg/call-diag-1.c
new file mode 100644 (file)
index 0000000..884baa0
--- /dev/null
@@ -0,0 +1,9 @@
+/* The warning for calling through a non-compatible type must not
+   disable the normal diagnostics from comparing the argument list
+   against the type of the called expression.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+void foo (void);
+void bar (void) { ((long (*)(int))foo) (); } /* { dg-error "too few arguments to function" } */