Enable/disable crypto-mdebug just like other features

Also always abort() on leak failure.

Reviewed-by: Stephen Henson <steve@openssl.org>
diff --git a/test/clienthellotest.c b/test/clienthellotest.c
index 289ca1e..9cb563c 100644
--- a/test/clienthellotest.c
+++ b/test/clienthellotest.c
@@ -211,7 +211,7 @@
     ERR_remove_thread_state(NULL);
     EVP_cleanup();
     CRYPTO_cleanup_all_ex_data();
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks(err);
 #endif
     BIO_free(err);
diff --git a/test/danetest.c b/test/danetest.c
index 02d0f79..09156a6 100644
--- a/test/danetest.c
+++ b/test/danetest.c
@@ -526,7 +526,7 @@
     ERR_free_strings();
     ERR_remove_thread_state(NULL);
     EVP_cleanup();
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks(bio_err);
 #endif
     BIO_free(bio_err);
diff --git a/test/dsatest.c b/test/dsatest.c
index 81c36cb..8190e6f 100644
--- a/test/dsatest.c
+++ b/test/dsatest.c
@@ -215,7 +215,7 @@
     CRYPTO_cleanup_all_ex_data();
     ERR_remove_thread_state(NULL);
     ERR_free_strings();
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks(bio_err);
 #endif
     BIO_free(bio_err);
diff --git a/test/ecdhtest.c b/test/ecdhtest.c
index c299c32..8be4e50 100644
--- a/test/ecdhtest.c
+++ b/test/ecdhtest.c
@@ -516,7 +516,7 @@
     BIO_free(out);
     CRYPTO_cleanup_all_ex_data();
     ERR_remove_thread_state(NULL);
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks_fp(stderr);
 #endif
     EXIT(ret);
diff --git a/test/ecdsatest.c b/test/ecdsatest.c
index 2e85d5e..5a27a38 100644
--- a/test/ecdsatest.c
+++ b/test/ecdsatest.c
@@ -545,7 +545,7 @@
     CRYPTO_cleanup_all_ex_data();
     ERR_remove_thread_state(NULL);
     ERR_free_strings();
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks(out);
 #endif
     BIO_free(out);
diff --git a/test/ectest.c b/test/ectest.c
index 9667772..fbf5081 100644
--- a/test/ectest.c
+++ b/test/ectest.c
@@ -1664,7 +1664,7 @@
     CRYPTO_cleanup_all_ex_data();
     ERR_free_strings();
     ERR_remove_thread_state(NULL);
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks_fp(stderr);
 #endif
 
diff --git a/test/enginetest.c b/test/enginetest.c
index f5924e0..32df0e4 100644
--- a/test/enginetest.c
+++ b/test/enginetest.c
@@ -249,7 +249,7 @@
     CRYPTO_cleanup_all_ex_data();
     ERR_free_strings();
     ERR_remove_thread_state(NULL);
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks_fp(stderr);
 #endif
     return to_return;
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c
index 0d6ad03..13dd262 100644
--- a/test/evp_extra_test.c
+++ b/test/evp_extra_test.c
@@ -466,7 +466,7 @@
     CRYPTO_cleanup_all_ex_data();
     ERR_remove_thread_state(NULL);
     ERR_free_strings();
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks_fp(stderr);
 #endif
 
diff --git a/test/evp_test.c b/test/evp_test.c
index 91caa4f..c8d1f60 100644
--- a/test/evp_test.c
+++ b/test/evp_test.c
@@ -610,7 +610,7 @@
     CRYPTO_cleanup_all_ex_data();
     ERR_remove_thread_state(NULL);
     ERR_free_strings();
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks_fp(stderr);
 #endif
     if (t.errors)
diff --git a/test/exptest.c b/test/exptest.c
index 53286d8..5fdcc38 100644
--- a/test/exptest.c
+++ b/test/exptest.c
@@ -300,7 +300,7 @@
     BN_free(m);
     BN_CTX_free(ctx);
     ERR_remove_thread_state(NULL);
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks(out);
 #endif
     BIO_free(out);
diff --git a/test/memleaktest.c b/test/memleaktest.c
index cccbcf6..cdb61a3 100644
--- a/test/memleaktest.c
+++ b/test/memleaktest.c
@@ -56,12 +56,23 @@
 #include <string.h>
 #include <openssl/bio.h>
 #include <openssl/crypto.h>
+#include <setjmp.h>
+
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
+static sigjmp_buf env;
+
+static void handler(int sig)
+{
+    siglongjmp(env, 1);
+}
+#endif
 
 int main(int argc, char **argv)
 {
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     char *p;
     char *lost;
+    int aborted = 0;
 
     p = getenv("OPENSSL_DEBUG_MEMORY");
     if (p != NULL && strcmp(p, "on") == 0)
@@ -74,15 +85,19 @@
         return 1;
     }
 
-    if (argv[1] && strcmp(argv[1], "freeit") == 0)
-        OPENSSL_free(lost);
+    signal(SIGABRT, handler);
 
-    CRYPTO_mem_leaks_fp(stderr);
-    return 0;
+    if (argv[1] && strcmp(argv[1], "freeit") == 0) {
+        OPENSSL_free(lost);
+        lost = NULL;
+    }
+
+    if (sigsetjmp(env, 0) == 0)
+        CRYPTO_mem_leaks_fp(stderr);
+    else
+        aborted = 1;
+    return ((lost != NULL) ^ (aborted == 1));
 #else
-    if (argv[1] && strcmp(argv[1], "freeit") == 0)
-        return 0;
-    fprintf(stderr, "Leak simulated\n");
-    return 1;
+    return 0;
 #endif
 }
diff --git a/test/p5_crpt2_test.c b/test/p5_crpt2_test.c
index 657d10e..303906f 100644
--- a/test/p5_crpt2_test.c
+++ b/test/p5_crpt2_test.c
@@ -205,7 +205,7 @@
     CRYPTO_cleanup_all_ex_data();
     ERR_remove_thread_state(NULL);
     ERR_free_strings();
-# ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks_fp(stderr);
 # endif
     return 0;
diff --git a/test/recipes/90-test_memleak.t b/test/recipes/90-test_memleak.t
index 7aff0c0..bc72f2e 100644
--- a/test/recipes/90-test_memleak.t
+++ b/test/recipes/90-test_memleak.t
@@ -3,5 +3,6 @@
 use OpenSSL::Test;
 
 setup("test_memleak");
-plan tests => 1;
-ok(!run(test(["memleaktest"])), "running memleaktest");
+plan tests => 2;
+ok(run(test(["memleaktest"])), "running leak test");
+ok(run(test(["memleaktest", "freeit"])), "running no leak test");
diff --git a/test/rsa_test.c b/test/rsa_test.c
index 3cfd32e..0b707bf 100644
--- a/test/rsa_test.c
+++ b/test/rsa_test.c
@@ -327,7 +327,7 @@
     CRYPTO_cleanup_all_ex_data();
     ERR_remove_thread_state(NULL);
 
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks_fp(stderr);
 #endif
 
diff --git a/test/srptest.c b/test/srptest.c
index 03e8cf4..f6555a6 100644
--- a/test/srptest.c
+++ b/test/srptest.c
@@ -146,7 +146,7 @@
     CRYPTO_cleanup_all_ex_data();
     ERR_remove_thread_state(NULL);
     ERR_free_strings();
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks(bio_err);
 #endif
     BIO_free(bio_err);
diff --git a/test/ssltest.c b/test/ssltest.c
index c3d655e..d6b6618 100644
--- a/test/ssltest.c
+++ b/test/ssltest.c
@@ -1821,7 +1821,7 @@
     ERR_free_strings();
     ERR_remove_thread_state(NULL);
     EVP_cleanup();
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks(bio_err);
 #endif
     BIO_free(bio_err);
diff --git a/test/verify_extra_test.c b/test/verify_extra_test.c
index 005fae6..8432520 100644
--- a/test/verify_extra_test.c
+++ b/test/verify_extra_test.c
@@ -207,7 +207,7 @@
     CRYPTO_cleanup_all_ex_data();
     ERR_remove_thread_state(NULL);
     ERR_free_strings();
-#ifdef CRYPTO_MDEBUG
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
     CRYPTO_mem_leaks_fp(stderr);
 #endif