-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloc_wrap.c
83 lines (75 loc) · 2.57 KB
/
alloc_wrap.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc-wrap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyork <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/18 22:04:36 by kyork #+# #+# */
/* Updated: 2016/10/17 14:55:05 by kyork ### ########.fr */
/* */
/* ************************************************************************** */
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
/*
** This library is neither thread-safe nor fork-safe.
**
** g_alloc_count provides a counter of the number of live allocations.
** You could check its value before and after a function call, for example.
** TODO: provide a way to turn off the atexit() print
**
** extern int g_alloc_count;
**
** g_malloc_inject is used to cause malloc() to return NULL. When it is set to
** 0, the next allocation will return NULL and g_malloc_inject will be set to
** -1. When it is greater than 0, malloc will behave normally and the counter
** will be decremented.
**
** extern int g_malloc_inject;
**
** It would be very stupid to turn this file in with your work, especially if
** it is included in the default compile configuration.
*/
int g_alloc_count = 0;
int g_malloc_inject = -1;
static int g_have_registered = 0;
static void print_alloc_count(void)
{
if (g_alloc_count != 0)
fprintf(stderr, "FAIL: %d mallocs not freed\n", g_alloc_count);
else
fprintf(stderr, "[OK]: all allocations freed\n");
}
void *malloc(size_t sz)
{
void *(*libc_malloc)(size_t);
void *addr_alloc;
if (!g_have_registered)
{
atexit(print_alloc_count);
g_have_registered = 1;
}
if (g_malloc_inject >= 0)
if (g_malloc_inject-- == 0)
{
fprintf(stderr, "malloc(%ld) = INJECT\n", sz);
return (0);
}
libc_malloc = dlsym(RTLD_NEXT, "malloc");
addr_alloc = libc_malloc(sz);
if (addr_alloc)
g_alloc_count++;
fprintf(stderr, "malloc(%ld) = %p\n", sz, addr_alloc);
return (addr_alloc);
}
void free(void *p)
{
void (*libc_free)(void*);
libc_free = dlsym(RTLD_NEXT, "free");
fprintf(stderr, "free(%p)\n", p);
libc_free(p);
if (p)
g_alloc_count--;
}