From c4a294e498cd456c5421b3ffebdd64b1e15a062d Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 30 Jun 2020 13:52:48 -0400 Subject: add wrapper library to LD_PRELOAD malloc(), calloc(), realloc(), and free() --- Makefile | 13 +++++++++++-- src/wrap.c | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/wrap.c diff --git a/Makefile b/Makefile index 3f7db7f..867b86c 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,10 @@ OBJDIR=obj SRCDIR=src INCDIR=include TESTDIR=test -CFLAGS=-I$(INCDIR) -Wall -Wextra -Wpedantic -g +CFLAGS=-I$(INCDIR) -Wall -Wextra -Wpedantic -g -fPIC OBJECTS=$(OBJDIR)/mapalloc.o +WRAPOBJECTS=$(OBJECTS) $(OBJDIR)/wrap.o TESTS=$(BINDIR)/overflow \ $(BINDIR)/underflow \ $(BINDIR)/zero \ @@ -16,7 +17,7 @@ TESTS=$(BINDIR)/overflow \ $(BINDIR)/use-after-free \ $(BINDIR)/double-free -all: $(LIBDIR)/libmapalloc.a +all: $(LIBDIR)/libmapalloc.a $(LIBDIR)/libwrapalloc.so #$(LIBDIR)/libmapalloc.so @@ -26,6 +27,10 @@ $(OBJDIR)/mapalloc.o: $(SRCDIR)/mapalloc.c $(INCDIR)/mapalloc.h @mkdir -p $(@D) $(CC) -c -o $@ $(CFLAGS) $(SRCDIR)/$(*F).c +$(OBJDIR)/wrap.o: $(SRCDIR)/wrap.c $(INCDIR)/mapalloc.h + @mkdir -p $(@D) + $(CC) -c -o $@ $(CFLAGS) $(SRCDIR)/$(*F).c + $(LIBDIR)/libmapalloc.a: $(OBJECTS) @mkdir -p $(@D) $(AR) $(ARFLAGS) $@ $< @@ -34,6 +39,10 @@ $(LIBDIR)/libmapalloc.so: $(OBJECTS) @mkdir -p $(@D) $(CC) -o $@ -shared $(OBJECTS) +$(LIBDIR)/libwrapalloc.so: $(WRAPOBJECTS) + @mkdir -p $(@D) + $(CC) -o $@ -shared $(WRAPOBJECTS) + $(TESTS): $(LIBDIR)/libmapalloc.a $(BINDIR)/overflow: $(TESTDIR)/overflow.c $(BINDIR)/underflow: $(TESTDIR)/underflow.c diff --git a/src/wrap.c b/src/wrap.c new file mode 100644 index 0000000..524d6d2 --- /dev/null +++ b/src/wrap.c @@ -0,0 +1,21 @@ +#include "mapalloc.h" + +void *malloc(size_t n) +{ + return map_malloc(n); +} + +void *calloc(size_t n, size_t e) +{ + return map_calloc(n, e); +} + +void *realloc(void *p, size_t n) +{ + return map_realloc(p, n); +} + +void free(void *p) +{ + map_free(p); +} -- cgit v1.2.1