DO NOT MERGE - Merge Android 10 into master
Bug: 139893257
Change-Id: I4ce0d90618c71838c6915dfe7107fe55dfa974ec
diff --git a/CleanSpec.mk b/CleanSpec.mk
new file mode 100644
index 0000000..1a1f660
--- /dev/null
+++ b/CleanSpec.mk
@@ -0,0 +1,51 @@
+# Copyright (C) 2007 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.
+#
+
+# If you don't need to do a full clean build but would like to touch
+# a file or delete some intermediate files, add a clean step to the end
+# of the list. These steps will only be run once, if they haven't been
+# run before.
+#
+# E.g.:
+# $(call add-clean-step, touch -c external/sqlite/sqlite3.h)
+# $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libz_intermediates)
+#
+# Always use "touch -c" and "rm -f" or "rm -rf" to gracefully deal with
+# files that are missing or have been moved.
+#
+# Use $(PRODUCT_OUT) to get to the "out/target/product/blah/" directory.
+# Use $(OUT_DIR) to refer to the "out" directory.
+#
+# If you need to re-do something that's already mentioned, just copy
+# the command and add it to the bottom of the list. E.g., if a change
+# that you made last week required touching a file and a change you
+# made today requires touching the same file, just copy the old
+# touch step and add it to the end of the list.
+#
+# *****************************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST ABOVE THE BANNER
+# *****************************************************************
+
+# For example:
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/AndroidTests_intermediates)
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/core_intermediates)
+#$(call add-clean-step, find $(OUT_DIR) -type f -name "IGTalkSession*" -print0 | xargs -0 rm -f)
+#$(call add-clean-step, rm -rf $(PRODUCT_OUT)/data/*)
+
+$(call add-clean-step, rm -rf $(TARGET_OUT_OPTIONAL_EXECUTABLES)/sanitizer-status)
+
+# ******************************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST ABOVE THIS BANNER
+# ******************************************************************
diff --git a/fuzzing/fuzz/Android.bp b/fuzzing/fuzz/Android.bp
new file mode 100644
index 0000000..1057c17
--- /dev/null
+++ b/fuzzing/fuzz/Android.bp
@@ -0,0 +1,5 @@
+cc_prebuilt_binary {
+ name: "fuzz",
+ srcs: ["fuzz"]
+}
+
diff --git a/fuzzing/fuzz/fuzz b/fuzzing/fuzz/fuzz
new file mode 100644
index 0000000..fe569ab
--- /dev/null
+++ b/fuzzing/fuzz/fuzz
@@ -0,0 +1,263 @@
+#!/system/bin/sh
+# Copyright (C) 2018 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.
+# Abort on any error
+
+# Global configuration
+FUZZER_BASES=( "/data/nativetest64/fuzzers" "/data/nativetest/fuzzers" )
+WORK_BASE="/data/local/tmp/fuzz"
+LAST_FUZZ="$WORK_BASE/last_session"
+
+#------------------------------ HELPER FUNCTIONS ------------------------------
+function die() {
+ echo "$@" >&2
+ exit 1
+}
+
+# Print the usage and exit.
+# Optionally takes an extra string to print.
+function usage() {
+ die "\
+Usage: $0 [-a artifacts_path] [-c corpus] [-e engine] [-l logname] [-n new_corpus] [-w workdir] fuzzer [-- fuzzer_option...]
+
+Run a fuzzing session.
+
+All of the information on the most recent session is symlinked at $LAST_FUZZ,
+which points to the most recent session's working directory.
+
+The working directory is standardized to contain the following directories (or symlinks,
+if non-default options are provided):
+
+ artifacts/ Crashing test cases and other fuzzer output
+ corpus/ Input corpus
+ corpus_new/ Mutated test cases which exercise coverage features not represented
+ in the input corpus.
+ fuzz.log Contents of stdout from the fuzzing session.
+
+Arguments:
+ fuzzer Name of the fuzzer, e.g. cxa_demangle_fuzzer, or a full path to the fuzzer
+ fuzzer_option Fuzzer-specific arguments
+
+Options:
+ -a Path where fuzzer artifacts (logs, timeouts, crashes) should be stored
+ (Default: \$WORK/artifacts)
+
+ -c Path where input corpus is stored. Must not be empty.
+ (Default: \$WORK/corpus)
+
+ -e Fuzzer engine to use. Defaults to libFuzzer.
+
+ -l Filename to use for the fuzzer log.
+ (Default: fuzz.log)
+
+ -n Path to store new corpus elements
+ (Default: \$WORK/corpus_new)
+
+ -w Work directory to use
+ (Default: $WORK_BASE/\$FUZZER)
+
+Examples:
+
+ $ adb shell fuzz IOMX
+ $ adb shell fuzz -e honggfuzz IOMX
+ $ adb shell fuzz -w /sdcard IOMX
+ $ adb shell fuzz -c /sdcard/corpus IOMX
+ $ adb shell fuzz /data/my_fuzzer -- --foo --bar
+$@"
+}
+
+# Create a symlink from $1 ==> $2, if $2 is specified.
+# Otherwise, ensure $1 exists and is a directory.
+function symlink_or_create() {
+ DIR=$1
+ TARGET=$2
+ if [ -n "$TARGET" ]; then
+ if [ ! -d "$TARGET" ]; then
+ die "Directory does not exist: $TARGET"
+ fi
+ rm -rf "$DIR"
+ ln -sv "$TARGET" "$DIR"
+ elif [ ! -d "$DIR" ]; then
+ mkdir -p "$DIR"
+ fi
+}
+
+# Ensure that there is "enough space" left on the device for
+# the path provided.
+function ensure_space() {
+ FREE_SPACE=$(df $1 | tail -1 | awk '{print $4}')
+ TEN_MEGABYTES=$((1024*10))
+
+ if [ $FREE_SPACE -lt $TEN_MEGABYTES ]; then
+ die "Not enough free space available at $(realpath $1):\n$(df -h $1)"
+ fi
+}
+
+# Ensure that the provided directory is not empty.
+function ensure_not_empty() {
+ if [ "$(find -H $1 -type f | wc -l)" -eq "0" ]; then
+ die "$1 is empty"
+ fi
+}
+
+#--------------------------- CHECK SYSTEM VIABILITY ---------------------------
+# Make sure ASAN and coverage work
+if ! sanitizer-status asan cov &>/dev/null; then
+ # repeat the command to show the output
+ die "Sanitizer Checks Failed!\n$(sanitizer-status)"
+fi
+
+#------------------------------ CHECK ARGUMENTS -------------------------------
+# Determine what fuzzer we want to run, and ensure that it's on the device
+OPT_ARTIFACTS=""
+OPT_CORPUS=""
+OPT_CORPUS_NEW=""
+OPT_ENGINE="libFuzzer"
+OPT_LOG="fuzz.log"
+OPT_WORKDIR=""
+
+while getopts "a:c:e:l:n:w:" o; do
+ case "${o}" in
+ a) OPT_ARTIFACTS="${OPTARG}" ;;
+ c) OPT_CORPUS="${OPTARG}" ;;
+ e) OPT_ENGINE="${OPTARG}" ;;
+ l) OPT_LOG="${OPTARG}" ;;
+ n) OPT_CORPUS_NEW="${OPTARG}" ;;
+ w) OPT_WORKDIR="${OPTARG}" ;;
+ *) usage ;;
+ esac
+done
+
+shift $((OPTIND-1))
+
+if [ $# -lt 1 ]; then
+ usage "\nMissing arguments: fuzzer"
+fi
+
+if [ -e $1 ]; then
+ FUZZER_BIN="$1"
+ FUZZER="$(basename $FUZZER_BIN)"
+else
+ FUZZER="${1%_fuzzer}"
+
+ for FUZZER_BASE in "${FUZZER_BASES[@]}"; do
+ FUZZER_BIN="${FUZZER_BASE}/${OPT_ENGINE}/${FUZZER}_fuzzer/${FUZZER}_fuzzer"
+ [ -e "$FUZZER_BIN" ] && break
+ done
+fi
+
+shift
+
+if [ ! -e "$FUZZER_BIN" ]; then
+ die "Invalid fuzzer path $FUZZER_BIN: File does not exist"
+fi
+
+#------------------------- CREATE DIRECTORY STRUCTURE -------------------------
+# First set up the root work directory.
+#
+# This directory is the default location where the corpus/, corpus_new/, and
+# artifacts/ directories will be created.
+#
+# We also create symlinks here if any of those options are provided, so that
+# the same directory structure is always available, and any external utilities
+# can easily find the last fuzzing session's data.
+WORK_ROOT="$WORK_BASE/$FUZZER"
+symlink_or_create "$WORK_ROOT" "$OPT_WORKDIR"
+WORK_ROOT="${OPT_WORKDIR:-$WORK_ROOT}"
+
+# Change into the work root, in case any of the OPT_XXX paths are relative.
+cd "$WORK_ROOT"
+
+# Create the rest of the directory structure
+ARTIFACTS="$WORK_ROOT/artifacts"
+CORPUS="$WORK_ROOT/corpus"
+CORPUS_NEW="$WORK_ROOT/corpus_new"
+
+symlink_or_create "$ARTIFACTS" "$OPT_ARTIFACTS"
+symlink_or_create "$CORPUS" "$OPT_CORPUS"
+symlink_or_create "$CORPUS_NEW" "$OPT_CORPUS_NEW"
+
+# Update the environment variables so that the "real" paths show up in
+# the command invocation.
+ARTIFACTS="${OPT_ARTIFACTS:-$ARTIFACTS}"
+CORPUS="${OPT_CORPUS:-$CORPUS}"
+CORPUS_NEW="${OPT_CORPUS_NEW:-$CORPUS_NEW}"
+
+# Check the contents of the corpus aren't empty, this indicates a user error.
+ensure_not_empty "$CORPUS"
+
+# Ensure that there's room to grow the corpus / dump artifacts.
+ensure_space "$CORPUS_NEW"
+ensure_space "$ARTIFACTS"
+
+#------------------------- UPDATE ENVIRONMENT OPTIONS -------------------------
+# Set up the ASAN_OPTIONS for optimal everything
+# Note that we are only appending options, so that we do not override the
+# default-at-boot ASAN_OPTIONS (e.g include=/system/asan.options).
+
+ASAN_OPTIONS+=:coverage=1
+ASAN_OPTIONS+=:atexit=1
+# ASAN_OPTIONS+=:verbosity=2
+ASAN_OPTIONS+=:print_cmdline=1
+ASAN_OPTIONS+=:print_stats=1
+ASAN_OPTIONS+=:print_legend=1
+ASAN_OPTIONS+=:print_scariness=1
+ASAN_OPTIONS+=:log_path=/dev/null
+
+export ASAN_OPTIONS="$ASAN_OPTIONS"
+echo "ASAN_OPTIONS=$ASAN_OPTIONS"
+
+
+#---------------------------- BUILD FUZZER COMMAND ----------------------------
+# Based on the fuzzer engine selected, build up the command line.
+case "${OPT_ENGINE}" in
+ libFuzzer)
+ # NOTE: We use '-jobs=-1' to get libFuzzer to fuzz forever.
+ set -A FUZZ_CMD -- \
+ "$FUZZER_BIN" \
+ -artifact_prefix="$ARTIFACTS" \
+ -print_coverage=1 \
+ -detect_leaks=0 \
+ -jobs=-1 \
+ "$CORPUS_NEW" \
+ "$CORPUS" \
+ "$@"
+ ;;
+ honggfuzz)
+ set -A FUZZ_CMD -- \
+ honggfuzz \
+ --persistent \
+ --sanitizers \
+ --tmout_sigvtalrm \
+ --workspace "$ARTIFACTS" \
+ --input "$CORPUS_NEW" \
+ --covdir_new "$CORPUS_NEW" \
+ -- "$FUZZER_BIN" "$@"
+ ;;
+ *)
+ die "Unknown fuzzer engine $FUZZER_TYPE"
+ ;;
+esac
+
+#--------------------------------- RUN FUZZER ---------------------------------
+# Set up a symlink for the "last fuzz session" so that we can easily find it.
+ln -svf "$WORK_ROOT" "$LAST_FUZZ"
+
+# Change into the artifacts directory, so that anything
+# the fuzzer emits to $PWD will also be captured.
+cd "$ARTIFACTS"
+
+echo "Running fuzzer: ${FUZZ_CMD[@]}"
+echo "------------------------------"
+${FUZZ_CMD[@]} | tee "${OPT_LOG}"
diff --git a/sanitizer-status/Android.bp b/sanitizer-status/Android.bp
new file mode 100644
index 0000000..86501df
--- /dev/null
+++ b/sanitizer-status/Android.bp
@@ -0,0 +1,37 @@
+cc_defaults {
+ name: "sanitizer-status_defaults",
+
+ c_std: "c11",
+ cflags: [
+ "-Wall",
+ "-Werror",
+ "-O0",
+ ],
+}
+
+sanitizer_status_library_shared {
+ name: "libsanitizer-status",
+ defaults: ["sanitizer-status_defaults"],
+
+ srcs: ["sanitizer-status.c"],
+}
+
+cc_binary {
+ name: "sanitizer-status",
+ defaults: ["sanitizer-status_defaults"],
+
+ srcs: ["main.c"],
+ shared_libs: ["libsanitizer-status"],
+}
+
+bootstrap_go_package {
+ name: "soong-sanitizer_status",
+ pkgPath: "android/soong/security/santizers_status",
+ deps: [
+ "blueprint",
+ "soong-android",
+ "soong-cc",
+ ],
+ srcs: ["sanitizer-status.go"],
+ pluginFor: ["soong_build"],
+}
diff --git a/sanitizer-status/MODULE_LICENSE_APACHE2 b/sanitizer-status/MODULE_LICENSE_APACHE2
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/sanitizer-status/MODULE_LICENSE_APACHE2
diff --git a/sanitizer-status/NOTICE b/sanitizer-status/NOTICE
new file mode 100644
index 0000000..c5b1efa
--- /dev/null
+++ b/sanitizer-status/NOTICE
@@ -0,0 +1,190 @@
+
+ Copyright (c) 2005-2008, 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.
+
+ 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.
+
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
diff --git a/sanitizer-status/main.c b/sanitizer-status/main.c
new file mode 100644
index 0000000..2186ccd
--- /dev/null
+++ b/sanitizer-status/main.c
@@ -0,0 +1,5 @@
+int sanitizer_status(int argc, char** argv);
+
+int main(int argc, char** argv) {
+ return sanitizer_status(argc, argv);
+}
diff --git a/sanitizer-status/sanitizer-status.c b/sanitizer-status/sanitizer-status.c
new file mode 100644
index 0000000..41af3f8
--- /dev/null
+++ b/sanitizer-status/sanitizer-status.c
@@ -0,0 +1,220 @@
+/*
+ * Copyright (C) 2008 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.
+ */
+
+#include <errno.h>
+#include <error.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <limits.h>
+#include <paths.h>
+#include <pwd.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+// crashes if built with -fsanitize=address
+void test_crash_malloc() {
+ volatile char* heap = malloc(32);
+ heap[32] = heap[32];
+ printf("ASAN: Heap Test Failed\n");
+}
+
+// crashes if built with -fsanitize=address
+void test_crash_stack() {
+ volatile char stack[32];
+ volatile char* p_stack = stack;
+ p_stack[32] = p_stack[32];
+ printf("ASAN: Stack Test Failed\n");
+}
+
+int data_asan_exists() {
+ int fd = open("/data/asan", O_DIRECTORY | O_PATH | O_CLOEXEC, 0);
+ if(fd < 0) {
+ printf("ASAN: Missing /data/asan\n");
+ return 1;
+ }
+ close(fd);
+ return 0;
+}
+
+// crashes if built with -fsanitize=memory
+void test_msan_crash_stack() {
+ volatile int stack[10];
+ stack[5] = 0;
+ if (stack[0]) {
+ stack[0] = 1;
+ }
+ printf("MSAN: Stack Test Failed\n");
+}
+
+// crashes if built with -fsanitize=integer
+void test_integer_overflow() {
+ size_t max = (size_t)-1;
+ max++;
+ printf("UBSAN: Integer Overflow Test Failed\n");
+}
+
+// returns 0 if kcov is enabled
+int test_kcov() {
+ const char* kcov_file = "/sys/kernel/debug/kcov";
+ int fd = open(kcov_file, O_RDWR);
+ if (fd == -1) {
+ printf("KCOV: Could not open %s\n", kcov_file);
+ return 1;
+ }
+ close(fd);
+ return 0;
+}
+
+// returns 0 if kasan was compiled in
+int test_kasan() {
+ // rely on the exit status of grep to propagate
+ if (system("gzip -d < /proc/config.gz | grep CONFIG_KASAN=y >/dev/null")) {
+ printf("KASAN: CONFIG_KASAN not in /proc/config.gz\n");
+ return 1;
+ }
+ return 0;
+}
+
+// executes a test that is expected to crash
+// returns 0 if the test crashes
+int test(void (*function)()) {
+ fflush(stdout);
+
+ pid_t child = fork();
+ int status = 0;
+
+ if (child == -1) {
+ perror("fork");
+ exit(1);
+ }
+
+ if (child == 0) {
+ // Silence the ASAN report that is generated
+ close(2);
+
+ // Invoke the target function. If it does not crash, terminate the process.
+ function();
+ exit(EXIT_SUCCESS);
+ }
+
+ // Wait for the child to either crash, or exit cleanly
+ while (child == waitpid(child, &status, 0)) {
+ if (!WIFEXITED(status))
+ continue;
+ if (WEXITSTATUS(status) == EXIT_SUCCESS)
+ return 1;
+ break;
+ }
+ return 0;
+}
+
+int have_option(const char* option, const char** argv, const int argc) {
+ for (int i = 1; i < argc; i++)
+ if (!strcmp(option, argv[i]))
+ return 1;
+ return 0;
+}
+
+int sanitizer_status(int argc, const char** argv) {
+ int test_everything = 0;
+ int failures = 0;
+
+ if (argc <= 1)
+ test_everything = 1;
+
+ if (test_everything || have_option("asan", argv, argc)) {
+ int asan_failures = 0;
+
+#if !defined(ANDROID_SANITIZE_ADDRESS) && !defined(ANDROID_SANITIZE_HWADDRESS)
+ asan_failures += 1;
+ printf("ASAN: Compiler flags failed!\n");
+#endif
+
+ asan_failures += test(test_crash_malloc);
+ asan_failures += test(test_crash_stack);
+ asan_failures += data_asan_exists();
+
+ if (!asan_failures)
+ printf("ASAN: OK\n");
+
+ failures += asan_failures;
+ }
+
+ if(test_everything || have_option("cov", argv, argc)) {
+ int cov_failures = 0;
+
+#ifndef ANDROID_SANITIZE_COVERAGE
+ printf("COV: Compiler flags failed!\n");
+ cov_failures += 1;
+#endif
+
+ if (!cov_failures)
+ printf("COV: OK\n");
+
+ failures += cov_failures;
+ }
+
+ if (test_everything || have_option("msan", argv, argc)) {
+ int msan_failures = 0;
+
+ msan_failures += test(test_msan_crash_stack);
+
+ if (!msan_failures)
+ printf("MSAN: OK\n");
+
+ failures += msan_failures;
+ }
+
+ if (test_everything || have_option("kasan", argv, argc)) {
+ int kasan_failures = 0;
+
+ kasan_failures += test_kasan();
+
+ if(!kasan_failures)
+ printf("KASAN: OK\n");
+
+ failures += kasan_failures;
+ }
+
+ if (test_everything || have_option("kcov", argv, argc)) {
+ int kcov_failures = 0;
+
+ kcov_failures += test_kcov();
+
+ if (!kcov_failures)
+ printf("KCOV: OK\n");
+
+ failures += kcov_failures;
+ }
+
+ if (test_everything || have_option("ubsan", argv, argc)) {
+ int ubsan_failures = 0;
+
+ ubsan_failures += test(test_integer_overflow);
+
+ if (!ubsan_failures)
+ printf("UBSAN: OK\n");
+
+ failures += ubsan_failures;
+ }
+
+ return failures > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}
diff --git a/sanitizer-status/sanitizer-status.go b/sanitizer-status/sanitizer-status.go
new file mode 100644
index 0000000..2c75e9f
--- /dev/null
+++ b/sanitizer-status/sanitizer-status.go
@@ -0,0 +1,39 @@
+package sanitizer_status
+
+import (
+ "android/soong/android"
+ "android/soong/cc"
+)
+
+func init() {
+ android.RegisterModuleType("sanitizer_status_library_shared",
+ libraryFactory)
+}
+
+func libraryFactory() android.Module {
+ module := cc.LibrarySharedFactory()
+ android.AddLoadHook(module, loadHook)
+ return module
+}
+
+func loadHook(ctx android.LoadHookContext) {
+ type props struct {
+ Cflags []string
+ }
+
+ p := &props{}
+
+ sanitizers := ctx.Config().SanitizeDevice()
+
+ if android.InList("address", sanitizers) {
+ p.Cflags = append(p.Cflags, "-DANDROID_SANITIZE_ADDRESS=1")
+ }
+ if android.InList("hwaddress", sanitizers) {
+ p.Cflags = append(p.Cflags, "-DANDROID_SANITIZE_HWADDRESS=1")
+ }
+ if android.InList("coverage", sanitizers) {
+ p.Cflags = append(p.Cflags, "-DANDROID_SANITIZE_COVERAGE=1")
+ }
+
+ ctx.AppendProperties(p)
+}