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]

[PATCH] Fix aliasing with builtin memcpy/memset


On Sat, Jul 01, 2000 at 01:36:49PM -0700, Richard Henderson wrote:
> On Sat, Jul 01, 2000 at 07:57:45PM +0200, Jakub Jelinek wrote:
> > Seems to be aliasing related, I've tried to create a testcase below, but I
> > wonder what is guaranteed about memset/memcpy and alias sets. Should the
> > testcase never call abort()?
> 
> Yes.  Both memset and memcpy should *always* use alias set 0.
> Clearly that's not happening...
> 

Here is a patch and testcase (which used to fail on alpha-redhat-linux).
Ok to commit?

2000-07-03  Jakub Jelinek  <jakub@redhat.com>

	* builtins.c (get_memory_rtx): Always put into alias set 0.

	* gcc.c-torture/execute/20000703-1.c: New test.

--- gcc/testsuite/gcc.c-torture/execute/20000703-1.c.jj	Mon Jul  3 10:39:37 2000
+++ gcc/testsuite/gcc.c-torture/execute/20000703-1.c	Mon Jul  3 10:42:08 2000
@@ -0,0 +1,39 @@
+void abort(void);
+void exit(int);
+struct baz 
+{
+  char a[17];
+  char b[3];
+  unsigned int c;
+  unsigned int d;
+};
+
+void foo(struct baz *p, unsigned int c, unsigned int d)
+{
+  __builtin_memcpy (p->b, "abc", 3);
+  p->c = c;
+  p->d = d;
+}
+
+void bar(struct baz *p, unsigned int c, unsigned int d)
+{
+  ({ void *s = (p);
+     __builtin_memset (s, '\0', sizeof (struct baz));
+     s; });
+  __builtin_memcpy (p->a, "01234567890123456", 17);
+  __builtin_memcpy (p->b, "abc", 3);
+  p->c = c;
+  p->d = d;
+}
+
+int main()
+{
+  struct baz p;
+  foo(&p, 71, 18);
+  if (p.c != 71 || p.d != 18)
+    abort();
+  bar(&p, 59, 26);
+  if (p.c != 59 || p.d != 26)
+    abort();
+  exit(0);
+}
--- gcc/builtins.c.jj	Thu Jun  1 11:56:18 2000
+++ gcc/builtins.c	Mon Jul  3 10:34:46 2000
@@ -558,6 +558,9 @@ get_memory_rtx (exp)
     return mem;
 
   set_mem_attributes (mem, exp, 0);
+
+  /* memcpy, memset and other builtin stringops can alias with anything. */
+  MEM_ALIAS_SET (mem) = 0;
   return mem;
 }
 


	Jakub

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