From dc4065fcc16d6736d920822c3e237b982bbb5b5e Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Fri, 27 May 2016 21:46:22 +0000 Subject: [PATCH] [libFuzzer] use __sanitizer_set_report_fd with -close_fd_mask. This allows us to keep asan reports when closing target's stderr git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271053 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/LibFuzzer.rst | 5 ++--- lib/Fuzzer/FuzzerIO.cpp | 6 ++++++ lib/Fuzzer/test/fuzzer.test | 4 +++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/LibFuzzer.rst b/docs/LibFuzzer.rst index 2bffb5aae4d..6bd276f0d9f 100644 --- a/docs/LibFuzzer.rst +++ b/docs/LibFuzzer.rst @@ -273,9 +273,8 @@ The most important command line options are: If 1 (default) and if LeakSanitizer is enabled try to detect memory leaks during fuzzing (i.e. not only at shut down). ``-close_fd_mask`` - Indicate output streams to close at startup. Be careful, this will also - remove diagnostic output from the tools in use; for example the messages - AddressSanitizer_ sends to ``stderr``/``stdout`` will also be lost. + Indicate output streams to close at startup. Be careful, this will + remove diagnostic output from target code (e.g. messages on assert failure). - 0 (default): close neither ``stdout`` nor ``stderr`` - 1 : close ``stdout`` diff --git a/lib/Fuzzer/FuzzerIO.cpp b/lib/Fuzzer/FuzzerIO.cpp index ac35d736bbf..171b188eab5 100644 --- a/lib/Fuzzer/FuzzerIO.cpp +++ b/lib/Fuzzer/FuzzerIO.cpp @@ -18,6 +18,10 @@ #include #include +extern "C" { +__attribute__((weak)) void __sanitizer_set_report_fd(void *); +} + namespace fuzzer { static FILE *OutputFile = stderr; @@ -122,6 +126,8 @@ void DupAndCloseStderr() { FILE *NewOutputFile = fdopen(OutputFd, "w"); if (NewOutputFile) { OutputFile = NewOutputFile; + if (__sanitizer_set_report_fd) + __sanitizer_set_report_fd(reinterpret_cast(OutputFd)); close(2); } } diff --git a/lib/Fuzzer/test/fuzzer.test b/lib/Fuzzer/test/fuzzer.test index cff7d6f74bc..b46d24eec33 100644 --- a/lib/Fuzzer/test/fuzzer.test +++ b/lib/Fuzzer/test/fuzzer.test @@ -11,7 +11,9 @@ RUN: LLVMFuzzer-SimpleTest -only_ascii=1 2>&1 RUN: LLVMFuzzer-SimpleCmpTest -max_total_time=1 2>&1 | FileCheck %s --check-prefix=MaxTotalTime MaxTotalTime: Done {{.*}} runs in {{.}} second(s) -RUN: not LLVMFuzzer-NullDerefTest 2>&1 | FileCheck %s --check-prefix=NullDerefTest +RUN: not LLVMFuzzer-NullDerefTest 2>&1 | FileCheck %s --check-prefix=NullDerefTest +RUN: not LLVMFuzzer-NullDerefTest -close_fd_mask=3 2>&1 | FileCheck %s --check-prefix=NullDerefTest +NullDerefTest: ERROR: AddressSanitizer: SEGV on unknown address NullDerefTest: Test unit written to ./crash- RUN: not LLVMFuzzer-NullDerefTest -artifact_prefix=ZZZ 2>&1 | FileCheck %s --check-prefix=NullDerefTestPrefix NullDerefTestPrefix: Test unit written to ZZZcrash-