1

Often it's not possible to free all memory when exiting from an utility forked child process. I want to suppress memory leak detection for these in a library.

I don't want to burden developers using the library with --show-leak-kinds=all to have to look at leak reports from a child process where nobody can really do anything about them.

I'm aware that --child-silent-after-fork=yes on the command line can be used to suppress reports for all forked processes, but this might also suppress reports from the main application and requires the users to remember to always specify this option.

A very common case where this is a problem is in code that uses fork/exec when an error path is triggered that needs to terminate the child process without executing the target executable (maybe some setup failed or maybe the executable was not executable after all). Other use cases might include helper processes that are longer lived (like the dmix process from alsa before the days of pulseaudio or similar code).

As this should be developer friendly i'm looking for solutions that can be implemented in a library without the need to manually add parameters to the valgrind invocation. Possible solutions might include using client requests, monitor commands using client requests or clever coding.

Minimal example:

// build with cc example.c -o example
// valgrind --leak-check=full --show-leak-kinds=all ./example

#include <stdlib.h>
#include <unistd.h>

void something_that_forks() {
    pid_t pid = fork();
    if (pid == 0) {
        _exit(0);
    }
}

int main(int argc, char* argv[]) {
    void *some_allocation = malloc(1000);
    something_that_forks();
    free(some_allocation);
    return 0;
}

I imagine something_that_forks is in some library. I'm looking for a way to modify this so that valgrind does not report leaks in the child process. The library of course does not know anything about the allocations in the main program by other components.

textshell
  • 1,746
  • 14
  • 21

1 Answers1

1

Currently (valgrind 3.15), the only way to load a suppression file is to specify it on the command line.

It however looks easy to add a monitor command to load a new suppression file. This monitor command can then be used interactively (using vgdb) and/or using the client request VALGRIND_MONITOR_COMMAND. With this, the code in a child process or in a library can load a suppression file specific to this library or process.

For example, to suppress all leaks after fork, you could then call the following just before _exit(0) :

VALGRIND_MONITOR_COMMAND("load_suppression a_specific_file_for_this_process_or_library.supp");

where the given suppression file contains a suppression entry for leaks matching all stack traces.

See patch attached to https://bugs.kde.org/show_bug.cgi?id=411134 This patch allows to change various command line options after startup, including the options telling if/how to do a leak search.

The patch was pushed to the Valgrind git repository as 3a803036.

phd
  • 3,669
  • 1
  • 11
  • 12
  • I don't think this is even possible with the current capabilities of suppression files. I don't think there is a way to create a blanket suppression that only is active in a specific forked child process (I'm not even sure if a suppression take effect after the allocation is already finished). On the other hand there are already bugs asking for a way for a library to auto load specific suppression files. But maybe non yet that suggests doing this via a monitor command. – textshell Aug 03 '19 at 20:52
  • @textshell Effectively, valgrind currently lacks the feature. The idea is to have a supp file that will only be loaded in a specific child process, by having the code in this child process using a client request to execute a (to be developed) monitor command load_suppression: VALGRIND_MONITOR_COMMAND("load_suppression a_specific_file_for_this_process_or_library.supp"); – phd Aug 04 '19 at 10:08
  • I've added a simple example to the question. Assuming valgrind could load a supp file by monitor command, what contents of the supp file would then work? – textshell Aug 04 '19 at 11:53
  • If you want to have no leaks reported by the child process when it calls _exit (0), you could do just before _exit(0): VALGRIND_MONITOR_COMMAND("load_suppression suppress_all_leaks.supp"); where suppress_all_leaks.supp contains a suppression entry matching all stack traces. The stack matching part could be something like fun:* – phd Aug 04 '19 at 14:51
  • See patch attached to https://bugs.kde.org/show_bug.cgi?id=411134 This patch allows to change various command line options after startup, including the options telling if/how to do a leak search. – phd Aug 21 '19 at 12:46