Skip to content

Commit

Permalink
Add -fsanitize=fuzzer-no-link flag to the driver.
Browse files Browse the repository at this point in the history
The flag will perform instrumentation necessary to the fuzzing,
but will NOT link libLLVMFuzzer.a library.
Necessary when modifying CFLAGS for projects which may produce
executables as well as a fuzzable target.

Differential Revision: https://reviews.llvm.org/D36600

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310733 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
George Karpenkov committed Aug 11, 2017
1 parent 58f8240 commit 078b7e8
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions include/clang/Basic/Sanitizers.def
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ SANITIZER("memory", Memory)
// libFuzzer
SANITIZER("fuzzer", Fuzzer)

// libFuzzer-required instrumentation, no linking.
SANITIZER("fuzzer-no-link", FuzzerNoLink)

// ThreadSanitizer
SANITIZER("thread", Thread)

Expand Down
7 changes: 5 additions & 2 deletions lib/Driver/SanitizerArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum : SanitizerMask {
RequiresPIE = DataFlow,
NeedsUnwindTables = Address | Thread | Memory | DataFlow,
SupportsCoverage = Address | KernelAddress | Memory | Leak | Undefined |
Integer | Nullability | DataFlow | Fuzzer,
Integer | Nullability | DataFlow | Fuzzer | FuzzerNoLink,
RecoverableByDefault = Undefined | Integer | Nullability,
Unrecoverable = Unreachable | Return,
LegacyFsanitizeRecoverMask = Undefined | Integer,
Expand Down Expand Up @@ -286,8 +286,11 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
Add &= ~InvalidTrappingKinds;
Add &= Supported;

// Enable coverage if the fuzzing flag is set.
if (Add & Fuzzer)
Add |= FuzzerNoLink;

// Enable coverage if the fuzzing flag is set.
if (Add & FuzzerNoLink)
CoverageFeatures |= CoverageTracePCGuard | CoverageIndirCall |
CoverageTraceCmp | CoveragePCTable;

Expand Down
1 change: 1 addition & 0 deletions lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,7 @@ SanitizerMask Darwin::getSupportedSanitizers() const {
Res |= SanitizerKind::Address;
Res |= SanitizerKind::Leak;
Res |= SanitizerKind::Fuzzer;
Res |= SanitizerKind::FuzzerNoLink;
if (isTargetMacOS()) {
if (!isMacosxVersionLT(10, 9))
Res |= SanitizerKind::Vptr;
Expand Down
1 change: 1 addition & 0 deletions lib/Driver/ToolChains/Linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ SanitizerMask Linux::getSupportedSanitizers() const {
SanitizerMask Res = ToolChain::getSupportedSanitizers();
Res |= SanitizerKind::Address;
Res |= SanitizerKind::Fuzzer;
Res |= SanitizerKind::FuzzerNoLink;
Res |= SanitizerKind::KernelAddress;
Res |= SanitizerKind::Vptr;
Res |= SanitizerKind::SafeStack;
Expand Down
5 changes: 5 additions & 0 deletions test/Driver/fuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
// RUN: %clang -fsanitize=fuzzer %s -shared -o %t.so -### 2>&1 | FileCheck --check-prefixes=CHECK-NOLIB-SO %s
// CHECK-NOLIB-SO-NOT: libLLVMFuzzer.a

// Check that we don't link in libFuzzer when compiling with -fsanitize=fuzzer-no-link.
// RUN: %clang -fsanitize=fuzzer-no-link %s -target x86_64-apple-darwin14 -### 2>&1 | FileCheck --check-prefixes=CHECK-NOLIB,CHECK-COV %s
// CHECK-NOLIB-NOT: libLLVMFuzzer.a
// CHECK-COV: -fsanitize-coverage-trace-pc-guard

// RUN: %clang -fsanitize=fuzzer -fsanitize-coverage=trace-pc %s -### 2>&1 | FileCheck --check-prefixes=CHECK-MSG %s
// CHECK-MSG-NOT: argument unused during compilation

Expand Down

0 comments on commit 078b7e8

Please sign in to comment.