diff mbox series

x86: Check cfun != NULL before accessing silent_p

Message ID 20210320122131.564376-1-hjl.tools@gmail.com
State New
Headers show
Series x86: Check cfun != NULL before accessing silent_p | expand

Commit Message

H.J. Lu March 20, 2021, 12:21 p.m. UTC
Since construct_container may be called with cfun == NULL, check
cfun != NULL before accessing silent_p.

gcc/

	PR target/99679
	* config/i386/i386.c (construct_container): Check cfun != NULL
	before accessing silent_p.

gcc/testsuite/

	PR target/99679
	* g++.target/i386/pr99679.C: New test.
---
 gcc/config/i386/i386.c                  |   4 +-
 gcc/testsuite/g++.target/i386/pr99679.C | 144 ++++++++++++++++++++++++
 2 files changed, 146 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.target/i386/pr99679.C

Comments

Martin Liška March 20, 2021, 1:46 p.m. UTC | #1
On 3/20/21 1:21 PM, H.J. Lu wrote:
> |Since construct_container may be called with cfun == NULL, check cfun != NULL before accessing silent_p. |

Thank you for the quick fix.

Please use the minimal reproducer for a test-case:

$ cat va-arg-pack-1.C
#include <stdarg.h>
void abort() {
   double ld;
   va_list ap;
   ld = va_arg(ap, long double);
   if (ld)
     abort();
}

Martin
diff mbox series

Patch

diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
index 714349094bd..7c41302c75b 100644
--- a/gcc/config/i386/i386.c
+++ b/gcc/config/i386/i386.c
@@ -2540,7 +2540,7 @@  construct_container (machine_mode mode, machine_mode orig_mode,
     {
       /* Return early if we shouldn't raise an error for invalid
 	 calls.  */
-      if (cfun->machine->silent_p)
+      if (cfun != NULL && cfun->machine->silent_p)
 	return NULL;
       if (in_return)
 	{
@@ -2568,7 +2568,7 @@  construct_container (machine_mode mode, machine_mode orig_mode,
 	{
 	  /* Return early if we shouldn't raise an error for invalid
 	     calls.  */
-	  if (cfun->machine->silent_p)
+	  if (cfun != NULL && cfun->machine->silent_p)
 	    return NULL;
 	  if (!issued_x87_ret_error)
 	    {
diff --git a/gcc/testsuite/g++.target/i386/pr99679.C b/gcc/testsuite/g++.target/i386/pr99679.C
new file mode 100644
index 00000000000..3570d34cf51
--- /dev/null
+++ b/gcc/testsuite/g++.target/i386/pr99679.C
@@ -0,0 +1,144 @@ 
+// { dg-do compile }
+// { dg-options "-Ofast -fipa-pta -mno-80387" }
+
+#include <stdarg.h>
+
+extern "C" void abort (void);
+
+int v1 = 8;
+long int v2 = 3;
+void *v3 = (void *) &v2;
+struct A { char c[16]; } v4 = { "foo" };
+long double v5 = 40;
+char seen[20];
+int cnt;
+
+__attribute__ ((noinline)) int
+foo1 (int x, int y, ...)
+{
+  int i;
+  long int l;
+  void *v;
+  struct A a;
+  long double ld;
+  va_list ap;
+
+  va_start (ap, y);
+  if (x < 0 || x >= 20 || seen[x])
+    abort ();
+  seen[x] = ++cnt;
+  if (y != 6)
+    abort ();
+  i = va_arg (ap, int);
+  if (i != 5)
+    abort ();
+  switch (x)
+    {
+    case 0:
+      i = va_arg (ap, int);
+      if (i != 9 || v1 != 9)
+	abort ();
+      a = va_arg (ap, struct A);
+      if (__builtin_memcmp (a.c, v4.c, sizeof (a.c)) != 0)
+	abort ();
+      v = (void *) va_arg (ap, struct A *);
+      if (v != (void *) &v4)
+	abort ();
+      l = va_arg (ap, long int);
+      if (l != 3 || v2 != 4)
+	abort ();
+      break;
+    case 1:
+      ld = va_arg (ap, long double);
+      if (ld != 41 || v5 != ld)
+	abort ();
+      i = va_arg (ap, int);
+      if (i != 8)
+	abort ();
+      v = va_arg (ap, void *);
+      if (v != &v2)
+	abort ();
+      break;
+    case 2:
+      break;
+    default:
+      abort ();
+    }
+  va_end (ap);
+  return x;
+}
+
+__attribute__ ((noinline)) int
+foo2 (int x, int y, ...)
+{
+  long long int ll;
+  void *v;
+  struct A a, b;
+  long double ld;
+  va_list ap;
+
+  va_start (ap, y);
+  if (x < 0 || x >= 20 || seen[x])
+    abort ();
+  seen[x] = ++cnt | 64;
+  if (y != 10)
+    abort ();
+  switch (x)
+    {
+    case 11:
+      break;
+    case 12:
+      ld = va_arg (ap, long double);
+      if (ld != 41 || v5 != 40)
+	abort ();
+      a = va_arg (ap, struct A);
+      if (__builtin_memcmp (a.c, v4.c, sizeof (a.c)) != 0)
+	abort ();
+      b = va_arg (ap, struct A);
+      if (__builtin_memcmp (b.c, v4.c, sizeof (b.c)) != 0)
+	abort ();
+      v = va_arg (ap, void *);
+      if (v != &v2)
+	abort ();
+      ll = va_arg (ap, long long int);
+      if (ll != 16LL)
+	abort ();
+      break;
+    case 2:
+      break;
+    default:
+      abort ();
+    }
+  va_end (ap);
+  return x + 8;
+}
+
+__attribute__ ((noinline)) int
+foo3 (void)
+{
+  return 6;
+}
+
+extern inline __attribute__ ((always_inline, gnu_inline)) int
+bar (int x, ...)
+{
+  if (x < 10)
+    return foo1 (x, foo3 (), 5, __builtin_va_arg_pack ());
+  return foo2 (x, foo3 () + 4, __builtin_va_arg_pack ());
+}
+
+int
+main (void)
+{
+  if (bar (0, ++v1, v4, &v4, v2++) != 0)
+    abort ();
+  if (bar (1, ++v5, 8, v3) != 1)
+    abort ();
+  if (bar (2) != 2)
+    abort ();
+  if (bar (v1 + 2) != 19)
+    abort ();
+  if (bar (v1 + 3, v5--, v4, v4, v3, 16LL) != 20)
+    abort ();
+  return 0;
+} // { dg-error "x87 register return with x87 disabled" "" { target { ! ia32 } } }