stratcliff.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #if defined(__GLIBC__) || defined(__UCLIBC__)
  131. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  132. {
  133. for (middle = MAX (outer, size - 64); middle < size; ++middle)
  134. {
  135. char *cp;
  136. adr[middle] = 'V';
  137. cp = rawmemchr (&adr[outer], 'V');
  138. if (cp - &adr[outer] != middle - outer)
  139. {
  140. printf ("rawmemchr flunked for outer = %d, middle = %d\n",
  141. outer, middle);
  142. result = 1;
  143. }
  144. adr[middle] = 'T';
  145. }
  146. }
  147. #endif
  148. /* strcpy test */
  149. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  150. {
  151. for (inner = MAX (outer, size - 64); inner < size; ++inner)
  152. {
  153. adr[inner] = '\0';
  154. if (strcpy (dest, &adr[outer]) != dest
  155. || strlen (dest) != (size_t) (inner - outer))
  156. {
  157. printf ("strcpy flunked for outer = %d, inner = %d\n",
  158. outer, inner);
  159. result = 1;
  160. }
  161. adr[inner] = 'T';
  162. }
  163. }
  164. /* strncpy tests */
  165. adr[size-1] = 'T';
  166. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  167. {
  168. size_t len;
  169. for (len = 0; len < size - outer; ++len)
  170. {
  171. if (strncpy (dest, &adr[outer], len) != dest
  172. || memcmp (dest, &adr[outer], len) != 0)
  173. {
  174. printf ("outer strncpy flunked for outer = %d, len = %Zd\n",
  175. outer, len);
  176. result = 1;
  177. }
  178. }
  179. }
  180. adr[size-1] = '\0';
  181. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  182. {
  183. for (inner = MAX (outer, size - 64); inner < size; ++inner)
  184. {
  185. size_t len;
  186. adr[inner] = '\0';
  187. for (len = 0; len < size - outer + 64; ++len)
  188. {
  189. if (strncpy (dest, &adr[outer], len) != dest
  190. || memcmp (dest, &adr[outer],
  191. MIN (inner - outer, len)) != 0
  192. || (inner - outer < len
  193. && strlen (dest) != (inner - outer)))
  194. {
  195. printf ("strncpy flunked for outer = %d, inner = %d, len = %Zd\n",
  196. outer, inner, len);
  197. result = 1;
  198. }
  199. if (strncpy (dest + 1, &adr[outer], len) != dest + 1
  200. || memcmp (dest + 1, &adr[outer],
  201. MIN (inner - outer, len)) != 0
  202. || (inner - outer < len
  203. && strlen (dest + 1) != (inner - outer)))
  204. {
  205. printf ("strncpy+1 flunked for outer = %d, inner = %d, len = %Zd\n",
  206. outer, inner, len);
  207. result = 1;
  208. }
  209. }
  210. adr[inner] = 'T';
  211. }
  212. }
  213. /* stpcpy test */
  214. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  215. {
  216. for (inner = MAX (outer, size - 64); inner < size; ++inner)
  217. {
  218. adr[inner] = '\0';
  219. if ((stpcpy (dest, &adr[outer]) - dest) != inner - outer)
  220. {
  221. printf ("stpcpy flunked for outer = %d, inner = %d\n",
  222. outer, inner);
  223. result = 1;
  224. }
  225. adr[inner] = 'T';
  226. }
  227. }
  228. /* stpncpy test */
  229. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  230. {
  231. for (middle = MAX (outer, size - 64); middle < size; ++middle)
  232. {
  233. adr[middle] = '\0';
  234. for (inner = 0; inner < size - outer; ++ inner)
  235. {
  236. if ((stpncpy (dest, &adr[outer], inner) - dest)
  237. != MIN (inner, middle - outer))
  238. {
  239. printf ("stpncpy flunked for outer = %d, middle = %d, "
  240. "inner = %d\n", outer, middle, inner);
  241. result = 1;
  242. }
  243. }
  244. adr[middle] = 'T';
  245. }
  246. }
  247. /* memcpy test */
  248. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  249. for (inner = 0; inner < size - outer; ++inner)
  250. if (memcpy (dest, &adr[outer], inner) != dest)
  251. {
  252. printf ("memcpy flunked for outer = %d, inner = %d\n",
  253. outer, inner);
  254. result = 1;
  255. }
  256. /* mempcpy test */
  257. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  258. for (inner = 0; inner < size - outer; ++inner)
  259. if (mempcpy (dest, &adr[outer], inner) != dest + inner)
  260. {
  261. printf ("mempcpy flunked for outer = %d, inner = %d\n",
  262. outer, inner);
  263. result = 1;
  264. }
  265. /* memccpy test */
  266. memset (adr, '\0', size);
  267. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  268. for (inner = 0; inner < size - outer; ++inner)
  269. if (memccpy (dest, &adr[outer], '\1', inner) != NULL)
  270. {
  271. printf ("memccpy flunked full copy for outer = %d, inner = %d\n",
  272. outer, inner);
  273. result = 1;
  274. }
  275. for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
  276. for (middle = 0; middle < size - outer; ++middle)
  277. {
  278. memset (dest, '\2', middle + 1);
  279. for (inner = 0; inner < middle; ++inner)
  280. {
  281. adr[outer + inner] = '\1';
  282. if (memccpy (dest, &adr[outer], '\1', middle + 128)
  283. != dest + inner + 1)
  284. {
  285. printf ("\
  286. memccpy flunked partial copy for outer = %d, middle = %d, inner = %d\n",
  287. outer, middle, inner);
  288. result = 1;
  289. }
  290. else if (dest[inner + 1] != '\2')
  291. {
  292. printf ("\
  293. memccpy copied too much for outer = %d, middle = %d, inner = %d\n",
  294. outer, middle, inner);
  295. result = 1;
  296. }
  297. adr[outer + inner] = '\0';
  298. }
  299. }
  300. }
  301. return result;
  302. }