stratcliff.c 9.0 KB

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