Actually use tcmalloc and test for it

Change-Id: I61e47db48fca960b77eafaf4c8bda4b4d9517617
diff --git a/build_tests/BUILD b/build_tests/BUILD
index cae87f0..cce2e01 100644
--- a/build_tests/BUILD
+++ b/build_tests/BUILD
@@ -51,3 +51,21 @@
   ],
   size = 'small',
 )
+
+cc_binary(
+  name = 'tcmalloc_build_test_binary',
+  srcs = [
+    'tcmalloc.cc',
+  ],
+)
+
+sh_test(
+  name = 'tcmalloc_build_test',
+  srcs = [
+    'tcmalloc_test.sh',
+  ],
+  data = [
+    ':tcmalloc_build_test_binary',
+  ],
+  size = 'small',
+)
diff --git a/build_tests/tcmalloc.cc b/build_tests/tcmalloc.cc
new file mode 100644
index 0000000..323ee58
--- /dev/null
+++ b/build_tests/tcmalloc.cc
@@ -0,0 +1,3 @@
+#include <stdlib.h>
+
+int main() { malloc(500000); }
diff --git a/build_tests/tcmalloc_test.sh b/build_tests/tcmalloc_test.sh
new file mode 100755
index 0000000..03c3bac
--- /dev/null
+++ b/build_tests/tcmalloc_test.sh
@@ -0,0 +1,17 @@
+set -e
+set -u
+
+OUTPUT=$(TCMALLOC_LARGE_ALLOC_REPORT_THRESHOLD=10 build_tests/tcmalloc_build_test_binary 2>&1)
+
+if [[ -z "${OUTPUT}" ]]; then
+  echo 'Empty output!' >&2
+  exit 1
+fi
+
+PATTERN='tcmalloc: large alloc [0-9]+ bytes =='
+
+if [[ ! "${OUTPUT}" =~ ${PATTERN} ]]; then
+  echo 'Unexpected output:' >&2
+  echo "${OUTPUT}" >&2
+  exit 1
+fi
diff --git a/tools/bazel.rc b/tools/bazel.rc
index 568cc30..7b16af4 100644
--- a/tools/bazel.rc
+++ b/tools/bazel.rc
@@ -6,6 +6,9 @@
 # Always include debug information in the non-.stripped binaries.
 build --strip=never
 
+# Use the malloc we want.
+build --custom_malloc=//tools/cpp:malloc
+
 # Show paths to a few more than just 1 target.
 build --show_result 15
 # Dump the output of the failing test to stdout.