Commit Graph

217 Commits

Author SHA1 Message Date
Mike Ash
ea785c191c [Runtime] Add ptrauth attribute to TargetGlobalActorReference conformance pointer.
When the TargetGlobalActorReference conformance is an indirect pointer, the indirect pointer is signed when ptrauth is enabled.

rdar://151945202
2025-05-28 21:06:20 -04:00
Alejandro Alonso
d26bde798e Start building the runtime demangle tree for extended existentials 2025-05-07 13:56:25 -07:00
Konrad 'ktoso' Malawski
e1dc854f8f [Concurrency] Change isIsolatingCurrent... to return Bool?
This changes the isIsolatingCurrentContext function to return `Bool?`
and removes all the witness table trickery we did previously to detect
if it was implemented or not. This comes at a cost of trying to invoke
it always, before `checkIsolated`, but it makes for an simpler
implementation and more checkable even by third party Swift code which
may want to ask this question.

Along with the `withSerialExecutor` function, this now enables us to
check the isolation at runtime when we have an `any Actor` e.g. from
`#isolation`.

Updates SE-0471 according to
https://forums.swift.org/t/se-0471-improved-custom-serialexecutor-isolation-checking-for-concurrency-runtime/78834/
review discussions
2025-04-29 20:08:55 +09:00
Nate Chandler
5534eb4043 [DefaultOverides] Install in vtables at runtime. 2025-03-25 07:22:44 -07:00
Nate Chandler
a4bf94dc95 [CoroutineAccessors] Store CFPs in descriptors.
As with AFPs, store CFPs in various descriptors.  Extend the execution
test correspondingly.
2025-03-19 12:03:24 -07: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
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
Doug Gregor
48aa75d86f [Isolated conformances] Cache resolved global actor for conformances
In the prior implementation of runtime resolution of isolated conformances,
the runtime had to look in both the protocol conformance descriptor and
in all conditional conformance requirements (recursively) to find any
isolated conformances. If it found one, it had to demangle the global
actor type to metadata. Since swift_conformsToProtocol is a hot path through
the runtime, we can't afford this non-constant-time work in the common
case.

Instead, cache the resolved global actor and witness table as part of the
conformance cache, so that we have access to this information every time
we look up a witness table for a conformance. Propagate this up through
various callers (e.g., generic requirement checking) to the point where
we either stash it in the cache or check it at runtime. This gets us down
to a very quick check (basically, NULL-or-not) for nonisolated conformances,
and just one check for isolated conformances.
2025-03-07 23:51:46 -08:00
Doug Gregor
951b535608 Metadata and runtime support for checking isolated conformances at runtime
Extend the metadata representation of protocol conformance descriptors
to include information about the global actor to which the conformance is
isolated (when there is one), as well as the conformance of that type to
the GlobalActor protocol. Emit this metadata whenever a conformance is
isolated.

When performing a conforms-to-protocol check at runtime, check whether
the conformance that was found is isolated. If so, extract the serial
executor for the global actor and check whether we are running on that
executor. If not, the conformance fails.
2025-03-07 23:51:25 -08:00
Nate Chandler
6581fec9e1 [CoroutineAccessors] PtrAuth. 2025-03-07 11:46:51 -08:00
Mike Ash
91e7daa693 [Reflection] Fix undersized reads in MetadataReader::readContextDescriptor.
Determining a descriptor's size requires reading its contents, but reading its contents from out of process requires knowing its size.

Build up the size incrementally by walking over the TrailingObjects. Take advantage of the fact that each trailing object's presence/count depends only on data that comes before it. This allows us to read prefixes that we gradually expand until we've covered the whole thing.

Add calls to TrailingObjects to allow iterating over the prefix sizes, and modify readContextDescriptor to use them. This replaces the old code which attempted to determine the descriptor size in an ad-hoc fashion that didn't always get it right.

rdar://146006006
2025-03-05 09:29:06 -05:00
Mike Ash
270ddf6bf4 [IRGen] Emit a pointer from nominal type descriptor to concrete metadata.
This allows external tools to locate the metadata pointer without needing to call the accessor function.

This is only useful for non-generic types, so we borrow the HasCanonicalMetadataPrespecializations flag to indicate the presence of this pointer on non-generic types, and it continues to indicate the presence of prespecializations for generic types.

Only emit this pointer for internal/private types with no runtime initialization. Public type metadata can be found with the symbol, and it's not useful for types that require runtime initialization.
2025-02-27 17:48:37 -05:00
Joe Groff
a184782a38 Introduce a Builtin.FixedArray type.
`Builtin.FixedArray<let N: Int, T: ~Copyable & ~Escapable>` has the layout of `N` elements of type `T` laid out
sequentially in memory (with the tail padding of every element occupied by the array). This provides a primitive
on which the standard library `Vector` type can be built.
2024-10-22 16:21:45 -07:00
Joe Groff
6e565b52ba Runtime: Set 'is not bitwise borrowable' bit for raw layout types.
When initializing metadata for layout-dependent raw layout types, set the
"is not bitwise borrowable" bit.
2024-09-27 11:27:47 -07:00
Alejandro Alonso
7c85261a77 Add runtime support 2024-09-04 15:13:27 -07:00
Egor Zhdan
b5a9da8942 [cxx-interop] NFC: update a couple outdated comments
`import_as_ref` attribute was renamed to `import_reference`.
2024-06-20 18:07:22 +01:00
Egor Zhdan
fd04cc3a4e [cxx-interop][Runtime] Initialize metadata of a Swift array of C++ references correctly
This fixes a crash at runtime when destroying a Swift array of values of a C++ foreign reference type.

Swift optimizes the amount of metadata emitted for `_ContiguousArrayStorage<Element>` by reusing `_ContiguousArrayStorage<AnyObject>` whenever possible (see `getContiguousArrayStorageType`). However, C++ foreign reference types are not `AnyObject`s, since they have custom retain/release operations.

This change disables the `_ContiguousArrayStorage` metadata optimization for C++ reference types, which makes sure that `swift_arrayDestroy` will call the correct release operation for elements of `[MyCxxRefType]`.

rdar://127154770
2024-05-15 19:20:44 +01:00
Doug Gregor
b84f8ab080 Rename "suppressible protocols" to "invertible protocols".
We've decided to use the "invertible protocols" terminology throughout
the runtime and compiler, so move over to that terminology
consistently.
2024-03-29 11:31:48 -07:00
Doug Gregor
af38d88ba3 Fix typos spotted by code review, thank you! 2024-03-22 08:28:45 -07:00
Doug Gregor
b167eece42 Metadata and runtime support for suppressible protocol requirements
Introduce metadata and runtime support for describing conformances to
"suppressible" protocols such as `Copyable`. The metadata changes occur
in several different places:

* Context descriptors gain a flag bit to indicate when the type itself has
  suppressed one or more suppressible protocols (e.g., it is `~Copyable`).
  When the bit is set, the context will have a trailing
  `SuppressibleProtocolSet`, a 16-bit bitfield that records one bit for
  each suppressed protocol. Types with no suppressed conformances will
  leave the bit unset (so the metadata is unchanged), and older runtimes
  don't look at the bit, so they will ignore the extra data.
* Generic context descriptors gain a flag bit to indicate when the type
  has conditional conformances to suppressible protocols. When set,
  there will be trailing metadata containing another
  `SuppressibleProtocolSet` (a subset of the one in the main context
  descriptor) indicating which suppressible protocols have conditional
  conformances, followed by the actual lists of generic requirements
  for each of the conditional conformances. Again, if there are no
  conditional conformances to suppressible protocols, the bit won't be
  set. Old runtimes ignore the bit and any trailing metadata.
* Generic requirements get a new "kind", which provides an ignored
  protocol set (another `SuppressibleProtocolSet`) stating which
  suppressible protocols should *not* be checked for the subject type
  of the generic requirement. For example, this encodes a requirement
  like `T: ~Copyable`. These generic requirements can occur anywhere
  that there is a generic requirement list, e.g., conditional
  conformances and extended existentials. Older runtimes handle unknown
  generic requirement kinds by stating that the requirement isn't
  satisfied.

Extend the runtime to perform checking of the suppressible
conformances on generic arguments as part of checking generic
requirements. This checking follows the defaults of the language, which
is that every generic argument must conform to each of the suppressible
protocols unless there is an explicit generic requirement that states
which suppressible protocols to ignore. Thus, a generic parameter list
`<T, Y where T: ~Escapable>` will check that `T` is `Copyable` but
not that it is `Escapable`, and check that `U` is both `Copyable` and
`Escapable`. To implement this, we collect the ignored protocol sets
from these suppressed requirements while processing the generic
requirements, then check all of the generic arguments against any
conformances not suppressed.

Answering the actual question "does `X` conform to `Copyable`?" (for
any suppressible protocol) looks at the context descriptor metadata to
answer the question, e.g.,

1. If there is no "suppressed protocol set", then the type conforms.
This covers types that haven't suppressed any conformances, including
all types that predate noncopyable generics.
2. If the suppressed protocol set doesn't contain `Copyable`, then the
type conforms.
3. If the type is generic and has a conditional conformance to
`Copyable`, evaluate the generic requirements for that conditional
conformance to answer whether it conforms.

The procedure above handles the bits of a `SuppressibleProtocolSet`
opaquely, with no mapping down to specific protocols. Therefore, the
same implementation will work even with future suppressible protocols,
including back deployment.

The end result of this is that we can dynamically evaluate conditional
conformances to protocols that depend on conformances to suppressible
protocols.

Implements rdar://123466649.
2024-03-21 14:57:47 -07:00
Konrad `ktoso` Malawski
c56a1e8be7 [Distributed] Handle mangling thunks in extensions with generic AS and $Stubs (#71914) 2024-02-29 04:22:00 -08:00
Konrad `ktoso` Malawski
e9c7f3c382 [Distributed] Target identifiers for protocol calls (#70928) 2024-02-16 07:19:20 -08:00
Mike Ash
4341102c92 [Tools] Add a library to build specialized generic metadata out of process.
This library uses GenericMetadataBuilder with a ReaderWriter that can read data and resolve pointers from MachO files, and emit a JSON representation of a dylib containing the built metadata.

We use LLVM's binary file readers to parse the MachO files and resolve fixups so we can follow pointers. This code is somewhat MachO specific, but could be generalized to other formats that LLVM supports.

rdar://116592577
2024-01-24 20:45:50 -05:00
Doug Gregor
40e07cf900 [Typed throws] IR generation and runtime support for function type metadata
Extend function type metadata with an entry for the thrown error type,
so that thrown error types are represented at runtime as well. Note
that this required the introduction of "extended" function type
flags into function type metadata, because we would have used the last
bit. Do so, and define one extended flag bit as representing typed
throws.

Add `swift_getExtendedFunctionTypeMetadata` to the runtime to build
function types that have the extended flags and a thrown error type.
Teach IR generation to call this function to form the metadata, when
appropriate.

Introduce all of the runtime mangling/demangling support needed for
thrown error types.
2023-10-29 09:12:32 -07:00
Doug Gregor
e82854d860 Reimplement TargetFunctionTypeMetadata using TrailingObjects
The layout is the same, but this eliminates the handwritten address
computations.
2023-10-29 09:12:32 -07:00
Dario Rexin
a0e48a3362 [Runtime] Assert when trying to get layout string from class 2023-08-21 11:02:59 -07:00
Dario Rexin
31de67c713 [IRGen+Runtime] Further fixes for layout strings 2023-08-21 10:54:14 -07:00
Dario Rexin
4a395cb8d3 [Runtime+IRGen] Some fixes and optimizations for layout strings 2023-08-21 10:53:44 -07:00
Dario Rexin
c25b3b43a2 [Runtime] Restructure BytecodeLayouts 2023-08-21 10:52:24 -07:00
Pavel Yaskevich
eee923bc42 [IRGen] [AST] NFC: Remove @runtimeMetadata related code and metadata records 2023-08-15 12:17:31 -07:00
Mike Ash
dbd2f82739 [Runtime] Fix up the heap destroyer in objc_setClassCopyFixupHandler.
We fix up the VWT pointer, but not the heap destroyer. This doesn't matter for classes which use ObjC refcounting, which is the common case for dynamic subclasses, because that doesn't use the heap destroyer pointer. But it does matter for classes that use native Swift refcounting, such as classes that don't inherit from NSObject, or actors.

rdar://113657917
2023-08-10 15:19:18 -04:00
Mike Ash
fe7e13bba5 [Runtime][IRGen] Sign type context descriptor pointers.
Ensure that context descriptor pointers are signed in the runtime by putting the ptrauth_struct attribute on the types.

We use the new __builtin_ptrauth_struct_key/disc to conditionally apply ptrauth_struct to TrailingObjects based on the signing of the base type, so that pointers to TrailingObjects get signed when used with a context descriptor pointer.

We add new runtime entrypoints that take signed pointers where appropriate, and have the compiler emit calls to the new entrypoints when targeting a sufficiently new OS.

rdar://111480914
2023-07-07 18:10:35 -04:00
Slava Pestov
d1cb9307c9 Runtime: Support for conditional conformances with pack conformance requirements 2023-06-21 13:42:57 -04:00
Slava Pestov
17086962c3 Runtime: Add swift_getTypePackByMangledName() entry point
For now this has SWIFT_RUNTIME_LIBRARY_VISIBILITY, but in the
future we might want to make it public so that IRGen can use it
to build packs out of concrete types.
2023-03-09 01:54:51 -05:00
Slava Pestov
8731eb4394 Runtime: On-heap packs know their own length 2023-03-09 01:54:51 -05:00
Dario Rexin
5fe716577b [IRGen] Add layout strings for generic and resilient types (#64023)
* [IRGen] Add layout strings for generic and resilient types

rdar://105837048

* Add some corner cases

* Add flag to enable generic instantiation and some fixes

* Fix resilient types

* Fix metadata accessor function pointers in combined layout strings
2023-03-05 15:26:45 -08:00
Slava Pestov
ac199ac7c6 Runtime: Metadata cache key support for packs 2023-03-01 15:48:01 -05:00
Slava Pestov
864f5f0b5f Runtime: GenericParamPackShapeDescriptor => GenericPackShapeDescriptor
We also need to track witness table packs, and store an index here for
convenient access from the metadata caching key comparison logic.
2023-03-01 14:58:08 -05:00
Slava Pestov
5215406bb5 Runtime: Plumb through the pack shape stuff in more places 2023-03-01 12:14:58 -05:00
Slava Pestov
70c56dee0c Runtime: Fix incorrect numTrailingObjects() overload in TargetExtendedExistentialTypeShape 2023-03-01 12:14:58 -05:00
Slava Pestov
d764167783 Runtime: Fix Windows build 2023-02-28 02:12:32 -05:00
Slava Pestov
560aad797e Runtime: Wrap pack pointers in a fancy TargetPackPointer template instead of twiddling the LSB by hand
This is more type safe in theory, but in practice it's just catnip for C++ fans.
2023-02-28 02:12:32 -05:00
Dario Rexin
a8d4d57f11 [IRGen] Generate compressed representation of value witnesses (#63813)
rdar://105837040

* WIP: Store layout string in type metadata

* WIP: More cases working

* WIP: Layout strings almost working

* Add layout string pointer to struct metadata

* Fetch bytecode layout strings from metadata in runtime

* More efficient bytecode layout

* Add support for interpreted generics in layout strings

* Layout string instantiation, take and more

* Remove duplicate information from layout strings

* Include size of previous object in next objects offset to reduce number of increments at runtime

* Add support for existentials

* Build type layout strings with StructBuilder to support target sizes and metadata pointers

* Add support for resilient types

* Properly cache layout strings in compiler

* Generic resilient types working

* Non-generic resilient types working

* Instantiate resilient type in layout when possible

* Fix a few issues around alignment and signing

* Disable generics, fix static alignment

* Fix MultiPayloadEnum size when no extra tag is necessary

* Fixes after rebase

* Cleanup

* Fix most tests

* Fix objcImplementattion and non-Darwin builds

* Fix BytecodeLayouts on non-Darwin

* Fix Linux build

* Fix sizes in linux tests

* Sign layout string pointers

* Use nullptr instead of debug value
2023-02-24 15:40:28 -08:00
Arnold Schwaighofer
0f9bfac94b Remove undefined behavior in RelativeWitnessTable::getDescription() 2023-02-02 07:02:21 -08:00
Arnold Schwaighofer
770648f161 Initial runtime changes to support relative protocol witness tables 2023-01-31 10:59:37 -08:00
Pavel Yaskevich
55a6b3442f [IRGen] RuntimeMetadata: Reference type descriptors indirectly when required
Type descriptors cannot always be referenced directly (i.e. when
a type is declared in a different module), so let's use
`getAddrOfLLVMVariableOrGOTEquivalent` facility that knows how
to handle that correctly.

Resolves: rdar://problem/103878515
2023-01-04 14:19:20 -08:00
Pavel Yaskevich
0d082df497 [ABI] RuntimeMetadata: Adjust type field to make it a symbolic reference
This is due to the fact that IRGen has been switched to use `getTypeRef`.
2023-01-04 12:13:59 -08:00
Pavel Yaskevich
8de03a2b38 [ABI] Metadata: Add a runtime discoverable attribute and its entry 2022-12-20 09:45:01 -08:00
Erik Eckstein
0138d04390 fix an error in Metadata.h when building with a newer clang
rdar://103311131
2022-12-19 17:57:30 +01:00
Anthony Latsis
4cb63c8a0f [NFC] Migrate remnant Jira issue references to GitHub issues 2022-12-04 08:20:34 +03:00