public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix ICE with bogus posix_memalign call (PR middle-end/67222)
@ 2015-08-17 18:38 Marek Polacek
  2015-08-18  8:50 ` Richard Biener
  0 siblings, 1 reply; 6+ messages in thread
From: Marek Polacek @ 2015-08-17 18:38 UTC (permalink / raw)
  To: GCC Patches

Here we were crashing on an invalid call to posix_memalign.  The code in
lower_builtin_posix_memalign assumed that the call had valid arguments.
The reason the C FE doesn't reject this code is, in short, that
int <T> () is compatible with int <T> (void **, size_t, size_t) and we
use the former -- so convert_arguments doesn't complain.

So I think let's validate the arguments in lower_stmt.  I decided to
give an error if we see an invalid usage of posix_memalign, since
other code (e.g. alias machinery) assumes correct arguments as well.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-08-17  Marek Polacek  <polacek@redhat.com>

	PR middle-end/67222
	* gimple-low.c: Include "builtins.h".
	(lower_stmt): Validate arguments of posix_memalign.

	* gcc.dg/torture/pr67222.c: New test.

diff --git gcc/gimple-low.c gcc/gimple-low.c
index d4697e2..03194f0 100644
--- gcc/gimple-low.c
+++ gcc/gimple-low.c
@@ -39,6 +39,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "langhooks.h"
 #include "gimple-low.h"
 #include "tree-nested.h"
+#include "builtins.h"
 
 /* The differences between High GIMPLE and Low GIMPLE are the
    following:
@@ -345,10 +346,22 @@ lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
 		data->cannot_fallthru = false;
 		return;
 	      }
-	    else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_POSIX_MEMALIGN
-		     && flag_tree_bit_ccp)
+	    else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_POSIX_MEMALIGN)
 	      {
-		lower_builtin_posix_memalign (gsi);
+		if (gimple_call_num_args (stmt) != 3
+		    || !validate_gimple_arglist (dyn_cast <gcall *> (stmt),
+						 POINTER_TYPE, INTEGER_TYPE,
+						 INTEGER_TYPE, VOID_TYPE))
+		  {
+		    error_at (gimple_location (stmt), "invalid arguments "
+			      "to %qD", decl);
+		    gsi_next (gsi);
+		    return;
+		  }
+		if (flag_tree_bit_ccp)
+		  lower_builtin_posix_memalign (gsi);
+		else
+		  gsi_next (gsi);
 		return;
 	      }
 	  }
diff --git gcc/testsuite/gcc.dg/torture/pr67222.c gcc/testsuite/gcc.dg/torture/pr67222.c
index e69de29..cf39aa1 100644
--- gcc/testsuite/gcc.dg/torture/pr67222.c
+++ gcc/testsuite/gcc.dg/torture/pr67222.c
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-Wno-implicit-function-declaration" } */
+
+void
+foo (void **p)
+{
+  posix_memalign (); /* { dg-error "invalid arguments" } */
+  posix_memalign (p); /* { dg-error "invalid arguments" } */
+  posix_memalign (0); /* { dg-error "invalid arguments" } */
+  posix_memalign (p, 1); /* { dg-error "invalid arguments" } */
+  posix_memalign (p, "foo"); /* { dg-error "invalid arguments" } */
+  posix_memalign ("gnu", "gcc"); /* { dg-error "invalid arguments" } */
+  posix_memalign (1, p); /* { dg-error "invalid arguments" } */
+  posix_memalign (1, 2); /* { dg-error "invalid arguments" } */
+  posix_memalign (1, 2, 3); /* { dg-error "invalid arguments" } */
+  posix_memalign (p, p, p); /* { dg-error "invalid arguments" } */
+  posix_memalign (p, "qui", 3); /* { dg-error "invalid arguments" } */
+  posix_memalign (p, 1, 2);
+}

	Marek

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-08-18 13:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-17 18:38 [PATCH] Fix ICE with bogus posix_memalign call (PR middle-end/67222) Marek Polacek
2015-08-18  8:50 ` Richard Biener
2015-08-18 10:43   ` Marek Polacek
2015-08-18 11:08     ` Richard Biener
2015-08-18 12:03       ` Marek Polacek
2015-08-18 13:13         ` Richard Biener

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).