Add malloc test support to unit tests.

Currently far from passing and I haven't even tried with a leak checker yet.
Also bn_test is slow.

Change-Id: I4fe2783aa5f7897839ca846062ae7e4a367d2469
Reviewed-on: https://boringssl-review.googlesource.com/4794
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt
index 7430b62..6858cbb 100644
--- a/crypto/CMakeLists.txt
+++ b/crypto/CMakeLists.txt
@@ -207,6 +207,8 @@
   constant_time_test
 
   constant_time_test.c
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(constant_time_test crypto)
@@ -215,6 +217,8 @@
   thread_test
 
   thread_test.c
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(thread_test crypto)
diff --git a/crypto/base64/CMakeLists.txt b/crypto/base64/CMakeLists.txt
index 8bc531a..42037a5 100644
--- a/crypto/base64/CMakeLists.txt
+++ b/crypto/base64/CMakeLists.txt
@@ -12,6 +12,8 @@
   base64_test
 
   base64_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(base64_test crypto)
diff --git a/crypto/bio/CMakeLists.txt b/crypto/bio/CMakeLists.txt
index f4122c4..dbf5951 100644
--- a/crypto/bio/CMakeLists.txt
+++ b/crypto/bio/CMakeLists.txt
@@ -22,6 +22,8 @@
   bio_test
 
   bio_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(bio_test crypto)
diff --git a/crypto/bn/CMakeLists.txt b/crypto/bn/CMakeLists.txt
index 4a33f7e..2e0cb45 100644
--- a/crypto/bn/CMakeLists.txt
+++ b/crypto/bn/CMakeLists.txt
@@ -70,6 +70,8 @@
   bn_test
 
   bn_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(bn_test crypto)
diff --git a/crypto/bytestring/CMakeLists.txt b/crypto/bytestring/CMakeLists.txt
index d1f0441..cbbacf2 100644
--- a/crypto/bytestring/CMakeLists.txt
+++ b/crypto/bytestring/CMakeLists.txt
@@ -14,6 +14,8 @@
   bytestring_test
 
   bytestring_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(bytestring_test crypto)
diff --git a/crypto/dh/CMakeLists.txt b/crypto/dh/CMakeLists.txt
index 9e487d5..d0c1da7 100644
--- a/crypto/dh/CMakeLists.txt
+++ b/crypto/dh/CMakeLists.txt
@@ -16,6 +16,8 @@
   dh_test
 
   dh_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(dh_test crypto)
diff --git a/crypto/digest/CMakeLists.txt b/crypto/digest/CMakeLists.txt
index 8cab46a..816d116 100644
--- a/crypto/digest/CMakeLists.txt
+++ b/crypto/digest/CMakeLists.txt
@@ -13,6 +13,8 @@
   digest_test
 
   digest_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(digest_test crypto)
diff --git a/crypto/dsa/CMakeLists.txt b/crypto/dsa/CMakeLists.txt
index dab2c4c..1bb8b63 100644
--- a/crypto/dsa/CMakeLists.txt
+++ b/crypto/dsa/CMakeLists.txt
@@ -14,6 +14,8 @@
   dsa_test
 
   dsa_test.c
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(dsa_test crypto)
diff --git a/crypto/ec/CMakeLists.txt b/crypto/ec/CMakeLists.txt
index a218c0d..b5ebefa 100644
--- a/crypto/ec/CMakeLists.txt
+++ b/crypto/ec/CMakeLists.txt
@@ -20,12 +20,16 @@
   example_mul
 
   example_mul.c
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 add_executable(
   ec_test
 
   ec_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(example_mul crypto)
diff --git a/crypto/ecdsa/CMakeLists.txt b/crypto/ecdsa/CMakeLists.txt
index c8645d1..f431e59 100644
--- a/crypto/ecdsa/CMakeLists.txt
+++ b/crypto/ecdsa/CMakeLists.txt
@@ -14,6 +14,8 @@
   ecdsa_test
 
   ecdsa_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(ecdsa_test crypto)
diff --git a/crypto/err/CMakeLists.txt b/crypto/err/CMakeLists.txt
index 89f96bd..5215eec 100644
--- a/crypto/err/CMakeLists.txt
+++ b/crypto/err/CMakeLists.txt
@@ -44,6 +44,8 @@
   err_test
 
   err_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(err_test crypto)
diff --git a/crypto/evp/CMakeLists.txt b/crypto/evp/CMakeLists.txt
index 6db9752..5769fa4 100644
--- a/crypto/evp/CMakeLists.txt
+++ b/crypto/evp/CMakeLists.txt
@@ -26,12 +26,15 @@
   evp_extra_test
 
   evp_extra_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 add_executable(
   evp_test
 
   evp_test.cc
+
   $<TARGET_OBJECTS:test_support>
 )
 
@@ -39,6 +42,8 @@
   pbkdf_test
 
   pbkdf_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(evp_extra_test crypto)
diff --git a/crypto/hkdf/CMakeLists.txt b/crypto/hkdf/CMakeLists.txt
index f8dd748..66d680a 100644
--- a/crypto/hkdf/CMakeLists.txt
+++ b/crypto/hkdf/CMakeLists.txt
@@ -12,6 +12,8 @@
   hkdf_test
 
   hkdf_test.c
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(hkdf_test crypto)
diff --git a/crypto/hmac/CMakeLists.txt b/crypto/hmac/CMakeLists.txt
index 1a08c55..11d267f 100644
--- a/crypto/hmac/CMakeLists.txt
+++ b/crypto/hmac/CMakeLists.txt
@@ -13,6 +13,7 @@
   hmac_test
 
   hmac_test.cc
+
   $<TARGET_OBJECTS:test_support>
 )
 
diff --git a/crypto/lhash/CMakeLists.txt b/crypto/lhash/CMakeLists.txt
index 0eaabed..c71b8a1 100644
--- a/crypto/lhash/CMakeLists.txt
+++ b/crypto/lhash/CMakeLists.txt
@@ -12,6 +12,8 @@
   lhash_test
 
   lhash_test.c
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(lhash_test crypto)
diff --git a/crypto/modes/CMakeLists.txt b/crypto/modes/CMakeLists.txt
index d50e97b..ffb29b6 100644
--- a/crypto/modes/CMakeLists.txt
+++ b/crypto/modes/CMakeLists.txt
@@ -58,6 +58,8 @@
   gcm_test
 
   gcm_test.c
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(gcm_test crypto)
diff --git a/crypto/pkcs8/CMakeLists.txt b/crypto/pkcs8/CMakeLists.txt
index c0f2746..4426f1e 100644
--- a/crypto/pkcs8/CMakeLists.txt
+++ b/crypto/pkcs8/CMakeLists.txt
@@ -15,6 +15,8 @@
   pkcs12_test
 
   pkcs12_test.cc
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(pkcs12_test crypto)
diff --git a/crypto/rsa/CMakeLists.txt b/crypto/rsa/CMakeLists.txt
index c438e1d..0ea12c8 100644
--- a/crypto/rsa/CMakeLists.txt
+++ b/crypto/rsa/CMakeLists.txt
@@ -16,6 +16,8 @@
   rsa_test
 
   rsa_test.c
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(rsa_test crypto)
diff --git a/crypto/test/CMakeLists.txt b/crypto/test/CMakeLists.txt
index 0d5ca81..84a6174 100644
--- a/crypto/test/CMakeLists.txt
+++ b/crypto/test/CMakeLists.txt
@@ -4,4 +4,5 @@
   OBJECT
 
   file_test.cc
+  malloc.cc
 )
diff --git a/crypto/test/malloc.cc b/crypto/test/malloc.cc
new file mode 100644
index 0000000..a24f77f
--- /dev/null
+++ b/crypto/test/malloc.cc
@@ -0,0 +1,140 @@
+/* Copyright (c) 2014, Google Inc.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+
+#include <openssl/base.h>
+
+#if defined(__has_feature)
+#if __has_feature(address_sanitizer) || __has_feature(memory_sanitizer)
+#define OPENSSL_ASAN
+#endif
+#endif
+
+// This file isn't built on ARM or Aarch64 because we link statically in those
+// builds and trying to override malloc in a static link doesn't work. It's also
+// disabled on ASan builds as this interferes with ASan's malloc interceptor.
+//
+// TODO(davidben): See if this and ASan's and MSan's interceptors can be made to
+// coexist.
+#if defined(__linux__) && !defined(OPENSSL_ARM) && \
+    !defined(OPENSSL_AARCH64) && !defined(OPENSSL_ASAN)
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <new>
+
+
+/* This file defines overrides for the standard allocation functions that allow
+ * a given allocation to be made to fail for testing. If the program is run
+ * with MALLOC_NUMBER_TO_FAIL set to a base-10 number then that allocation will
+ * return NULL. If MALLOC_ABORT_ON_FAIL is also defined then the allocation
+ * will abort() rather than return NULL.
+ *
+ * This code is not thread safe. */
+
+static uint64_t current_malloc_count = 0;
+static uint64_t malloc_number_to_fail = 0;
+static char failure_enabled = 0, abort_on_fail = 0;
+static int in_call = 0;
+
+extern "C" {
+/* These are other names for the standard allocation functions. */
+extern void *__libc_malloc(size_t size);
+extern void *__libc_calloc(size_t num_elems, size_t size);
+extern void *__libc_realloc(void *ptr, size_t size);
+}
+
+static void exit_handler(void) {
+  if (failure_enabled && current_malloc_count > malloc_number_to_fail) {
+    _exit(88);
+  }
+}
+
+static void cpp_new_handler() {
+  // Return to try again. It won't fail a second time.
+  return;
+}
+
+/* should_fail_allocation returns true if the current allocation should fail. */
+static int should_fail_allocation() {
+  static int init = 0;
+  char should_fail;
+
+  if (in_call) {
+    return 0;
+  }
+
+  in_call = 1;
+
+  if (!init) {
+    const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
+    if (env != NULL && env[0] != 0) {
+      char *endptr;
+      malloc_number_to_fail = strtoull(env, &endptr, 10);
+      if (*endptr == 0) {
+        failure_enabled = 1;
+        atexit(exit_handler);
+        std::set_new_handler(cpp_new_handler);
+      }
+    }
+    abort_on_fail = (NULL != getenv("MALLOC_ABORT_ON_FAIL"));
+    init = 1;
+  }
+
+  in_call = 0;
+
+  if (!failure_enabled) {
+    return 0;
+  }
+
+  should_fail = (current_malloc_count == malloc_number_to_fail);
+  current_malloc_count++;
+
+  if (should_fail && abort_on_fail) {
+    abort();
+  }
+  return should_fail;
+}
+
+extern "C" {
+
+void *malloc(size_t size) {
+  if (should_fail_allocation()) {
+    return NULL;
+  }
+
+  return __libc_malloc(size);
+}
+
+void *calloc(size_t num_elems, size_t size) {
+  if (should_fail_allocation()) {
+    return NULL;
+  }
+
+  return __libc_calloc(num_elems, size);
+}
+
+void *realloc(void *ptr, size_t size) {
+  if (should_fail_allocation()) {
+    return NULL;
+  }
+
+  return __libc_realloc(ptr, size);
+}
+
+}  // extern "C"
+
+#endif  /* defined(linux) && !ARM && !AARCH64 && !ASAN */
diff --git a/crypto/x509/CMakeLists.txt b/crypto/x509/CMakeLists.txt
index 96cf35c..3bb5704 100644
--- a/crypto/x509/CMakeLists.txt
+++ b/crypto/x509/CMakeLists.txt
@@ -59,6 +59,8 @@
   pkcs7_test
 
   pkcs7_test.c
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(pkcs7_test crypto)
diff --git a/crypto/x509v3/CMakeLists.txt b/crypto/x509v3/CMakeLists.txt
index ffa5a4a..c7e6054 100644
--- a/crypto/x509v3/CMakeLists.txt
+++ b/crypto/x509v3/CMakeLists.txt
@@ -47,6 +47,8 @@
   v3name_test
 
   v3nametest.c
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(v3name_test crypto)
@@ -55,6 +57,8 @@
   tab_test
 
   tabtest.c
+
+  $<TARGET_OBJECTS:test_support>
 )
 
 target_link_libraries(tab_test crypto)