Skip to content

[Sanitizers] Add Scudo support #28538

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
Jan 27, 2020
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
1 change: 1 addition & 0 deletions include/swift/Basic/Sanitizers.def
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ SANITIZER(0, Address, address, asan)
SANITIZER(1, Thread, thread, tsan)
SANITIZER(2, Undefined, undefined, ubsan)
SANITIZER(3, Fuzzer, fuzzer, fuzzer)
SANITIZER(4, Scudo, scudo, scudo)

#undef SANITIZER
30 changes: 30 additions & 0 deletions lib/Option/SanitizerOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,36 @@ OptionSet<SanitizerKind> swift::parseSanitizerArgValues(
+ toStringRef(SanitizerKind::Thread)).toStringRef(b2));
}

// Scudo can only be run with ubsan.
if (sanitizerSet & SanitizerKind::Scudo) {
OptionSet<SanitizerKind> allowedSet;
allowedSet |= SanitizerKind::Scudo;
allowedSet |= SanitizerKind::Undefined;

auto forbiddenOptions = sanitizerSet - allowedSet;

if (forbiddenOptions) {
SanitizerKind forbidden;

if (forbiddenOptions & SanitizerKind::Address) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this if-else needs to be extended whenever the SanitizerKind enum gets extended, but I don't see a better way of doing this with the current API of OptionSet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. OptionSet absolutely could answer this question, but I don’t know how much value we get from ramming that new single-use API into this patch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not requesting anything additional for this PR, just wanted to point it out; maybe someone has an idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I understood you weren’t asking for new API, I was just adding clarification on why I didn’t add it as part of this patch.

forbidden = SanitizerKind::Address;
} else if (forbiddenOptions & SanitizerKind::Thread) {
forbidden = SanitizerKind::Thread;
} else {
assert(forbiddenOptions & SanitizerKind::Fuzzer);
forbidden = SanitizerKind::Fuzzer;
}

SmallString<128> b1;
SmallString<128> b2;
Diags.diagnose(SourceLoc(), diag::error_argument_not_allowed_with,
(A->getOption().getPrefixedName()
+ toStringRef(SanitizerKind::Scudo)).toStringRef(b1),
(A->getOption().getPrefixedName()
+ toStringRef(forbidden)).toStringRef(b2));
}
}

return sanitizerSet;
}

Expand Down
40 changes: 40 additions & 0 deletions test/Driver/sanitize_scudo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target x86_64-unknown-linux-gnu %s | %FileCheck -check-prefix=SCUDO_LINUX %s
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target arm64-apple-ios7.1 %s 2>&1 | %FileCheck -check-prefix=SCUDO_IOS %s
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target arm64-apple-tvos9.0 %s 2>&1 | %FileCheck -check-prefix=SCUDO_tvOS %s
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target armv7k-apple-watchos2.0 %s 2>&1 | %FileCheck -check-prefix=SCUDO_watchOS %s
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target i386-apple-watchos2.0 %s 2>&1 | %FileCheck -check-prefix=SCUDO_watchOS_SIM %s
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target i386-apple-macosx10.9 %s 2>&1 | %FileCheck -check-prefix=SCUDO_OSX_32 %s
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target x86_64-apple-ios7.1 %s 2>&1 | %FileCheck -check-prefix=SCUDO_IOSSIM %s
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target x86_64-apple-macosx10.9 %s 2>&1 | %FileCheck -check-prefix=SCUDO_OSX_64 %s
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target x86_64-apple-tvos9.0 %s 2>&1 | %FileCheck -check-prefix=SCUDO_tvOS_SIM %s
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target x86_64-unknown-windows-msvc %s 2>&1 | %FileCheck -check-prefix=SCUDO_WINDOWS %s

// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo,address -target x86_64-unknown-linux-gnu %s 2>&1 | %FileCheck -check-prefix=SCUDO_ASAN %s
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo,thread -target x86_64-unknown-linux-gnu %s 2>&1 | %FileCheck -check-prefix=SCUDO_TSAN %s
// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo,undefined -target x86_64-unknown-linux-gnu %s 2>&1 | %FileCheck -check-prefix=SCUDO_UBSAN_LINUX %s


/*
* Make sure we don't accidentally add the sanitizer library path when building libraries or modules
*/
// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -emit-library -sanitize=scudo -target x86_64-unknown-linux-gnu %s 2>&1 | %FileCheck -check-prefix=SCUDO_LIBRARY_LINUX %s

// SCUDO_LINUX: bin/clang
// SCUDO_LINUX-SAME: -pie
// SCUDO_LINUX-SAME: -fsanitize=scudo
// SCUDO_OSX_32: unsupported option '-sanitize=scudo' for target 'i386-apple-macosx10.9'
// SCUDO_OSX_64: unsupported option '-sanitize=scudo' for target 'x86_64-apple-macosx10.9'
// SCUDO_IOSSIM: unsupported option '-sanitize=scudo' for target 'x86_64-apple-ios7.1'
// SCUDO_IOS: unsupported option '-sanitize=scudo' for target 'arm64-apple-ios7.1'
// SCUDO_tvOS_SIM: unsupported option '-sanitize=scudo' for target 'x86_64-apple-tvos9.0'
// SCUDO_tvOS: unsupported option '-sanitize=scudo' for target 'arm64-apple-tvos9.0'
// SCUDO_watchOS_SIM: unsupported option '-sanitize=scudo' for target 'i386-apple-watchos2.0'
// SCUDO_watchOS: unsupported option '-sanitize=scudo' for target 'armv7k-apple-watchos2.0'
// SCUDO_WINDOWS: unsupported option '-sanitize=scudo' for target 'x86_64-unknown-windows-msvc'

// SCUDO_ASAN: argument '-sanitize=scudo' is not allowed with '-sanitize=address'
// SCUDO_TSAN: argument '-sanitize=scudo' is not allowed with '-sanitize=thread'
// SCUDO_UBSAN_LINUX: -fsanitize=undefined,scudo
// SCUDO_LIBRARY_LINUX: bin/clang
// SCUDO_LIBRARY_LINUX-NOT: -fsanitize=scudo

10 changes: 10 additions & 0 deletions test/Sanitizers/scudo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-swiftc_driver %s -target %sanitizers-target-triple -g -sanitize=scudo -o %t_scudo-binary
// RUN: not %target-run %t_scudo-binary 2>&1 | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: OS=linux-gnu

let allocated = UnsafeMutableRawPointer.allocate(byteCount: 128, alignment: 1)
allocated.deallocate()
allocated.deallocate()

// CHECK: ERROR: invalid chunk state