summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-06-30 13:52:48 -0400
committerJakob Kaivo <jkk@ung.org>2020-06-30 13:52:48 -0400
commitc4a294e498cd456c5421b3ffebdd64b1e15a062d (patch)
tree913ba7732e0b5da439f9d88bd8094e5c20890df4
parentbbac7666e763af9b65ef919b451f5930608a516a (diff)
add wrapper library to LD_PRELOAD malloc(), calloc(), realloc(), and free()
-rw-r--r--Makefile13
-rw-r--r--src/wrap.c21
2 files changed, 32 insertions, 2 deletions
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);
+}