1 /* A minor test-program for memmove.
2 Copyright (C) 2005 Axis Communications.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 2. Neither the name of Axis Communications nor the names of its
13 contributors may be used to endorse or promote products derived
14 from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS
20 COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE. */
28
29 /* Test moves of 0..MAX bytes; overlapping-src-higher,
30 overlapping-src-lower and non-overlapping. The overlap varies with
31 1..N where N is the size moved. This means an order of MAX**2
32 iterations. The size of an octet may seem appropriate for MAX and
33 makes an upper limit for simple testing. For the CRIS simulator,
34 making this 256 added 90s to the test-run (2GHz P4) while 64 (4s) was
35 enough to spot the bugs that had crept in, hence the number chosen. */
36 #define MAX 64
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #define TOO_MANY_ERRORS 11
43 int errors = 0;
44
45 #define DEBUGP \
46 if (errors == TOO_MANY_ERRORS) \
47 printf ("Further errors omitted\n"); \
48 else if (errors < TOO_MANY_ERRORS) \
49 printf
50
51 /* A safe target-independent memmove. */
52
53 void
mymemmove(unsigned char * dest,unsigned char * src,size_t n)54 mymemmove (unsigned char *dest, unsigned char *src, size_t n)
55 {
56 if ((src <= dest && src + n <= dest)
57 || src >= dest)
58 while (n-- > 0)
59 *dest++ = *src++;
60 else
61 {
62 dest += n;
63 src += n;
64 while (n-- > 0)
65 *--dest = *--src;
66 }
67 }
68
69 /* It's either the noinline attribute or forcing the test framework to
70 pass -fno-builtin-memmove. */
71 void
72 xmemmove (unsigned char *dest, unsigned char *src, size_t n)
73 __attribute__ ((__noinline__));
74
75 void
xmemmove(unsigned char * dest,unsigned char * src,size_t n)76 xmemmove (unsigned char *dest, unsigned char *src, size_t n)
77 {
78 void *retp;
79 retp = memmove (dest, src, n);
80
81 if (retp != dest)
82 {
83 errors++;
84 DEBUGP ("memmove of n bytes returned %p instead of dest=%p\n",
85 retp, dest);
86 }
87 }
88
89
90 /* Fill the array with something we can associate with a position, but
91 not exactly the same as the position index. */
92
93 void
fill(unsigned char dest[MAX * 3])94 fill (unsigned char dest[MAX*3])
95 {
96 size_t i;
97 for (i = 0; i < MAX*3; i++)
98 dest[i] = (10 + i) % MAX;
99 }
100
101 int
main(void)102 main (void)
103 {
104 size_t i;
105 int errors = 0;
106
107 /* Leave some room before and after the area tested, so we can detect
108 overwrites of up to N bytes, N being the amount tested. If you
109 want to test using valgrind, make these malloced instead. */
110 unsigned char from_test[MAX*3];
111 unsigned char to_test[MAX*3];
112 unsigned char from_known[MAX*3];
113 unsigned char to_known[MAX*3];
114
115 /* Non-overlap. */
116 for (i = 0; i < MAX; i++)
117 {
118 /* Do the memmove first before setting the known array, so we know
119 it didn't change any of the known array. */
120 fill (from_test);
121 fill (to_test);
122 xmemmove (to_test + MAX, 1 + from_test + MAX, i);
123
124 fill (from_known);
125 fill (to_known);
126 mymemmove (to_known + MAX, 1 + from_known + MAX, i);
127
128 if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
129 {
130 errors++;
131 DEBUGP ("memmove failed non-overlap test for %lu bytes\n", (unsigned long) i);
132 }
133 }
134
135 /* Overlap-from-before. */
136 for (i = 0; i < MAX; i++)
137 {
138 size_t j;
139 for (j = 0; j < i; j++)
140 {
141 fill (to_test);
142 xmemmove (to_test + MAX * 2 - i, to_test + MAX * 2 - i - j, i);
143
144 fill (to_known);
145 mymemmove (to_known + MAX * 2 - i, to_known + MAX * 2 - i - j, i);
146
147 if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
148 {
149 errors++;
150 DEBUGP ("memmove failed for %lu bytes,"
151 " with src %lu bytes before dest\n",
152 (unsigned long) i, (unsigned long) j);
153 }
154 }
155 }
156
157 /* Overlap-from-after. */
158 for (i = 0; i < MAX; i++)
159 {
160 size_t j;
161 for (j = 0; j < i; j++)
162 {
163 fill (to_test);
164 xmemmove (to_test + MAX, to_test + MAX + j, i);
165
166 fill (to_known);
167 mymemmove (to_known + MAX, to_known + MAX + j, i);
168
169 if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
170 {
171 errors++;
172 DEBUGP ("memmove failed when moving %lu bytes,"
173 " with src %lu bytes after dest\n",
174 (unsigned long) i, (unsigned long) j);
175 }
176 }
177 }
178
179 if (errors != 0)
180 abort ();
181 exit (0);
182 }
183