lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <errno.h> |
| 2 | #include <pthread.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | #include <unistd.h> |
| 7 | #include <limits.h> |
| 8 | #include <sys/mman.h> |
| 9 | #include <sys/resource.h> |
| 10 | |
| 11 | static void use_stack (size_t needed); |
| 12 | |
| 13 | void (*use_stack_ptr) (size_t) = use_stack; |
| 14 | |
| 15 | static void |
| 16 | use_stack (size_t needed) |
| 17 | { |
| 18 | size_t sz = sysconf (_SC_PAGESIZE); |
| 19 | char *buf = alloca (sz); |
| 20 | memset (buf, '\0', sz); |
| 21 | |
| 22 | if (needed > sz) |
| 23 | use_stack_ptr (needed - sz); |
| 24 | } |
| 25 | |
| 26 | static void |
| 27 | use_up_memory (void) |
| 28 | { |
| 29 | struct rlimit rl; |
| 30 | getrlimit (RLIMIT_AS, &rl); |
| 31 | rl.rlim_cur = 10 * 1024 * 1024; |
| 32 | setrlimit (RLIMIT_AS, &rl); |
| 33 | |
| 34 | char *c; |
| 35 | int PAGESIZE = getpagesize (); |
| 36 | while (1) |
| 37 | { |
| 38 | c = mmap (NULL, PAGESIZE, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0); |
| 39 | if (c == MAP_FAILED) |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | static void * |
| 45 | child (void *arg) |
| 46 | { |
| 47 | sleep (1); |
| 48 | return arg; |
| 49 | } |
| 50 | |
| 51 | static int |
| 52 | do_test (void) |
| 53 | { |
| 54 | int err; |
| 55 | pthread_t tid; |
| 56 | |
| 57 | /* Allocate the memory needed for the stack. */ |
| 58 | use_stack_ptr (PTHREAD_STACK_MIN); |
| 59 | |
| 60 | use_up_memory (); |
| 61 | |
| 62 | err = pthread_create (&tid, NULL, child, NULL); |
| 63 | if (err != 0) |
| 64 | { |
| 65 | printf ("pthread_create returns %d: %s\n", err, |
| 66 | err == EAGAIN ? "OK" : "FAIL"); |
| 67 | return err != EAGAIN; |
| 68 | } |
| 69 | |
| 70 | /* We did not fail to allocate memory despite the preparation. Oh well. */ |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | #define TEST_FUNCTION do_test () |
| 75 | #include "../test-skeleton.c" |