tcmalloc: add run-time CHECK to smoke-test allocator overrides

Original CL:

- https://codereview.chromium.org/1577883002

  Add run-time CHECK to smoke-test allocator overrides

  As part of the refactoring work on the allocator, it emerged that it
  would be nice to have a test that ensures that we don't accidentally
  break things along the way and not detected that.
  This CL adds a runtime CHECK() that verifies that:
   - On Windows (non-component build) the shim layer has been
     initialized. Concretely this checks that chrome called the shimmed
     _heap_init() method and not the one from libcmt.
   - On Linux desktop, the malloc symbols are being overridden by
     tcmalloc (only when USE_TCMALLOC is defined).

  The rationale of this test is: if _heap_init was shimmed there are
  very good chances that malloc (& friends) were shimmed as well.
  Likewise on Linux for mallopt() <-> malloc().

  BUG=564618
  Committed: https://crrev.com/be9d6fce01061afde845fd7aa2335e5757d6a421
  Cr-Commit-Position: refs/heads/master@{#369007}

BUG=724399,b:70905156

Change-Id: I047ac19bb71346fbf601825d335bd97729b9f688
Reviewed-on: https://chromium-review.googlesource.com/1130793
Commit-Queue: Gabriel Marin <gmx@chromium.org>
Reviewed-by: Will Harris <wfh@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#585633}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 748aa79fbcb880ab33b2dcaef0c0a6274a934789
diff --git a/src/gperftools/tcmalloc.h b/src/gperftools/tcmalloc.h
index 78b49d5..91b32ae 100644
--- a/src/gperftools/tcmalloc.h
+++ b/src/gperftools/tcmalloc.h
@@ -68,6 +68,12 @@
 
 #endif
 
+// When passed to mallopt() as first argument causes it to return
+// TC_MALLOPT_IS_OVERRIDDEN_BY_TCMALLOC. Used to detect the sanity of the
+// overriding mechanisms at runtime.
+#define TC_MALLOPT_IS_OVERRIDDEN_BY_TCMALLOC 0xbeef42
+
+// Annoying stuff for windows -- makes sure clients can import these functions
 #ifndef PERFTOOLS_DLL_DECL
 #ifdef _WIN32
 #define PERFTOOLS_DLL_DECL __declspec(dllimport)
diff --git a/src/tcmalloc.cc b/src/tcmalloc.cc
index 92a55e5..6c5bd8b 100644
--- a/src/tcmalloc.cc
+++ b/src/tcmalloc.cc
@@ -1658,7 +1658,13 @@
 }
 
 inline int do_mallopt(int cmd, int value) {
-  return 1;     // Indicates error
+  if (cmd == TC_MALLOPT_IS_OVERRIDDEN_BY_TCMALLOC)
+    return TC_MALLOPT_IS_OVERRIDDEN_BY_TCMALLOC;
+
+  // 1 is the success return value according to man mallopt(). However (see the
+  // BUGS section in the manpage), most implementations return always 1.
+  // This code is just complying with that (buggy) expectation.
+  return 1;
 }
 
 #ifdef HAVE_STRUCT_MALLINFO