Blame src/regression/malloc-brk-fail.c

Szabolcs Nagy 0b44f3
// commit 5446303328adf4b4e36d9fba21848e6feb55fab4 2014-04-02
Szabolcs Nagy 2b9639
// malloc should not fail if brk fails but mmap can still allocate
Szabolcs Nagy 2b9639
#include <stdlib.h>
Szabolcs Nagy 2b9639
#include <errno.h>
Szabolcs Nagy 2b9639
#include <string.h>
Szabolcs Nagy 46f33c
#include <sys/mman.h>
Szabolcs Nagy 2b9639
#include <sys/resource.h>
Szabolcs Nagy 2b9639
#include "test.h"
Szabolcs Nagy 2b9639
Szabolcs Nagy 2b9639
#define T(f) ((f)==0 || (t_error(#f " failed: %s\n", strerror(errno)), 0))
Szabolcs Nagy 2b9639
Szabolcs Nagy 2b9639
int main(void)
Szabolcs Nagy 2b9639
{
Szabolcs Nagy 2b9639
	void *p;
Szabolcs Nagy 2b9639
	void *q;
Szabolcs Nagy 2b9639
	size_t n;
Szabolcs Nagy 2b9639
	int r;
Szabolcs Nagy 2b9639
Szabolcs Nagy 2b9639
	// fill memory, largest mmaped area is [p,p+n)
Szabolcs Nagy 2b9639
	if (t_vmfill(&p, &n, 1) < 1 || n < 2*65536) {
Szabolcs Nagy 2b9639
		t_error("vmfill failed\n");
Szabolcs Nagy 2b9639
		return 1;
Szabolcs Nagy 2b9639
	}
Szabolcs Nagy 2b9639
Szabolcs Nagy 2b9639
	// malloc should fail here
Szabolcs Nagy 2b9639
	errno = 0;
Szabolcs Nagy 2b9639
	q = malloc(10000);
Szabolcs Nagy 2b9639
	if (q)
Szabolcs Nagy 2b9639
		t_error("malloc(10000) succeeded after memory is filled\n");
Szabolcs Nagy 2b9639
	else if (errno != ENOMEM)
Szabolcs Nagy 2b9639
		t_error("malloc did not fail with ENOMEM, got %s\n", strerror(errno));
Szabolcs Nagy 2b9639
Szabolcs Nagy a07fb6
	// make space available for mmap, but ensure it's not contiguous with brk
Szabolcs Nagy a07fb6
	T(munmap((char*)p+65536, n-65536));
Szabolcs Nagy 2b9639
Szabolcs Nagy 2b9639
	// malloc should succeed now
Szabolcs Nagy 2b9639
	q = malloc(10000);
Szabolcs Nagy 2b9639
	if (!q)
Szabolcs Nagy 2b9639
		t_error("malloc(10000) failed (eventhough 64k is available to mmap): %s\n", strerror(errno));
Szabolcs Nagy 2b9639
Szabolcs Nagy 2b9639
	return t_status;
Szabolcs Nagy 2b9639
}