From 15df273eb45dc53669739bb33388a12c1dfce962 Mon Sep 17 00:00:00 2001 From: Julian Lettner Date: Thu, 31 Jan 2019 01:24:01 +0000 Subject: [PATCH] [libFuzzer] Set default sanitizer options in fuzzer tests Summary: Set default `ASAN_OPTIONS` when running libFuzzer tests. This allows us to remove special casing in code for Darwin where we usually pass `abort_on_error=0` to override platform defaults for tests. A previous commit changed the code to make the tests pass: https://github.com/llvm/llvm-project/commit/7764a04af007eca68eafcf5caaea560ed05e35a9 Adapted a few tests to use `%env_asan_opts=` instead of directly setting the environment variable. rdar://problem/47515276 Reviewers: kcc, george.karpenkov Differential Revision: https://reviews.llvm.org/D57465 llvm-svn: 352711 --- compiler-rt/lib/fuzzer/FuzzerLoop.cpp | 5 +++-- compiler-rt/test/fuzzer/fuzzer-segv.test | 4 ++-- compiler-rt/test/fuzzer/large.test | 2 +- compiler-rt/test/fuzzer/lit.cfg | 7 +++++++ compiler-rt/test/fuzzer/minimize_two_crashes.test | 2 +- compiler-rt/test/fuzzer/strncmp-oob.test | 2 +- 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/compiler-rt/lib/fuzzer/FuzzerLoop.cpp b/compiler-rt/lib/fuzzer/FuzzerLoop.cpp index 959d5a2..bf60016 100644 --- a/compiler-rt/lib/fuzzer/FuzzerLoop.cpp +++ b/compiler-rt/lib/fuzzer/FuzzerLoop.cpp @@ -232,8 +232,9 @@ void Fuzzer::StaticFileSizeExceedCallback() { } void Fuzzer::CrashCallback() { - if (EF->__sanitizer_acquire_crash_state) - EF->__sanitizer_acquire_crash_state(); + if (EF->__sanitizer_acquire_crash_state && + !EF->__sanitizer_acquire_crash_state()) + return; Printf("==%lu== ERROR: libFuzzer: deadly signal\n", GetPid()); PrintStackTrace(); Printf("NOTE: libFuzzer has rudimentary signal handlers.\n" diff --git a/compiler-rt/test/fuzzer/fuzzer-segv.test b/compiler-rt/test/fuzzer/fuzzer-segv.test index 0c4fafe..7ae9049 100644 --- a/compiler-rt/test/fuzzer/fuzzer-segv.test +++ b/compiler-rt/test/fuzzer/fuzzer-segv.test @@ -1,8 +1,8 @@ RUN: %cpp_compiler %S/NullDerefTest.cpp -o %t-NullDerefTest -RUN: env ASAN_OPTIONS=handle_segv=0 not %run %t-NullDerefTest 2>&1 | FileCheck %s --check-prefix=LIBFUZZER_OWN_SEGV_HANDLER +RUN: %env_asan_opts=handle_segv=0 not %run %t-NullDerefTest 2>&1 | FileCheck %s --check-prefix=LIBFUZZER_OWN_SEGV_HANDLER LIBFUZZER_OWN_SEGV_HANDLER: == ERROR: libFuzzer: deadly signal LIBFUZZER_OWN_SEGV_HANDLER: SUMMARY: libFuzzer: deadly signal LIBFUZZER_OWN_SEGV_HANDLER: Test unit written to ./crash- -RUN: env ASAN_OPTIONS=handle_segv=1 not %run %t-NullDerefTest 2>&1 | FileCheck %s --check-prefix=LIBFUZZER_ASAN_SEGV_HANDLER +RUN: %env_asan_opts=handle_segv=1 not %run %t-NullDerefTest 2>&1 | FileCheck %s --check-prefix=LIBFUZZER_ASAN_SEGV_HANDLER LIBFUZZER_ASAN_SEGV_HANDLER: ERROR: AddressSanitizer: {{SEGV|access-violation}} on unknown address diff --git a/compiler-rt/test/fuzzer/large.test b/compiler-rt/test/fuzzer/large.test index a2e0ac0..91d279b 100644 --- a/compiler-rt/test/fuzzer/large.test +++ b/compiler-rt/test/fuzzer/large.test @@ -1,6 +1,6 @@ REQUIRES: linux RUN: %cpp_compiler %S/LargeTest.cpp -o %t-LargeTest RUN: %run %t-LargeTest -runs=10000 -RUN: ASAN_OPTIONS=handle_segv=0 %run %t-LargeTest -runs=10000 -lazy_counters=1 2>&1 | FileCheck %s +RUN: %env_asan_opts=handle_segv=0 %run %t-LargeTest -runs=10000 -lazy_counters=1 2>&1 | FileCheck %s CHECK: pages of counters where protected; libFuzzer's SEGV handler must be installed diff --git a/compiler-rt/test/fuzzer/lit.cfg b/compiler-rt/test/fuzzer/lit.cfg index 608991c..2ce3473 100644 --- a/compiler-rt/test/fuzzer/lit.cfg +++ b/compiler-rt/test/fuzzer/lit.cfg @@ -119,6 +119,13 @@ config.substitutions.append(('%msan_compiler', generate_compiler_cmd(is_cpp=True, fuzzer_enabled=True, msan_enabled=True) )) +default_asan_opts_str = ':'.join(config.default_sanitizer_opts) +if default_asan_opts_str: + config.environment['ASAN_OPTIONS'] = default_asan_opts_str + default_asan_opts_str += ':' +config.substitutions.append(('%env_asan_opts=', + 'env ASAN_OPTIONS=' + default_asan_opts_str)) + if config.host_os == 'Darwin': if config.target_arch in ["x86_64", "x86_64h"]: config.parallelism_group = "darwin-64bit-sanitizer" diff --git a/compiler-rt/test/fuzzer/minimize_two_crashes.test b/compiler-rt/test/fuzzer/minimize_two_crashes.test index cba88ee..e101094 100644 --- a/compiler-rt/test/fuzzer/minimize_two_crashes.test +++ b/compiler-rt/test/fuzzer/minimize_two_crashes.test @@ -6,7 +6,7 @@ RUN: %cpp_compiler -O0 %S/TwoDifferentBugsTest.cpp -o %t-TwoDifferentBugsTest RUN: rm -rf %t && mkdir %t RUN: echo H12345678901234667888090 > %t/long_crash -RUN: env ASAN_OPTIONS=dedup_token_length=3 %run %t-TwoDifferentBugsTest -seed=1 -minimize_crash=1 %t/long_crash -exact_artifact_path=%t/result 2>&1 | FileCheck %s +RUN: %env_asan_opts=dedup_token_length=3 %run %t-TwoDifferentBugsTest -seed=1 -minimize_crash=1 %t/long_crash -exact_artifact_path=%t/result 2>&1 | FileCheck %s CHECK: DedupToken1: DEDUP_TOKEN: Bar CHECK: DedupToken2: DEDUP_TOKEN: Bar diff --git a/compiler-rt/test/fuzzer/strncmp-oob.test b/compiler-rt/test/fuzzer/strncmp-oob.test index a0365d9..3d1f197 100644 --- a/compiler-rt/test/fuzzer/strncmp-oob.test +++ b/compiler-rt/test/fuzzer/strncmp-oob.test @@ -1,6 +1,6 @@ RUN: %cpp_compiler %S/StrncmpOOBTest.cpp -o %t-StrncmpOOBTest -RUN: env ASAN_OPTIONS=strict_string_checks=1 not %run %t-StrncmpOOBTest -seed=1 -runs=1000000 2>&1 | FileCheck %s --check-prefix=STRNCMP +RUN: %env_asan_opts=strict_string_checks=1 not %run %t-StrncmpOOBTest -seed=1 -runs=1000000 2>&1 | FileCheck %s --check-prefix=STRNCMP STRNCMP: AddressSanitizer: heap-buffer-overflow STRNCMP-NOT: __sanitizer_weak_hook_strncmp STRNCMP: in LLVMFuzzerTestOneInput -- 2.7.4