Commit Graph

3157 Commits

Author SHA1 Message Date
Dmitrii Galimzianov
7989dbe24e Protocol conformance cache for generic types
This change adds a new type of cache (cache by type descriptor) to the protocol conformance lookup system. This optimization is beneficial for generic types, where the
same conformance can be reused across different instantiations of the generic type.

Key changes:
- Add a `GetOrInsertManyScope` class to `ConcurrentReadableHashMap` for performing
  multiple insertions under a single lock
- Add type descriptor-based caching for protocol conformances
- Add environment variables for controlling and debugging the conformance cache
- Add tests to verify the behavior of the conformance cache
- Fix for https://github.com/swiftlang/swift/issues/82889

The implementation is controlled by the `SWIFT_DEBUG_ENABLE_CACHE_PROTOCOL_CONFORMANCES_BY_TYPE_DESCRIPTOR`
environment variable, which is enabled by default.
2025-07-11 03:54:42 +02:00
Doug Gregor
15123f5771 [Runtime] Don't fail conformances when the global actor hook is missing
When the concurrency library's "is current global actor" hook is not
available, assume that we are already executing on the right global
actor. This mimics the behavior of earlier standard libraries when
faced with an isolated conformance, as well as dealing with odd
configurations where the code might not load the concurrency library
yet

At the moment, WebAssembly ends up in this configuration because we
don't run the initialization for the concurrency library. That makes
this also a workaround for issue #82682 / rdar://154762027.
2025-07-05 08:50:26 -07:00
Alastair Houghton
76f235154d Merge pull request #82624 from al45tair/eng/PR-154282813
[Backtracing][Linux] Fix crash handler for musl.
2025-07-01 11:17:28 +01:00
Alastair Houghton
b498b39b79 [Backtracing][Linux] Fix crash handler for musl.
Musl's `clone()` wrapper returns `EINVAL` if you try to use `CLONE_THREAD`,
which seems a bit wrong (certainly it is in this particular application,
since we *really* don't care whether the thread is a valid C library
thread at this point).

Also properly support ELF images that are built with a base address other
than zero (this typically isn't an issue for dynamically linked programs,
as they will be relocated at runtime anyway, but for statically linked
binaries it's very common to set the base address to a non-zero value).

rdar://154282813
2025-06-30 14:21:27 +01:00
David Smith
08233afee9 Extend the existing memoization of NSString's Hashable conformance to cover AnyObjects that are actually NSStrings as well (#82440)
Fixes rdar://154123172 (Consider memoizing the lookup for NSString's
conformance to Hashable)
2025-06-24 08:21:13 -07:00
Anthony Latsis
8e16bc0644 runtime: Silence a -Wglobal-constructors error than appears with upstream clang
Landing this on main to minimize rebranch-specific changes.
2025-06-02 17:20:57 +01:00
Mike Ash
dfc964791b Merge pull request #81562 from mikeash/error-object-weak-reference
[Runtime] Allow weak references to ErrorObjects.
2025-05-19 13:33:59 -04:00
Mike Ash
aea9c0c65a [Runtime] Allow weak references to ErrorObjects.
When ObjC interop is not available, Error values are represented in ErrorObject boxes. These are full HeapObjects, but unowned refcounting ops asserted that the metadata was class metadata. This assert would be hit when destroying an ErrorObject that was weakly referenced. Expand the asserts to accept ErrorObject metadata as well.

rdar://150214921
2025-05-16 14:01:29 -04:00
David Smith
e795eb0d13 Cache the last ObjC bridging conformance we looked up (#81545)
Fixes rdar://151475392
2025-05-16 11:00:13 -07:00
Alejandro Alonso
d26bde798e Start building the runtime demangle tree for extended existentials 2025-05-07 13:56:25 -07:00
Mike Ash
22c34dd254 Merge pull request #81041 from mikeash/unowned-destroyed-error-message
[Runtime] Change the unowned reference fatal error to "was already destroyed."
2025-04-28 19:49:06 -04:00
Konrad 'ktoso' Malawski
284c060d0a [Distributed] thread-safety also for parameter type metadata
We had fixed this bug in https://github.com/swiftlang/swift/pull/79381
but missed to realize the same problem existed for parameters as well.

This corrects the swift_func_getParameterTypeInfo impl, and also removes
the entire "unsafe" method, we no longer use it anywhere.

Resolves rdar://146679254
2025-04-26 21:30:13 +09:00
Mike Ash
66111530e1 [Runtime] Change the unowned reference fatal error to "was already destroyed."
The "was already deallocated" message is incorrect, since the target of an unowned reference stays allocated even after being deinitialized. We could say "was already deinitialized" but that's a bit of a niche term. "Was already destroyed" conveys what happened without the reader needing to worry about deinitialization versus deallocation.

rdar://149237704
2025-04-23 15:28:16 -04:00
Evan Wilde
6f39a52afc [SwiftCore]: Don't always clobber memory
The new build system set `SWIFT_RUNTIME_CLOBBER_FREED_OBJECTS` to 0.
Unfortunately, the check in the Swift runtime used `#ifdef`, so even
though it was turned off, it was actually enabled in some cases.

Fixing the issue in the build system as well as switching the check to
verify that value of `SWIFT_RUNTIME_CLOBBER_FREED_OBJECTS` is taken into
account in the sources. C/C++ implicitly defines macro values to 1 when
set without a value and 0 when it is not set.

Also making the hex a bit more recognizable and grep'able by including
it as a comment.

Fixes: rdar://149210738
2025-04-22 16:51:28 -07:00
Doug Gregor
885f829a63 [Conformance cache] Add allocated extended-storage for entries to the free list
This memory is part of the conformance cache concurrent hash map, so
when we clear the conformance cache, record each of the allocated
pointers within the concurrent map's free list. This way, it'll be
freed with the rest of the concurrent map when it's safe to do so.
2025-04-16 15:11:59 -07:00
Doug Gregor
d576fa30c9 [Runtime] Replace use of C++ new with malloc() 2025-04-16 14:04:14 -07:00
Doug Gregor
9eebee8b7c Merge pull request #80844 from DougGregor/conformance-cache-entry-stole-the-wrong-bit
[Runtime] Don't use the low bit of a WitnessTable pointer in the conformance cache
2025-04-16 13:45:16 -07:00
Doug Gregor
5f0043711c [Conformance cache] Handle missing protocol descriptors
This can happen when running against an older version of a library that
doesn't have the protocol defined.
2025-04-15 21:11:19 -07:00
Doug Gregor
aeb7056991 [Runtime] Don't use the low bit of a WitnessTable pointer in the conformance cache
With relative witness tables, the low bit of a witness table pointer is
an indicator that we need to load from the given pointer. We were also
using the low bit of the witness table pointer in the conformance
cache entry as part of a pointer union. Hilarity ensures [*].

Switch to another low bit by exploding the conformance cache key
into separate fields and taking the low bit of one of those pointers
that isn't reserved.

Fixes the remainder of rdar://149326058, I hope.

[*] No, I am not laughing.
2025-04-15 18:12:18 -07:00
Doug Gregor
663ef8aa2e [Runtime] Handle relative witness tables in _getIsolationCheckingOptionsFromExecutorWitnessTable
Part of rdar://148687148.
2025-04-15 13:47:26 -07:00
Mike Ash
ee5290a5ee Merge pull request #80600 from mikeash/check-witness-table-isolation-ptrauth
[Runtime] Handle relative witness tables in _checkWitnessTableIsolation.
2025-04-09 13:14:48 -04:00
Mike Ash
f01d46ce38 [Runtime] Handle relative witness tables in _checkWitnessTableIsolation.
We need to use lookThroughOptionalConditionalWitnessTable when relative witness tables are being used.

rdar://148687148
2025-04-08 16:14:48 -04:00
Mike Ash
1f0696ae05 Merge pull request #80505 from mikeash/metadata-cycle-race-fix
[Runtime] Fix a false metadata cycle diagnostic when threads race to instantiate cyclical metadata.
2025-04-07 16:59:27 -04:00
Mike Ash
9ad534bc4a [Runtime] Fix a false metadata cycle diagnostic when threads race to instantiate cyclical metadata.
The metadata creation system detects cycles where metadata depends on other metadata which depends on the first one again and raises a fatal error if the cycle can't be fulfilled.

Some cycles can be fulfilled. The cycle may involve a requirement for a metadata state less than full transitive completeness which can be reached without resolving the entire cycle. We only want to raise a fatal error when we detect a cycle that can't be fulfilled.

Normally this happens because the cycle checking in `blockOnMetadataDependency` only sees a cycle when it can't be fulfilled. Metadata initialization is advanced as far as it can be at each stage, so a cycle that can be fulfilled will see a fulfilling state and won't generate the dependency in the first place, since we only generate dependencies that haven't yet been met.

However, when two threads race to create types in a cycle, we can end up with such a dependency, because the dependency may be generated before another thread fulfilled yet. The cycle checker doesn't account for this and incorrectly raises a fatal error in that case.

Fix this by checking the cyclic dependency against the metadata's current state. If we have a dependency that's already been fulfilled, then there isn't really a dependency cycle. In that case, don't raise a fatal error.

rdar://135036243
2025-04-04 17:49:55 -04:00
Nate Chandler
66ca0d8d4b [CoroutineAccessors] Use async bit in descriptors.
To facilitate back deployment, make use of the fact that the async bit
has up to now never been set for read and modify accessors and claim
that set bit to indicate that it is a callee-allocated coroutine.  This
has the virtue of being completely back deployable because like async
function pointers coro function pointers must be auth'd and signed as
data.
2025-04-03 19:49:44 -07:00
Julian Lettner
a7ade95529 Remove Malloc Type Descriptor cache (#80355)
Remove Malloc Type Descriptor cache and trivialize
`computeMallocTypeSummary()` to only provide
language.  The remaining info in
`malloc_type_summary_t` are currently not used by
the allocator.

The principled, long-term solution is to have the
Swift compiler compute type descriptors for Swift
types.

rdar://137993434
2025-04-01 10:07:29 -07:00
Allan Shortlidge
8f2d5a759e Merge pull request #80321 from tshortli/warnings 2025-03-30 16:14:21 -07:00
Allan Shortlidge
aef0d2105b Runtime: Fix unused declaration warnings in Backtrace.cpp. 2025-03-28 12:33:39 -07:00
Nate Chandler
2af30b2f04 [NFC] CoroutineAccessors: Remove old runtime files
The functions are open-coded now.
2025-03-27 19:23:09 -07:00
Nate Chandler
c141586838 [CoroutineAccessors] Open code dealloc fn. 2025-03-27 19:23:09 -07:00
Nate Chandler
dd238343bb [CoroutineAccessors] Open code alloc fn.
Replace the runtime function with an open-coded version.
2025-03-27 19:23:09 -07:00
nate-chandler
b878155a5a Merge pull request #80335 from nate-chandler/general-coro/20250326/2
[CoroutineAccessors] Move functions and add dealloc bit.
2025-03-27 19:19:25 -07:00
Nate Chandler
dbcd038959 [CoroutineAccessors] Add wrapper for dealloc.
Make the fast path faster by avoiding a dyld stub.
2025-03-27 11:37:26 -07:00
Nate Chandler
f30d8a603c [CoroutineAccessors] Rehome runtime functions.
Put them where they're meant to be, in swiftCore.
2025-03-27 11:37:26 -07:00
Doug Gregor
edd118af72 [Runtime] Fix dynamic cast call for non-ObjC runtime 2025-03-27 06:59:03 -07:00
Doug Gregor
43df05a89c [SE-0470] Prohibit isolated conformances in dynamic casts marked as such
Certain dynamic casts cannot work safely with isolated conformances,
regardless of what executor the code runs on. For such cases, reject
all attempts to conform to the type.
2025-03-26 22:31:52 -07:00
Mike Ash
5f5d1cfae6 Merge pull request #80119 from mikeash/allocation-failure-fatal-error
[Runtime] Include size/alignment in allocation failure fatal error message.
2025-03-25 18:32:21 -04:00
Mike Ash
ed3e6356ec Merge pull request #80243 from mikeash/ignore-constructor-warnings
[Runtime] Ignore some global constructor warnings.
2025-03-25 17:42:16 -04:00
Nate Chandler
5534eb4043 [DefaultOverides] Install in vtables at runtime. 2025-03-25 07:22:44 -07:00
Nate Chandler
ddb89af283 [NFC] Runtime: Return early here.
In preparation for adding more work to this branch.
2025-03-25 07:22:43 -07:00
Nate Chandler
9a9c2ffee8 [NFC] Runtime: Use a range-based for loop.
Replace this direct use of the count in a table header as the upper
bound of a for loop with a range-based for loop over the range which was
being indexed into.  That range was constructed using that count to
begin with.
2025-03-25 07:22:13 -07:00
Nate Chandler
c576c61122 [NFC] Runtime: Extract fields from param aggregate
Rather than pass a MethodOverrideDescriptor directly, instead pass the
fields from it that are needed by the callee.  In preparation for adding
another caller which doesn't have a MethodOverrideDescriptor.
2025-03-25 07:22:13 -07:00
Nate Chandler
4bfdaed78a [NFC] Runtime: Extract function.
Break this chunk of functionality out into a separate function in
preparation for adding another caller.
2025-03-25 07:22:13 -07:00
Nate Chandler
5d53d3bdaa [NFC] Runtime: Mark parameter const.
The implementation doesn't depend on it being non-const and a
forthcoming caller will require it be const.
2025-03-25 07:22:13 -07:00
Saleem Abdulrasool
82dc5bba03 runtime: adjust API usage for Windows (NFCI)
This replaces `strdup` with `_strdup` on Windows to avoid the POSIX API
deprecation warnings. It also does a minor simplification by removing
the superfluous `else`.
2025-03-24 15:00:10 -07:00
Mike Ash
6a383b270e [Runtime] Ignore some global constructor warnings.
We have a few constructor functions that aren't wrapped in SWIFT_ALLOWED_RUNTIME_GLOBAL_CTOR_BEGIN/SWIFT_ALLOWED_RUNTIME_GLOBAL_CTOR_END and which have started to produce warnings in a new clang version. Explicitly allow these constructors by adding those.

rdar://147703947
2025-03-24 12:43:03 -04:00
Alexander Kornienko
dd83d1e1e0 Fix a compilation error
This fixes a `copy constructor must pass its first argument by reference` compilation error when compiled with a recent enough Clang (after fe0d3e3764)
2025-03-24 11:46:10 +01:00
Hamish Knight
f8ab391737 Introduce type sugar for InlineArray (#80087)
* [CS] Decline to handle InlineArray in shrink

Previously we would try the contextual type `(<int>, <element>)`,
which is wrong. Given we want to eliminate shrink, let's just bail.

* [Sema] Sink `ValueMatchVisitor` into `applyUnboundGenericArguments`

Make sure it's called for sugar code paths too. Also let's just always
run it since it should be a pretty cheap check.

* [Sema] Diagnose passing integer to non-integer type parameter

This was previously missed, though would have been diagnosed later
as a requirement failure.

* [Parse] Split up `canParseType` 

While here, address the FIXME in `canParseTypeSimpleOrComposition`
and only check to see if we can parse a type-simple, including
`each`, `some`, and `any` for better recovery.

* Introduce type sugar for InlineArray

Parse e.g `[3 x Int]` as type sugar for InlineArray. Gated behind
an experimental feature flag for now.
2025-03-23 15:31:37 -07:00
Mike Ash
953a7a5b74 [Runtime] Include size/alignment in allocation failure fatal error message.
It's hard to tell why a crash occurred with just "Could not allocate memory." Modify the message to include the size/alignment, which will help distinguish between an actual lack of memory and a request for an excessively large allocation.

While we're in there, add \n to a bunch of other fatal error helper functions that didn't have it.
2025-03-19 11:18:43 -04:00
Konrad `ktoso` Malawski
85fcd69833 [Concurrency] Implement detecting isIsolatingCurrentContext user impls (#79946)
* [Concurrency] Detect non-default impls of isIsolatingCurrentContext

* [Concurrency] No need for trailing info about isIsolating... in conformance

* Apply changes from review
2025-03-18 09:39:11 +09:00