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