Skip to content

Instantly share code, notes, and snippets.

@opsJson
Last active September 22, 2022 03:40
Show Gist options
  • Save opsJson/0a75524f11f22850a893f0549ce89514 to your computer and use it in GitHub Desktop.
Save opsJson/0a75524f11f22850a893f0549ce89514 to your computer and use it in GitHub Desktop.
ELF - Elegant Leaks Finder
#ifndef ELF_H
#define ELF_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef ELF_CAPACITY
#define ELF_CAPACITY 1024
#endif
#ifndef ELF_CLEAR_SCREEN_LENGTH
#define ELF_CLEAR_SCREEN_LENGTH 60
#endif
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
#define ELF_CLEAR_SCREEN "cls"
#else
#define ELF_CLEAR_SCREEN "clear"
#endif
#define ELF_SCREEN_STR \
"================================================================================================================"
struct elf_t {
char* file;
char* func;
int line;
int size;
void* memory;
};
static struct elf_t elf_heap[ELF_CAPACITY] = {{0}};
static void elf_display() {
unsigned int i, aux, length = 0;
int memory_in_use = 0;
int memory_num_allocated = 0;
char temp[1024];
for (i = 0; i < ELF_CAPACITY; i++) {
if (elf_heap[i].memory != 0) {
snprintf(temp, sizeof(temp)-1, "\t%i bytes in %.*s() at %.*s:%i\n",
elf_heap[i].size,
(int)strlen(elf_heap[i].func) % 50, elf_heap[i].func,
(int)strlen(elf_heap[i].file) % 100, elf_heap[i].file,
elf_heap[i].line);
aux = strlen(temp);
if (aux > length) length = aux;
}
}
length += ELF_CLEAR_SCREEN_LENGTH;
system(ELF_CLEAR_SCREEN);
printf("%.*s\n", length, ELF_SCREEN_STR);
for (i = 0; i < ELF_CAPACITY; i++) {
if (elf_heap[i].memory != 0) {
fprintf(stderr, "\t%i bytes in %.*s() at %.*s:%i\n",
elf_heap[i].size,
(int)strlen(elf_heap[i].func) % 50, elf_heap[i].func,
(int)strlen(elf_heap[i].file) % 100, elf_heap[i].file,
elf_heap[i].line);
memory_in_use += elf_heap[i].size;
memory_num_allocated++;
}
}
if (memory_in_use == 0) {
printf("\t\tNO MEMORY IN USE.\n");
}
printf("%.*s\n", length, ELF_SCREEN_STR);
printf("\t\tIn use: %i bytes\tAllocated: %i pointers\n", memory_in_use, memory_num_allocated);
printf("%.*s\n", length, ELF_SCREEN_STR);
}
static void elf_push(void *memory, int size, const char *func, char *file, int line) {
unsigned int i;
struct elf_t leak;
leak.memory = memory;
leak.size = size;
leak.func = (char*)func;
leak.file = file;
leak.line = line;
for (i = 0; i < ELF_CAPACITY; i++) {
if (elf_heap[i].memory == 0) {
elf_heap[i] = leak;
elf_display();
return;
}
}
fprintf(stderr, "ELF_CAPACITY REACHED!\n");
fprintf(stderr, "Try #define ELF_CAPACITY [bigger size]\n");
exit(1);
}
static void elf_pop(void *memory, int dealloc, const char *func, char *file, int line) {
unsigned int i;
for (i = 0; i < ELF_CAPACITY; i++) {
if (elf_heap[i].memory == memory) {
elf_heap[i].memory = 0;
if (dealloc) free(memory);
elf_display();
return;
}
}
fprintf(stderr, "TRIED TO FREE NOT ALLOCATED ADDRESS IN %s() AT %s:%i\n\n",
func, file, line);
fprintf(stderr, "Try #include \"elf.h\" at the top.\n");
exit(1);
}
void *elf_alloc(unsigned int size, const char *func, char *file, int line) {
void *memory;
if ((memory = malloc(size)) == NULL) return NULL;
elf_push(memory, size, func, file, line);
return memory;
}
void *elf_realloc(void *memory, int size, const char* func, char* file, int line) {
void *realloced;
if ((realloced = realloc(memory, size)) == NULL) return NULL;
elf_pop(memory, 0, func, file, line);
elf_push(realloced, size, func, file, line);
return realloced;
}
#define malloc(size) elf_alloc(size, __FUNCTION__, __FILE__, __LINE__)
#define calloc(num, size) elf_alloc(num * size, __FUNCTION__, __FILE__, __LINE__)
#define realloc(memory, size) elf_realloc(memory, size, __FUNCTION__, __FILE__, __LINE__)
#define free(memory) elf_pop(memory, 1, __FUNCTION__, __FILE__, __LINE__)
#endif /* ELF_H */
/*///////////////////////////////////
Testing:
///////////////////////////////////*/
#include <unistd.h>
int main(void) {
char* ptr;
while (1) {
ptr = malloc(69);
ptr = malloc(1);
sleep(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment