Commit Graph

21 Commits

Author SHA1 Message Date
Mike Ash
6397e30856 [Concurrency] Eliminate StatusRecordLockRecord.
Move to a recursive lock inline in the Task. This avoids the need to allocate a lock record and simplifies the code somewhat.

Change Task's OpaquePrivateStorage to compute its size at build time based on the sizes of its components, rather than having it be a fixed size. It appears that the fixed size was intended to be part of the ABI, but that didn't happen and we're free to change this size. We need to expand it slightly when using pthread_mutex as the recursive lock, as pthread_mutex is pretty big. Other recursive locks allow it to shrink slightly.

We don't have a recursive mutex in our Threading support code, so add a RecursiveMutex type.

rdar://113898653
2025-03-26 14:52:37 -04:00
Allan Shortlidge
4ab3aae7c5 Gardening: Suppress -Winvalid-offsetof warnings in static asserts.
There cannot be undefined behavior at compile time, so these warnings can be
ignored when `offsetof` is used in a static assert:

```
warning: offset of on non-standard-layout type 'AsyncTask' [-Winvalid-offsetof]
```
2024-10-29 15:52:53 -07:00
Alexander Cyon
c21b1e68fd [stdlib] Fix typos 2024-07-06 13:09:57 +02: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
Allan Shortlidge
4eacb4d4cb NFC: Suppress -Wdeprecated-declarations warnings in Mutex.h.
The `is_literal` trait is deprecated in C++17.
2024-01-12 17:42:21 -08:00
Priyansh Prajapat
dd11cc5e13 fix: typo in Task.h from intialized to initialized 2023-10-31 12:59:46 +05:30
Konrad `ktoso` Malawski
cad608eb81 [Discarding] Don't leak retained "first error" task when retaining it 2023-05-24 18:03:28 +02:00
Doug Gregor
554925d0fe Fix crash when back-deploying concurrency to iOS 15.0/15.1-era OSs
The introduction of the library providing back-deployment fixes for
Swift 5.6 concurrency libraries (and older) introduced a concurrency
crash in the following OS version ranges

  - macOS [12.0, 12.1)
  - iOS [15.0, 15.1)
  - tvOS [15.0, 15.1)
  - watchOS [8.0, 8.2)

Neither older nor newer versions of the listed OSs were affected.

The actual bug involved a miscommunication between the code in the
library that back-deploys fixes (`libswiftCompatibility56.a`) and the
concurrency library in the OS itself. Specifically, the OS versions at
the end of the ranges above introduced voucher support into the
concurrency runtime in the OS (an important feature for performance).
The code in `libswiftCompatibility56.a` that back-ports concurrency fixes
also included the voucher support, which provides a consistent state
for those OS versions and anything newer.

OS versions that predate the introduction of concurrency in the OS are
similarly unaffected, because the embedded
`libswift_Concurrency.dylib` matches that of Swift 5.6, which includes
voucher support.

The OS versions in the affected range include a concurrency library in
the OS that does not manage vouchers. The `libswiftCompatibility56.a`
back-deployed fixes library has code that works on the same data
structures but does manage vouchers, leading to crashes when (say) a
job allocated by the OS version didn't set the "voucher" field, but
the `libswiftCompatibility56.a` tried to free it, essentially a form of
overrelease.

The fix is to teach the voucher-handling code in
`libswiftCompatibility56.a` to first check what version of the OS it
is executing on. If it's in the affected range, all handling of
vouchers is disables so it acts like the concurrency library in the
OS. For both earlier OS versions and later OS versions the
voucher-handler code executes unchanged. This entirely library is
disabled when running on OS versions containing the Swift 5.7
concurrency library (or newer), so those don't need to pay for the
extra check when dealing with vouchers.

Fixes rdar://108837762, rdar://108864311, rdar://108958765.
2023-05-08 03:01:01 -07:00
Rokhini Prabhu
ee98c1c6f4 Remove dead code around the Deadline Task Status record from the
compatibility library

Radar-Id: rdar://problem/88093007
2023-02-14 14:09:45 -08:00
Evan Wilde
c67e22e6aa Merge pull request #63192 from etcwilde/ewilde/fix-backdeploy-compat56-crash
Fixes for the compat56 library
2023-02-07 10:22:50 -08:00
Evan Wilde
97806c9c32 Hide more symbols
Apparently `-fvisibility=hidden` is not sufficient to hide some of the
symbols in this library. I've explicitly marked the symbols that were
flagged as being incorrectly exported as "hidden" so hopefully no one
drags them out again.

This is a statically linked library, so the symbols shouldn't need to be
exported for this work work. To ensure that this is the case and that
we're still hitting the overridden API with all of the hidden symbols, I
added a debug log line to the fixed `AsyncTask::waitFuture`
implementation and have verified that we see the compat56 log message
emitted from executables that need the compat56 library.
2023-01-24 10:15:12 -08:00
Evan Wilde
4f7f786ee3 Fix SWIFT_TASK_DEBUG_LOG macro
The threading library changed in the BackDeployConcurrency library
resulting in the `SWIFT_TASK_DEBUG_LOG` macro not compiling. Fixing
that. Also adding the logging pieces to the Compatibility 56 library.
2023-01-24 10:15:12 -08:00
Evan Wilde
7ad89fe394 Replace Task.h with right (5.6) version
I seem to have copied over the wrong version of Task.h. There is an ABI
mismatch in the size of AsyncContext due to the removal of Flags. This
resulted in programs crashing when running against the backdeploy
library and should have crashed when running on the swift 5.6 runtime.

The successResultPointer pointer was set in AsyncTask::waitFuture, but
with the wrong layout. When the pointer was read in the concurrency
backdeploy library, it was at a different offset, and thus contained a
nullptr.

I pulled the AsyncContextKind and AsyncContextFlags from the old
MetadataValues.h into Task.h as they were removed in commit
aca744b211, but are necessary with the
flags included.
2023-01-23 13:34:47 -08:00
Evan Wilde
188c7bd626 Break link against compiler-rt
This patch replaces the platform version check with a dlsym to grab the
symbol we need. If we're on a new enough platform, the symbol should be
available and we can use it. Otherwise, it will be empty and we should
return true. This breaks the link requirement for the platform version
symbol.
2023-01-19 14:37:10 -08:00
Konrad `ktoso` Malawski
54dec38a34 initial complete impl 2023-01-05 16:19:05 +09:00
Evan Wilde
c329bf6c4a [Backdeploy5.6] Hide task_future_wait[_throwing]
Symbols from the backdeploy libraries shouldn't be exported by dylibs.

Also found an undefined declaration (asyncMainDrainQueue) and a
duplicate declaration for `swift_task_getCurrent`, so those were cleaned
up.
2022-10-12 16:23:40 -07:00
Evan Wilde
64b19f7f45 Backdeploy task_wait_future fix to Swift 5.6 (#61254)
* Backdeploy swift_task_future_wait

This patch adds the implementation for `swift_task_future_wait`
entrypoint to the backdeploy library.

This involves pulling in `AsyncTask::waitFuture`, which relies on a fair
bit.

Please note, this pulls in the `StaticMutex` implementation from Swift
5.6. There are some challenges here. The concurrency version of the
`StaticMutex` involves a fairly nasty set of ODR violations in the
normal setup. See `public/Concurrency/Mutex.cpp`, which includes the
Mutex implementations cpp files directly, while defining a single macro
to replace the implementation of swift::fatalError with
swift_concurrency_fatalError. We only need the concurrency mutex (at
least for now), so I have hard-coded the `swift_concurrency_fatalError`
version into this library. If we should need the other implementation,
we are forced to include ODR-related undefined behavior.

We need symbols from C++, so I've added an implicit linker flag whenever
the static library is used, namely, it passes `-lc++` to the linker.
Since we only backdeploy on Apple platforms, this should be fine.

Some of the platform runtimes we need to backdeploy to have the
enter/exitThreadLocalContext functions defined, while others don't. We
define our own backdeploy56 shim function that dlsym's the function
pointer for these symbols if we have exclusivity checking available.
Otherwise, it doesn't do anything. If concurrency exclusivity checking
is available, we'll use it, otherwise we wont'.

The same dlsym check is done for `swift_task_escalate`. Not all
platforms we need to backdeploy to have a concurrency runtime. The
symbols that do need to use pieces of the concurrency runtime should not
be getting hit when deploying to systems that don't have concurrency. In
the event that you've gotten around the language blocking you from
calling these symbols and you've managed to call concurrency pieces
without using concurrency, we'll abort because something is seriously
wrong.

* Backdeploy swift_task_future_wait_throwing

Drop the remaining pieces in for adding
`swift_task_future_wait_throwing`.

* Apply task_wait_future fix

Actually apply the fix from ef80a315f8.

This deviates slightly from the original patch.

AsyncTask::PrivateStorage::_Status() does not exist in the Swift 5.6
library. Instead I am using `AsyncTask::PrivateStorage::Status`.

* Workaround missing compiler-rt linking

Working around the missing link against compiler-rt in these test.
They are a bit brittle as if anything in them uses compiler-rt, they
will start failing. The backdeploy 5.6 library uses some symbols from
compiler-rt, thus causes them to fail to link.

Disabling the runtime compatibility version checking to avoid these
symbols. This should be fine for the MicroStdlib test, but we should fix
'%target-ld' to handle this better in the future.
rdar://100868842
2022-10-07 09:36:17 -07:00
Egor Zhdan
84a1ffcb33 [Shims] Include SwiftShims headers without ../
This replaces a number of `#include`-s like this:
```
#include "../../../stdlib/public/SwiftShims/Visibility.h"
```
with this:
```
#include "swift/shims/Visibility.h"
```

This is needed to allow SwiftCompilerSources to use C++ headers which include SwiftShims headers. Currently trying to do that results in errors:
```
swift/swift/include/swift/Demangling/../../../stdlib/public/SwiftShims/module.modulemap:1:8: error: redefinition of module 'SwiftShims'
module SwiftShims {
       ^
Builds.noindex/swift/swift/bootstrapping0/lib/swift/shims/module.modulemap:1:8: note: previously defined here
module SwiftShims {
       ^
```
This happens because the headers in both the source dir and the build dir refer to SwiftShims headers by relative path, and both the source root and the build root contain SwiftShims headers (which are equivalent, but since they are located in different dirs, Clang treats them as different modules).
2022-09-14 11:14:50 +01:00
Evan Wilde
f7810ada24 Revert "Merge pull request #60459 from etcwilde/ewilde/revert-backdeploy56"
This reverts commit 93387f8a0b, reversing
changes made to 88304c327f.
2022-09-01 10:07:44 -07:00
Evan Wilde
15b3659484 Revert "Merge pull request #60368 from etcwilde/ewilde/backdeploy56"
This reverts commit a3941bf215, reversing
changes made to b39302a585.
2022-08-09 07:16:02 -07:00
Evan Wilde
2ac2249801 Stubbing out backdeploy library
This patch gets everything to the point of building the library, but it
doesn't run yet since I have missing symbols.

Unlike previous compatibility libraries and the concurrency
compatibility library, I'm organizing the headers a bit more. This is
because we're merging the two libraries into one. They share some common
header names, and while I could rename them for namespacing purposes,
it's easier to just use a directory structure for this.

The `include/Runtime` and corresponding `Runtime/` directories are for
backdeployed changes to the stdlib itself.

The `include/Concurrency` and corresponding `Concurrency/` directories
are for backdeployed changes to the concurrency runtimes.
2022-08-02 14:47:26 -07:00