forked from danluu/malloc-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.c
43 lines (35 loc) · 1.29 KB
/
wrapper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Wrapper does LD_PRELOAD of our malloc.
// Using this because if we LD_PRELOAD our buggy malloc, gdb segfaults
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv) {
// Ccheck that we have at least one arg.
if (argc == 1) {
printf("You must supply a program to be invoked to use your replacement malloc() script.\n");
printf("...you may use any program, even system programs, such as `ls`.\n");
printf("\n");
printf("Example: %s /bin/ls\n", argv[0]);
return 1;
}
/*
* Set up the environment to pre-load our 'malloc.so' shared library, which
* will replace the malloc(), calloc(), realloc(), and free() that is defined
* by standard libc.
*/
char **env = malloc(2 * sizeof(char *));
env[0] = malloc(100 * sizeof(char));
sprintf(env[0], "LD_PRELOAD=./malloc.so");
env[1] = NULL;
/*
* Replace the current running process with the process specified by the command
* line options. If exec() fails, we won't even try and recover as there's likely
* nothing we could really do; however, we do our best to provide useful output
* with a call to perror().
*/
execve(argv[1], argv + 1, env); /* Note that exec() will not return on success. */
perror("exec() failed");
free(env[0]);
free(env);
return 2;
}