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]

[PATCH] Fix PR42944


We're a bit overeager in assuming that malloc/calloc do not clobber
memory.  Because glibc appearantly chooses to set errno.

Fixed as follows, bootstrapped and tested on x86_64-unknown-linux-gnu.

The testcase will show whether there are any systems that have
a plain errno declaration.

Committed to trunk.

Richard.

2010-02-03  Richard Guenther  <rguenther@suse.de>

        PR tree-optimization/42944
        * tree-ssa-alias.c (ref_maybe_used_by_call_p_1): Handle
        calloc.
        (call_may_clobber_ref_p_1): Likewise.  Properly handle
        malloc and calloc clobbering errno.

        * gcc.dg/errno-1.c: New testcase.



Index: gcc/tree-ssa-alias.c
===================================================================
*** gcc/tree-ssa-alias.c	(revision 156463)
--- gcc/tree-ssa-alias.c	(working copy)
*************** ref_maybe_used_by_call_p_1 (gimple call,
*** 963,968 ****
--- 963,969 ----
  	/* The following builtins do not read from memory.  */
  	case BUILT_IN_FREE:
  	case BUILT_IN_MALLOC:
+ 	case BUILT_IN_CALLOC:
  	case BUILT_IN_MEMSET:
  	case BUILT_IN_FREXP:
  	case BUILT_IN_FREXPF:
*************** call_may_clobber_ref_p_1 (gimple call, a
*** 1190,1195 ****
--- 1191,1211 ----
  	/* Allocating memory does not have any side-effects apart from
  	   being the definition point for the pointer.  */
  	case BUILT_IN_MALLOC:
+ 	case BUILT_IN_CALLOC:
+ 	  /* Unix98 specifies that errno is set on allocation failure.
+ 	     Until we properly can track the errno location assume it
+ 	     is not a plain decl but anonymous storage in a different
+ 	     translation unit.  */
+ 	  if (flag_errno_math)
+ 	    {
+ 	      struct ptr_info_def *pi;
+ 	      if (DECL_P (base))
+ 		return false;
+ 	      if (INDIRECT_REF_P (base)
+ 		  && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
+ 		  && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0))))
+ 		return pi->pt.anything || pi->pt.nonlocal;
+ 	    }
  	  return false;
  	/* Freeing memory kills the pointed-to memory.  More importantly
  	   the call has to serve as a barrier for moving loads and stores
Index: gcc/testsuite/gcc.dg/errno-1.c
===================================================================
*** gcc/testsuite/gcc.dg/errno-1.c	(revision 0)
--- gcc/testsuite/gcc.dg/errno-1.c	(revision 0)
***************
*** 0 ****
--- 1,17 ----
+ /* { dg-do compile } */
+ /* { dg-options "-O2" } */
+ 
+ #include <errno.h>
+ #include <stdlib.h>
+ 
+ int main()
+ {
+   void *p;
+   errno = 0;
+   p = malloc (-1);
+   if (errno != 0)
+     do_not_optimize_away ();
+   return 0;
+ }
+ 
+ /* { dg-final { scan-assembler "do_not_optimize_away" } } */


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