Commit Graph

652 Commits

Author SHA1 Message Date
Mishal Shah
34350e45cd Revert "[Demangler][Tests] Add an explicit test for getObjCClassByMangledName." 2021-08-16 14:00:39 -07:00
Alastair Houghton
fb351be64f [Demangler][Tests] Address Doug's comments.
Moved the test for the metadata kind to MetadataLookup.cpp.

Added an assertion (for debug builds) to Metadata.cpp to catch the case where
something manages to bypass that test.
2021-08-16 10:59:01 +01:00
Alastair Houghton
8db56f09a3 [Demangler][Tests] Add an explicit test for getObjCClassByMangledName.
Added a special test for getObjCClassByMangledName; this needs testing
separately as it uses the DecodedMetadataBuilder, which doesn't get exercised
by the normal demangling tests.

Added all the test cases from rdar://63485806, rdar://63488139, rdar://63496478,
rdar://63410196 and rdar://68449341.  The test cases from rdar://63485806 are
disabled for now because the problem there is the error handling mechanism (or
lack thereof), rather than us not handling errors.

Fixes the remaining cases from

rdar://63488139
rdar://63496478
2021-08-04 16:21:32 +01:00
Alastair Houghton
21ac407a21 <rdar://64939529> Convert unnecessary dlsym() calls into direct references.
Added SWIFT_RUNTIME_WEAK_IMPORT/CHECK/USE macros.

Everything supports fast dealloc except x86 iOS simulators, so we no longer need
to look up objc_has_weak_formation_callout.

Added direct references for

  objc_setHook_lazyClassNamer
	_objc_realizeClassFromSwift
	objc_setHook_getClass
	os_system_version_get_current_version
	_dyld_is_objc_constant
2021-06-18 10:16:30 +01:00
swift-ci
e65ae80172 Merge pull request #36955 from mininny/add-dynamic-subclass-kvo-doc 2021-06-08 18:59:17 -07:00
Doug Gregor
b814e225dd Implement (de-)mangling and type metadata for global actor function types.
Implement name mangling, type metadata, runtime demangling, etc. for
global-actor qualified function types. Ensure that the manglings
round-trip through the various subsystems.

Implements rdar://78269642.
2021-06-02 23:34:22 -07:00
Minhyuk Kim
3ca45557fa [docs] Add short note about dynamic subclassing and KVO 2021-04-18 21:34:57 +09:00
Nate Chandler
d3e0e7af07 [ptrauth] Signed AsyncFunctionPointers as data.
Previously, AsyncFunctionPointer constants were signed as code.  That
was incorrect considering that these constants are in fact data.  Here,
that is fixed.

rdar://76118522
2021-04-05 18:24:41 -07:00
Mike Ash
6d4015e3c7 Merge pull request #36652 from mikeash/getobjcclassmetadata-improvement
[Runtime] Make debug builds accept NULL in swift_getObjCClassMetadata.
2021-03-31 17:40:49 -04:00
Richard Wei
d997526948 Fix function differentiability kind metadata and mangling. (#36601)
* Move differentiability kinds from target function type metadata to trailing objects so that we don't exhaust all remaining bits of function type metadata.
  * Differentiability kind is now stored in a tail-allocated word when function type flags say it's differentiable, located immediately after the normal function type metadata's contents (with proper alignment in between).
  * Add new runtime function `swift_getFunctionTypeMetadataDifferentiable` which handles differentiable function types.
* Fix mangling of different differentiability kinds in function types. Mangle it like `ConcurrentFunctionType` so that we can drop special cases for escaping functions.
    ```
    function-signature ::= params-type params-type async? sendable? throws? differentiable? // results and parameters
    ...
    differentiable ::= 'jf'                    // @differentiable(_forward) on function type
    differentiable ::= 'jr'                    // @differentiable(reverse) on function type
    differentiable ::= 'jd'                    // @differentiable on function type
    differentiable ::= 'jl'                    // @differentiable(_linear) on function type
    ```

Resolves rdar://75240064.
2021-03-30 09:59:06 -07:00
Mike Ash
8743ebd9ea [Runtime] Make debug builds accept NULL in swift_getObjCClassMetadata.
The previous fix here switched dyn_cast for dyn_cast_or_null, but this left us with an assertion failure in cast<>. Instead, explicitly check for NULL at the top of the function.
2021-03-30 12:45:29 -04:00
Mike Ash
c4e4e44f71 Merge pull request #36390 from mikeash/concurrency-compatibility-overrides
[Concurrency] Add compatibility overrides to Concurrency library.
2021-03-24 13:07:07 -04:00
Mike Ash
dc7d838853 [Runtime] Accept nil in swift_getObjCClassFromMetadata.
Somehow, clang was generating code that accepted nil and returned nil in swift_getObjCClassFromMetadata, but is no longer doing so. Some code relies on this to work, so switch to dyn_cast_or_null to accept nil explicitly.

rdar://74895271
2021-03-23 16:01:58 -04:00
Mike Ash
6aab257c33 [Concurrency] Add compatibility overrides to Concurrency library.
Take the existing CompatibilityOverride mechanism and generalize it so it can be used in both the runtime and Concurrency libraries. The mechanism is preprocessor-heavy, so this requires some tricks. Use the SWIFT_TARGET_LIBRARY_NAME define to distinguish the libraries, and use a different .def file and mach-o section name accordingly.

We want the global/main executor functions to be a little more flexible. Instead of using the override mechanism, we expose function pointers that can be set by the compatibility library, or by any other code that wants to use a custom implementation.

rdar://73726764
2021-03-22 11:09:06 -04:00
Mike Ash
7c1b968666 [Runtime] Silence warnings on NULL checks of ObjC function addresses.
Some ObjC runtime calls are weak or strong depending on the deployment target. When strong, we get warnings that the NULL checks always succeed; silence them.

Some of the adjacent code looked up functions using dlsym when they aren't provided by the SDK. Our current minimum SDK always has them, so remove the dlsym workaround.
2021-03-02 15:44:45 -05:00
swift-ci
9ba714a0aa Merge pull request #35892 from varungandhi-apple/vg-fix-vwt-pointer-copy 2021-02-12 09:49:25 -08:00
Varun Gandhi
80889749a6 [Runtime] Copy the value witness table pointer manually.
Fixes rdar://74136916.
2021-02-11 19:34:06 -08:00
Mike Ash
3a2ea08d75 [Runtime] Fix race condition in protocol conformance lookups that caused false negatives.
In the uncached case, we'd scan conformances, cache them, then re-query the cache. This worked fine when the cache always grew, but now we clear the cache when loading new Swift images into the process. If that happens between the scan and the re-query, we lose the entry and return a false negative.

Instead, track what we've found in the scan in a separate local table, then query that after completing the scan.

While we're in there, fix a bug in TypeLookupError where operator= accidentally copied this->Context instead of other.Context. This caused the runtime to crash when trying to print error messages due to the false negative.

Add a no-parameter constructor to TypeLookupErrorOr<> to distinguish the case where it's being initialized with nothing from the case where it's being initialized with nullptr.
2021-02-08 19:28:27 -05:00
Mike Ash
b47607c34e [Runtime] Validate scribble data when metadata allocation scribbling is enabled.
Instead of scribbling each allocation as it's parceled out, scribble the entire chunk up-front. Then, when handing out an allocation, check to make sure it still has the right scribbled data in it.
2020-12-01 16:04:52 -05:00
Nate Chandler
e09e4a72ff [Runtime] Check that extraDataSize matches extraDataPattern.
To prevent rdar://problem/68997282 from regressing, verify at runtime in
debug builds that in calls to swift_allocateGenericValueMetadata the
extraDataSize argument matches the OffsetInWords and SizeInWords
specified by the GenericMetadataPartialPattern available within the
pattern argument.
2020-11-19 08:01:24 -08:00
Mike Ash
fd5b87f4d0 [Runtime] Don't write to rodata if values already match.
initClassFieldOffsetVector writes the instanceStart and size to the class's rodata. In some cases they already match, and this write will dirty memory unnecessarily, and prevent the compiler from emitting those rodatas into read-only memory.

rdar://problem/71119533
2020-11-06 16:59:51 -05:00
nate-chandler
5c59babfeb Merge pull request #34490 from nate-chandler/generic-metadata-prespecialization-components/special-entry
[metadata prespecialization] Add canonical records to cache.
2020-11-04 19:35:40 -08:00
Mike Ash
12d114713f [Runtime] Switch MetadataCache to ConcurrentReadableHashMap. (#34307)
* [Runtime] Switch MetadataCache to ConcurrentReadableHashMap.

Use StableAddressConcurrentReadableHashMap. MetadataCacheEntry's methods for awaiting a particular state assume a stable address, where it will repeatedly examine `this` in a loop while waiting on a condition variable, so we give it a stable address to accommodate that. Some of these caches may be able to tolerate unstable addresses if this code is changed to perform the necessary table lookup each time through the loop instead. Some of them store metadata inline and we assume metadata never moves, so they'll have to stay this way.

* Have StableAddressConcurrentReadableHashMap remember the last found entry and check that before doing a more expensive lookup.

* Make a SmallMutex type that stores the mutex data out of line, and use it to get LockingConcurrentMapStorage to fit into the available space on 32-bit.

rdar://problem/70220660
2020-11-04 15:10:50 -05:00
Nate Chandler
cda11b90a1 [Runtime] Add prespecialized-caching getGenericMetadata variant.
Add a new entry point for getting generic metadata which adds the
canonical metadata records attached to the nominal type descriptor to
the metadata cache.

Change the implementation of the primary entry-point
swift_getGenericMetadata to stop looking through canonical
prespecialized records.

Change the implementation of swift_getCanonicalSpecializedMetadata to
use the caching token attached to the nominal type descriptor to add
canonical prespecialized metadata records to the metadata cache only
once rather than using the cache variables to limit the number of times
the attempt was made.
2020-11-01 13:35:03 -08:00
Alejandro Alonso
424802fb34 Revert SE-0283 (#34492)
Reverted despite build failures.
2020-10-29 17:32:06 -07:00
Nate Chandler
79dd09c7f8 [Runtime] Auth args to compareTypeContextDescriptors.
When swift_compareTypeContextDescriptors was added, it did not auth the
TypeContextDesriptor arguments that were passed to it.  Fix that here.

There are not any uses of this function yet, so there are no signs that
need to be added.
2020-10-27 15:38:17 -07:00
Kuba (Brecka) Mracek
d7dfa3e942 Bring up tests + validation tests for the 'freestanding' build and the standalone_minimal preset (#34386) 2020-10-26 16:32:36 -07:00
Azoy
7a6ea99eab [Runtime] Clean up the assembly around Tuple builtin conformances
forgot + 1
2020-10-22 18:28:37 -04:00
Azoy
e60ef84bd2 Implement Tuple Equatable Conformance 2020-10-22 18:24:28 -04:00
Azoy
4b71d6776e [ABI] Introduce MetadataKind TypeRef 2020-10-22 18:24:28 -04:00
Mike Ash
630aff7b19 [Runtime] Change SimpleGlobalCache to use ConcurrentReadableHashMap instead of ConcurrentMap.
This gives us faster lookups and a small advantage in memory usage. Most of these maps need stable addresses for their entries, so we add a level of indirection to ConcurrentReadableHashMap for these cases to accommodate that. This costs some extra memory, but it's still a net win.

A new StableAddressConcurrentReadableHashMap type handles this indirection and adds a convenience getOrInsert to take advantage of it.

ConcurrentReadableHashMap is tweaked to avoid any global constructors or destructors when using it as a global variable.

ForeignWitnessTables does not need stable addresses and it now uses ConcurrentReadableHashMap directly.

rdar://problem/70056398
2020-10-08 14:57:39 -04:00
John McCall
0fb407943f [NFC] Rename swift_runtime_unreachable to swift_unreachable and make it use LLVM's support when available. 2020-10-03 02:54:56 -04:00
Dave Lee
389d099705 [stdlib] Silence a few assert related warnings 2020-09-09 09:43:56 -07:00
Mike Ash
fd6922f92d Add error reporting when looking up types by demangled name. 2020-08-28 14:43:51 -04:00
Kuba Mracek
99e518bb85 Guard Obj-C specifics in Metadata.cpp with SWIFT_OBJC_INTEROP 2020-08-14 21:20:34 -07:00
Nate Chandler
a5a7c98be0 [metadata prespecialization] getGenericMetadata finds records.
Previously, the metadata accessor for which canonical prespecializations
had been formed included checks against the passed-in arguments to
determine whether the access matched a prespecialized record or not.

Now that the prespecialized records are attached to the nominal type
descriptor for the type, eliminate this hard-coded generated code and
instead let swift_getGenericMetadata do the work of looking through the
prespecializations.
2020-08-14 11:19:10 -07:00
Nate Chandler
b1408028a7 [metadata prespecialization] Allow more noncanonical records.
Previously, noncanonical records were only allowed if one of the
arguments was from the module where the record was to be emitted.

Here, that restriction is lifted.  Now getSpecializedGenericMetadata
will, on first run, register all canonical metadata with the global
cache before attempting to bless the passed-in noncanonical record,
allowing noncanonical records to be for the same arguments as a
canonical record (since in the case that a canonical record does exist,
it will be returned).
2020-08-13 17:44:49 -07:00
Joe Groff
bac92e926b Merge pull request #32894 from jckarter/singleton-witness-table-instantiation
[WIP] Avoid creating a ConcurrentMap for nongeneric instantiated witness tables
2020-07-17 14:15:47 -07:00
Joe Groff
88d9641df8 Avoid creating a ConcurrentMap for nongeneric instantiated witness tables 2020-07-16 11:59:57 -07:00
Nate Chandler
b0fc8daa83 [Runtime] Add entry point to canonicalize metadata.
The new function swift_getCanonicalSpecializedMetadata takes a metadata
request, a prespecialized non-canonical metadata, and a cache as its
arguments.  The idea of the function is either to bless the provided
prespecialized metadata as canonical if there is not currently a
canonical metadata record for the type it describes or else to return
the actual canonical metadata.

When called, the metadata cache checks for a preexisting entry for this
metadata.  If none is found, the passed-in prespecialized metadata is
added to the cache.  Otherwise, the metadata record found in the cache
is returned.

rdar://problem/56995359
2020-07-15 15:27:36 -07:00
Nate Chandler
2701a0809b [metadata prespecialization] Ptrauth for compared protocol conformances.
Two protocol conformance descriptors are passed to
swift_compareProtocolConformanceDecriptors from generic metadata
accessors when there is a canonical prespecialization and one of the
generic arguments has a protocol requirement.

Previously, the descriptors were incorrectly being passed without
ptrauth processing: one from the witness table in the arguments that are
passed in to the accessor and one known statically.

Here, the descriptor in the witness table is authed using the
ProtocolConformanceDescriptor schema.  Then, both descriptors are signed
using the ProtocolConformanceDescriptorsAsArguments schema.  Finally, in
the runtime function, the descriptors are authed.
2020-07-09 11:45:16 -07:00
Nate Chandler
6d0c34caf3 [Runtime] Add entry point to compare conformance descriptors.
The new function swift_compareProtocolConformanceDescriptors calls
through to the preexisting code in MetadataCacheKey which has been
extracted out from MetadataCacheKey::compareWitnessTables into a new
public static function
MetadataCacheKey::compareProtocolConformanceDescriptors.

The new function's availability is "future" for now.
2020-06-22 15:01:28 -07:00
swift-ci
ecad335f70 Merge pull request #32476 from nate-chandler/runtime/add-swift_compareTypeContextDescriptors 2020-06-22 14:40:35 -07:00
Nate Chandler
2c6d7d78df [Runtime] Add entry point to compare type context descriptors.
The new function `swift_compareTypeContextDescriptors` is equivalent to
a call through to swift::equalContexts.  The implementation it the same
as that of swift::equalContexts with the following removals:
- Handling of context descriptors of kind other outside of
  ContextDescriptorKind::Type_First...ContextDescriptorKind::Type_Last.
  Because the arguments are both TypeContextDescriptors, the kinds are
  known to fall within that range.
- Casting to TypeContextDescriptor.  The arguments are already of that
  type.

For now, the new function has "future" availability.
2020-06-22 12:07:02 -07:00
nate-chandler
d76a8b9321 Merge pull request #32408 from nate-chandler/runtime/memset-pattern-metadata-on-flag
[Runtime] Scribble on metadata allocations.
2020-06-16 20:37:29 -07:00
Nate Chandler
03659813ca [Runtime] Scribble on metadata allocations.
Previously, when NDEBUG was not defined, the allocations made for value
metadata records were filled with 0xAA bytes.  Here, that behavior is
both expanded to all metadata records and enabled in release builds when
the environment variable SWIFT_DEBUG_ENABLE_MALLOC_SCRIBBLE is set.
2020-06-16 12:46:43 -07:00
Doug Gregor
7d7a35e445 [Runtime] Zero out the entire witness table during instantiation.
During witness table instantiation, the witness table is constructed
several sources: the pattern, the resilient witnesses, the private
data, and default implementations. The private data area is the only
one that was being zeroed out; the rest we rely on always filling in
the data from the conformance descriptor and provided info.

However, witness table instantiation uses the presence of a NULL
pointer for a particular witness in the resulting table to indicate
that no witness fulfilled that requirement, so that it can fill in the
default witnesss. Except that, without zeroing that part of the table
beforehand, we aren't guaranteed to have a NULL pointer for witness
entries that the client (protocol conformance) did not know about at
the time it was compiled.

Zero out the entire witness table so default implementations can be
filled in appropriately.

Fixes rdar://problem/64295849.
2020-06-13 23:54:45 -07:00
Mike Ash
6527a216fb Merge pull request #32244 from mikeash/metadata-allocation-backtrace-inspection
[Runtime][Reflection][swift-inspect] Add facilities for tracking and examining backtraces for metadata allocations.
2020-06-12 12:50:52 -04:00
Mike Ash
cd624bf380 [Runtime][Reflection][swift-inspect] Add facilities for tracking and examining backtraces for metadata allocations.
rdar://problem/63674755
2020-06-11 14:49:52 -04:00
Mike Ash
d80a405a46 [Runtime] Clean up verifyMangledNameRoundtrip comment. 2020-06-08 16:44:10 -04:00