Linux memory leakage detection (2) Customized malloc/free

Source: Internet
Author: User

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_symbolWill 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 calltomallocWill call the function__wrap_mallocInstead. The call__real_mallocIn__wrap_mallocWill call the real "malloc" function.

You may wish to provide__real_mallocFunction as well, so that links without the-wrap option will succeed. If you do this, you should not put the definition__real_mallocIn 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 yousymbolWhen using the wrap function, anysymbolIs actually used__wrap_symbolSymbol

Considering your__wrap_symbolJustsymbolAdding a layer of packaging may still require the realsymbol, Just need your__wrap_symbolCalling__real_symbolBecause any__real_symbolIs actually usedsymbol

In other wordssymbolWhen the symbol uses the wrap function, it will get the following effect:

(1) When you callsymbolIs actually called__wrap_symbol

(2) When you call__real_symbolIs actually calledsymbol

(3) You can setsymbolDuring the packaging operation__wrap_symbolAnd then__wrap_symbolCall__real_wrapIs equivalentsymbolPreviously, 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_freeFunction 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.