Skip to content

Update -sanitize=fuzzer option to take into account new libFuzzer location #11595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmake/modules/SwiftComponents.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
# * clang-resource-dir-symlink -- install a symlink to the Clang resource
# directory (which contains builtin headers) under 'lib/swift/clang'. This is
# useful when Clang and Swift are installed side-by-side.
# * llvm-resource-dir-symlink -- install a symlink to the LLVM resource directory.
# * stdlib -- the Swift standard library.
# * stdlib-experimental -- the Swift standard library module for experimental
# APIs.
Expand All @@ -67,7 +66,7 @@
# * toolchain-dev-tools -- install development tools useful in a shared toolchain
# * dev -- headers and libraries required to use Swift compiler as a library.
set(_SWIFT_DEFINED_COMPONENTS
"autolink-driver;compiler;clang-builtin-headers;clang-resource-dir-symlink;llvm-resource-dir-symlink;clang-builtin-headers-in-clang-resource-dir;stdlib;stdlib-experimental;swift-syntax;sdk-overlay;editor-integration;tools;testsuite-tools;toolchain-dev-tools;dev;license;sourcekit-xpc-service;sourcekit-inproc;swift-remote-mirror;swift-remote-mirror-headers")
"autolink-driver;compiler;clang-builtin-headers;clang-resource-dir-symlink;clang-builtin-headers-in-clang-resource-dir;stdlib;stdlib-experimental;swift-syntax;sdk-overlay;editor-integration;tools;testsuite-tools;toolchain-dev-tools;dev;license;sourcekit-xpc-service;sourcekit-inproc;swift-remote-mirror;swift-remote-mirror-headers")

macro(swift_configure_components)
# Set the SWIFT_INSTALL_COMPONENTS variable to the default value if it is not passed in via -D
Expand Down
57 changes: 16 additions & 41 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,10 +1063,13 @@ getDarwinLibraryNameSuffixForTriple(const llvm::Triple &triple) {
}

static std::string
getSanitizerRuntimeLibNameForDarwin(StringRef Sanitizer, const llvm::Triple &Triple) {
getSanitizerRuntimeLibNameForDarwin(StringRef Sanitizer,
const llvm::Triple &Triple,
bool shared = true) {
return (Twine("libclang_rt.")
+ Sanitizer + "_"
+ getDarwinLibraryNameSuffixForTriple(Triple) + "_dynamic.dylib").str();
+ getDarwinLibraryNameSuffixForTriple(Triple)
+ (shared ? "_dynamic.dylib" : ".a")).str();
}

static std::string
Expand Down Expand Up @@ -1141,16 +1144,19 @@ addLinkRuntimeLibForLinux(const ArgList &Args, ArgStringList &Arguments,
static void
addLinkSanitizerLibArgsForDarwin(const ArgList &Args,
ArgStringList &Arguments,
StringRef Sanitizer, const ToolChain &TC) {
StringRef Sanitizer,
const ToolChain &TC,
bool shared = true
) {
// Sanitizer runtime libraries requires C++.
Arguments.push_back("-lc++");
// Add explicit dependency on -lc++abi, as -lc++ doesn't re-export
// all RTTI-related symbols that are used.
Arguments.push_back("-lc++abi");

addLinkRuntimeLibForDarwin(Args, Arguments,
getSanitizerRuntimeLibNameForDarwin(Sanitizer, TC.getTriple()),
/*AddRPath=*/ true, TC);
getSanitizerRuntimeLibNameForDarwin(Sanitizer, TC.getTriple(), shared),
/*AddRPath=*/ shared, TC);
}

static void
Expand All @@ -1175,40 +1181,6 @@ addLinkSanitizerLibArgsForLinux(const ArgList &Args,
Arguments.push_back("-ldl");
}

static void
addLinkFuzzerLibArgsForDarwin(const ArgList &Args,
ArgStringList &Arguments,
const ToolChain &TC) {

// libFuzzer requires C++.
Arguments.push_back("-lc++");

// Link libfuzzer.
SmallString<128> Dir;
getRuntimeLibraryPath(Dir, Args, TC);
llvm::sys::path::remove_filename(Dir);
llvm::sys::path::append(Dir, "llvm", "libLLVMFuzzer.a");
SmallString<128> P(Dir);

Arguments.push_back(Args.MakeArgString(P));
}

static void
addLinkFuzzerLibArgsForLinux(const ArgList &Args,
ArgStringList &Arguments,
const ToolChain &TC) {
Arguments.push_back("-lstdc++");

// Link libfuzzer.
SmallString<128> Dir;
getRuntimeLibraryPath(Dir, Args, TC);
llvm::sys::path::remove_filename(Dir);
llvm::sys::path::append(Dir, "llvm", "libLLVMFuzzer.a");
SmallString<128> P(Dir);

Arguments.push_back(Args.MakeArgString(P));
}

ToolChain::InvocationInfo
toolchains::Darwin::constructInvocation(const LinkJobAction &job,
const JobContext &context) const {
Expand Down Expand Up @@ -1348,7 +1320,8 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
// Only link in libFuzzer for executables.
if (job.getKind() == LinkKind::Executable &&
(context.OI.SelectedSanitizers & SanitizerKind::Fuzzer))
addLinkFuzzerLibArgsForDarwin(context.Args, Arguments, *this);
addLinkSanitizerLibArgsForDarwin(
context.Args, Arguments, "fuzzer", *this, /*shared=*/false);

if (context.Args.hasArg(options::OPT_embed_bitcode,
options::OPT_embed_bitcode_marker)) {
Expand Down Expand Up @@ -1724,7 +1697,9 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
addLinkSanitizerLibArgsForLinux(context.Args, Arguments, "tsan", *this);

if (context.OI.SelectedSanitizers & SanitizerKind::Fuzzer)
addLinkFuzzerLibArgsForLinux(context.Args, Arguments, *this);
addLinkRuntimeLibForLinux(context.Args, Arguments,
getSanitizerRuntimeLibNameForLinux(
"fuzzer", this->getTriple()), *this);
}
}

Expand Down
6 changes: 0 additions & 6 deletions stdlib/public/SwiftShims/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,6 @@ swift_install_symlink_component(clang-resource-dir-symlink
TARGET ../clang/${CLANG_VERSION}
DESTINATION "lib/swift")

# Adding link to "llvm" at install time.
swift_install_symlink_component(llvm-resource-dir-symlink
LINK_NAME llvm
TARGET ../
DESTINATION "lib/swift")

# Possibly install Clang headers under Clang's resource directory in case we
# need to use a different version of the headers than the installed Clang. This
# should be used in conjunction with clang-resource-dir-symlink.
Expand Down
2 changes: 1 addition & 1 deletion test/Driver/fuzzer.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %swiftc_driver -driver-print-jobs -sanitize=fuzzer,address %s | %FileCheck -check-prefix=LIBFUZZER %s

// LIBFUZZER: libLLVMFuzzer.a
// LIBFUZZER: libclang_rt.fuzzer
@_cdecl("LLVMFuzzerTestOneInput") public func fuzzOneInput(Data: UnsafePointer<CChar>, Size: CLong) -> CInt {
return 0;
}