~jkaivo/mapalloc

f2a8df6171b3eae8c949c892e8a35d7acd51084c — Jakob Kaivo 2 years ago 62961fe
add a test program that can be wrapped with LD_PRELOAD
2 files changed, 27 insertions(+), 1 deletions(-)

M Makefile
A test/wrapper.c
M Makefile => Makefile +5 -1
@@ 21,7 21,7 @@ all: $(LIBDIR)/libmapalloc.a $(LIBDIR)/libwrapalloc.so

#$(LIBDIR)/libmapalloc.so

tests: $(TESTS)
tests: $(TESTS) $(BINDIR)/wrapper

$(OBJDIR)/mapalloc.o: $(SRCDIR)/mapalloc.c $(INCDIR)/mapalloc.h
	@mkdir -p $(@D)


@@ 51,6 51,10 @@ $(BINDIR)/realloc: $(TESTDIR)/realloc.c
$(BINDIR)/use-after-free: $(TESTDIR)/use-after-free.c
$(BINDIR)/double-free: $(TESTDIR)/double-free.c

$(BINDIR)/wrapper: $(TESTDIR)/wrapper.c
	@mkdir -p $(@D)
	$(CC) -o $@ $(CFLAGS) $(TESTDIR)/$(@F).c

$(TESTS):
	@mkdir -p $(@D)
	$(CC) -o $@ $(CFLAGS) $(TESTDIR)/$(@F).c -L$(LIBDIR) -lmapalloc

A test/wrapper.c => test/wrapper.c +22 -0
@@ 0,0 1,22 @@
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
	const char buf[] = "THIS IS A CONSTANT STRING";

	char *ptr = realloc(NULL, sizeof(buf));
	memcpy(ptr, buf, sizeof(buf));
	printf("%p: %s\n", ptr, ptr);

	ptr = realloc(ptr, sizeof(buf) * 2);
	memcpy(ptr + sizeof(buf) - 1, buf, sizeof(buf));
	printf("%p: %s\n", ptr, ptr);

	ptr = realloc(ptr, sizeof(buf) * sysconf(_SC_PAGESIZE));
	memcpy(ptr + (2 * sizeof(buf)) - 2, buf, sizeof(buf));
	printf("%p: %s\n", ptr, ptr);
}