tst-malloc-thread-fail.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* Test allocation function behavior on allocation failure.
  2. Copyright (C) 2015-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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 License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If
  14. not, see <https://www.gnu.org/licenses/>. */
  15. /* This test case attempts to trigger various unusual conditions
  16. related to allocation failures, notably switching to a different
  17. arena, and falling back to mmap (via sysmalloc). */
  18. #include <errno.h>
  19. #include <malloc.h>
  20. #include <pthread.h>
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <sys/resource.h>
  26. #include <sys/wait.h>
  27. #include <unistd.h>
  28. /* Wrapper for calloc with an optimization barrier. */
  29. static void *
  30. __attribute_optimization_barrier__
  31. allocate_zeroed (size_t a, size_t b)
  32. {
  33. return calloc (a, b);
  34. }
  35. /* System page size, as determined by sysconf (_SC_PAGE_SIZE). */
  36. static unsigned long page_size;
  37. /* Test parameters. */
  38. static size_t allocation_size;
  39. static size_t alignment;
  40. static enum {
  41. with_malloc,
  42. with_realloc,
  43. with_aligned_alloc,
  44. with_memalign,
  45. with_posix_memalign,
  46. with_valloc,
  47. with_pvalloc,
  48. with_calloc,
  49. last_allocation_function = with_calloc
  50. } allocation_function;
  51. /* True if an allocation function uses the alignment test
  52. parameter. */
  53. const static bool alignment_sensitive[last_allocation_function + 1] =
  54. {
  55. [with_aligned_alloc] = true,
  56. [with_memalign] = true,
  57. [with_posix_memalign] = true,
  58. };
  59. /* Combined pointer/expected alignment result of an allocation
  60. function. */
  61. struct allocate_result {
  62. void *pointer;
  63. size_t alignment;
  64. };
  65. /* Call the allocation function specified by allocation_function, with
  66. allocation_size and alignment (if applicable) as arguments. No
  67. alignment check. */
  68. static struct allocate_result
  69. allocate_1 (void)
  70. {
  71. switch (allocation_function)
  72. {
  73. case with_malloc:
  74. return (struct allocate_result)
  75. {malloc (allocation_size), _Alignof (max_align_t)};
  76. case with_realloc:
  77. {
  78. void *p = realloc (NULL, 16);
  79. void *q;
  80. if (p == NULL)
  81. q = NULL;
  82. else
  83. {
  84. q = realloc (p, allocation_size);
  85. if (q == NULL)
  86. free (p);
  87. }
  88. return (struct allocate_result) {q, _Alignof (max_align_t)};
  89. }
  90. case with_aligned_alloc:
  91. {
  92. void *p = aligned_alloc (alignment, allocation_size);
  93. return (struct allocate_result) {p, alignment};
  94. }
  95. case with_memalign:
  96. {
  97. void *p = memalign (alignment, allocation_size);
  98. return (struct allocate_result) {p, alignment};
  99. }
  100. case with_posix_memalign:
  101. {
  102. void *p;
  103. if (posix_memalign (&p, alignment, allocation_size))
  104. {
  105. if (errno == ENOMEM)
  106. p = NULL;
  107. else
  108. {
  109. printf ("error: posix_memalign (p, %zu, %zu): %m\n",
  110. alignment, allocation_size);
  111. abort ();
  112. }
  113. }
  114. return (struct allocate_result) {p, alignment};
  115. }
  116. case with_valloc:
  117. {
  118. void *p = valloc (allocation_size);
  119. return (struct allocate_result) {p, page_size};
  120. }
  121. case with_pvalloc:
  122. {
  123. void *p = pvalloc (allocation_size);
  124. return (struct allocate_result) {p, page_size};
  125. }
  126. case with_calloc:
  127. {
  128. char *p = allocate_zeroed (1, allocation_size);
  129. /* Check for non-zero bytes. */
  130. if (p != NULL)
  131. for (size_t i = 0; i < allocation_size; ++i)
  132. if (p[i] != 0)
  133. {
  134. printf ("error: non-zero byte at offset %zu\n", i);
  135. abort ();
  136. }
  137. return (struct allocate_result) {p, _Alignof (max_align_t)};
  138. }
  139. }
  140. abort ();
  141. }
  142. /* Call allocate_1 and perform the alignment check on the result. */
  143. static void *
  144. allocate (void)
  145. {
  146. struct allocate_result r = allocate_1 ();
  147. if ((((uintptr_t) r.pointer) & (r.alignment - 1)) != 0)
  148. {
  149. printf ("error: allocation function %d, size %zu not aligned to %zu\n",
  150. (int) allocation_function, allocation_size, r.alignment);
  151. abort ();
  152. }
  153. return r.pointer;
  154. }
  155. /* Barriers to synchronize thread creation and termination. */
  156. static pthread_barrier_t start_barrier;
  157. static pthread_barrier_t end_barrier;
  158. /* Thread function which performs the allocation test. Called by
  159. pthread_create and from the main thread. */
  160. static void *
  161. allocate_thread (void *closure)
  162. {
  163. /* Wait for the creation of all threads. */
  164. {
  165. int ret = pthread_barrier_wait (&start_barrier);
  166. if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
  167. {
  168. errno = ret;
  169. printf ("error: pthread_barrier_wait: %m\n");
  170. abort ();
  171. }
  172. }
  173. /* Allocate until we run out of memory, creating a single-linked
  174. list. */
  175. struct list {
  176. struct list *next;
  177. };
  178. struct list *head = NULL;
  179. while (true)
  180. {
  181. struct list *e = allocate ();
  182. if (e == NULL)
  183. break;
  184. e->next = head;
  185. head = e;
  186. }
  187. /* Wait for the allocation of all available memory. */
  188. {
  189. int ret = pthread_barrier_wait (&end_barrier);
  190. if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
  191. {
  192. errno = ret;
  193. printf ("error: pthread_barrier_wait: %m\n");
  194. abort ();
  195. }
  196. }
  197. /* Free the allocated memory. */
  198. while (head != NULL)
  199. {
  200. struct list *next = head->next;
  201. free (head);
  202. head = next;
  203. }
  204. return NULL;
  205. }
  206. /* Number of threads (plus the main thread. */
  207. enum { thread_count = 8 };
  208. /* Thread attribute to request creation of threads with a non-default
  209. stack size which is rather small. This avoids interfering with the
  210. configured address space limit. */
  211. static pthread_attr_t small_stack;
  212. /* Runs one test in multiple threads, all in a subprocess so that
  213. subsequent tests do not interfere with each other. */
  214. static void
  215. run_one (void)
  216. {
  217. /* Isolate the tests in a subprocess, so that we can start over
  218. from scratch. */
  219. pid_t pid = fork ();
  220. if (pid == 0)
  221. {
  222. /* In the child process. Create the allocation threads. */
  223. pthread_t threads[thread_count];
  224. for (unsigned i = 0; i < thread_count; ++i)
  225. {
  226. int ret = pthread_create (threads + i, &small_stack, allocate_thread, NULL);
  227. if (ret != 0)
  228. {
  229. errno = ret;
  230. printf ("error: pthread_create: %m\n");
  231. abort ();
  232. }
  233. }
  234. /* Also run the test on the main thread. */
  235. allocate_thread (NULL);
  236. for (unsigned i = 0; i < thread_count; ++i)
  237. {
  238. int ret = pthread_join (threads[i], NULL);
  239. if (ret != 0)
  240. {
  241. errno = ret;
  242. printf ("error: pthread_join: %m\n");
  243. abort ();
  244. }
  245. }
  246. _exit (0);
  247. }
  248. else if (pid < 0)
  249. {
  250. printf ("error: fork: %m\n");
  251. abort ();
  252. }
  253. /* In the parent process. Wait for the child process to exit. */
  254. int status;
  255. if (waitpid (pid, &status, 0) < 0)
  256. {
  257. printf ("error: waitpid: %m\n");
  258. abort ();
  259. }
  260. if (status != 0)
  261. {
  262. printf ("error: exit status %d from child process\n", status);
  263. exit (1);
  264. }
  265. }
  266. /* Run all applicable allocation functions for the current test
  267. parameters. */
  268. static void
  269. run_allocation_functions (void)
  270. {
  271. for (int af = 0; af <= last_allocation_function; ++af)
  272. {
  273. /* Run alignment-sensitive functions for non-default
  274. alignments. */
  275. if (alignment_sensitive[af] != (alignment != 0))
  276. continue;
  277. allocation_function = af;
  278. run_one ();
  279. }
  280. }
  281. int
  282. do_test (void)
  283. {
  284. /* Limit the number of malloc arenas. We use a very low number so
  285. that despute the address space limit configured below, all
  286. requested arenas a can be created. */
  287. if (mallopt (M_ARENA_MAX, 2) == 0)
  288. {
  289. printf ("error: mallopt (M_ARENA_MAX) failed\n");
  290. return 1;
  291. }
  292. /* Determine the page size. */
  293. {
  294. long ret = sysconf (_SC_PAGE_SIZE);
  295. if (ret < 0)
  296. {
  297. printf ("error: sysconf (_SC_PAGE_SIZE): %m\n");
  298. return 1;
  299. }
  300. page_size = ret;
  301. }
  302. /* Limit the size of the process, so that memory allocation in
  303. allocate_thread will eventually fail, without impacting the
  304. entire system. */
  305. {
  306. struct rlimit limit;
  307. if (getrlimit (RLIMIT_AS, &limit) != 0)
  308. {
  309. printf ("getrlimit (RLIMIT_AS) failed: %m\n");
  310. return 1;
  311. }
  312. long target = 200 * 1024 * 1024;
  313. if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
  314. {
  315. limit.rlim_cur = target;
  316. if (setrlimit (RLIMIT_AS, &limit) != 0)
  317. {
  318. printf ("setrlimit (RLIMIT_AS) failed: %m\n");
  319. return 1;
  320. }
  321. }
  322. }
  323. /* Initialize thread attribute with a reduced stack size. */
  324. {
  325. int ret = pthread_attr_init (&small_stack);
  326. if (ret != 0)
  327. {
  328. errno = ret;
  329. printf ("error: pthread_attr_init: %m\n");
  330. abort ();
  331. }
  332. unsigned long stack_size = ((256 * 1024) / page_size) * page_size;
  333. if (stack_size < 4 * page_size)
  334. stack_size = 8 * page_size;
  335. ret = pthread_attr_setstacksize (&small_stack, stack_size);
  336. if (ret != 0)
  337. {
  338. errno = ret;
  339. printf ("error: pthread_attr_setstacksize: %m\n");
  340. abort ();
  341. }
  342. }
  343. /* Initialize the barriers. We run thread_count threads, plus 1 for
  344. the main thread. */
  345. {
  346. int ret = pthread_barrier_init (&start_barrier, NULL, thread_count + 1);
  347. if (ret != 0)
  348. {
  349. errno = ret;
  350. printf ("error: pthread_barrier_init: %m\n");
  351. abort ();
  352. }
  353. ret = pthread_barrier_init (&end_barrier, NULL, thread_count + 1);
  354. if (ret != 0)
  355. {
  356. errno = ret;
  357. printf ("error: pthread_barrier_init: %m\n");
  358. abort ();
  359. }
  360. }
  361. allocation_size = 144;
  362. run_allocation_functions ();
  363. allocation_size = page_size;
  364. run_allocation_functions ();
  365. alignment = 128;
  366. allocation_size = 512;
  367. run_allocation_functions ();
  368. allocation_size = page_size;
  369. run_allocation_functions ();
  370. allocation_size = 17 * page_size;
  371. run_allocation_functions ();
  372. /* Deallocation the barriers and the thread attribute. */
  373. {
  374. int ret = pthread_barrier_destroy (&end_barrier);
  375. if (ret != 0)
  376. {
  377. errno = ret;
  378. printf ("error: pthread_barrier_destroy: %m\n");
  379. return 1;
  380. }
  381. ret = pthread_barrier_destroy (&start_barrier);
  382. if (ret != 0)
  383. {
  384. errno = ret;
  385. printf ("error: pthread_barrier_destroy: %m\n");
  386. return 1;
  387. }
  388. ret = pthread_attr_destroy (&small_stack);
  389. if (ret != 0)
  390. {
  391. errno = ret;
  392. printf ("error: pthread_attr_destroy: %m\n");
  393. return 1;
  394. }
  395. }
  396. return 0;
  397. }
  398. /* The repeated allocations take some time on slow machines. */
  399. #define TIMEOUT 100
  400. #define TEST_FUNCTION do_test ()
  401. #include "../test-skeleton.c"