Suppress leak check in a specific forked child

703 views Asked by At

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.

1

There are 1 answers

5
phd On

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.