regression test for brk failing in malloc
[libc-test] / src / regression / malloc-brk-fail.c
1 // malloc should not fail if brk fails but mmap can still allocate
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <sys/resource.h>
6 #include "test.h"
7
8 #define T(f) ((f)==0 || (t_error(#f " failed: %s\n", strerror(errno)), 0))
9
10 int main(void)
11 {
12         void *p;
13         void *q;
14         size_t n;
15         int r;
16
17         // fill memory, largest mmaped area is [p,p+n)
18         if (t_vmfill(&p, &n, 1) < 1 || n < 2*65536) {
19                 t_error("vmfill failed\n");
20                 return 1;
21         }
22         errno = 0;
23         T(t_setrlim(RLIMIT_DATA, 0));
24
25         // malloc should fail here
26         errno = 0;
27         q = malloc(10000);
28         if (q)
29                 t_error("malloc(10000) succeeded after memory is filled\n");
30         else if (errno != ENOMEM)
31                 t_error("malloc did not fail with ENOMEM, got %s\n", strerror(errno));
32
33         // make some space available for mmap
34         T(munmap(p+65536, 65536));
35
36         // malloc should succeed now
37         q = malloc(10000);
38         if (!q)
39                 t_error("malloc(10000) failed (eventhough 64k is available to mmap): %s\n", strerror(errno));
40
41         return t_status;
42 }