Add test_wrap.c
This commit is contained in:
1
proj/test1/.gitignore
vendored
1
proj/test1/.gitignore
vendored
@@ -1 +1,2 @@
|
||||
/main
|
||||
/main_wrapped
|
||||
|
||||
@@ -5,13 +5,16 @@ LDFLAGS=-lc
|
||||
|
||||
.PHONY: all clean
|
||||
all: default
|
||||
default: main test.so
|
||||
default: main main_wrapped test_preload.so
|
||||
|
||||
test.so: src
|
||||
$(CC) -shared -fPIC -o $@ $< $(CFLAGS) $(LDFLAGS)
|
||||
test_preload.so: src/test_preload.c
|
||||
$(CC) -shared -fPIC -o $@ $^ $(CFLAGS) $(LDFLAGS)
|
||||
|
||||
main: src
|
||||
$(CC) -o $@ $< $(CFLAGS) $(LDFLAGS)
|
||||
main: src/main.c
|
||||
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
|
||||
|
||||
main_wrapped: src/main.c src/test_wrap.c
|
||||
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) -Wl,--wrap=malloc -Wl,--wrap=free -Wl,--wrap=calloc -Wl,--wrap=realloc -Wl,--wrap=getopt
|
||||
|
||||
clean:
|
||||
rm -rf main test.so
|
||||
rm -rf main test_preload.so main_wrapped
|
||||
|
||||
@@ -37,7 +37,7 @@ static void log_array_str(FILE *out, char *const array[], int n) {
|
||||
|
||||
void *malloc(size_t size) {
|
||||
fprintf(stderr, PREFIX "malloc(%li)\n", size);
|
||||
void *(* _malloc)(size_t);
|
||||
void *(*_malloc)(size_t);
|
||||
if ((_malloc = dlsym(RTLD_NEXT, "malloc")) == NULL) {
|
||||
errno = ENOSYS;
|
||||
return NULL;
|
||||
@@ -49,7 +49,7 @@ void *malloc(size_t size) {
|
||||
|
||||
void *calloc(size_t nmemb, size_t size) {
|
||||
fprintf(stderr, PREFIX "calloc(%li, %li)\n", nmemb, size);
|
||||
void *(* _calloc)(size_t, size_t);
|
||||
void *(*_calloc)(size_t, size_t);
|
||||
if ((_calloc = dlsym(RTLD_NEXT, "calloc")) == NULL) {
|
||||
errno = ENOSYS;
|
||||
return NULL;
|
||||
@@ -61,7 +61,7 @@ void *calloc(size_t nmemb, size_t size) {
|
||||
|
||||
void *realloc(void *ptr, size_t size) {
|
||||
fprintf(stderr, PREFIX "realloc(%p, %li)\n", ptr, size);
|
||||
void *(* _realloc)(void *, size_t);
|
||||
void *(*_realloc)(void *, size_t);
|
||||
if ((_realloc = dlsym(RTLD_NEXT, "realloc")) == NULL) {
|
||||
errno = ENOSYS;
|
||||
return NULL;
|
||||
@@ -73,7 +73,7 @@ void *realloc(void *ptr, size_t size) {
|
||||
|
||||
void free(void *ptr) {
|
||||
fprintf(stderr, PREFIX "free(%p)\n", ptr);
|
||||
void (* _free)(void *);
|
||||
void (*_free)(void *);
|
||||
if ((_free = dlsym(RTLD_NEXT, "free")) == NULL) {
|
||||
errno = ENOSYS;
|
||||
return;
|
||||
@@ -89,7 +89,7 @@ int getopt(const int argc, char *const argv[], const char *shortopts) {
|
||||
log_str(stderr, shortopts);
|
||||
fputs(")\n", stderr);
|
||||
|
||||
int (* _getopt)(int, char *const[], const char *);
|
||||
int (*_getopt)(int, char *const[], const char *);
|
||||
if ((_getopt = dlsym(RTLD_NEXT, "getopt")) == NULL) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
90
proj/test1/src/test_wrap.c
Normal file
90
proj/test1/src/test_wrap.c
Normal file
@@ -0,0 +1,90 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define PREFIX "---====[ "
|
||||
|
||||
static void log_str(FILE *out, const char *str) {
|
||||
fprintf(out, "%p:\"", str);
|
||||
for (char ch; (ch = *str) != 0; str++) {
|
||||
if (ch == '\\' || ch == '"') {
|
||||
fputc('\\', out);
|
||||
fputc(ch, out);
|
||||
} else if (ch == '\t') {
|
||||
fputs("\\t", out);
|
||||
} else if (ch == '\n') {
|
||||
fputs("\\n", out);
|
||||
} else if (ch == '\r') {
|
||||
fputs("\\r", out);
|
||||
} else if ((ch >= 0 && ch < 0x20) || ch == 0x7F) {
|
||||
fprintf(out, "\\x%02x", ch);
|
||||
} else {
|
||||
fputc(ch, out);
|
||||
}
|
||||
}
|
||||
fputc('"', out);
|
||||
}
|
||||
|
||||
static void log_array_str(FILE *out, char *const array[], int n) {
|
||||
fprintf(out, "%p:[", (void *)array);
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (i > 0) fputs(", ", stderr);
|
||||
log_str(stderr, array[i]);
|
||||
}
|
||||
fputc(']', out);
|
||||
}
|
||||
|
||||
extern void *__real_malloc(size_t size);
|
||||
|
||||
void *__wrap_malloc(size_t size) {
|
||||
fprintf(stderr, PREFIX "malloc(%li)\n", size);
|
||||
void *ret = __real_malloc(size);
|
||||
fprintf(stderr, PREFIX "-> %p\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
extern void *__real_calloc(size_t nmemb, size_t size);
|
||||
|
||||
void *__wrap_calloc(size_t nmemb, size_t size) {
|
||||
fprintf(stderr, PREFIX "calloc(%li, %li)\n", nmemb, size);
|
||||
void *ret = __real_calloc(nmemb, size);
|
||||
fprintf(stderr, PREFIX "-> %p\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
extern void *__real_realloc(void *ptr, size_t size);
|
||||
|
||||
void *__wrap_realloc(void *ptr, size_t size) {
|
||||
fprintf(stderr, PREFIX "realloc(%p, %li)\n", ptr, size);
|
||||
void *ret = __real_realloc(ptr, size);
|
||||
fprintf(stderr, PREFIX "-> %p\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
extern void __real_free(void *ptr);
|
||||
|
||||
void __wrap_free(void *ptr) {
|
||||
fprintf(stderr, PREFIX "free(%p)\n", ptr);
|
||||
__real_free(ptr);
|
||||
fprintf(stderr, PREFIX "-> void\n");
|
||||
}
|
||||
|
||||
extern int __real_getopt(int argc, char *const argv[], const char *shortopts);
|
||||
|
||||
int __wrap_getopt(const int argc, char *const argv[], const char *shortopts) {
|
||||
fprintf(stderr, PREFIX "getopt(%i, ", argc);
|
||||
log_array_str(stderr, argv, argc);
|
||||
fputs(", ", stderr);
|
||||
log_str(stderr, shortopts);
|
||||
fputs(")\n", stderr);
|
||||
|
||||
int ret = __real_getopt(argc, argv, shortopts);
|
||||
fprintf(stderr, PREFIX "-> %i", ret);
|
||||
if (ret >= 0x20 && ret < 0x7F) {
|
||||
fprintf(stderr, " ('%c')\n", ret);
|
||||
} else {
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user