1
0

thesis: Complete 2.2

This commit is contained in:
2025-07-14 17:26:24 +02:00
parent 2b384aeecc
commit 846a0c49c4
6 changed files with 150 additions and 43 deletions

View File

@@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <dlfcn.h>
static void *(*__real_malloc)(size_t);
static int mode = 0;
static void fin(void) {
if (mode > 0) fprintf(stderr, "intercept: stopped\n");
mode = 0;
}
static void init(void) {
if (mode) return;
mode = -1;
if (((__real_malloc) = dlsym(RTLD_NEXT, "malloc")) == NULL) {
fprintf(stderr, "intercept: unable to load symbol '%s': %s",
"malloc", strerror(errno));
return;
}
atexit(fin);
fprintf(stderr, "intercept: intercepting function calls\n");
mode = 1;
}
void *malloc(size_t size) {
init();
// before call to malloc
void *ret = __real_malloc(size);
// after call to malloc
return ret;
}

View File

@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
extern void *__real_malloc(size_t);
static int mode = 0;
static void fin(void) {
if (mode > 0) fprintf(stderr, "intercept: stopped\n");
mode = 0;
}
static void init(void) {
if (mode) return;
mode = -1;
atexit(fin);
fprintf(stderr, "intercept: intercepting function calls\n");
mode = 1;
}
void *__wrap_malloc(size_t size) {
init();
// before call to malloc
void *ret = __real_malloc(size);
// after call to malloc
return ret;
}

View File

@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <dlfcn.h>
#ifdef INTERCEPT_PRELOAD
#define load(name) \
if (((__real_ ## name) = dlsym(RTLD_NEXT, #name)) == NULL) { \
fprintf(stderr, "intercept: unable to load symbol '%s': %s", \
#name, strerror(errno)); \
return; \
}
#define sym(name) name
#define func_def(ret, name) static ret (*__real_ ## name)
#else
#define sym(name) __wrap_ ## name
#define func_def(ret, name) extern ret __real_ ## name
#endif
func_def(void *, malloc)(size_t);
static int mode = 0;
static void fin(void) {
if (mode > 0) fprintf(stderr, "intercept: stopped\n");
mode = 0;
}
static void init(void) {
if (mode) return;
mode = -1;
#ifdef INTERCEPT_PRELOAD
load(malloc);
#endif
atexit(fin);
fprintf(stderr, "intercept: intercepting function calls\n");
mode = 1;
}
void *sym(malloc)(size_t size) {
init();
// before call to malloc
void *ret = __real_malloc(size);
// after call to malloc
return ret;
}

15
thesis/listings/preload.c Normal file
View File

@@ -0,0 +1,15 @@
#include <stdlib.h>
#include <dlfcn.h>
#include <errno.h>
void *malloc(size_t size) {
// before call to malloc
void *(*_malloc)(size_t);
if ((_malloc = dlsym(RTLD_NEXT, "malloc")) == NULL) {
errno = ENOSYS;
return NULL;
}
void *ret = _malloc(size);
// after call to malloc
return ret;
}

10
thesis/listings/wrap.c Normal file
View File

@@ -0,0 +1,10 @@
#include <stddef.h>
extern void *__real_malloc(size_t size);
void *__wrap_malloc(size_t size) {
// before call to malloc
void *ret = __real_malloc(size);
// after call to malloc
return ret;
}