1 /* This program is free software; you can redistribute it and/or modify
2    it under the terms of the GNU General Public License as published by
3    the Free Software Foundation; either version 2 of the License, or
4    (at your option) any later version.
5 
6    This program is distributed in the hope that it will be useful, but
7    WITHOUT ANY WARRANTY; without even the implied warranty of
8    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9    General Public License for more details.
10 
11    You should have received a copy of the GNU General Public License
12    along with this program; if not, write to the Free Software
13    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
14    02110-1301, USA.  */
15 
16 /* Verify memcpy operation.  */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/mman.h>
22 #include <libitm.h>
23 
24 #define BEG_TRANSACTION \
25   _ITM_beginTransaction (pr_instrumentedCode | pr_hasNoAbort \
26 			 | pr_hasNoIrrevocable)
27 #define END_TRANSACTION \
28   _ITM_commitTransaction ()
29 
30 #define MEMCPY	_ITM_memcpyRtWt
31 
32 static unsigned char *buf1, *buf2;
33 static size_t bufsize, page_size;
34 static int fail;
35 int getpagesize (void);
36 
37 static void
do_test(size_t align1,size_t align2,size_t len)38 do_test (size_t align1, size_t align2, size_t len)
39 {
40   size_t i, j;
41   unsigned char *s1, *s2;
42   unsigned char c1, c2;
43 
44   if (align1 + len >= bufsize)
45     return;
46   if (align2 + len >= bufsize)
47     return;
48 
49   c1 = random () >> 8;
50   c2 = random () >> 8;
51   memset (buf1, c1, bufsize);
52   memset (buf2, c2, bufsize);
53 
54   s1 = buf1 + align1;
55   s2 = buf2 + align2;
56 
57   for (i = 0, j = 1; i < len; i++, j += 23)
58     s1[i] = (j == c1 ? j + 1 : j);
59 
60   BEG_TRANSACTION;
61   MEMCPY (s2, s1, len);
62   END_TRANSACTION;
63 
64   if (memcmp (s1, s2, len) != 0)
65     {
66       printf ("Wrong result: dalign %zd salign %zd len %zd\n",
67 	      align2, align1, len);
68       fail = 1;
69       return;
70     }
71 
72   for (i = (align2 > 64 ? align2 - 64 : 0); i < align2; ++i)
73     if (buf2[i] != c2)
74       {
75 	printf ("Garbage before: ofs %zd\n", i);
76 	fail = 1;
77 	break;
78       }
79   for (i = align2 + len, j = i+64 < bufsize ? i+64 : bufsize; i < j; ++i)
80     if (buf2[i] != c2)
81       {
82 	printf ("Garbage after: ofs %zd\n", i);
83 	fail = 1;
84 	break;
85       }
86 }
87 
88 #ifndef MAP_ANONYMOUS
89 #  ifdef MAP_ANON
90 #    define MAP_ANONYMOUS MAP_ANON
91 #  endif
92 #endif
93 
main()94 int main()
95 {
96   size_t i, j;
97 
98   page_size = getpagesize ();
99   bufsize = 2 * page_size;
100 
101   buf1 = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE,
102 	       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
103   if (buf1 == MAP_FAILED)
104     return 1;
105   buf2 = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE,
106 	       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
107   if (buf2 == MAP_FAILED)
108     return 1;
109 
110   if (mprotect (buf1, page_size, PROT_NONE))
111     return 1;
112   buf1 += page_size;
113   if (mprotect (buf1 + bufsize, page_size, PROT_NONE))
114     return 1;
115 
116   if (mprotect (buf2, page_size, PROT_NONE))
117     return 1;
118   buf2 += page_size;
119   if (mprotect (buf2 + bufsize, page_size, PROT_NONE))
120     return 1;
121 
122   for (i = 0; i < 18; ++i)
123     {
124       size_t len = 1 << i;
125 
126       do_test (0, 0, len);
127       do_test (i, 0, len);
128       do_test (0, i, len);
129       do_test (i, i, len);
130 
131       do_test (0, bufsize - len, len);
132       do_test (bufsize - len, 0, len);
133       do_test (i, bufsize - len, len);
134       do_test (bufsize - len, i, len);
135     }
136 
137   for (i = 0; i < 32; ++i)
138     {
139       do_test (i, 0, i);
140       do_test (0, i, i);
141       do_test (i, i, i);
142 
143       for (j = 0; j < 32; ++j)
144 	{
145 	  do_test (i, bufsize - i - j, i);
146 	  do_test (bufsize - i - j, i, i);
147 	}
148     }
149 
150   for (i = 3; i < 32; ++i)
151     {
152       if ((i & (i - 1)) == 0)
153 	continue;
154       do_test (0, 0, 16 * i);
155       do_test (i, 0, 16 * i);
156       do_test (0, i, 16 * i);
157       do_test (i, i, 16 * i);
158     }
159 
160   return fail;
161 }
162