← Back to context

Comment by 1718627440

3 months ago

    $ cat test.c
    void main (void) {
      malloc (1000);
    }
    
    $ make test
    cc     test.c   -o test
    
    $ valgrind --leak-check=full --show-leak-kinds=all -s ./test
    Memcheck, a memory error detector
    Command: ./test
    
    HEAP SUMMARY:
        in use at exit: 1,000 bytes in 1 blocks
      total heap usage: 1 allocs, 0 frees, 1,000 bytes allocated
    
    1,000 bytes in 1 blocks are still reachable in loss record 1 of 1
       at 0x483877F: malloc (vg_replace_malloc.c:307)
       by 0x109142: main (in test.c:2)
    
    LEAK SUMMARY:
       definitely lost: 0 bytes in 0 blocks
       indirectly lost: 0 bytes in 0 blocks
         possibly lost: 0 bytes in 0 blocks
       still reachable: 1,000 bytes in 1 blocks
            suppressed: 0 bytes in 0 blocks

> vg_replace_malloc.c:307

What do you think that is? Valgrind tracks allocations by providing other implementations for malloc/free/... .

Are you trying to explain to me how Valgrind works? If you do know more than me then please join us and become a Valgrind developer.

Mostly it wraps system calls and library calls. Wrapping means that it does some checking or recording before and maybe after the call. Very occasionally it needs to modify the arguments to the call. The rest of the time it passes the arguments on to the kernel or libc/libpthread/C++ lib.

There are also functions and syscalls that it needs to replace. That needs to be a fully functional replacement, not just looking the same as in mocking.

I don’t have any exact figures. The number of syscalls varies quite a lot by platform and on most platforms there are many obsolete syscalls that are not implemented. At a rough guess, I’d say there are something like 300 syscalls and 100 lib calls that are handled of which 3/4 are wrapped and 1/4 are replaced.

  • > Are you trying to explain to me how Valgrind works?

    Sorry that wasn't my intention. You are a Valgrind developer? Thanks, it's a good project.

    It seems like I have a different understanding of mocking than other people in the thread and it shows. My understanding was, that Valgrind provides function replacements via dynamic linking, that then call into the real libc. I would call that mocking, but YMMV.