1570af302Sopenharmony_ci// commit 5446303328adf4b4e36d9fba21848e6feb55fab4 2014-04-02 2570af302Sopenharmony_ci// malloc should not fail if brk fails but mmap can still allocate 3570af302Sopenharmony_ci#include <stdlib.h> 4570af302Sopenharmony_ci#include <errno.h> 5570af302Sopenharmony_ci#include <string.h> 6570af302Sopenharmony_ci#include <sys/mman.h> 7570af302Sopenharmony_ci#include <sys/resource.h> 8570af302Sopenharmony_ci#include "test.h" 9570af302Sopenharmony_ci 10570af302Sopenharmony_ci#define T(f) ((f)==0 || (t_error(#f " failed: %s\n", strerror(errno)), 0)) 11570af302Sopenharmony_ci 12570af302Sopenharmony_ciint main(void) 13570af302Sopenharmony_ci{ 14570af302Sopenharmony_ci void *p; 15570af302Sopenharmony_ci void *q; 16570af302Sopenharmony_ci size_t n; 17570af302Sopenharmony_ci int r; 18570af302Sopenharmony_ci 19570af302Sopenharmony_ci // fill memory, largest mmaped area is [p,p+n) 20570af302Sopenharmony_ci if (t_vmfill(&p, &n, 1) < 1 || n < 2*65536) { 21570af302Sopenharmony_ci t_error("vmfill failed\n"); 22570af302Sopenharmony_ci return 1; 23570af302Sopenharmony_ci } 24570af302Sopenharmony_ci errno = 0; 25570af302Sopenharmony_ci T(t_setrlim(RLIMIT_DATA, 0)); 26570af302Sopenharmony_ci 27570af302Sopenharmony_ci // malloc should fail here 28570af302Sopenharmony_ci errno = 0; 29570af302Sopenharmony_ci q = malloc(10000); 30570af302Sopenharmony_ci if (q) 31570af302Sopenharmony_ci t_error("malloc(10000) succeeded after memory is filled\n"); 32570af302Sopenharmony_ci else if (errno != ENOMEM) 33570af302Sopenharmony_ci t_error("malloc did not fail with ENOMEM, got %s\n", strerror(errno)); 34570af302Sopenharmony_ci 35570af302Sopenharmony_ci // make some space available for mmap 36570af302Sopenharmony_ci T(munmap((char*)p+65536, 65536)); 37570af302Sopenharmony_ci 38570af302Sopenharmony_ci // malloc should succeed now 39570af302Sopenharmony_ci q = malloc(10000); 40570af302Sopenharmony_ci if (!q) 41570af302Sopenharmony_ci t_error("malloc(10000) failed (eventhough 64k is available to mmap): %s\n", strerror(errno)); 42570af302Sopenharmony_ci 43570af302Sopenharmony_ci return t_status; 44570af302Sopenharmony_ci} 45