Valgrind and QEMU - Unable to detect memory leak

早过忘川 提交于 2020-02-02 16:01:22

问题


I want to test my C++ code for memory leaks with Valgrind (memcheck) x86.

But the software gets cross-compiled and is running on ARM.

In order to do some automated testing I decided to emulate my ARM hardware via QEMU.

And I also decided to use the cpputest unit test ARM binaries to ensure a deterministic behaviour and search for memory leaks within the scope the unit test covers.

All in all, I have an ARM binary which should be emulated via QEMU user mode.

My call looks like that:

./valgrind --smc-check=all qemu-arm-static -L ... arm-ptest-binary

My C++ code looks like that. It has a memory leak of 20 byte and the valgrind call do not find this leak when using it with QEMU. After I insert a memory allocation and no freeing mechanism I'd have expected an memory leak

int test_func ()
{
  int *foo;
  foo = new int [5];
  printf("test_func called!\n");
  return 1;
}

Valgrind output:

==19300== HEAP SUMMARY:
==19300==     in use at exit: 1,103,129 bytes in 2,316 blocks
==19300==   total heap usage: 4,259 allocs, 1,943 frees, 1,866,916 bytes allocated
==19300== 
==19300== LEAK SUMMARY:
==19300==    definitely lost: 0 bytes in 0 blocks
==19300==    indirectly lost: 0 bytes in 0 blocks
==19300==      possibly lost: 304 bytes in 1 blocks
==19300==    still reachable: 1,102,825 bytes in 2,315 blocks
==19300==         suppressed: 0 bytes in 0 blocks
[...]

When I run this program on ARM hardware the valgrind-arm finds the leak with the exact same binary.

Does anyone of you have an idea why Valgrind does not find the memory leak in combination with QEMU user mode?

Thanks in advance


回答1:


You are running Valgrind on QEMU itself, which will cause valgrind to report memory leaks in QEMU's own code, but valgrind does not have sufficient visibility into what the guest program running under QEMU is doing to be able to report leaks in the guest. In particular, Valgrind works by intercepting calls to malloc, free, operator new, etc -- it will be doing this for the host QEMU process's (x86) allocation and free calls, but has no way to intercept the (arm) calls your guest process makes.

You might look at running an entire guest OS under QEMU's system emulation mode, and then running the Arm Valgrind inside that on your guest program.



来源:https://stackoverflow.com/questions/55161252/valgrind-and-qemu-unable-to-detect-memory-leak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!