summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Bottomley <James.Bottomley@HansenPartnership.com>2020-04-16 18:08:27 -0700
committerJames Bottomley <James.Bottomley@HansenPartnership.com>2020-04-16 18:08:27 -0700
commit2d144e66124e8d93b71bebccb3fe8ff871a12840 (patch)
treebc81720c47ae9c0c5ccb50c6176b63dfa7832ccb
parent85f04d9c4a781809db9d94930d7c1b122c64d1cf (diff)
downloadsecret-memory-preloader-2d144e66124e8d93b71bebccb3fe8ff871a12840.tar.gz
Add a rudimentary test routine for the preloader malloc
-rw-r--r--Makefile5
-rw-r--r--preload_test.c68
2 files changed, 72 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index d752e98..35f1b62 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,10 @@ LIBS=-lcrypto
all: preload.so openssl_test
clean:
- rm -f *.o *.so openssl_test
+ rm -f *.o *.so openssl_test preload_test
+
+check: preload_test preload.so
+ LD_PRELOAD=./preload.so MALLOC_DEBUG=1 ./preload_test
%.so: %.c
gcc -g -shared -fPIC -o $@ $^
diff --git a/preload_test.c b/preload_test.c
new file mode 100644
index 0000000..8d13215
--- /dev/null
+++ b/preload_test.c
@@ -0,0 +1,68 @@
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <openssl/crypto.h>
+
+#define CHUNK_SIZE 256
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
+
+static void *myalloc(void)
+{
+ size_t size = (random() &0xfff);
+ return OPENSSL_malloc(size);
+}
+
+int main(int argc, const char *argv[])
+{
+ void *mem[7];
+ int i;
+
+ printf("\nAlloc array\n\n");
+ for (i = 0; i < ARRAY_SIZE(mem); i++)
+ mem[i] = myalloc();
+
+ printf("\nFree array ascending\n\n");
+ for (i = 0; i < ARRAY_SIZE(mem); i++)
+ OPENSSL_free(mem[i]);
+
+ printf("\nAlloc array\n\n");
+ for (i = 0; i < ARRAY_SIZE(mem); i++)
+ mem[i] = myalloc();
+
+ printf("\nFree array descending\n\n");
+ for (i = ARRAY_SIZE(mem) - 1; i >=0; i--)
+ OPENSSL_free(mem[i]);
+
+ printf("\nAlloc array\n\n");
+ for (i = 0; i < ARRAY_SIZE(mem); i++)
+ mem[i] = myalloc();
+
+ printf("\nFree *2\n\n");
+ for (i = 0; i < ARRAY_SIZE(mem); i += 2)
+ OPENSSL_free(mem[i]);
+ printf("\nFree holes\n\n");
+ for (i = 1; i < ARRAY_SIZE(mem); i += 2)
+ OPENSSL_free(mem[i]);
+
+
+ printf("\nAlloc array for split\n\n");
+ mem[1] = OPENSSL_malloc(0x10000);
+ mem[0] = OPENSSL_malloc(1);
+ OPENSSL_free(mem[1]);
+
+ for (i = 1; i < ARRAY_SIZE(mem); i++)
+ mem[i] = myalloc();
+
+ printf("\nFree *2\n\n");
+ for (i = 0; i < ARRAY_SIZE(mem); i += 2)
+ OPENSSL_free(mem[i]);
+ printf("\nFree holes\n\n");
+ for (i = 1; i < ARRAY_SIZE(mem); i += 2)
+ OPENSSL_free(mem[i]);
+
+
+
+ exit(0);
+}
+