Merge changes I9716568a,I6d121907,I61e47db4

* changes:
  Finish pulling info over from the GYP build files
  Add the various sanitizers from the GYP setup
  Actually use tcmalloc and test for it
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/third_party/googletest/BUILD b/third_party/googletest/BUILD
index a424c49..f342e66 100644
--- a/third_party/googletest/BUILD
+++ b/third_party/googletest/BUILD
@@ -6,6 +6,8 @@
 # build targets here, the libraries are independent of their location in
 # a more straightforward way.
 
+load('/tools/build_rules/empty_main', 'empty_main_if_asan')
+
 licenses(["notice"])
 
 cc_library(
@@ -252,7 +254,7 @@
 
 cc_test(
 	name = "googletest_sample10_test",
-	srcs = ["googletest/samples/sample10_unittest.cc"],
+	srcs = empty_main_if_asan(["googletest/samples/sample10_unittest.cc"]),
 	deps = [
 	    ":googletest_main",
 	    ":googletest_sample_libs",
diff --git a/third_party/libevent/BUILD b/third_party/libevent/BUILD
index ee778f0..0d35bd6 100644
--- a/third_party/libevent/BUILD
+++ b/third_party/libevent/BUILD
@@ -1,6 +1,6 @@
 licenses(['notice'])
 
-load('/tools/build_rules/select', 'compiler_select', 'address_size_select', 'cpu_select')
+load('/tools/build_rules/select', 'compiler_select', 'address_size_select')
 
 cc_library(
   name = 'libevent',
@@ -158,11 +158,6 @@
       '_EVENT_SIZEOF_SIZE_T=8',
       '_EVENT_SIZEOF_VOID_P=8',
     ],
-  }) + cpu_select({
-    'amd64': [
-      '_EVENT_HAVE_TAILQFOREACH=1',
-    ],
-    'roborio': [],
   }),
 
   copts = [
diff --git a/tools/BUILD b/tools/BUILD
index 747b9c1..22ce501 100644
--- a/tools/BUILD
+++ b/tools/BUILD
@@ -2,7 +2,7 @@
 
 exports_files(['test_sharding_compliant'])
 
-# Don't use these directly! Use //tools/build_rules/select.bzl instead.
+# Don't use these directly! Use //tools/build_rules/*.bzl instead.
 config_setting(
   name = 'compiler_clang',
   values = {'compiler': 'clang'}
@@ -19,3 +19,11 @@
   name = 'cpu_roborio',
   values = {'cpu': 'roborio'},
 )
+config_setting(
+  name = 'has_asan',
+  values = {'copt': '-fsanitize=address'},
+)
+config_setting(
+  name = 'has_tsan',
+  values = {'copt': '-fsanitize=thread'},
+)
diff --git a/tools/bazel.rc b/tools/bazel.rc
index 568cc30..0f89dc9 100644
--- a/tools/bazel.rc
+++ b/tools/bazel.rc
@@ -6,6 +6,38 @@
 # Always include debug information in the non-.stripped binaries.
 build --strip=never
 
+# Use the malloc we want.
+build --custom_malloc=//tools/cpp:malloc
+
+build:asan --copt -fsanitize=address
+build:asan --linkopt -fsanitize=address --linkopt -ldl
+build:asan --platform_suffix=-asan
+build:asan --test_env ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer-3.6
+build:asan --test_env ASAN_OPTIONS=detect_leaks=1:check_initialization_order=1:strict_init_order=1:detect_stack_use_after_return=1:detect_odr_violation=2:allow_user_segv_handler=1
+
+build:tsan --copt -fsanitize=thread --copt -DAOS_SANITIZER_thread
+build:tsan --linkopt -fsanitize=thread
+build:tsan --platform_suffix=-tsan
+build:tsan --test_env TSAN_OPTIONS=external_symbolizer_path=/usr/bin/llvm-symbolizer-3.6:detect_deadlocks=1:second_deadlock_stack=1
+
+build:isan --copt -fsanitize=integer
+build:isan --linkopt -fsanitize=integer
+build:isan --platform_suffix=-isan
+build:isan --test_env LLVM_SYMBOLIZER=/usr/bin/llvm-symbolizer-3.6
+
+build:ubsan --copt -fsanitize=undefined --copt -fno-sanitize-recover
+# Bad alignment is just slow on x86 and traps on ARM, so we'll find
+# it other ways, and some x86 code does it on purpose.
+build:ubsan --copt -fno-sanitize=alignment
+build:ubsan --linkopt -fsanitize=undefined
+build:ubsan --platform_suffix=-ubsan
+build:ubsan --test_env LLVM_SYMBOLIZER=/usr/bin/llvm-symbolizer-3.6
+
+build:msan --copt -fsanitize=memory --copt -fsanitize-memory-track-origins
+build:msan --linkopt -fsanitize=memory --ldopt -fsanitize-memory-track-origins
+build:msan --platform_suffix=-msan
+build:msan --test_env MSAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer-3.6
+
 # Show paths to a few more than just 1 target.
 build --show_result 15
 # Dump the output of the failing test to stdout.
diff --git a/tools/build_rules/empty_main.bzl b/tools/build_rules/empty_main.bzl
index 050907c..dbe9120 100644
--- a/tools/build_rules/empty_main.bzl
+++ b/tools/build_rules/empty_main.bzl
@@ -1,5 +1,6 @@
 '''Returns a select which is either srcs or an empty main function.'''
 def empty_main_if_asan(srcs):
   return select({
+    '//tools:has_asan': [ '//tools/cpp:empty_main' ],
     '//conditions:default': srcs,
   })
diff --git a/tools/build_rules/fortran.bzl b/tools/build_rules/fortran.bzl
index 0162244..635e7d7 100644
--- a/tools/build_rules/fortran.bzl
+++ b/tools/build_rules/fortran.bzl
@@ -7,7 +7,7 @@
 
   cmd = toolchain_cflags + ['-c', ctx.file.src.path, '-o', ctx.outputs.pic_o.path]
   filtered_cmd = []
-  # Strip out the C/C++ specific flags.
+  # Strip out the C/C++/Clang specific flags.
   exclude_flags = ['-fcolor-diagnostics',
                    '-Wswitch-enum',
                    '-Wpointer-arith',
@@ -18,7 +18,8 @@
                    '-Werror',
                    '-Wextra',
                    '-Wno-builtin-macro-redefined',
-                   '-D__has_feature(x)=0']
+                   '-D__has_feature(x)=0',
+                   '-fmacro-backtrace-limit=0']
 
   for flag in cmd:
     if flag not in exclude_flags:
diff --git a/tools/cpp/BUILD b/tools/cpp/BUILD
index 9196d44..f6203eb 100644
--- a/tools/cpp/BUILD
+++ b/tools/cpp/BUILD
@@ -1,10 +1,17 @@
 package(default_visibility = ['//visibility:public'])
 
 cc_library(
+  name = 'empty_main',
+  srcs = [ 'empty_main.c' ],
+)
+
+cc_library(
   name = 'malloc',
-  deps = [
-    '//third_party/gperftools:tcmalloc',
-  ],
+  deps = select({
+    '//tools:has_asan': [],
+    '//tools:has_tsan': [],
+    '//conditions:default': ['//third_party/gperftools:tcmalloc'],
+  }),
 )
 
 cc_library(
diff --git a/tools/cpp/CROSSTOOL b/tools/cpp/CROSSTOOL
index 2917076..d159e5d 100644
--- a/tools/cpp/CROSSTOOL
+++ b/tools/cpp/CROSSTOOL
@@ -131,6 +131,9 @@
       flag_group {
         flag: "-DAOS_DEBUG=1"
       }
+      flag_group {
+        flag: "-fno-omit-frame-pointer"
+      }
     }
   }
 
@@ -224,6 +227,8 @@
   # Enable coloring even if there's no attached terminal. Bazel removes the
   # escape sequences if --nocolor is specified.
   compiler_flag: "-fcolor-diagnostics"
+  compiler_flag: "-fmessage-length=80"
+  compiler_flag: "-fmacro-backtrace-limit=0"
 
   compiler_flag: "-Wall"
   compiler_flag: "-Wextra"
@@ -373,6 +378,9 @@
       flag_group {
         flag: "-DAOS_DEBUG=1"
       }
+      flag_group {
+        flag: "-fno-omit-frame-pointer"
+      }
     }
   }
 
@@ -395,15 +403,9 @@
   feature {
     name: "all_modes"
     flag_set {
-      action: "c-compile"
-      flag_group {
-        flag: "-std=gnu99"
-      }
-    }
-    flag_set {
       action: "preprocess-assemble"
       action: "assemble"
-      action: "c++-link"
+      action: "c-compile"
       flag_group {
         flag: "-std=gnu99"
       }
@@ -433,6 +435,10 @@
     }
   }
 
+  compiler_flag: "-mfpu=neon"
+  # TODO(brians): See if it will run with this enabled.
+  #compiler_flag: "-mhwdiv=arm,thumb"
+
   # Anticipated future default.
   # This makes GCC and Clang do what we want when called through symlinks.
   unfiltered_cxx_flag: "-no-canonical-prefixes"
@@ -484,6 +490,7 @@
   compiler_flag: "-Wsign-compare"
   compiler_flag: "-Wformat=2"
   compiler_flag: "-Werror"
+  compiler_flag: "-Wunused-local-typedefs"
 
   # Keep stack frames for debugging, even in opt mode.
   compiler_flag: "-fno-omit-frame-pointer"
diff --git a/tools/cpp/empty_main.c b/tools/cpp/empty_main.c
new file mode 100644
index 0000000..79996de
--- /dev/null
+++ b/tools/cpp/empty_main.c
@@ -0,0 +1,3 @@
+// Used by //tools/build_rules:empty_main.bzl.
+
+int main() {}