Merge "Build sanitizer libraries"
diff --git a/Android.bp b/Android.bp
index 6caaa40..dc6496c 100644
--- a/Android.bp
+++ b/Android.bp
@@ -53,9 +53,6 @@
     },
 }
 
-// Don't build compiler-rt without clang
-// ANDROIDMK TRANSLATION ERROR: unsupported directive
-// ifneq ($(WITHOUT_TARGET_CLANG), true)
 //=====================================================================
 // Device and Host Static Library: libcompiler_rt
 //=====================================================================
@@ -409,8 +406,30 @@
     stl: "none",
 }
 
-// Build asan, lsan, etc.
-// ANDROIDMK TRANSLATION ERROR: unsupported include
-// include $(call all-makefiles-under,$(LOCAL_PATH)/lib)
-// ANDROIDMK TRANSLATION ERROR: unsupported directive
-// endif
+cc_defaults {
+    name: "asan_arch_defaults",
+
+    enabled: false,
+    target: {
+        android_arm: {
+            enabled: true,
+        },
+        android_arm64: {
+            enabled: true,
+        },
+        android_x86: {
+            enabled: true,
+        },
+        linux: {
+            enabled: true,
+        },
+    },
+}
+
+subdirs=[
+    "lib/asan",
+    //"lib/interception",
+    "lib/sanitizer_common",
+    //"lib/tsan",
+    "lib/ubsan",
+]
diff --git a/lib/asan/Android.bp b/lib/asan/Android.bp
new file mode 100644
index 0000000..66aefac
--- /dev/null
+++ b/lib/asan/Android.bp
@@ -0,0 +1,300 @@
+//
+// Copyright (C) 2012 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//
+
+ASAN_NEEDS_SEGV = "0"
+ASAN_HAS_EXCEPTIONS = "1"
+ASAN_FLEXIBLE_MAPPING_AND_OFFSET = "0"
+
+asan_rtl_files = [
+    "asan_activation.cc",
+    "asan_allocator.cc",
+    "asan_fake_stack.cc",
+    "asan_flags.cc",
+    "asan_globals.cc",
+    "asan_interceptors.cc",
+    "asan_linux.cc",
+    "asan_mac.cc",
+    "asan_malloc_linux.cc",
+    "asan_malloc_mac.cc",
+    "asan_malloc_win.cc",
+    "asan_poisoning.cc",
+    "asan_posix.cc",
+    "asan_report.cc",
+    "asan_rtl.cc",
+    "asan_stack.cc",
+    "asan_stats.cc",
+    "asan_suppressions.cc",
+    "asan_thread.cc",
+    "asan_win.cc",
+    "../interception/interception_linux.cc",
+    "../lsan/lsan_common.cc",
+    "../lsan/lsan_common_linux.cc",
+    "../sanitizer_common/sanitizer_allocator.cc",
+    "../sanitizer_common/sanitizer_common.cc",
+    "../sanitizer_common/sanitizer_common_libcdep.cc",
+    "../sanitizer_common/sanitizer_coverage_libcdep.cc",
+    "../sanitizer_common/sanitizer_coverage_mapping_libcdep.cc",
+    "../sanitizer_common/sanitizer_deadlock_detector1.cc",
+    "../sanitizer_common/sanitizer_deadlock_detector2.cc",
+    "../sanitizer_common/sanitizer_flags.cc",
+    "../sanitizer_common/sanitizer_flag_parser.cc",
+    "../sanitizer_common/sanitizer_libc.cc",
+    "../sanitizer_common/sanitizer_libignore.cc",
+    "../sanitizer_common/sanitizer_linux.cc",
+    "../sanitizer_common/sanitizer_linux_libcdep.cc",
+    "../sanitizer_common/sanitizer_mac.cc",
+    "../sanitizer_common/sanitizer_persistent_allocator.cc",
+    "../sanitizer_common/sanitizer_platform_limits_linux.cc",
+    "../sanitizer_common/sanitizer_platform_limits_posix.cc",
+    "../sanitizer_common/sanitizer_posix.cc",
+    "../sanitizer_common/sanitizer_posix_libcdep.cc",
+    "../sanitizer_common/sanitizer_printf.cc",
+    "../sanitizer_common/sanitizer_procmaps_common.cc",
+    "../sanitizer_common/sanitizer_procmaps_freebsd.cc",
+    "../sanitizer_common/sanitizer_procmaps_linux.cc",
+    "../sanitizer_common/sanitizer_procmaps_mac.cc",
+    "../sanitizer_common/sanitizer_stackdepot.cc",
+    "../sanitizer_common/sanitizer_stacktrace.cc",
+    "../sanitizer_common/sanitizer_stacktrace_libcdep.cc",
+    "../sanitizer_common/sanitizer_stacktrace_printer.cc",
+    "../sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc",
+    "../sanitizer_common/sanitizer_suppressions.cc",
+    "../sanitizer_common/sanitizer_symbolizer.cc",
+    "../sanitizer_common/sanitizer_symbolizer_libbacktrace.cc",
+    "../sanitizer_common/sanitizer_symbolizer_libcdep.cc",
+    "../sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc",
+    "../sanitizer_common/sanitizer_symbolizer_process_libcdep.cc",
+    "../sanitizer_common/sanitizer_symbolizer_win.cc",
+    "../sanitizer_common/sanitizer_thread_registry.cc",
+    "../sanitizer_common/sanitizer_tls_get_addr.cc",
+    "../sanitizer_common/sanitizer_unwind_posix_libcdep.cc",
+    "../sanitizer_common/sanitizer_win.cc",
+]
+
+asan_rtl_cxx_files = ["asan_new_delete.cc"]
+
+asan_rtl_cflags = [
+    "-fvisibility=hidden",
+    "-fno-exceptions",
+    "-DASAN_LOW_MEMORY=1",
+    "-DASAN_NEEDS_SEGV=" + ASAN_NEEDS_SEGV,
+    "-DASAN_HAS_EXCEPTIONS=" + ASAN_HAS_EXCEPTIONS,
+    "-DASAN_FLEXIBLE_MAPPING_AND_OFFSET=" + ASAN_FLEXIBLE_MAPPING_AND_OFFSET,
+    "-Wno-covered-switch-default",
+    "-Wno-non-virtual-dtor",
+    "-Wno-sign-compare",
+    "-Wno-unused-parameter",
+    "-std=c++11",
+    "-fno-rtti",
+    "-fno-builtin",
+]
+
+cc_library_static {
+    name: "libasan",
+    //host_supported: true,
+    defaults: ["asan_arch_defaults"],
+    include_dirs: [
+        "external/compiler-rt/lib",
+        "external/compiler-rt/include",
+    ],
+    cflags: asan_rtl_cflags,
+    target: {
+        android: {
+            srcs: ["asan_preinit.cc"],
+        },
+        host: {
+            srcs: asan_rtl_files,
+            whole_static_libs: ["libubsan"],
+        },
+    },
+
+    clang: true,
+    sdk_version: "19",
+    stl: "none",
+
+    sanitize: {
+        never: true,
+    },
+}
+
+cc_library_host_static {
+    name: "libasan_cxx",
+    include_dirs: [
+        "external/compiler-rt/lib",
+        "external/compiler-rt/include",
+    ],
+    cflags: asan_rtl_cflags,
+    srcs: asan_rtl_cxx_files,
+
+    clang: true,
+    compile_multilib: "both",
+
+    sanitize: {
+        never: true,
+    },
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+}
+
+cc_defaults {
+    name: "libclang_rt_defaults",
+
+    // TODO:
+    // This library must go on /system partition, even in SANITIZE_TARGET mode (when all libraries are
+    // installed on /data). That's because /data may not be available until vold does some magic and
+    // vold itself depends on this library.
+
+    arch: {
+        arm: {
+            instruction_set: "arm",
+        },
+    },
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+    include_dirs: [
+        "external/compiler-rt/lib",
+        "external/compiler-rt/include",
+    ],
+    cflags: asan_rtl_cflags,
+    srcs: asan_rtl_files + asan_rtl_cxx_files,
+    static_libs: ["libubsan"],
+    clang: true,
+    sanitize: {
+        never: true,
+    },
+    sdk_version: "19",
+    stl: "none",
+}
+
+/*cc_library_shared {
+    name: "libclang_rt.asan-arm-android",
+    defaults: ["libclang_rt_defaults"],
+    enabled: false,
+    arch: {
+        arm: {
+            enabled: true,
+        },
+    },
+}*/
+
+cc_binary {
+    name: "asanwrapper",
+    defaults: ["asan_arch_defaults"],
+
+    srcs: ["asanwrapper.cc"],
+
+    cppflags: ["-std=c++11"],
+    sanitize: {
+        never: true,
+    },
+
+    stl: "libc++",
+}
+
+// ANDROIDMK TRANSLATION ERROR: unsupported conditional
+// ifneq (true,$(SKIP_LLVM_TESTS))
+cc_library_static {
+    name: "libasan_noinst_test",
+    host_supported: true,
+    defaults: ["asan_arch_defaults"],
+
+    tags: ["tests"],
+    include_dirs: [
+        "external/gtest/include",
+        "external/compiler-rt/include",
+        "external/compiler-rt/lib",
+        "external/compiler-rt/lib/asan/tests",
+        "external/compiler-rt/lib/sanitizer_common/tests",
+    ],
+    cflags: [
+        "-Wno-non-virtual-dtor",
+        "-Wno-unused-parameter",
+        "-Wno-sign-compare",
+        "-DASAN_UAR=0",
+        "-DASAN_HAS_BLACKLIST=1",
+        "-DASAN_HAS_EXCEPTIONS=" + ASAN_HAS_EXCEPTIONS,
+        "-DASAN_NEEDS_SEGV=" + ASAN_NEEDS_SEGV,
+        "-std=c++11",
+    ],
+    srcs: [
+        "tests/asan_noinst_test.cc",
+        "tests/asan_test_main.cc",
+    ],
+
+    clang: true,
+    sanitize: {
+        never: true,
+    },
+
+    stl: "libc++",
+}
+
+cc_test {
+    name: "asan_test",
+    //host_supported: true,
+    defaults: ["asan_arch_defaults"],
+
+    tags: ["tests"],
+    include_dirs: [
+        "external/compiler-rt/lib",
+        "external/compiler-rt/lib/asan/tests",
+        "external/compiler-rt/lib/sanitizer_common/tests",
+    ],
+    cflags: [
+        "-DASAN_LOW_MEMORY=1",
+        "-DASAN_UAR=0",
+        "-DASAN_NEEDS_SEGV=" + ASAN_NEEDS_SEGV,
+        "-DASAN_HAS_EXCEPTIONS=" + ASAN_HAS_EXCEPTIONS,
+        "-DASAN_HAS_BLACKLIST=1",
+        "-Wno-covered-switch-default",
+        "-Wno-non-virtual-dtor",
+        "-Wno-sign-compare",
+        "-Wno-unused-parameter",
+        "-std=c++11",
+    ],
+
+    srcs: [
+        "tests/asan_globals_test.cc",
+        "tests/asan_test.cc",
+        //"tests/asan_noinst_test.cc",
+        //"tests/asan_test_main.cc",
+    ],
+
+    static_libs: ["libasan_noinst_test"],
+    sanitize: {
+        address: true,
+        blacklist: "tests/asan_test.ignore",
+    },
+    clang: true,
+
+    stl: "libc++",
+    target: {
+        host: {
+            host_ldlibs: ["-lrt"],
+        },
+    },
+}
+
+// ANDROIDMK TRANSLATION ERROR: endif from unsupported contitional
+// endif
+// SKIP_LLVM_TESTS
diff --git a/lib/sanitizer_common/Android.bp b/lib/sanitizer_common/Android.bp
new file mode 100644
index 0000000..0348d67
--- /dev/null
+++ b/lib/sanitizer_common/Android.bp
@@ -0,0 +1,96 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//
+
+//###############################################################################
+// Host modules
+
+cc_library_host_static {
+    name: "libsan",
+
+    include_dirs: ["external/compiler-rt/lib"],
+    cppflags: [
+        "-fvisibility=hidden",
+        "-fno-exceptions",
+        "-std=c++11",
+        "-Wall",
+        "-Werror",
+        "-Wno-unused-parameter",
+    ],
+    srcs: [
+        // rtl
+        "sanitizer_allocator.cc",
+        "sanitizer_common.cc",
+        "sanitizer_deadlock_detector1.cc",
+        "sanitizer_deadlock_detector2.cc",
+        "sanitizer_flags.cc",
+        "sanitizer_flag_parser.cc",
+        "sanitizer_libc.cc",
+        "sanitizer_libignore.cc",
+        "sanitizer_linux.cc",
+        "sanitizer_mac.cc",
+        "sanitizer_persistent_allocator.cc",
+        "sanitizer_platform_limits_linux.cc",
+        "sanitizer_platform_limits_posix.cc",
+        "sanitizer_posix.cc",
+        "sanitizer_printf.cc",
+        "sanitizer_procmaps_common.cc",
+        "sanitizer_procmaps_freebsd.cc",
+        "sanitizer_procmaps_linux.cc",
+        "sanitizer_procmaps_mac.cc",
+        "sanitizer_stackdepot.cc",
+        "sanitizer_stacktrace.cc",
+        "sanitizer_stacktrace_printer.cc",
+        "sanitizer_suppressions.cc",
+        "sanitizer_symbolizer.cc",
+        "sanitizer_symbolizer_libbacktrace.cc",
+        "sanitizer_symbolizer_win.cc",
+        "sanitizer_tls_get_addr.cc",
+        "sanitizer_thread_registry.cc",
+        "sanitizer_win.cc",
+
+        // cdep
+        "sanitizer_common_libcdep.cc",
+        "sanitizer_coverage_libcdep.cc",
+        "sanitizer_coverage_mapping_libcdep.cc",
+        "sanitizer_linux_libcdep.cc",
+        "sanitizer_posix_libcdep.cc",
+        "sanitizer_stacktrace_libcdep.cc",
+        "sanitizer_stoptheworld_linux_libcdep.cc",
+        "sanitizer_symbolizer_libcdep.cc",
+        "sanitizer_symbolizer_posix_libcdep.cc",
+        "sanitizer_unwind_linux_libcdep.cc",
+    ],
+    stl: "none",
+    sanitize: {
+        never: true,
+    },
+    compile_multilib: "both",
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+}
+
+subdirs = ["tests"]
+
+// ANDROIDMK TRANSLATION ERROR: unsupported conditional
+// ifndef SANITIZE_HOST
+// ANDROIDMK TRANSLATION ERROR: unsupported include
+// include $(LOCAL_PATH)/tests/Android.mk
+// ANDROIDMK TRANSLATION ERROR: endif from unsupported contitional
+// endif
diff --git a/lib/sanitizer_common/tests/Android.bp b/lib/sanitizer_common/tests/Android.bp
new file mode 100644
index 0000000..4547e6a
--- /dev/null
+++ b/lib/sanitizer_common/tests/Android.bp
@@ -0,0 +1,107 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//
+
+cc_defaults {
+    name: "san_test_defaults",
+
+    cppflags: [
+        "-fvisibility=hidden",
+        "-fno-exceptions",
+        "-fno-rtti",
+        "-std=c++11",
+        "-Wall",
+        "-Werror",
+        "-Wno-unused-parameter",
+        "-Wno-non-virtual-dtor",
+    ],
+
+    include_dirs: ["external/compiler-rt/lib"],
+}
+
+
+cc_test_host {
+    name: "san_test",
+    defaults: ["san_test_defaults"],
+
+    srcs: [
+        "sanitizer_allocator_test.cc",
+        "sanitizer_atomic_test.cc",
+        "sanitizer_bitvector_test.cc",
+        "sanitizer_bvgraph_test.cc",
+        "sanitizer_common_test.cc",
+        "sanitizer_deadlock_detector_test.cc",
+        "sanitizer_flags_test.cc",
+        "sanitizer_format_interceptor_test.cc",
+        "sanitizer_ioctl_test.cc",
+        "sanitizer_libc_test.cc",
+        "sanitizer_linux_test.cc",
+        "sanitizer_list_test.cc",
+        "sanitizer_mutex_test.cc",
+        "sanitizer_nolibc_test.cc",
+        "sanitizer_posix_test.cc",
+        "sanitizer_printf_test.cc",
+        "sanitizer_procmaps_test.cc",
+        "sanitizer_stackdepot_test.cc",
+        "sanitizer_stacktrace_printer_test.cc",
+        "sanitizer_stacktrace_test.cc",
+        "sanitizer_stoptheworld_test.cc",
+        "sanitizer_suppressions_test.cc",
+        "sanitizer_test_main.cc",
+        "sanitizer_thread_registry_test.cc",
+    ],
+    static_libs: ["libsan"],
+    host_ldlibs: [
+        "-ldl",
+        "-lrt",
+    ],
+    sanitize: {
+        never: true,
+    },
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+}
+
+cc_binary_host {
+    name: "san_test-Nolibc",
+    defaults: ["san_test_defaults"],
+
+    include_dirs: ["external/gtest/include"],
+    srcs: ["sanitizer_nolibc_test_main.cc"],
+    static_libs: [
+        "libsan",
+        "libgtest_host",
+    ],
+    ldflags: [
+        "-nostdlib",
+        "-Qunused-arguments",
+    ],
+    host_ldlibs: [
+        "-ldl",
+        "-lrt",
+    ],
+    sanitize: {
+        never: true,
+    },
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+}
diff --git a/lib/ubsan/Android.bp b/lib/ubsan/Android.bp
new file mode 100644
index 0000000..b8834fb
--- /dev/null
+++ b/lib/ubsan/Android.bp
@@ -0,0 +1,120 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//
+
+ubsan_rtl_files = [
+    "ubsan_diag.cc",
+    "ubsan_init.cc",
+    "ubsan_flags.cc",
+    "ubsan_handlers.cc",
+    "ubsan_value.cc",
+]
+
+ubsan_cxx_rtl_files = [
+    "ubsan_handlers_cxx.cc",
+    "ubsan_type_hash.cc",
+    "ubsan_type_hash_itanium.cc",
+    "ubsan_type_hash_win.cc",
+]
+
+ubsan_rtl_cppflags = [
+    "-fvisibility=hidden",
+    "-fno-exceptions",
+    "-std=c++11",
+    "-Wall",
+    "-Werror",
+    "-Wno-unused-parameter",
+    "-Wno-non-virtual-dtor",
+]
+
+ubsan_rtl_c_includes = ["external/compiler-rt/lib"]
+
+cc_library_static {
+    name: "libubsan",
+    host_supported: true,
+    defaults: ["asan_arch_defaults"],
+
+    include_dirs: ubsan_rtl_c_includes,
+    cppflags: ubsan_rtl_cppflags,
+    srcs: ubsan_rtl_files,
+    sdk_version: "19",
+    stl: "none",
+    sanitize: {
+        never: true,
+    },
+    compile_multilib: "both",
+    target: {
+        host: {
+            cflags: ["-fno-rtti"],
+        },
+    },
+}
+
+//###############################################################################
+// Host modules
+
+cc_library_host_static {
+    name: "libubsan_standalone",
+
+    include_dirs: ubsan_rtl_c_includes,
+    cppflags: ubsan_rtl_cppflags + ["-fno-rtti"],
+    srcs: ubsan_rtl_files,
+    whole_static_libs: ["libsan"],
+    stl: "none",
+    sanitize: {
+        never: true,
+    },
+    compile_multilib: "both",
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+}
+
+cc_library_host_static {
+    name: "libubsan_cxx",
+
+    include_dirs: ubsan_rtl_c_includes,
+    cppflags: ubsan_rtl_cppflags,
+    srcs: ubsan_cxx_rtl_files,
+    sanitize: {
+        never: true,
+    },
+    compile_multilib: "both",
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+}
+
+cc_library_host_static {
+    name: "libubsan_standalone_cxx",
+
+    include_dirs: ubsan_rtl_c_includes,
+    cppflags: ubsan_rtl_cppflags,
+    srcs: ubsan_cxx_rtl_files,
+    sanitize: {
+        never: true,
+    },
+    compile_multilib: "both",
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+}