This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Fix unsafe function attributes for special functions (PR 71876)


On 07/20/16 20:08, Richard Biener wrote:
> On July 20, 2016 6:54:48 PM GMT+02:00, Bernd Edlinger <bernd.edlinger@hotmail.de> wrote:
>>
>> But I think that alloca just should not be recognized by name any
>> more.
>
> It was introduced to mark calls that should not be duplicated by inlining or unrolling to avoid increasing stack usage too much.  Sth worthwhile to keep even with -ffreestanding.
>
> Richard.
>

On second thought I start to think that an external alloca function
might still work.  And returning ECF_MAY_BE_ALLOCA just based on the
name could be made safe by checking the malloc attribute at the right
places.

With this new incremental patch the example

extern "C"
void *alloca(unsigned long);
void bar(unsigned long n)
{
   char *x = (char*) alloca(n);
   if (x)
     *x = 0;
}

might actually work when -ansi is used,
i.e. it does no longer assume that alloca cannot return null,
but still creates a frame pointer, which it would not have done
for allocb for instance.

But the built-in alloca is still recognized because the builtin
does have ECF_MAY_BE_ALLOCA and ECF_MALLOC.


Is it OK for trunk after boot-strap and reg-testing?


Thanks
Bernd.
2016-07-19  Bernd Edlinger  <bernd.edlinger@hotmail.de>

	PR middle-end/71876
	* fold-const.c (tree_expr_nonzero_warnv_p): Check for real built-in
	alloca.
	* tree-vrp.c (gimple_stmt_nonzero_warnv_p): Likewise.

Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	(revision 238513)
+++ gcc/fold-const.c	(working copy)
@@ -9018,7 +9018,7 @@ tree_expr_nonzero_warnv_p (tree t, bool *strict_ov
 	    && lookup_attribute ("returns_nonnull",
 		 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
 	  return true;
-	return alloca_call_p (t);
+	return alloca_call_p (t) && DECL_IS_MALLOC (fndecl);
       }
 
     default:
Index: gcc/tree-vrp.c
===================================================================
--- gcc/tree-vrp.c	(revision 238513)
+++ gcc/tree-vrp.c	(working copy)
@@ -1065,7 +1065,7 @@ gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *s
 	    lookup_attribute ("returns_nonnull",
 			      TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
 	  return true;
-	return gimple_alloca_call_p (stmt);
+	return gimple_alloca_call_p (stmt) && DECL_IS_MALLOC (fndecl);
       }
     default:
       gcc_unreachable ();

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]