[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AW: memory tests using cmocka


Hi André,

 

you wrote:

 

I am currently testing legacy code (which I cannot change for now). The structure is roughly as follows:

·         Malloc’ing some “struct something” in module A

·         Pass the pointer to “struct something” to a function in module B

·         The function in module B conditionally free’s an element / a field of “struct something”

 

This architecture assumes that each module works with the same heap. Depending on your module structure, C-compiler and type of operating system this is not necessarily the case. Therefore memory should always been freed in the same module where it was previously allocated. Depending the type of C-library and selected heap-checking options you will see unpredictable results - such as heap corruption assertions. You might consider providing a “free”-function in module A that can be called from module B. 

 

Regards,
Lars

 

 

Von: André Busche [mailto:a.busche@xxxxxxxxxx] 
Gesendet: Mittwoch, 12. August 2015 11:38
An: cmocka@xxxxxxxxxxxxxx
Betreff: memory tests using cmocka

 

Dear list,

 

I might be blind (while looking through the docs) or dumb; if not, I think I found something strange (which not necessarily needs to be related to cmocka, but to internals of C or my project setup itself) while getting memory tests up and running using cmocka. The basic question is: how do I set up the wrapper for malloc / free / … correctly?

 

I am currently testing legacy code (which I cannot change for now). The structure is roughly as follows:

·         Malloc’ing some “struct something” in module A

·         Pass the pointer to “struct something” to a function in module B

·         The function in module B conditionally free’s an element / a field of “struct something”

 

Find a minimal working example (without using structs but plain pointers) attached.

 

What happens is a glibc error as follows, when module B tries to free the pointer (type in “make”):

*** glibc detected *** ./check: munmap_chunk(): invalid pointer: 0x09f22060 ***

… blabla …

 

I played a bit around …

1)      When *not* using #define malloc(num) _test_malloc(num, __FILE__, __LINE__) (and alike), therefore bypassing the cmocka wrappers for memory management, the test cases work fine, and valgrind does not tell me any memory leaks.

2)      (not shown in the example; but I can send it around as well) When using –Wl,-wrap=malloc (and alike), and defining the __wrap_malloc to return test_malloc(length); (from cmocka), memory leaks are detected successfully by cmocka. However, this removes the feature of getting the source code line number the malloc took place, as it is always the line number of the __wrap_malloc implementation.

 

… how do I set up cmocka to capture memory allocations and free’s correctly across module boundaries? Or is the use case so uncommon that this problem was not observed before?

 

My environment: Linux Debian wheezy, Gcc updated manually to version 4.9.1.

 

Thanks and best,

  André

 

 

 

Mit freundlichen Grüßen / Kind regards, 

Dr. André Busche

Software Designer

                  

a.busche@xxxxxxxxxx |  <http://www.brunel.de/> www.brunel.de |  <https://www.xing.com/company/brunel> cid:image001.png@01CFBB86.C8474740  <http://www.linkedin.com/company/brunel/careers> cid:image002.png@01CFBB86.C8474740  <https://twitter.com/brunel_de> cid:image003.png@01CFBB86.C8474740  <http://www.youtube.com/user/BrunelDeutschland> cid:image004.png@01CFBB86.C8474740
T +49 5121 1760-754 | F +49 5121 1760-999

Brunel GmbH | Bereich Communications | Daimlerring 9 | 31135 Hildesheim


Brunel | access to excellence
experts, projects and know-how

 

 <http://www.brunelsailing.net/> cid:image005.png@01CFBB86.C8474740

 

Hauptsitz: Airport City | Hermann-Köhl-Str. 1 | 28199 Bremen

Registergericht: Bremen HRB 16 935

General Manager: Johan Arie van Barneveld, Markus Eckhardt

 

P Please consider the environment before printing this e-mail

 

PNG image

PNG image

PNG image

PNG image

GIF image


References:
memory tests using cmockaAndré Busche <a.busche@xxxxxxxxxx>