Commit 40b01b815c for openssl.org

commit 40b01b815cbfd13292d29bff9a833100bc309524
Author: Neil Horman <nhorman@openssl.org>
Date:   Wed Jan 7 15:48:29 2026 -0500

    Add a method to run our test suite under valgrind

    As part of our effort to provide a supression file for valgrind that we
    can maintain, we should have the ability to run our tests under
    valgrind.

    Add an environment variable OSSL_USE_VALGRIND to prefix all our app and
    test executions with the valgrind tool so that we can run it
    automatically

    Fixes openssl/project#1801

    Reviewed-by: Saša NedvÄ›dický <sashan@openssl.org>
    Reviewed-by: Norbert Pocs <norbertp@openssl.org>
    MergeDate: Thu Jan 29 16:37:26 2026
    (Merged from https://github.com/openssl/openssl/pull/29573)

diff --git a/test/README.md b/test/README.md
index 746a0156ce..71faaac9d7 100644
--- a/test/README.md
+++ b/test/README.md
@@ -184,3 +184,19 @@ To randomise the test ordering:
 To run the tests using the order defined by the random seed `42`:

     $ make OPENSSL_TEST_RAND_ORDER=42 test
+
+Running Tests under Valgrind
+----------------------------
+
+Normally, testing for memory leaks is accomplished by building Openssl with the
+enable-asan option, which links the library with the compiler asan library.  However
+Some people prefer to use valgrind to do dynamic instrumentation for memory leak checking.
+OpenSSL also offers a suppression file to suppress reachable memory leaks, that are often
+inappropriately considered to be true leaks.  In order to maintain and test this
+suppression file, OpenSSL tests can be run under valgrind automatically.
+
+To run the test suite under valgrind:
+
+    $ make OSSL_USE_VALGRIND=yes test
+
+Doing so will create valgrind.log file for each test under the test-runs subdirectory.
diff --git a/util/perl/OpenSSL/Test.pm b/util/perl/OpenSSL/Test.pm
index a0396499a7..bc470ace0f 100644
--- a/util/perl/OpenSSL/Test.pm
+++ b/util/perl/OpenSSL/Test.pm
@@ -328,8 +328,13 @@ sub app {
     return sub {
         my @cmdargs = ( @{$cmd} );
         my @prog = __fixup_prg(__apps_file(shift @cmdargs, __exeext()));
-        return cmd([ @prog, @cmdargs ],
-                   exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
+        if ($ENV{OSSL_USE_VALGRIND} eq "yes") {
+            return cmd([ "valgrind", "--leak-check=full", "--show-leak-kinds=all", "--gen-suppressions=all", "--log-file=./valgrind.log", @prog, @cmdargs ],
+                       exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
+        } else {
+            return cmd([ @prog, @cmdargs ],
+                       exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
+        }
     }
 }

@@ -350,8 +355,13 @@ sub test {
     return sub {
         my @cmdargs = ( @{$cmd} );
         my @prog = __fixup_prg(__test_file(shift @cmdargs, __exeext()));
-        return cmd([ @prog, @cmdargs ],
+        if ($ENV{OSSL_USE_VALGRIND} eq "yes") {
+           return cmd([ "valgrind", "--leak-check=full", "--show-leak-kinds=all", "--gen-suppressions=all", "--log-file=./valgrind.log", @prog, @cmdargs ],
+                   exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
+        } else {
+            return cmd([ @prog, @cmdargs ],
                    exe_shell => $ENV{EXE_SHELL}, %opts) -> (shift);
+        }
     }
 }