From 1abb10008fa9d321bbdeda8176dfc83c40e33c71 Mon Sep 17 00:00:00 2001 From: axtloss Date: Wed, 3 Jul 2024 18:38:44 +0200 Subject: silly-malloc: Add project --- silly-malloc/README | 10 ++++++++++ silly-malloc/meow.c | 10 ++++++++++ silly-malloc/teehee.c | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 silly-malloc/README create mode 100644 silly-malloc/meow.c create mode 100644 silly-malloc/teehee.c (limited to 'silly-malloc') 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 +#include + +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 +#include +#include +#include + +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); + } +} -- cgit v1.2.3