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

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<mailto:a.busche@xxxxxxxxxx> | www.brunel.de<http://www.brunel.de/> | [cid:image001.png@01CFBB86.C8474740] <https://www.xing.com/company/brunel>  [cid:image002.png@01CFBB86.C8474740] <http://www.linkedin.com/company/brunel/careers>  [cid:image003.png@01CFBB86.C8474740] <https://twitter.com/brunel_de>  [cid:image004.png@01CFBB86.C8474740] <http://www.youtube.com/user/BrunelDeutschland>
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

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

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

Attachment: testing_free.tar
Description: testing_free.tar


Follow-Ups:
AW: memory tests using cmocka"Lars Diehl" <lars_diehl@xxxxxxxxxxx>