stratcliff.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* Test for string function add boundaries of usable memory.
  2. Copyright (C) 1996,1997,1999-2002,2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  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. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #define _GNU_SOURCE 1
  17. /* Make sure we don't test the optimized inline functions if we want to
  18. test the real implementation. */
  19. #undef __USE_STRING_INLINES
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/mman.h>
  25. #include <sys/param.h>
  26. #ifndef MAX
  27. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  28. #endif
  29. int
  30. main (int argc, char *argv[])
  31. {
  32. int size = sysconf (_SC_PAGESIZE);
  33. char *adr, *dest;
  34. int result = 0;
  35. adr = (char *) mmap (NULL, 3 * size, PROT_READ | PROT_WRITE,
  36. MAP_PRIVATE | MAP_ANON, -1, 0);
  37. dest = (char *) mmap (NULL, 3 * size, PROT_READ | PROT_WRITE,
  38. MAP_PRIVATE | MAP_ANON, -1, 0);
  39. if (adr == MAP_FAILED || dest == MAP_FAILED)
  40. {
  41. if (errno == ENOSYS)
  42. puts ("No test, mmap not available.");
  43. else
  44. {
  45. printf ("mmap failed: %s", strerror(errno));
  46. result = 1;
  47. }
  48. }
  49. else
  50. {
  51. int inner, middle, outer;
  52. mprotect(adr, size, PROT_NONE);
  53. mprotect(adr + 2 * size, size, PROT_NONE);
  54. adr += size;
  55. mprotect(dest, size, PROT_NONE);
  56. mprotect(dest + 2 * size, size, PROT_NONE);
  57. dest += size;
  58. memset (adr, 'T', size);
  59. /* strlen test */
  60. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  61. {
  62. for (inner = MAX (outer, size - 64); inner < size; ++inner)
  63. {
  64. adr[inner] = '\0';
  65. if (strlen (&adr[outer]) != (size_t) (inner - outer))
  66. {
  67. printf ("strlen flunked for outer = %d, inner = %d\n",
  68. outer, inner);
  69. result = 1;
  70. }
  71. adr[inner] = 'T';
  72. }
  73. }
  74. /* strchr test */
  75. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  76. {
  77. for (middle = MAX (outer, size - 64); middle < size; ++middle)
  78. {
  79. for (inner = middle; inner < size; ++inner)
  80. {
  81. char *cp;
  82. adr[middle] = 'V';
  83. adr[inner] = '\0';
  84. cp = strchr (&adr[outer], 'V');
  85. if ((inner == middle && cp != NULL)
  86. || (inner != middle
  87. && (cp - &adr[outer]) != middle - outer))
  88. {
  89. printf ("strchr flunked for outer = %d, middle = %d, "
  90. "inner = %d\n", outer, middle, inner);
  91. result = 1;
  92. }
  93. adr[inner] = 'T';
  94. adr[middle] = 'T';
  95. }
  96. }
  97. }
  98. /* Special test. */
  99. adr[size - 1] = '\0';
  100. if (strchr (&adr[size - 1], '\n') != NULL)
  101. {
  102. puts ("strchr flunked for test of empty string at end of page");
  103. result = 1;
  104. }
  105. /* strrchr test */
  106. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  107. {
  108. for (middle = MAX (outer, size - 64); middle < size; ++middle)
  109. {
  110. for (inner = middle; inner < size; ++inner)
  111. {
  112. char *cp;
  113. adr[middle] = 'V';
  114. adr[inner] = '\0';
  115. cp = strrchr (&adr[outer], 'V');
  116. if ((inner == middle && cp != NULL)
  117. || (inner != middle
  118. && (cp - &adr[outer]) != middle - outer))
  119. {
  120. printf ("strrchr flunked for outer = %d, middle = %d, "
  121. "inner = %d\n", outer, middle, inner);
  122. result = 1;
  123. }
  124. adr[inner] = 'T';
  125. adr[middle] = 'T';
  126. }
  127. }
  128. }
  129. /* rawmemchr test */
  130. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  131. {
  132. for (middle = MAX (outer, size - 64); middle < size; ++middle)
  133. {
  134. char *cp;
  135. adr[middle] = 'V';
  136. cp = rawmemchr (&adr[outer], 'V');
  137. if (cp - &adr[outer] != middle - outer)
  138. {
  139. printf ("rawmemchr flunked for outer = %d, middle = %d\n",
  140. outer, middle);
  141. result = 1;
  142. }
  143. adr[middle] = 'T';
  144. }
  145. }
  146. /* strcpy test */
  147. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  148. {
  149. for (inner = MAX (outer, size - 64); inner < size; ++inner)
  150. {
  151. adr[inner] = '\0';
  152. if (strcpy (dest, &adr[outer]) != dest
  153. || strlen (dest) != (size_t) (inner - outer))
  154. {
  155. printf ("strcpy flunked for outer = %d, inner = %d\n",
  156. outer, inner);
  157. result = 1;
  158. }
  159. adr[inner] = 'T';
  160. }
  161. }
  162. /* strncpy tests */
  163. adr[size-1] = 'T';
  164. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  165. {
  166. size_t len;
  167. for (len = 0; len < size - outer; ++len)
  168. {
  169. if (strncpy (dest, &adr[outer], len) != dest
  170. || memcmp (dest, &adr[outer], len) != 0)
  171. {
  172. printf ("outer strncpy flunked for outer = %d, len = %Zd\n",
  173. outer, len);
  174. result = 1;
  175. }
  176. }
  177. }
  178. adr[size-1] = '\0';
  179. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  180. {
  181. for (inner = MAX (outer, size - 64); inner < size; ++inner)
  182. {
  183. size_t len;
  184. adr[inner] = '\0';
  185. for (len = 0; len < size - outer + 64; ++len)
  186. {
  187. if (strncpy (dest, &adr[outer], len) != dest
  188. || memcmp (dest, &adr[outer],
  189. MIN (inner - outer, len)) != 0
  190. || (inner - outer < len
  191. && strlen (dest) != (inner - outer)))
  192. {
  193. printf ("strncpy flunked for outer = %d, inner = %d, len = %Zd\n",
  194. outer, inner, len);
  195. result = 1;
  196. }
  197. if (strncpy (dest + 1, &adr[outer], len) != dest + 1
  198. || memcmp (dest + 1, &adr[outer],
  199. MIN (inner - outer, len)) != 0
  200. || (inner - outer < len
  201. && strlen (dest + 1) != (inner - outer)))
  202. {
  203. printf ("strncpy+1 flunked for outer = %d, inner = %d, len = %Zd\n",
  204. outer, inner, len);
  205. result = 1;
  206. }
  207. }
  208. adr[inner] = 'T';
  209. }
  210. }
  211. /* stpcpy test */
  212. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  213. {
  214. for (inner = MAX (outer, size - 64); inner < size; ++inner)
  215. {
  216. adr[inner] = '\0';
  217. if ((stpcpy (dest, &adr[outer]) - dest) != inner - outer)
  218. {
  219. printf ("stpcpy flunked for outer = %d, inner = %d\n",
  220. outer, inner);
  221. result = 1;
  222. }
  223. adr[inner] = 'T';
  224. }
  225. }
  226. /* stpncpy test */
  227. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  228. {
  229. for (middle = MAX (outer, size - 64); middle < size; ++middle)
  230. {
  231. adr[middle] = '\0';
  232. for (inner = 0; inner < size - outer; ++ inner)
  233. {
  234. if ((stpncpy (dest, &adr[outer], inner) - dest)
  235. != MIN (inner, middle - outer))
  236. {
  237. printf ("stpncpy flunked for outer = %d, middle = %d, "
  238. "inner = %d\n", outer, middle, inner);
  239. result = 1;
  240. }
  241. }
  242. adr[middle] = 'T';
  243. }
  244. }
  245. /* memcpy test */
  246. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  247. for (inner = 0; inner < size - outer; ++inner)
  248. if (memcpy (dest, &adr[outer], inner) != dest)
  249. {
  250. printf ("memcpy flunked for outer = %d, inner = %d\n",
  251. outer, inner);
  252. result = 1;
  253. }
  254. /* mempcpy test */
  255. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  256. for (inner = 0; inner < size - outer; ++inner)
  257. if (mempcpy (dest, &adr[outer], inner) != dest + inner)
  258. {
  259. printf ("mempcpy flunked for outer = %d, inner = %d\n",
  260. outer, inner);
  261. result = 1;
  262. }
  263. /* memccpy test */
  264. memset (adr, '\0', size);
  265. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  266. for (inner = 0; inner < size - outer; ++inner)
  267. if (memccpy (dest, &adr[outer], '\1', inner) != NULL)
  268. {
  269. printf ("memccpy flunked full copy for outer = %d, inner = %d\n",
  270. outer, inner);
  271. result = 1;
  272. }
  273. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  274. for (middle = 0; middle < size - outer; ++middle)
  275. {
  276. memset (dest, '\2', middle + 1);
  277. for (inner = 0; inner < middle; ++inner)
  278. {
  279. adr[outer + inner] = '\1';
  280. if (memccpy (dest, &adr[outer], '\1', middle + 128)
  281. != dest + inner + 1)
  282. {
  283. printf ("\
  284. memccpy flunked partial copy for outer = %d, middle = %d, inner = %d\n",
  285. outer, middle, inner);
  286. result = 1;
  287. }
  288. else if (dest[inner + 1] != '\2')
  289. {
  290. printf ("\
  291. memccpy copied too much for outer = %d, middle = %d, inner = %d\n",
  292. outer, middle, inner);
  293. result = 1;
  294. }
  295. adr[outer + inner] = '\0';
  296. }
  297. }
  298. }
  299. return result;
  300. }