Fork 0
a memory-mapping backed allocator http://src.kaivo.net/lib/mapalloc/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Jakob Kaivo c4b21f6aeb document how to use the wrapper 2 years ago
include add optional macros to directly provide the <stdlib.h> interface 2 years ago
src munmap() extra pages during realloc() 2 years ago
test add tests for attempting to free() or realloc() on an invalid address 2 years ago
.gitignore add simple realloc() test 2 years ago
LICENSE Add new file 2 years ago
Makefile add tests for attempting to free() or realloc() on an invalid address 2 years ago
README.md document how to use the wrapper 2 years ago



MapAlloc is a memory mapping based allocator with an API compatible with the C <stdlib.h> dynamic memory functions. By using memory mappings for each allocation, MapAlloc is able to provide guard pages to detect heap overflow and underflow, as well as potentially identifying use-after-free or double-free errors (in all cases, your program will crash, which is preferable to being silently exploited).

MapAlloc also maintains metadata independently from the heap, so it eliminates an entire class of vulnerabilities which rely on easy access to this metadata.


Just run make. This will give you libmapalloc.a, which you can statically link into your own programs, as well as libwrapalloc.so, which can be used with LD_PRELOAD to override your standard C library's dynamic memory functions.

Including in your program

In your source file:

#include "mapalloc.h"

This will give you:

void *MA_malloc(size_t n);
void *MA_calloc(size_t nelem, size_t elsize);
void *MA_realloc(void *ptr, size_t n);
void MA_free(void *ptr);

Or, you can ask for macros to provide the same interfaces as <stdlib.h> (note that if you need to also include <stdlib.h>, you should include it before "mapalloc.h"):

#include "mapalloc.h"

This will give you access to the same set of functions as above, but also provide macros:

#define malloc(n)	MA_malloc(n)
#define calloc(n, e)	MA_calloc(n, e)
#define realloc(p, n)	MA_realloc(p, n)
#define free(p)		MA_free(p)

Link your program with -lmapalloc (you may also need to specify -L with the path to where libmapalloc.a is if you don't copy it to part of your linker's default search path).


The dynamic library libwrapalloc.so is also built by default. This can be used to wrap the standard libc functions malloc(), calloc(), realloc(), and free() to their MapAlloc equivalents if your dynamic linker supports this. For example, on Linux systems with the GNU linker:

LD_PRELOAD=libwrapalloc.so command args...