summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraxtloss <axtlos@getcryst.al>2024-07-03 18:38:44 +0200
committeraxtloss <axtlos@getcryst.al>2024-07-03 18:38:44 +0200
commit1abb10008fa9d321bbdeda8176dfc83c40e33c71 (patch)
tree2e3c2589a1ec6d30aac8abdb23f05fe54db7a215
downloadrandom-1abb10008fa9d321bbdeda8176dfc83c40e33c71.tar.gz
random-1abb10008fa9d321bbdeda8176dfc83c40e33c71.tar.bz2
silly-malloc: Add project
-rw-r--r--silly-malloc/README10
-rw-r--r--silly-malloc/meow.c10
-rw-r--r--silly-malloc/teehee.c22
3 files changed, 42 insertions, 0 deletions
diff --git a/silly-malloc/README b/silly-malloc/README
new file mode 100644
index 0000000..0d26192
--- /dev/null
+++ b/silly-malloc/README
@@ -0,0 +1,10 @@
+# Silly-Malloc
+
+Randomly allocate less memory than wanted
+
+## Usage
+```bash
+gcc -fPIC -shared teehee.c -o teehee.so
+gcc meow.c -o meow
+LD_PRELOAD=./teehee.so ./meow
+```
diff --git a/silly-malloc/meow.c b/silly-malloc/meow.c
new file mode 100644
index 0000000..88c7e88
--- /dev/null
+++ b/silly-malloc/meow.c
@@ -0,0 +1,10 @@
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <stdio.h>
+
+int main () {
+ char *merow = malloc (9);
+ sprintf (merow, "meowmeow");
+ printf ("%s\n", merow);
+ free (merow);
+}
diff --git a/silly-malloc/teehee.c b/silly-malloc/teehee.c
new file mode 100644
index 0000000..635a413
--- /dev/null
+++ b/silly-malloc/teehee.c
@@ -0,0 +1,22 @@
+#include <stdlib.h>
+#include <time.h>
+#include <dlfcn.h>
+#include <stdio.h>
+
+typedef void *(*malloc_t)(size_t size);
+static malloc_t real_malloc;
+
+extern void *malloc (size_t size) {
+ if (!real_malloc)
+ real_malloc = dlsym (RTLD_NEXT, "malloc");
+ if (!real_malloc)
+ return NULL; // lmao
+ srand (time (NULL));
+ int val = rand () % 3;
+ if (val > 1) {
+ long loss = rand () % (int) size;
+ return real_malloc (size - loss);
+ } else {
+ return real_malloc (size);
+ }
+}