Skip to content

Commit 1cf55b6

Browse files
committedFeb 22, 2021
use .so with LD_PRELOAD to greatly simplify usage
1 parent 46c5a9a commit 1cf55b6

File tree

6 files changed

+53
-47
lines changed

6 files changed

+53
-47
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.o
22
*.d
3+
*.so
34
examples/examples
45
.gdb_history

‎.vscode/launch.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"program": "${workspaceFolder}/examples/examples",
99
"stopAtEntry": true,
1010
"cwd": "${workspaceFolder}/examples",
11-
"environment": [],
11+
"environment": [
12+
{"name":"LD_PRELOAD", "value":"../visualize-c-memory/visualize-c-memory.so"},
13+
],
1214
"externalConsole": false,
1315
"linux": {
1416
"MIMode": "gdb",

‎examples/Makefile

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
CC = gcc
33
CFLAGS = -Wall -g
44

5-
# This allows to wrap malloc/free/... (see malloc-wrapper.c) so that we can track heap allocations
6-
LDFLAGS = -Wl,--wrap=malloc -Wl,--wrap=free
7-
8-
OBJS = examples.o ../visualize-c-memory/malloc-wrapper.o
5+
LIB = ../visualize-c-memory
6+
OBJS = examples.o
97
EXEC = examples
108

11-
$(EXEC): $(OBJS)
9+
$(EXEC): $(OBJS) $(LIB)/visualize-c-memory.so
1210
$(CC) $(OBJS) -o $(EXEC) $(LDFLAGS)
1311

12+
$(LIB)/visualize-c-memory.so:
13+
$(MAKE) -C $(LIB) visualize-c-memory.so
14+
1415
clean:
1516
rm -f $(OBJS) $(EXEC)

‎visualize-c-memory/Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# options
2+
CC = gcc
3+
CFLAGS = -Wall -g -fpic
4+
LDFLAGS = -shared
5+
6+
OBJS = visualize-c-memory.o
7+
LIB = visualize-c-memory.so
8+
9+
$(LIB): $(OBJS)
10+
$(CC) $(OBJS) -o $(LIB) $(LDFLAGS)
11+
12+
clean:
13+
rm -f $(OBJS) $(LIB)
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
#include <string.h>
22

3-
// malloc/free wrapper
4-
// It simply calls the real malloc/free while recording all
5-
// currenly allocated memory in the heap_contents linked list.
3+
// This file should be either linked with a C program directly, or
4+
// compiled into a shared library and loaded via LD_PRELOAD.
5+
//
6+
// It overrides malloc/realloc/free, calling the real ones while recording
7+
// all currenly allocated memory in the heap_contents linked list.
68

7-
void* __real_malloc(size_t);
8-
void __real_free(void*);
9-
10-
// use weak symbols, to make wrapping of the following functions optional
11-
void* __attribute__((weak)) __real_calloc(size_t, size_t);
12-
void* __attribute__((weak)) __real_realloc(void*, size_t);
13-
char* __attribute__((weak)) __real_strdup(const char*);
14-
char* __attribute__((weak)) __real_strndup(const char*, size_t);
9+
extern void* __libc_malloc(size_t);
10+
extern void* __libc_realloc(void*, size_t);
11+
extern void* __libc_calloc(size_t, size_t);
12+
extern void* __libc_free(void*);
1513

1614

1715
// list management //////////////////////////////////////////////////////////////
@@ -36,7 +34,7 @@ static void insert_or_update_pointer(void* pointer, int size, void* old_pointer)
3634
}
3735
}
3836

39-
heap_node* node = __real_malloc(sizeof(*node));
37+
heap_node* node = __libc_malloc(sizeof(*node));
4038
node->pointer = pointer;
4139
node->size = size;
4240
node->next = NULL;
@@ -49,7 +47,7 @@ static void remove_pointer(void* pointer) {
4947
heap_node* node = prev->next;
5048
if(node != NULL && node->pointer == pointer) {
5149
prev->next = node->next;
52-
__real_free(node);
50+
__libc_free(node);
5351
break;
5452
}
5553
}
@@ -58,39 +56,29 @@ static void remove_pointer(void* pointer) {
5856

5957
// wrappers ////////////////
6058

61-
void* __wrap_malloc(size_t size) {
62-
void* pointer = __real_malloc(size);
63-
insert_or_update_pointer(pointer, size, NULL);
64-
return pointer;
65-
}
59+
void* malloc(size_t size) {
60+
void* pointer = __libc_malloc(size);
61+
62+
// libc seems to allocte 1024 bytes on start for its own use. Ignore it.
63+
if(!(heap_contents.next == NULL && size == 1024))
64+
insert_or_update_pointer(pointer, size, NULL);
6665

67-
void* __wrap_calloc(size_t n, size_t size) {
68-
void* pointer = __real_calloc(n, size);
69-
insert_or_update_pointer(pointer, n * size, NULL);
7066
return pointer;
7167
}
7268

73-
void* __wrap_realloc(void* old_pointer, size_t size) {
74-
void* pointer = __real_realloc(old_pointer, size);
69+
void* realloc(void* old_pointer, size_t size) {
70+
void* pointer = __libc_realloc(old_pointer, size);
7571
insert_or_update_pointer(pointer, size, old_pointer);
7672
return pointer;
7773
}
7874

79-
void __wrap_free(void* pointer) {
80-
remove_pointer(pointer);
81-
__real_free(pointer);
82-
}
83-
84-
char* __wrap_strdup(const char* s) {
85-
char* new_s = __real_strdup(s);
86-
insert_or_update_pointer(new_s, strlen(new_s) + 1, NULL);
87-
return new_s;
75+
void* calloc(size_t n, size_t size) {
76+
void* pointer = __libc_calloc(n, size);
77+
insert_or_update_pointer(pointer, n * size, NULL);
78+
return pointer;
8879
}
8980

90-
char* __wrap_strndup(const char* s, size_t n) {
91-
char* new_s = __real_strndup(s, n);
92-
insert_or_update_pointer(new_s, strlen(new_s) + 1, NULL);
93-
return new_s;
81+
void free(void* pointer) {
82+
remove_pointer(pointer);
83+
__libc_free(pointer);
9484
}
95-
96-

‎visualize-c-memory/visualize-c-memory.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def visualize_memory():
3636
# display errors using the text visualizer
3737
return json.dumps({
3838
'kind': { 'text': True },
39-
'text': str(e) + "\n\n" + traceback.format_exc()
39+
'text': str(e) + "\n\n\n\n\n\n\n" + traceback.format_exc()
4040
})
4141

4242
def svg_of_memory():
@@ -240,8 +240,9 @@ def rec_of_heap():
240240
except:
241241
raise Exception(
242242
"Heap information not found.\n"
243-
"You need to link your program with malloc-wrapper.c\n"
244-
"and pass -Wl,--wrap=malloc -Wl,--wrap=free to the linker.\n"
243+
"You need to load visualize-c-memory.so by setting the environment variable\n"
244+
" LD_PRELOAD=<path-to>/visualize-c-memory.so\n"
245+
"_or_ link your program with visualize-c-memory.c"
245246
)
246247

247248
while int(heap_node_ptr) != 0:

0 commit comments

Comments
 (0)
Please sign in to comment.