-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add Undefined Behavior sanitizer to Swift Driver #18553
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
Add Undefined Behavior sanitizer to Swift Driver #18553
Conversation
test/Driver/sanitizers.swift
Outdated
|
||
// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86_64-apple-macosx10.9 %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_OSX %s | ||
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86-apple-macosx10.9 %s 2>&1 | %FileCheck -check-prefix=UBSAN_OSX_32 %s | ||
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86_64-apple-ios7.1 %s 2>&1 | %FileCheck -check-prefix=UBSAN_IOSSIM %s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this doesn't seem right. I'm pretty sure we support UBSan on all Apple platforms. @cheshire?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you’re right - I was largely basing these off the existing tsan tests and didn’t see the “not” prefix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also one thing I’ve been trying to find an answer for is if ubsan is supported on Windows. If so I’ll update the windows toolchain as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like not yet. Even if we're wrong about that, we can always add it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I've omitted it for now. Updated the tests, let me know if this looks right
3cfd43b
to
05575f6
Compare
test/Driver/sanitizers.swift
Outdated
// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target arm64-apple-tvos9.0 %s 2>&1 | %FileCheck -check-prefix=UBSAN_tvOS %s | ||
// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target i386-apple-watchos2.0 %s 2>&1 | %FileCheck -check-prefix=UBSAN_watchOS_SIM %s | ||
// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target armv7k-apple-watchos2.0 %s 2>&1 | %FileCheck -check-prefix=UBSAN_watchOS %s | ||
// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86_64-unknown-windows-msvc %s 2>&1 | %FileCheck -check-prefix=UBSAN_WINDOWS %s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Confirm windows doesn't support ubsan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think UBSan is supported on Windows - Clang 5.0.1 installed clang_rt.ubsan_standalone-x86_64.lib
and variants on my Windows installation.
test/Driver/sanitizers.swift
Outdated
*/ | ||
|
||
// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86_64-apple-macosx10.9 %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_OSX %s | ||
// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86-apple-macosx10.9 %s 2>&1 | %FileCheck -check-prefix=UBSAN_OSX_32 %s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The positive checks should be checking both -check-prefix=UBSAN
and -check-prefix=UBSAN_<OS>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixing! Is there a reason the Address Sanitizer Linux / Windows positive checks don't specify -check-prefix=ASAN
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect it was just a copy/paste mistake, like yours were.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, wait, no, the rpath
command is Apple-platform-specific. I guess it's okay to leave those as they were.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, I'll address and run the test suite locally before updating
test/Driver/sanitizers.swift
Outdated
// UBSAN_watchOS_SIM: lib/swift/clang/lib/darwin/libclang_rt.ubsan_watchossim_dynamic.dylib | ||
// UBSAN_watchOS: lib/swift/clang/lib/darwin/libclang_rt.ubsan_watchos_dynamic.dylib | ||
// UBSAN_LINUX: lib/swift/clang/lib/linux/libclang_rt.ubsan-x86_64.a | ||
// UBSAN_WINDOWS: lib/swift/clang/lib/windows/clang_rt.ubsan_standalone-x86_64.lib |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This lib name is based off @troughton earlier comment but I believe the logic in the windows driver will look for clang_rt.ubsan-x86_64.lib
so this might require an update
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you should be able to run the tests locally to find out! docs/Testing.md has information on how to run just the one test.
@jrose-apple @troughton - Thanks for the comments, updated! |
@swift-ci Please test |
@jrose-apple - I ran into the same test failure locally but it doesn't quite make sense to me why The test error:
This error occurs if you pass an unsupported value for |
Maybe we just don't build the UBSan support by default. @cheshire, @Rostepher? |
@rahul-malik Thanks! The source looks fine. The driver uses a somewhat convoluted logic to mark the sanitizer as unavailable if the file for it is not found in resource directory. Could you try putting a breakpoint there and seeing what happens? |
@cheshire - Here is the current local state. I'll keep debugging but just wanted to provide more information.
The dylib is referenced from this method in DarwinToolchain.cpp
Given that I would expect a lookup of |
@rahul-malik yeah it's weird. What if you change the lookup to use the archive file, does everything work then? |
@cheshire - Thanks! Haven't been able to get back to this for a few days but will look into your suggestion tonight. |
@cheshire - Changing to the static lib (.a) didn't fix the test for me locally. Any chance you can patch and help me investigate? |
@rahul-malik Sorry, I am still somewhat confused on what exactly the problem is and to which degree is it isolated. Is it correct to say that the test is failing, and you could not quite understand why? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems you need to add empty stub files for ubsan into test/Driver/Inputs/fake-resource-dir
Thank you! Yes I was unclear how to go about resolving the missing file stub issue. I’ll update the PR next week with this change |
@rahul-malik NP! In general, I find that such issues are best resolved with debuggers: bisection narrows it down to the file not being found, and then debugger would let you know that it searches in a stub location. |
This change allows the swift driver to link the ubsan runtime if `-sanitize=undefined` is specified.
- Update positive asan tests that don't contain `-check-prefix=ASAN` - Add ubsan to windows toolchain
c3e3ac1
to
27fa64f
Compare
@cheshire @jrose-apple - Got around to updating with new inputs and rebasing the latest sources. Should be good to go 👍 |
@swift-ci please test |
Build failed |
Build failed |
There are tests failing, but none of them are the result of your patch. |
I just merged Bruno's fix for this, so let's try again. @swift-ci Please test |
@jrose-apple @cheshire - Yay! Tests passed :) Is this PR ready to accept / merge? |
OK, Thanks! |
Should I merge it, or do you want to do it? You would need to use the "squash and merge" option. |
Ah, external contributors don't have commit privileges by default, so you should be the one to merge it, George. |
What's in this pull request?
This PR adds the ability to use the undefined behavior sanitizer. It accomplishes this by adding
--sanitize=undefined
to the Swift driver and linking the runtime library (if available) when it is specified.This change is a dependency of adding this feature to SPM as discussed in this thread