In this project, I do some research about the virtual memory, process address space, the malloc GNU C library. Then modify the malloc source code inside glibc to add some feature allowing me to track memory leak.
The my_malloc.c file is a small example to allocate & deallocate memory to simulate the actual running program with malloc & free. We can use Makefile (for static linking) or use-glibc.sh (for dynamic linking) to compile and link to our specific glibc.
You can choose a version of glibc & learn how to install them as a second library for your system. Here I choose the library glibc-2.22
git clone git://sourceware.org/git/glibc.git
cd glibc
git checkout glibc-2.22
Place glibc source code at /usr/local/src/glibc-2.22/
cd /usr/local/src
mkdir glibc-build -p && cd glibc-build
Config the install directory as /usr/local/lib/glibc-install/, if you choose the install directory as /usr, the new glibc will override the glibc of your system and that's not a good idea!
../glibc-2.22/configure --prefix=/usr/local/lib/glibc-install/ --enable-add-ons --with-tls --disable-werror
Build the source code
make -j $(nproc)
Make install to the specific directory
make install -j $(nproc)
Now you have the secondary library in your system and you can build an application with linking to your new glibc.
First of all, we do some research about data structure inside the glibc malloc, such as Arena, Heap and Chunk. And the malloc source code, it's actually a wrapper function of internal malloc
The main arena and thread arena (with single heap segment) look like: