Add the various sanitizers from the GYP setup

Everything doesn't build and many tests don't pass, but the basics are
there.

Change-Id: I6d1219077318d9e168a81d9c3f620a8f77c97c96
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 7b16af4..f07bba7 100644
--- a/tools/bazel.rc
+++ b/tools/bazel.rc
@@ -9,6 +9,32 @@
 # 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
+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
+build:msan --linkopt -fsanitize=memory
+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/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/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() {}