Linux memory leakage detection (2) Customized malloc/free
The simplest method for detecting memory leaks in linux is introduced. Although this method is simple, there are many practical problems, as a result, it cannot be used in actual production.
It is unrealistic to directly use this method, because:
(1) change all the calls to malloc/free in the project to my_malloc/my_free. The code is greatly changed.
(2) The Code of the dynamic and static libraries is usually not permitted to be modified.
Today, we will solve this problem and dynamically decide whether to allow the program to use its own system memory management interface.
Wrap options
If you do not want to modify the product code, the interface for applying for/releasing memory is malloc/free.
To add the counting function to the interface, we need to implement another interface for applying for/releasing memory. The new interface cannot be renamed with malloc/free. This is too contradictory.
If you can customize a malloc/free file by yourself.
Fortunately, GCC also thought about this and provided us with the wrap option.
Here is a description of man ld:
-Wrap = symbol
Use a wrapper function for symbol. Any undefined reference to symbol will be resolved__wrap_symbol
. Any undefined reference__real_symbol
Will be resolved to symbol.
This can be used to provide a wrapper for a system function. The wrapper function shocould be called__wrap_symbol
. If it wishes to call the system function, it shocould call__real_symbol
.
Here is a trivial example:
>
void * __wrap_malloc (size_t c){ printf ("malloc called with %zu\n", c); return __real_malloc (c);}
If you link other code with this file using-wrap malloc, then all calltomalloc
Will call the function__wrap_malloc
Instead. The call__real_malloc
In__wrap_malloc
Will call the real "malloc" function.
You may wish to provide__real_malloc
Function as well, so that links without the-wrap option will succeed. If you do this, you should not put the definition__real_malloc
In the same file__wrap_malloc
; If you do, the handler er may resolve the call before the linker has a chance to wrap itmalloc
.
I will explain this story in English (if you are good at English, you can skip it ):
Wrapper is the meaning of packaging in English, that is, adding a customized packaging layer on the outside of an unmodifiable symbol (usually a system symbol, in this way, we can reuse the original code and add new functions.
When yousymbol
When using the wrap function, anysymbol
Is actually used__wrap_symbol
Symbol
Considering your__wrap_symbol
Justsymbol
Adding a layer of packaging may still require the realsymbol
, Just need your__wrap_symbol
Calling__real_symbol
Because any__real_symbol
Is actually usedsymbol
In other wordssymbol
When the symbol uses the wrap function, it will get the following effect:
(1) When you callsymbol
Is actually called__wrap_symbol
(2) When you call__real_symbol
Is actually calledsymbol
(3) You can setsymbol
During the packaging operation__wrap_symbol
And then__wrap_symbol
Call__real_wrap
Is equivalentsymbol
Previously, we made our own custom additional functions.
Customize your own malloc/free
It seems that this wrap function meets our needs. Let's take a look at how to use it.
(1) wrap can be used for both variable and function symbols, but now we only need the function symbols. Specifically, they are malloc and free.
(2) This is an option that plays a role in the Link process.-Wl,--wrap,malloc -Wl,--wrap,free
(3)__wrap_malloc/__wrap_free
Function implementation
void * __wrap_malloc(int size){ malloc_count++; return __real_malloc(size);}void __wrap_free(void *ptr){ free_count++; __real_free(ptr);}
(4) test
int main(){ malloc_count = 0; free_count = 0; int *p1 = (int *)malloc(sizeof(int)); int *p2 = (int *)malloc(sizeof(int)); free( p1); if(malloc_count != free_count) printf("memory leak!\n"); return 0;}
(5) Run
gcc -o test main.c -Wl,--wrap,malloc -Wl,--wrap,free
Analysis advantages
(1) Easy to use-you only need to modify the compilation option without modifying the product code.
(2) full range-wrap is a link option that works for all files linked through _ wrap_malloc and _ wrap_free, both static and dynamic libraries.
Disadvantages
(1) This method requires that the printed analysis generated during running be known at the end of running.
(2) Applicable only to C language and not to C ++
(3) You can only check for leaks without specific information, such as how much space is leaked.
(4) It cannot be specified which line of code caused the leakage.
Improvement
The detection method has been initially improved, but cannot meet this requirement. we can predict the next improvement and check the next decomposition.