1 /* Copyright (C) 1990-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <support/support.h>
22 
23 static int
do_test(void)24 do_test (void)
25 {
26   char *mem, *memp;
27   char *rand_mem;
28   char *lo_around, *hi_around;
29   int size, max_size;
30   int src_off, dst_off;
31   int i;
32   int space_around = 10;
33 
34   max_size = 256;
35 
36   mem = xmalloc (max_size + 2 * max_size + 2 * space_around);
37   rand_mem = xmalloc (max_size);
38   lo_around = xmalloc (space_around);
39   hi_around = xmalloc (space_around);
40   memp = mem + space_around;
41 
42   /* Fill RAND_MEM with random bytes, each non-zero.  */
43   for (i = 0; i < max_size; i++)
44     {
45       int x;
46       do
47 	x = random ();
48       while (x == 0);
49       rand_mem[i] = x;
50     }
51 
52   for (size = 0; size < max_size; size++)
53     {
54       printf("phase %d\n", size);
55       for (src_off = 0; src_off <= 16; src_off++)
56 	{
57 	  for (dst_off = 0; dst_off <= 16; dst_off++)
58 	    {
59 	      /* Put zero around the intended destination, to check
60 		 that it's not clobbered.  */
61 	      for (i = 1; i < space_around; i++)
62 		{
63 		  memp[dst_off - i] = 0;
64 		  memp[dst_off + size - 1 + i] = 0;
65 		}
66 
67 	      /* Fill the source area with known contents.  */
68 	      for (i = 0; i < size; i++)
69 		memp[src_off + i] = rand_mem[i];
70 
71 	      /* Remember the contents around the destination area.
72 		 (It might not be what we wrote some lines above, since
73 		 the src area and the dst area overlap.)  */
74 	      for (i = 1; i < space_around; i++)
75 		{
76 		  lo_around[i] = memp[dst_off - i];
77 		  hi_around[i] = memp[dst_off + size - 1 + i];
78 		}
79 
80 	      memmove (memp + dst_off, memp + src_off, size);
81 
82 	      /* Check that the destination area has the same
83 		 contents we wrote to the source area.  */
84 	      for (i = 0; i < size; i++)
85 		{
86 		  if (memp[dst_off + i] != rand_mem[i])
87 		    abort ();
88 		}
89 
90 	      /* Check that the area around the destination is not
91 		 clobbered.  */
92 	      for (i = 1; i < space_around; i++)
93 		{
94 		  if (memp[dst_off - i] != lo_around[i])
95 		    abort ();
96 		  if (memp[dst_off + size - 1 + i] != hi_around[i])
97 		    abort ();
98 		}
99 	    }
100 	}
101     }
102 
103   puts ("Test succeeded.");
104 
105   return 0;
106 }
107 
108 #include <support/test-driver.c>
109