public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C] Issue an error on scalar va_list with reverse storage order
@ 2015-12-03 14:54 Eric Botcazou
  2015-12-03 23:29 ` Joseph Myers
  2015-12-08 15:34 ` Matthew Wahab
  0 siblings, 2 replies; 4+ messages in thread
From: Eric Botcazou @ 2015-12-03 14:54 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 965 bytes --]

Hi,

further testing revealed an issue with va_arg handling and reverse scalar 
storage order on some platforms: when va_list is scalar, passing a field of a 
structure with reverse SSO as first argument to va_start/va_arg/va_end doesn't 
work because the machinery takes its address and this is not allowed for such 
a field (it's really a corner case but gcc.c-torture/execute/stdarg-2.c does 
exercise it).  Hence the attached patch which issues an error in this case.

Tested on x86_64-suse-linux, OK for the mainline?


2015-12-03  Eric Botcazou  <ebotcazou@adacore.com>

	* c-tree.h (c_build_va_arg): Adjust prototype.
	* c-parser.c (c_parser_postfix_expression): Adjust call to above.
	* c-typeck.c (c_build_va_arg): Rename LOC parameter to LOC2, add LOC1
	parameter, adjust throughout and issue an error if EXPR is a component
	with reverse storage order.


2015-12-03  Eric Botcazou  <ebotcazou@adacore.com>

	* gcc.dg/sso-9.c: New test.

-- 
Eric Botcazou

[-- Attachment #2: sso_va_arg.diff --]
[-- Type: text/x-patch, Size: 2553 bytes --]

Index: c-parser.c
===================================================================
--- c-parser.c	(revision 231206)
+++ c-parser.c	(working copy)
@@ -7485,7 +7485,7 @@ c_parser_postfix_expression (c_parser *p
 	    else
 	      {
 		tree type_expr = NULL_TREE;
-		expr.value = c_build_va_arg (loc, e1.value,
+		expr.value = c_build_va_arg (start_loc, e1.value, loc,
 					     groktypename (t1, &type_expr, NULL));
 		if (type_expr)
 		  {
Index: c-tree.h
===================================================================
--- c-tree.h	(revision 231206)
+++ c-tree.h	(working copy)
@@ -661,7 +661,7 @@ extern tree c_finish_omp_task (location_
 extern void c_finish_omp_cancel (location_t, tree);
 extern void c_finish_omp_cancellation_point (location_t, tree);
 extern tree c_finish_omp_clauses (tree, bool, bool = false);
-extern tree c_build_va_arg (location_t, tree, tree);
+extern tree c_build_va_arg (location_t, tree, location_t, tree);
 extern tree c_finish_transaction (location_t, tree, int);
 extern bool c_tree_equal (tree, tree);
 extern tree c_build_function_call_vec (location_t, vec<location_t>, tree,
Index: c-typeck.c
===================================================================
--- c-typeck.c	(revision 231206)
+++ c-typeck.c	(working copy)
@@ -13426,20 +13426,28 @@ c_build_qualified_type (tree type, int t
 /* Build a VA_ARG_EXPR for the C parser.  */
 
 tree
-c_build_va_arg (location_t loc, tree expr, tree type)
+c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
 {
   if (error_operand_p (type))
     return error_mark_node;
+  /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
+     order because it takes the address of the expression.  */
+  else if (handled_component_p (expr)
+	   && reverse_storage_order_for_component_p (expr))
+    {
+      error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
+      return error_mark_node;
+    }
   else if (!COMPLETE_TYPE_P (type))
     {
-      error_at (loc, "second argument to %<va_arg%> is of incomplete "
+      error_at (loc2, "second argument to %<va_arg%> is of incomplete "
 		"type %qT", type);
       return error_mark_node;
     }
   else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
-    warning_at (loc, OPT_Wc___compat,
+    warning_at (loc2, OPT_Wc___compat,
 		"C++ requires promoted type, not enum type, in %<va_arg%>");
-  return build_va_arg (loc, expr, type);
+  return build_va_arg (loc2, expr, type);
 }
 
 /* Return truthvalue of whether T1 is the same tree structure as T2.

[-- Attachment #3: sso-9.c --]
[-- Type: text/x-csrc, Size: 502 bytes --]

/* Test support of scalar_storage_order attribute */

/* { dg-do compile } */

#include <stdarg.h>

int x;

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
struct __attribute__((scalar_storage_order("big-endian"))) Rec
{
  va_list v;
};
#else
struct __attribute__((scalar_storage_order("little-endian"))) Rec
{
  va_list v;
};
#endif

void foo (int i, ...)
{
  struct Rec a;
  va_start (a.v, i);
  a.v = a.v, x = va_arg (a.v, int); /* { dg-error "array type|reverse storage order" } */
  va_end (a.v);
}

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

* Re: [C] Issue an error on scalar va_list with reverse storage order
  2015-12-03 14:54 [C] Issue an error on scalar va_list with reverse storage order Eric Botcazou
@ 2015-12-03 23:29 ` Joseph Myers
  2015-12-08 15:34 ` Matthew Wahab
  1 sibling, 0 replies; 4+ messages in thread
From: Joseph Myers @ 2015-12-03 23:29 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: gcc-patches

On Thu, 3 Dec 2015, Eric Botcazou wrote:

> Hi,
> 
> further testing revealed an issue with va_arg handling and reverse scalar 
> storage order on some platforms: when va_list is scalar, passing a field of a 
> structure with reverse SSO as first argument to va_start/va_arg/va_end doesn't 
> work because the machinery takes its address and this is not allowed for such 
> a field (it's really a corner case but gcc.c-torture/execute/stdarg-2.c does 
> exercise it).  Hence the attached patch which issues an error in this case.
> 
> Tested on x86_64-suse-linux, OK for the mainline?

OK.

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: [C] Issue an error on scalar va_list with reverse storage order
  2015-12-03 14:54 [C] Issue an error on scalar va_list with reverse storage order Eric Botcazou
  2015-12-03 23:29 ` Joseph Myers
@ 2015-12-08 15:34 ` Matthew Wahab
  2015-12-08 17:09   ` Eric Botcazou
  1 sibling, 1 reply; 4+ messages in thread
From: Matthew Wahab @ 2015-12-08 15:34 UTC (permalink / raw)
  To: Eric Botcazou, gcc-patches

Hello

On 03/12/15 14:53, Eric Botcazou wrote:
> further testing revealed an issue with va_arg handling and reverse scalar storage
> order on some platforms: when va_list is scalar, passing a field of a structure
> with reverse SSO as first argument to va_start/va_arg/va_end doesn't work because
> the machinery takes its address and this is not allowed for such a field (it's
> really a corner case but gcc.c-torture/execute/stdarg-2.c does exercise it). Hence
> the attached patch which issues an error in this case.

The new gcc.dg/sso-9.c test is failing for aarch64 and arm targets. There's no error
generated if I compile the test from the command line for aarch64-none-elf. GCC for
x86_64 does generate the error.

Matthew

> 2015-12-03  Eric Botcazou  <ebotcazou@adacore.com>
>
> * c-tree.h (c_build_va_arg): Adjust prototype. * c-parser.c
> (c_parser_postfix_expression): Adjust call to above. * c-typeck.c
> (c_build_va_arg): Rename LOC parameter to LOC2, add LOC1 parameter, adjust
> throughout and issue an error if EXPR is a component with reverse storage order.
>
>
> 2015-12-03  Eric Botcazou  <ebotcazou@adacore.com>
>
> * gcc.dg/sso-9.c: New test.


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

* Re: [C] Issue an error on scalar va_list with reverse storage order
  2015-12-08 15:34 ` Matthew Wahab
@ 2015-12-08 17:09   ` Eric Botcazou
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Botcazou @ 2015-12-08 17:09 UTC (permalink / raw)
  To: Matthew Wahab; +Cc: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 330 bytes --]

> The new gcc.dg/sso-9.c test is failing for aarch64 and arm targets. There's
> no error generated if I compile the test from the command line for
> aarch64-none-elf. GCC for x86_64 does generate the error.

Fixed like so, applied as obvious, thanks for the heads up.


	* gcc.dg/sso-9.c (foo): Robustify trick.

-- 
Eric Botcazou

[-- Attachment #2: p.diff --]
[-- Type: text/x-patch, Size: 437 bytes --]

Index: gcc.dg/sso-9.c
===================================================================
--- gcc.dg/sso-9.c	(revision 231394)
+++ gcc.dg/sso-9.c	(working copy)
@@ -22,6 +22,6 @@ void foo (int i, ...)
 {
   struct Rec a;
   va_start (a.v, i);
-  a.v = a.v, x = va_arg (a.v, int); /* { dg-error "array type|reverse storage order" } */
+  a.v = 0, x = va_arg (a.v, int); /* { dg-error "type|reverse storage order" } */
   va_end (a.v);
 }

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

end of thread, other threads:[~2015-12-08 17:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-03 14:54 [C] Issue an error on scalar va_list with reverse storage order Eric Botcazou
2015-12-03 23:29 ` Joseph Myers
2015-12-08 15:34 ` Matthew Wahab
2015-12-08 17:09   ` Eric Botcazou

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