Commit Graph

3151 Commits

Author SHA1 Message Date
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
Mike Ash
7a50a8fe67 [Runtime] Fix memory ordering and TSan compatibility in MetadataAllocator.
We need a store-release when putting a new page onto the metadata allocator so that other threads see the initialization. We also need to tell TSan about our memory barriers here, to avoid false positives in __swift_instantiateConcreteTypeFromMangledName when it reads from memory allocated here.

rdar://145778813
2025-03-14 12:46:34 -04:00
Ben Barham
a1a8bde65d [Runtime] Return ConformanceLookupResult from findConformanceWithDyld 2025-03-11 21:30:28 -07:00
Konrad `ktoso` Malawski
e056c63c89 [Concurrency] Implement isIsolatingCurrentContext requirement and mode (#79788) 2025-03-11 09:48:38 +09:00
Doug Gregor
d7f687619e Revert "[WebAssembly] Temporarily work around lack of __attribute__((constructor))"
This reverts commit 8ef9f7fcad.
2025-03-08 08:06:28 -08:00
Doug Gregor
8ef9f7fcad [WebAssembly] Temporarily work around lack of __attribute__((constructor))
We don't have a great way to ensure that the current-global-actor hook
will get installed by the concurrency library with WebAssembly, so
temporarily work around the issue by relying on the fact that we also
aren't doing actual concurrency with WebAssembly.
2025-03-07 23:52:25 -08:00
Doug Gregor
296e14662a Rework runtime entrypoints for isolated conformance checking
Replace the pair of global actor type/conformance we are passing around with
a general "conformance execution context" that could grow new functionality
over time. Add three external symbols to the runtime:

* swift_conformsToProtocolWithExecutionContext: a conforms-to-protocol check
  that also captures the execution context that should be checked before
  using the conformance for anything. The only execution context right now
  is for an isolated conformance.
* swift_isInConformanceExecutionContext: checks whether the function is
  being executed in the given execution context, i.e., running on the
  executor for the given global actor.
* swift_ConformanceExecutionContextSize: the size of the conformance
  execution context. Client code outside of the Swift runtime can allocate
  a pointer-aligned region of memory of this size to use with the runtime
  functions above.
2025-03-07 23:52:20 -08:00