Commit Graph

34 Commits

Author SHA1 Message Date
michael-yuji
9a948a9c3b Merge branch 'main' into mchiu/freebsd 2025-05-21 16:29:16 -07:00
Yuta Saito
678585238d [wasm] Add WASI as a supported target for sanitizers 2025-04-30 09:54:23 +00:00
Michael Chiu
7f0f2ac4dd Merge swiftlang#38335 2025-03-14 02:15:15 -04:00
Blue Gaston
68923d4035 Adds valid arguments to -sanitize-coverage: pc-table and inline-8bit-counter.
As is, you cannot set all default fuzzer options directly using -sanitize-coverage. Because of this you can use default fuzzer sanitize-coverage args, or a limited number coverage options.

This PR adds pc-table and inline-8bit-counter as valid args to be parsed for sanitize-coverage flag. These are default fuzzer options -- and will allow customizing sanitizer-coverage in variations of fuzzer defaults. (i.e. w/o pc-table enabled).

In upstream clang the option -fno-sanitize-coverage exists to disable various coverage options. Swift currently does not have a convention of using excluding args for sanitizer flags. This aims to limit the need for a new flag by inclusively setting desired coverage up to current fuzzer defaults.

rdar://127881891
2024-05-13 07:51:25 -07:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
usama
3053fe01b5 [Sanitizers] Add new sanitize-stable-abi flag for libsanitizers.
This patch adds a new flag sanitize-stable-abi to support linking
against the Sanitizers stable ABI added recently in compiler-rt. The
patch also passes extra options for the ASan pass when using this flag
to outline instrumentation code and remove version check.

rdar://112915278
2023-12-16 14:13:00 -08:00
Evan Wilde
b0d3b398d8 Diagnose sanitizer use with scudo
Scudo is now a standalone sanitizer that must be used in isolation.
Fixing the driver logic to identify and diagnose when it is used with
any other sanitizer.
2023-08-23 15:36:24 -07:00
Evan Wilde
41d59b215a Update Triple.h location
Triple moved from ADT to TargetParser. Updating includes to reflect
that.
2023-07-17 10:53:42 -07:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Josh Soref
719400c4da spelling: existence
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2022-05-01 04:32:23 -04:00
Dan Liew
9208b52de1 Disable "UseOdrIndicator" ASan instrumentation mode by default.
Previously Swift enabled the "UseOdrIndicator" ASan instrumentation mode
and gave no option to disable this. This probably wasn't intentional but
happened due to the fact the
`createModuleAddressSanitizerLegacyPassPass()` function has a default
value for the `UseOdrIndicator` parameter of `true` and in Swift we
never specified this parameter explicitly.

Clang disables the "UseOdrIndicator" mode by default but allows it to be
enabled using the `-fsanitize-address-use-odr-indicator` flag.
Having "UseOdrIndicator" off by default is probably the right
default choice because it bloats the binary. So this patch changes the
Swift compiler to match Clang's behavior.

This patch disables the "UseOdrIndicator" mode by default but adds a
hidden driver and frontend flag (`-sanitize-address-use-odr-indicator`)
to enable it. The flag is hidden so that we can remove it in the future
if needed.

A side effect  of disabling "UseOdrIndicator" is that by we will no
longer use private aliases for poisoning globals. Private aliases were
introduced to avoid crashes
(https://github.com/google/sanitizers/issues/398) due to ODR violations
with non-instrumented binaries. On Apple platforms the use of two-level
namespaces probably means that using private aliases wasn't ever really
necessary to avoid crashes. On platforms with a flat linking namespace
(e.g. Linux) using private aliases might matter more but should users
actually run into problems they can either:

* Fix their environment to remove the ODR, thus avoiding the crash.
* Instrument the previously non-instrumented code to avoid the crash.
* Use the new `-sanitize-address-use-odr-indicator` flag

rdar://problem/69335186
2020-12-15 11:09:30 -08:00
Cory Benfield
565298620b [Sanitizers] Add Scudo support (#28538)
LLVM ships a hardened memory allocator called Scudo:
https://llvm.org/docs/ScudoHardenedAllocator.html. This allocator
provides additional mitigations against heap-based vulnerabilities, but
retains sufficient performance to be safely run in production
applications.

While ideal Swift applications are obviously written in pure Swift, in
practice most applications contain some amount of code written in
less-safe languages. Additionally, plenty of Swift programs themselves
contain unsafe code, particularly when attempting to implement
high-performance data structures. These sources of unsafety introduce
the risk of memory issues, and having the option to use the Scudo
allocator is a useful defense-in-depth tool.

This patch enables `-sanitize=scudo` as an extra `swiftc` flag. This
sanitizer is only supported on Linux, so no further work is required to
enable it on Windows or Apple platforms. As this "sanitizer" is only a
runtime component, we do not require any wider changes to instrument
code. This is similar to clang's `-fsanitize=scudo` flag.

The Swift driver rejects platforms that don't support Scudo using an
existing mechanism in the Driver that is not part of this patch. This
mechanism is in swift::parseSanitizerArgValues(...)
(lib/Option/SanitizerOptions.cpp). The mechanism determines if a
sanitizer is supported by checking for the existence of the
corresponding sanitizer runtime library in the compiler's resource
directory. The Scudo runtime library currently only exists in the
Linux compiler resource directory. This results in the driver only
allowing Scudo when targeting Linux.
2020-01-26 17:27:14 -08:00
Dan Liew
63e72909b5 [Sanitizers] Add Driver/Frontend option to enable sanitizer instrumentation that supports error recovery.
The new option `-sanitize-recover=` takes a list of sanitizers that
recovery instrumentation should be enabled for. Currently we only
support it for Address Sanitizer.

If the option is not specified then the generated instrumentation does
not allow error recovery.

This option mirrors the `-fsanitize-recover=` option of Clang.

We don't enable recoverable instrumentation by default because it may
lead to code size blow up (control flow has to be resumable).

The motivation behind this change is that today, setting
`ASAN_OPTIONS=halt_on_error=0` at runtime doesn't always work. If you
compile without the `-sanitize-recover=address` option (equivalent to
the current behavior of the swift compiler) then the generated
instrumentation doesn't allow for error recovery. What this means is
that if you set `ASAN_OPTIONS=halt_on_error=0` at runtime and if an ASan
issue is caught via instrumentation then the process will always halt
regardless of how `halt_on_error` is set. However, if ASan catches an
issue via one of its interceptors (e.g. memcpy) then `the halt_on_error`
runtime option is respected.

With `-sanitize-recover=address` the generated instrumentation allows
for error recovery which means that the `halt_on_error` runtime option
is also respected when the ASan issue is caught by instrumentation.

ASan's default for `halt_on_error` is true which means this issue only
effects people who choose to not use the default behavior.

rdar://problem/56346688
2019-11-12 11:33:58 -08:00
Julian Lettner
34f830116d [Driver][NFC] Define sanitizer mapping <kind, name, file> in one place
Lower maintenance cost for explicit mapping between sanitizer enum and
string values.
2019-03-28 13:56:54 -07:00
Julian Lettner
e24585dc17 [Driver] Delegate linking of sanitizer runtimes to underlying Clang
For Unix tool chains.
2019-03-26 10:56:52 -07:00
Rahul Malik
d3cc043e58 Add Undefined Behavior sanitizer to Swift Driver (#18553)
This change allows the swift driver to link the ubsan runtime if
`-sanitize=undefined` is specified.
This is useful for sanitizing linked Objective-C code.
2018-11-13 12:11:56 -08:00
Thomas Roughton
5bb6ac2e12 Implement a Windows toolchain. 2018-04-18 15:26:05 +12:00
George Karpenkov
a65da887f2 Determine whether a given sanitizer is available based on the presenc… (#14919)
Determine whether a given sanitizer is available based on the presence of the library.

rdar://37192887
2018-03-02 16:57:45 -08:00
George Karpenkov
efe143c2f4 Adding support for -sanitize=fuzzer flag (#11381)
Similarly to Clang, the flag enables coverage instrumentation, and links
`libLLVMFuzzer.a` to the produced binary.
Additionally, this change affects the driver logic, and enables the
concurrent usage of multiple sanitizers.
2017-08-07 18:16:51 -07:00
Carl Brown
dacbb8750f Linux Address and Thread Sanitizer Support (#6446) 2017-07-02 12:42:01 -07:00
George Karpenkov
071f23eb63 Code review comments. 2017-06-16 13:26:46 -07:00
George Karpenkov
479aeca88c Addressing code review comments. 2017-06-16 13:26:46 -07:00
George Karpenkov
9637bd2c36 Change driver logic for sanitizers support.
With this patch different sanitizers (tsan/asan) will be enabled or
disabled on the driver level on a particular OS depending on whether
the required library is present.

The current patch only supports Darwin architectures, but Linux support
should not be hard to add.
2017-06-16 13:26:46 -07:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Dan Liew
827f573d6b Teach the Swift front-end to generate code with
"Sanitizer Coverage" with a new flag ``-sanitize-coverage=``. This
flag is analogous to Clang's ``-fsanitize-coverage=``.

This instrumentation currently requires ASan or TSan to be enabled
because the module pass created by ``createSanitizerCoverageModulePass()``
inserts calls into functions found in compiler-rt's "sanitizer_common".
"sanitizer_common" is not shipped as an individual library but instead
exists in several of the sanitizer runtime libraries so we have to
link with one of them to avoid linking errors.

The rationale between adding this feature is to allow experimentation
with libFuzzer which currently relies on "Sanitizer Coverage"
instrumentation.
2016-05-27 13:34:31 -07:00
practicalswift
212aba51c6 [gardening] Fix recently introduced typos: "’" → "'", "represention" → "representation", "arcitectures" → "architectures", "refernce" → "reference", "incovation" → "invocation" 2016-04-08 21:22:25 +02:00
Anna Zaks
92fae2e9a4 Add experimental support for Thread Sanitizer.
This patch threads the TSan option through the front end.
2016-04-06 11:53:43 -07:00
Xin Tong
94f52ce025 Merge pull request #1497 from practicalswift/typo-fixes-20160301
[gardening] Fix recently introduced typos: "contet" → "context", "dependcy" → "dependency", "traget" → "target"
2016-03-01 07:47:55 -08:00
practicalswift
5e491e31d6 [gardening] Fix recently introduced typo: "traget" → "target" 2016-03-01 11:29:07 +01:00
practicalswift
8cb15568b0 [gardening] Fix header formatting for some recently introduced files 2016-03-01 11:05:13 +01:00
zaks
2110235fd3 [asan] Cleanup based on Jordan's review comments 2016-02-26 16:02:40 -08:00