Skip to content

Instantly share code, notes, and snippets.

@ice799

ice799/build.sh Secret

Created August 19, 2012 22:42
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ice799/0ee6dfeae09600d30c5e to your computer and use it in GitHub Desktop.
Save ice799/0ee6dfeae09600d30c5e to your computer and use it in GitHub Desktop.
shitty shim code
gcc -dynamiclib -fno-common -o mallshim.dylib mallshim.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc/malloc.h>
void (*real_free)(malloc_zone_t *zone, void *ptr);
void (*real_free_definite_size)(malloc_zone_t *zone, void *ptr, size_t size);
void my_free(malloc_zone_t *zone, void *ptr)
{
char *tmp = ptr;
char tmp_buf[1025] = {0};
size_t total = 0;
while (*tmp != '\0') {
tmp_buf[total] = *tmp;
total++;
if (total == 1024)
break;
tmp++;
}
malloc_printf("%s\n", tmp_buf);
real_free(zone, ptr);
}
void my_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size)
{
char tmp_buf[1024] = {0};
if (size < 1024) {
memcpy(tmp_buf, ptr, size);
} else {
memcpy(tmp_buf, ptr, 1023);
}
malloc_printf("%s\n", tmp_buf);
real_free_definite_size(zone, ptr, size);
}
void __attribute__((constructor)) my_init() {
malloc_zone_t *zone = malloc_default_zone();
real_free = zone->free;
real_free_definite_size = zone->free_definite_size;
zone->free_definite_size = my_free_definite_size;
zone->free = my_free;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment