Commit Graph

652 Commits

Author SHA1 Message Date
Slava Pestov
01acda7730 Runtime: Remove linear order on metadata cache keys 2023-03-02 10:22:11 -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
Slava Pestov
5663910827 Runtime: Add swift_allocateMetadataPack() and swift_allocateWitnessTablePack()
Both return the pack immediately if its already heap allocated, by
checking the least significant bit of the pack pointer.

Then,

- swift_allocateMetadataPack() uniques the metadata pack by the
  pointer equality of elements.

- swift_allocateWitnessTablePack() does not unique the pack.

Both return a pack pointer with the least significant bit set,
indicating heap allocation.
2023-02-28 02:12:32 -05:00
Dario Rexin
96d988a431 [Runtime] Move bytecode layout handling into runtime (#63901)
rdar://105904548
2023-02-25 10:18:45 -08: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
1fa68ff82b Sign relative protocol witness tables
rdar://98583748
2023-02-20 12:29:13 -08:00
Arnold Schwaighofer
9e2308295d runtime: conditionally generate relative protocol witness tables entry points 2023-02-03 08:54:58 -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
Erik Eckstein
77fd3a7004 runtime: fix a wrong fall-through caused by an assert(false)
Replace the `assert(false)` with `swift_unreachable`.
This is more robust in release builds where the `assert(false)` is a no-op and would result in a wrong fall-though to the next switch case.
Fixes a warning in release builds.
2022-11-14 20:40:56 +01:00
Anthony Latsis
c731089068 Gardening: Migrate stdlib sources to GH issues 2022-09-26 06:30:49 +03:00
Anton Korobeynikov
c89e270b7d Implement value witness table for @differentiable functions (#60875)
@differentiable function is actually a triple (function, jvp, vjp). Previously normal thick function value witness table was used. As a result, for example,  only function was copied, but none of differential components.

This was the cause of uninitialized memory accesses and subsequent segfaults.

Should fix now unavailable TF-1122
2022-09-01 03:09:39 -07:00
Mike Ash
91b86912e6 [Runtime] Fix rare crash when fixing up dynamic subclasses.
Swift vtables can contain NULL entries. This happens when the compiler is able to prove that a method is never called.

When ptrauth is enabled, those NULL entries are still signed. However, there's an inconsistency in the runtime. Most places where we copy a vtable with swift_ptrauth_copy_code_or_data, we accept an unsigned NULL value. But when fixing up a dynamic ObjC subclass with swift_objc_classCopyFixupHandler, we don't.

Since the compiler seems to always sign these NULL values, this still works, except in the case where a signed NULL value coincidentally has a ptrauth signature of all zeroes. When that happens, the code to accept an unsigned NULL value sees this NULL with an all-zero signature as an unsigned NULL, and copies it verbatim. Then we really do have an unsigned NULL in the destination.

If that class is then dynamically subclassed, we'll call swift_objc_classCopyFixupHandler which does not accept unsigned NULL values, and fail ptrauth.

Adjust swift_objc_classCopyFixupHandler to accept unsigned NULL like the rest. These entries should never be called, and any attempt to call them will crash regardless, so it makes little difference.

rdar://92800322
2022-07-25 22:38:55 -04:00
zoecarver
f972f664d3 [cxx-interop] Runtime support for foreign reference types. 2022-06-14 12:18:05 -07:00
Alastair Houghton
0e9318cec5 [Threading] Put everything through git clang-format.
Just formatting changes.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
66f6eb6bc6 [Runtime][Windows] A couple of files need to include <windows.h>
We shouldn't include <windows.h> implicitly from .cpp files, but should
do it directly so that we know it's there.

Also, if we're including <windows.h>, do it at the top of the file.

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
f5bdb858e0 [Threading] Create new threading library and use it.
Moved all the threading code to one place.  Added explicit support for
Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new
implementations of Once for Linux, Pthreads, C11 and Win32.

rdar://90776105
2022-06-07 07:39:51 +01:00
Alex Hoppen
4aa2bbbf06 Revert "Merge pull request #42447 from al45tair/eng/PR-90776105"
This reverts commit 8bcb71140f, reversing
changes made to c4dd271d36.
2022-06-02 18:03:23 +02:00
Alastair Houghton
b5bd267ff1 [Threading] Put everything through git clang-format.
Just formatting changes.

rdar://90776105
2022-05-24 14:57:41 +01:00
Alastair Houghton
79e2d0d37d [Runtime][Windows] A couple of files need to include <windows.h>
We shouldn't include <windows.h> implicitly from .cpp files, but should
do it directly so that we know it's there.

Also, if we're including <windows.h>, do it at the top of the file.

rdar://90776105
2022-05-24 14:57:40 +01:00
Alastair Houghton
63a09007a1 [Threading] Create new threading library and use it.
Moved all the threading code to one place.  Added explicit support for
Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new
implementations of Once for Linux, Pthreads, C11 and Win32.

rdar://90776105
2022-05-24 14:57:39 +01:00
Jonathan Grynspan
770fd107de 58686: swift_slowAlloc() _et al._ should be marked returns-nonnull to improve codegen 2022-05-05 23:18:48 -04:00
Jonathan Grynspan
b749dd395d Revert "runtime: allow over-aligned types in the runtime"
This reverts commit b694ce4634.
2022-05-05 11:10:12 -04:00
Robert Widmann
9918050dfa [Remote Mirrors] Support Extended Existential Type Metadata
Teach Remote Mirrors to read extended existential type metadata.
2022-04-27 13:18:50 -07:00
Robert Widmann
e15ca51526 Update Runtime Generalized Existential Metadata
Tidy up the metadata definitions.

* Generalize a number of metadata kinds for out-of-process clients
* Introduce conveniences to make runtime lookups easier
* Introduce TargetExistentialTypeExpression to TrailingObjects stops complaining about OverloadTokens being ambiguous

Note that there is no impact on the layout of the metadata - the changes here are all ABI-compatible.
2022-04-25 16:43:51 -07:00
John McCall
bd77714537 Unique extended existential shapes using the generalized AST type.
I wrote out this whole analysis of why different existential types
might have the same logical content, and then I turned around and
immediately uniqued existential shapes purely by logical content
rather than the (generalized) formal type.  Oh well.  At least it's
not too late to make ABI changes like this.

We now store a reference to a mangling of the generalized formal
type directly in the shape.  This type alone is sufficient to unique
the shape:

- By the nature of the generalization algorithm, every type parameter
  in the generalization signature should be mentioned in the
  generalized formal type in a deterministic order.

- By the nature of the generalization algorithm, every other
  requirement in the generalization signature should be implied
  by the positions in which generalization type parameters appear
  (e.g. because the formal type is C<T> & P, where C constrains
  its type parameter for well-formedness).

- The requirement signature and type expression are extracted from
  the existential type.

As a result, we no longer rely on computing a unique hash at
compile time.

Storing this separately from the requirement signature potentially
allows runtimes with general shape support to work with future
extensions to existential types even if they cannot demangle the
generalized formal type.

Storing the generalized formal type also allows us to easily and
reliably extract the formal type of the existential.  Otherwise,
it's quite a heroic endeavor to match requirements back up with
primary associated types.  Doing so would also only allows us to
extract *some* matching formal type, not necessarily the *right*
formal type.  So there's some good synergy here.
2022-04-21 23:47:01 -04:00
Josh Soref
5fab3d1f58 Spelling stdlib/public/runtime (#42439)
* spelling: access

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: bridgeable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: canonical

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: clazz

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: compatibility

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: language

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: necessary

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: platform

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: recursive

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: related

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: repeated

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: satisfy

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: that

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: the

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: verification

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-19 14:03:03 -07:00
Saleem Abdulrasool
a8b0ee24dc runtime: blanket application of namespacing and inclusion of new
Apply a blanket pass of including `new` for the placement new allocation
and namespacing the call to the global placement new allocator.  This
should repair the Android ARMv7 builds.
2022-04-14 14:21:12 -07:00
Saleem Abdulrasool
b694ce4634 runtime: allow over-aligned types in the runtime
Not all targets have a 16-byte type alignment guarantee.  For the types
which are not naturally aligned, provide a type specific `operator new`
overload to ensure that we are properly aligning the type on allocation
as we run the risk of under-aligned allocations otherwise.

This should no longer be needed with C++17 and newer which do a two
phase `operator new` lookup preferring
`operator new(std::size, std::align_val_t)` if needed.  The base type
would be fully pre-processed away.  The empty base class optimization
should help ensure that we do not pay any extra size costs for the
alignment fixes.

As we are a C++14 codebase, we must locally implement some of the
standard type_traits utilities, namely `void_t`.  We take the minimal
definition here, assuming that the compiler is up-to-date with C++14 DR
reports which fixed an issue in SFINAE.  We use the SFINAE for detecting
the presence of the `operator new` overload to guide the over-alignment,
which is inherited through the new `swift::overaligned_type<>`  base
type.

Annotate the known classes which request explicit alignment which is
non-pointer alignment.  This list was identified by
`git grep ' alignas(.*) '`.
2022-04-04 18:26:20 -07:00
Yuta Saito
8c598e98f7 [Wasm][Runtime] Interpret absolute function pointer in runtime structures
When SWIFT_COMPACT_ABSOLUTE_FUNCTION_POINTER is enabled, relative direct
pointers whose pointees are functions will be turned into absolute
pointer at compile-time.
2022-04-02 04:32:41 +00:00
John McCall
148357f899 Implement extended existential shapes and type metadata.
The immediate use case is only concretely-constrained existential
types, which could use a much simpler representation, but I've
future-proofed the representation as much as I can; thus, the
requirement signature can have arbitrary parameters and
requirements, and the type can have an arbitrary type as the
sub-expression.  The latter is also necessary for existential
metatypes.

The chief implementation complexity here is that we must be able
to agree on the identity of an existential type that might be
produced by substitution.  Thus, for example, `any P<T>` when
`T == Int` must resolve to the same type metadata as
`any P<Int>`.  To handle this, we identify the "shape" of the
existential type, consisting of those parts which cannot possibly
be the result of substitution, and then abstract the substitutable
"holes" as an application of a generalization signature.  That
algorithm will come in a later patch; this patch just represents
it.

Uniquing existential shapes from the requirements would be quite
complex because of all the symbolic mangled names they use.
This is particularly true because it's not reasonable to require
translation units to agree about what portions they mangle vs.
reference symbolically.  Instead, we expect the compiler to do
a cryptographic hash of a mangling of the shape, then use that
as the unique key identifying the shape.

This is just the core representation and runtime interface; other
parts of the runtime, such as dynamic casting and demangling
support, will come later.
2022-03-27 18:24:17 -04:00
John McCall
91c1d79665 [NFC] Add GenericSignatureLayout and use it in metadata caching
We'll need more information than this when we introduce packs,
so we might as well create a common abstraction for it now.
2022-03-24 00:53:45 -04:00
Kuba (Brecka) Mracek
e3b9c987b1 Fix RelativePtr ptrauth in swift_getAssociatedConformanceWitnessSlowImpl to sign *then* cast (#41354) 2022-02-12 06:47:24 -08:00
Saleem Abdulrasool
1b5b9d3573 Runtime: change atomic implementation for AllocationPool
The `AllocationPool` type is reflected upon and inspected by debugging
tools.  This requires that the layout of the atomically wrapped type
must store the value at offset 0.  However, the `std::atomic` does not
make any such guarantees, and a layout differing would render debug
utilities useless.  Switch instead from a `std::atomic` to
`swift::atomic`.  In order to do so, we also need to introduce the
`compare_exchange_strong` helper which maps to
`compare_exchange_strong_explicit` mirroring `compare_exchange_weak`.
2022-02-06 12:37:48 -08:00
Kuba (Brecka) Mracek
a268f02c65 Account for -disable-preallocated-instantiation-caches for non-dependent wtables in swift_getWitnessTable (#41199) 2022-02-04 06:52:36 -08:00
Kuba (Brecka) Mracek
17c5d6f0de Add a flag to disable compile-time preallocated instantiation caches (for type metadata and protocol conformances) (#41148) 2022-02-02 12:06:16 -08:00
Kuba (Brecka) Mracek
c92f2a24ed When ptrauth-copying vtable/wtables, allow NULL entries (due to VFE) (#40578)
* When ptrauth-copying vtable/wtables, allow NULL entries (due to VFE)

* Mark virtual-function-elimination-generics-exec.swift UNSUPPORTED: arm64e until the rebranch

* Fix test expectations
2021-12-16 16:49:24 -08:00
John McCall
749ed09f8c Eliminate priority inversions in the metadata completion runtime.
The current system is based on MetadataCompletionQueueEntry
objects which are allocated and then enqueued on dependencies.
Blocking is achieved using a condition variable associated
with the lock on the appropriate metadata cache.  Condition
variables are inherently susceptible to priority inversions
because the waiting threads have no dynamic knowledge of
which thread will notify the condition.  In the current system,
threads that unblock dependencies synchronously advance their
dependent metadata completions, which means the signaling
thread is unreliable even if we could represent it in condition
variables.  As a result, the current system is wholly unsuited
for eliminating these priority inversions.

An AtomicWaitQueue is an object containing a lock.  The queue
is eagerly allocated, and the lock is held, whenever a thread
is doing work that other threads might wish to block on.  In
the metadata completion system, this means whenever we construct
a metadata cache entry and the metadata isn't already allocated
and transitively complete after said construction.  Blocking
is done by safely acquiring a shared reference to the queue
object (which, in the current implementation, requires briefly
taking a lock that's global to the surrounding metadata cache)
and then acquiring the contained lock.  For typical lock
implementations, this avoids priority inversions by temporarily
propagating the priority of waiting threads to the locking
threads.

Dependencies are unblocked by simply releasing the lock held
in the queue.  The unblocking thread doesn't know exactly what
metadata are blocked on it and doesn't make any effort to
directly advance their completion; instead, the blocking
thread will wake up and then attempt to advance the dependent
metadata completion itself, eliminating a source of priority
overhang that affected the old system.  Successive rounds of
unblocking (e.g. when a metadata makes partial progress but
isn't yet complete) can be achieved by creating a new queue
and unlocking the old one.  We can still record dependencies
and use them to dynamically diagnose metadata cycles.

The new system allocates more eagerly than the old one.
Formerly, metadata completions which were never blocked never
needed to allocate a MetadataCompletionQueueEntry; we were
then unable to actually deallocate those entries once they
were allocated.  The new system will allocate a queue for
most metadata completions, although, on the positive side,
we can reliably deallocate these queues.  Cache entries are
also now slightly smaller because some of the excess storage
for status has been folded into the queue.

The fast path of an actual read of the metadata remains a
simple load-acquire.  Slow paths may require a bit more
locking.  On Darwin, the metadata cache lock can now use
os_unfair_lock instead of pthread_mutex_t (which is a massive
improvement) because it does not need to support associated
condition variables.

The excess locking could be eliminated with some sort of
generational scheme.  Sadly, those are not portable, and I
didn't want to take it on up-front.

rdar://76127798
2021-12-14 22:18:46 -05:00
Kuba Mracek
5bbf15053d Add #ifndef NDEBUG around assert() calls that use helper functions/variables that are also under #ifndef NDEBUG 2021-11-16 15:00:23 -08:00
Mike Ash
9a2f449c04 Merge pull request #40139 from mikeash/metadataallocator-null-check
[Runtime] Add a NULL check to MetadataAllocator.
2021-11-15 13:39:10 -05:00
Kuba (Brecka) Mracek
7cd5e5b110 Remove a bunch of unused #include's from Metadata.cpp (#40178) 2021-11-14 15:13:32 -08:00
Mike Ash
64f8bca351 [Runtime] Add a NULL check to MetadataAllocator.
MetadataAllocator should never return NULL, but bugs or corruption could potentially make that happen. On the large path, switch from malloc to swift_slowAlloc, which aborts on failure. On the pool path, check for a NULL allocation pointer, and log a bunch of information about the allocation request and the allocator's current state.

rdar://84503396
2021-11-11 12:16:54 -05:00
Alastair Houghton
e339feb2da [Runtime] Add format string attributes.
Added some attributes to mark functions as taking format strings.

rdar://84571859
2021-10-25 13:46:05 +01:00
Kuba (Brecka) Mracek
5ff64f8443 Introduce a CMake option to disable MetadataAllocator and use malloc+free instead, use it for freestandning preset (#39678) 2021-10-12 16:20:08 -07:00
Alastair Houghton
8be084f30a Merge pull request #39187 from al45tair/problem/79725187
[Demangler] Improve remangler error handling
2021-09-27 17:03:49 +01:00
Kuba (Brecka) Mracek
b5b008f9b3 Replace strcpy -> strlcpy in swift_getTupleTypeMetadata (#39181) 2021-09-08 12:56:52 -07:00
Alastair Houghton
3f01f853a6 [Demangling] Add error handling to the remangler.
Mangling can fail, usually because the Node structure has been built
incorrectly or because something isn't supported with the old remangler.
We shouldn't just terminate the program when that happens, particularly
if it happens because someone has passed bad data to the demangler.

rdar://79725187
2021-09-06 17:49:09 +01:00
Saleem Abdulrasool
cd7d105c6d runtime: remove workaround for Windows ARM64
If anyone else is building Windows ARM64 they should be using a new
enough Visual Studio.  This workaround is more difficult to keep working
properly and the CI hosts should have a new enough Visual Studio
installation hopefully in order to enable the ARM64 builds of the
runtime.  If they do not, we can re-evaluate whether to re-instate the
workaround.  This allows building part of the runtime with Visual Studio
2022 and reduces the maintenance overheads for the runtime.
2021-09-03 11:19:26 -07:00
Alastair Houghton
4b2e31654e [Demangler][Tests] Re-add getObjcClassByMangledName test.
This was reverted in ec3ccd72 because it broke the iOS simulator build.
2021-08-17 09:46:21 +01:00