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