Bug 182042

Summary: valgrind silently ignores VALGRIND_OPTS env var if environment is not propagated to the process
Product: [Developer tools] valgrind Reporter: Nuno Lopes <nunoplopes>
Component: generalAssignee: Julian Seward <jseward>
Status: RESOLVED NOT A BUG    
Severity: normal CC: greened, njn, pjfloyd
Priority: NOR    
Version First Reported In: unspecified   
Target Milestone: wanted3.6.0   
Platform: Compiled Sources   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:
Attachments: test case
output of the tests

Description Nuno Lopes 2009-01-27 00:05:54 UTC
Version:           3.4.0 (using Devel)
OS:                Linux
Installed from:    Compiled sources

valgrind ignores the VALGRIND_OPTS environment var if executing a new process without the environment var set.
test case and output follow soon.
Comment 1 Nuno Lopes 2009-01-27 00:06:52 UTC
Created attachment 30649 [details]
test case

compile with gcc -o vg-test vg-test.c
Comment 2 Nuno Lopes 2009-01-27 00:08:39 UTC
Created attachment 30650 [details]
output of the tests

output of the tests with and without using the VALGRIND_OPTS env var. note that I expected both outputs to be similar.
Comment 3 David Greene 2010-02-17 23:33:48 UTC
I also see this problem in 3.5.0
Comment 4 David Greene 2010-02-17 23:52:05 UTC
(In reply to comment #3)
> I also see this problem in 3.5.0

Ah, my mistake.  It appears to work in 3.5.0.
Comment 5 Paul Floyd 2023-01-26 07:34:17 UTC
I don't see how Valgrind can miraculously recover envp from the child when you pass it a null environment.

You need to progagate the parent envp to the child like this:

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

int main(int argc, char **argv, char **envp)
{
	char *args[] = {"vg-test", "child", NULL};

	// main process
	if (argc == 1) {
		execve("./vg-test", args, envp);
	} else {
		(void)malloc(3); // leak in the child
	}

	return 0;
}

This gives me

valgrind --trace-children=yes --leak-check=full ./vg-test
==36462== Memcheck, a memory error detector
==36462== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==36462== Using Valgrind-3.21.0.GIT and LibVEX; rerun with -h for copyright info
==36462== Command: ./vg-test
==36462== 
==36462== Memcheck, a memory error detector
==36462== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==36462== Using Valgrind-3.21.0.GIT and LibVEX; rerun with -h for copyright info
==36462== Command: ./vg-test child
==36462== 
==36462== 
==36462== HEAP SUMMARY:
==36462==     in use at exit: 3 bytes in 1 blocks
==36462==   total heap usage: 1 allocs, 0 frees, 3 bytes allocated
==36462== 
==36462== 3 bytes in 1 blocks are definitely lost in loss record 1 of 1
==36462==    at 0x484CBC4: malloc (in /usr/local/libexec/valgrind/vgpreload_memcheck-amd64-freebsd.so)
==36462==    by 0x20196D: main (vg-test.c:12)
==36462== 
==36462== LEAK SUMMARY:
==36462==    definitely lost: 3 bytes in 1 blocks
==36462==    indirectly lost: 0 bytes in 0 blocks
==36462==      possibly lost: 0 bytes in 0 blocks
==36462==    still reachable: 0 bytes in 0 blocks
==36462==         suppressed: 0 bytes in 0 blocks
==36462== 
==36462== For lists of detected and suppressed errors, rerun with: -s
==36462== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)