Commit Graph

278 Commits

Author SHA1 Message Date
Doug Gregor
859fb0dc45 [Runtime] Handle dynamic casting to NSObject via error bridging.
The dynamic casting machinery failed to account for NSError bridging when
casting to NSObject; check for it explicitly.
2019-11-20 15:56:24 -08:00
Brent Royal-Gordon
bc5d59ecb7 Revert "[Runtime] Handle dynamic casting to NSObject via error bridging." 2019-11-20 10:38:44 -08:00
Doug Gregor
ab062fcd14 [Runtime] Handle dynamic casting to NSObject via error bridging.
The dynamic casting machinery failed to account for NSError bridging when
casting to NSObject; check for it explicitly.
2019-11-18 23:00:57 -08:00
Mike Ash
3ff277d31d [Runtime] Use _conformsToProtocols instead of manually checking protocol conformance.
This is cleaner and it fixes a bunch of cases the old code didn't handle: @objc protocols, class bounds, and superclass constraints.

rdar://problem/56044443
2019-10-10 14:14:41 -04:00
Mike Ash
d56173eb14 [Runtime] Handle existential target types in swift_dynamicCastMetatypeImpl.
This fixes cases like `type is T.Type` when T is generic specialized to a protocol type.

Note: the compiler can still optimize these checks away and will optimize this check down to `false` in some cases. We'll want to fix that as well.

rdar://problem/56044443
2019-10-08 11:37:39 -04:00
Joe Groff
f1e84994ed Demangler: Make symbolic reference resolver part of demangle(Symbol|Type) calls.
This makes for a cleaner and less implicit-context-heavy API, and makes it easier for symbolic
reference resolvers to do context-dependent things (like map the in-memory base address back to a
remote address in MetadataReader).
2019-09-13 15:47:08 -07:00
Karoy Lorentey
1162ed6cba [runtime] Report an error for casting failure in _bridgeNonVerbatimFromObjectiveC 2019-03-14 18:20:41 -07:00
Saleem Abdulrasool
3ec6e12520 runtime: correct signature for swift_swiftValueConformsTo
The swift side signature for `swift_swiftValueConformsTo` is:
`func swift_swiftValueConformsTo<T>(_: T.self) -> Bool`

This translates to:
`bool swift_swiftValueConformsTo(const Metadata *, const Metadata *)`

The elided parameter would be passed invalid values..  Running this on
Windows with optimizations triggered an optimization where the parameter
happened to be null as `rdx` is the second parameter rather than the 4th
parameter.
2019-02-16 21:24:36 -08:00
John McCall
2ba7090fe8 Remove the extra-inhabitant value witness functions.
This is essentially a long-belated follow-up to Arnold's #12606.
The key observation here is that the enum-tag-single-payload witnesses
are strictly more powerful than the XI witnesses: you can simulate
the XI witnesses by using an extra case count that's <= the XI count.
Of course the result is less efficient than the XI witnesses, but
that's less important than overall code size, and we can work on
fast-paths for that.

The extra inhabitant count is stored in a 32-bit field (always present)
following the ValueWitnessFlags, which now occupy a fixed 32 bits.
This inflates non-XI VWTs on 32-bit targets by a word, but the net effect
on XI VWTs is to shrink them by two words, which is likely to be the
more important change.  Also, being able to access the XI count directly
should be a nice win.
2018-12-11 22:18:44 -05:00
Joe Groff
b2ceb4b753 Merge pull request #21102 from jckarter/unconditional-cast-source-loc-abi
Runtime: Provide ABI space for source location info in unconditional casts.
2018-12-07 19:04:59 -08:00
Joe Groff
bce1f5ef4a Runtime: Provide ABI space for source location info in unconditional casts.
Currently ignored, but this will allow future compilers to pass down source location information for cast
failure runtime errors without backward deployment constraints.
2018-12-06 14:58:14 -08:00
Mike Ash
6f7143bfaf [Runtime] Allow casts from AnyHashable to a Hashable enum to succeed.
rdar://problem/46472361
2018-12-06 10:33:35 -05:00
Mike Ash
f1f83968d6 [Runtime] Fix leak in tryDynamicCastBoxedSwiftValue.
The call to swift_unboxFromSwiftValueWithType needs to destroy the value on success when requested in the flags.

rdar://problem/44686587
2018-11-29 12:59:44 -05:00
Mike Ash
331f0d2394 [Runtime] Fix leak when casting to AnyHashable.
_dynamicCastToAnyHashable assumed that _swift_convertToAnyHashableIndirect takes its argument at +1, but that is no longer the case.

rdar://problem/44686587
2018-11-28 14:54:13 -05:00
Doug Gregor
452e1b12e1 [Runtime] Check for @objc existentials conforming to @objc protocols.
When checking conformance requirements against an @objc protocol, also
check for an @objc existential using protocol_conformsToProtocol().
Fixes rdar://problem/45685649.
2018-11-01 06:37:39 -07:00
Doug Gregor
c5e226bb16 [Runtime] Use CF superclass metadata in runtime lookups.
When searching the superclasses at runtime, e.g., to find a suitable
protocol conformance record, also consider the superclasses of CF
types, which were recorded in the metadata but otherwise unused.
2018-10-16 21:42:02 -07:00
Doug Gregor
aba018c1e8 [ABI] Pass requirement base descriptor to swift_getAssociatedTypeWitness().
Have clients pass the requirement base descriptor to
swift_getAssociatedTypeWitness(), so that the witness index is just one
subtraction away, avoiding several dependent loads (witness table ->
conformance descriptor -> protocol descriptor -> requirement offset)
in the hot path.
2018-10-10 22:45:59 -07:00
Mike Ash
a78a01a392 [Stdlib] Make _swift_extractDynamicValue INTERNAL as it's used by Foundation. 2018-10-04 13:20:53 -04:00
Doug Gregor
b531b3923f [ABI] Use mangled names for associated type witnesses.
Rather than storing associated type metadata access functions in
witness tables, initially store a pointer to a mangled type name.
On first access, demangle that type name and replace the witness
table entry with the resulting type metadata.

This reduces the code size of protocol conformances, because we no
longer need to create associated type metadata access functions for
every associated type, and the mangled names are much smaller (and
sharable). The same code size improvements apply to defaulted
associated types for resilient protocols, although those are more
rare. Witness tables themselves are slightly smaller, because we
don’t need separate private entries in them to act as caches.

On the caller side, associated type metadata is always produced via
a call to swift_getAssociatedTypeWitness(), which handles the demangling
and caching behavior.

In all, this reduces the size of the standard library by ~70k. There
are additional code-size wins that are possible with follow-on work:

* We can stop emitting type metadata access functions for non-resilient
types that have constant metadata (like `Int`), because they’re only
currently used as associated type metadata access functions.
* We can stop emitting separate associated type reflection metadata,
because the reflection infrastructure can use these mangled names
directly.
2018-09-26 23:19:33 -07:00
Mike Ash
49c3547449 Merge remote-tracking branch 'origin/master' into rename-conflicting-classes-and-methods 2018-09-21 15:52:38 -04:00
Doug Gregor
50d12e05db [Runtime] Fix _ObjectiveCBridgeableWitnessTable::ObjectiveCType
The signature of this function pointer was incorrect, because it had not
yet been updated for the metadata request/response architecture. We
were getting lucky (!).
2018-09-14 20:59:03 -07:00
Mike Ash
798edb9d0e [Runtime][Stdlib][Overlays] Rename various Objective-C classes and methods that would conflict when loading old Swift libraries into a process alongside ABI-stable libraries.
rdar://problem/35768222
2018-09-13 16:55:10 -04:00
Mike Ash
46309d9794 [Runtime] Rename swift_unknown* functions to swift_unknownObject*.
These functions don't accept local variable heap memory, although the names make it sound like they work on anything. When you try, they mistakenly identify such things as ObjC objects, call through to the equivalent objc_* function, and crash confusingly. This adds Object to the name of each one to make it more clear what they accept.

rdar://problem/37285743
2018-08-15 17:48:23 -04:00
Azoy
b8fc8b333c Remove _interface 2018-07-29 10:41:22 -04:00
Doug Gregor
c7a02a26a1 [ABI] Distinguish Swift/ObjC protocols in TargetGenericRequirement.
In a generic requirement, distinguish between Swift and
Objective-C protocols using a spare bit within the relative
(indirectable) reference to the protocol.
2018-07-22 22:48:57 -07:00
Doug Gregor
3464929638 [ABI] Rework existential type metadata to use ProtocolDescriptorRef.
Use ProtocolDescriptorRefs within the runtime representation of
existential type metadata (TargetExistentialTypeMetadata) instead of
bare protocol descriptor pointers. Start rolling out the use of
ProtocolDescriptorRef in a few places in the runtime that touch this
code. Note that we’re not yet establishing any strong invariants on
the TargetProtocolDescriptorRef instances.

While here, replace TargetExistentialTypeMetadata’s hand-rolled pointer 
arithmetic with swift::ABI::TrailingObjects and centralize knowledge of
its layout better.
2018-07-20 20:54:49 -07:00
Michael Gottesman
5648ef219d [+0-all-args] Clean up remains of +1 convention from the runtime. 2018-07-06 23:10:12 -07:00
Mike Ash
e992f46764 [Runtime] Extend ObjC bridging casts to convert NSError to Error when nested in a container type.
rdar://problem/39349762
2018-06-28 13:35:25 -04:00
Doug Gregor
7952a19f31 [Mangling] Introduce known manglings for many more standard library types.
Since the mangling scheme and set of standard library types is effectively
fixed now, introduce known mangling substitutions for a number of new
standard library types, filling out the S[A-Za-z] space.

Reduces standard library binary size by ~195k.
2018-06-19 23:24:38 -07:00
Saleem Abdulrasool
b33a1e1766 runtime: workaround GCC 4.8 libstdc++ C++11 conformance
libstdc++ included with GCC 4.8 does not define `std::max_align_t` as
required by the C++11 specification.  As a workaround, explicitly create
the definition locally.  This was fixed in GCC 4.9 and later.
2018-06-12 12:03:31 -07:00
Pavel Yaskevich
92c4189e2d [Runtime] NFC: remove C style "stddef.h" header from casting 2018-06-10 20:36:27 -07:00
Saleem Abdulrasool
638b89d12d runtime: use std::max_align_t over max_align_t (NFC)
MSVC does not provide a full C99 environment.  Use the C++ equivalent
spelling for the value.  This allows the build to succeed on Windows.
2018-06-08 14:36:19 -07:00
Lily Vulcano
1377ab7d7e Turn on ‘as’ bridging on Darwin. 2018-05-30 15:07:22 -07:00
Mike Ash
0dae9b3dc0 [Runtime] Have swift_dynamicCast unwrap multiple levels of optionals in the source.
The real work is done in checkDynamicCastFromOptional. This code tried to unwrap the source and returned the payload on success. It only did this once, so a type like Optional<Optional<Int>> would come out as Optional<Int> and then a cast to Int would fail.

This change makes checkDynamicCastFromOptional recursive, which makes it unwrap as many levels of Optional as it encounters.

Fixes rdar://problem/40171034 and SR-7664.
2018-05-22 15:05:09 -04:00
Joe Groff
681a96b45c Runtime: Tolerate unknown metadata kinds.
We want to be able to potentially introduce new metadata kinds in future Swift compilers, so a runtime ought to be able to degrade gracefully in the face of metadata kinds it doesn't know about. Remove attempts to exhaustively switch over metadata kinds and instead treat unknown metadata kinds as opaque.
2018-05-17 15:35:06 -07:00
Michael Gottesman
0d1f98a4ef Merge pull request #16259 from gottesmm/pr-aab473d24268b0934ac216bf2683f0a2c8e3be0f
[+0-all-args] Make _bridgeAnythingNonVerbatimToObjectiveC() take its …
2018-04-30 13:14:46 -07:00
Michael Gottesman
c31033c525 [+0-all-args] Make _bridgeAnythingNonVerbatimToObjectiveC() take its parameter at +1.
This hoists out the retain into Swift code from the casting runtime and along a
few paths in the runtime allows us to eliminate a dynamic retain release.

rdar://38196046
rdar://38771331
2018-04-30 11:05:00 -07:00
Michael Gottesman
87681375e2 [+0-all-args] Change _swift_stdlib_bridgeErrorToNSError to take its parameter at +1.
This is truly a consuming operation. This can be seen since we always would need
to retain the argument here. This makes guaranteed -> owned less transformation
effective. Instead represent it taking a +1 argument so that the retain happens
outside the builtin instead of inside the builtin.

This also allows me to remove an extra copy from dynamicCastValueToNSError

rdar://38771331
2018-04-30 10:16:41 -07:00
Joe Groff
78b5ff8b6b Runtime: Bridge Error-conforming types to id as NSError instances.
NSError is a more useful box for a swift Error than the generic box. Fixes rdar://problem/38631791 | SR-7232.
2018-04-10 12:23:49 -07:00
Mike Ash
34be7f6f15 Merge branch 'master' into casting-hooks 2018-03-28 14:45:48 -04:00
Mike Ash
f4224b092e [Runtime] Redo compatibility overrides boilerplate using a .def file.
rdar://problem/36997475
2018-03-28 13:39:55 -04:00
Mike Ash
d024fd7487 [Runtime] Fill out a bunch more compatibility hooks.
rdar://problem/36997475
2018-03-22 09:14:55 -07:00
Mike Ash
9e8d4da76f [Runtime] Add a hooking mechanism to allow future apps to extend dynamic casting and type lookup. Make swift_dynamicCast and swift_getTypeByMangledName use these hooks.
rdar://problem/36997475
2018-03-20 16:33:37 -07:00
Arnold Schwaighofer
9d8c381ab4 Remove resilient tag indices 2018-03-20 13:19:56 -07:00
Joe Groff
0d7c42ce66 Runtime: Do the bare minimum balancing for bridge casting at +0.
The logic here is causing leaks in some situations, and it's likely we'll want
bridging calls to go back to +1 again anyway, so do the minimum adaptation to
the +0 convention for now.

rdar://34222540
2018-03-10 02:17:24 -08:00
Michael Gottesman
e66fd1ad0c [+0-all-args] When casting a value to NSError, release the value passed to _swift_stdlib_bridgeErrorToNSError since it is at +0 now.
rdar://34222540
2018-03-05 12:22:09 -08:00
John McCall
f2bb319bdb Change the pattern of generic class metadata instantiation.
Minimize the generic class metadata template by removing the
class header and base-class members.  Add back the set of
information that's really required for instantiation.
Teach swift_allocateGenericClass how to allocate classes without
superclass metadata.  Reorder generic initialization to establish
a stronger phase-ordering between allocation (the part that doesn't
really care about the generic arguments) and initialization (the
part that really does care about the generic arguments and therefore
might need to be delayed to handle metadata cycles).

A similar thing needs to happen for resilient class relocation.
2018-03-04 00:01:56 -05:00
Michael Gottesman
ba9946a131 Add a dumper/verifier for OpaqueExistentialContainers (i.e. Any) in c++.
The dumper method dumps:

1. The container's metadata pointer.
2. A pointer to the container's value.
3. Whether or not said value is stored inline in the container.

This provides a general overview that can be used even when working with SIL
code in the debugger by grabbing a pointer to swift Anys and then calling the
c++ any method upon them.

The verifier is intended to be used in conjunction with ASAN for maximum
effect to catch use-after-frees of existential boxes.

While implementing this I refactored some code from ExistentialTypeMetadata into
methods on OpaqueExistentialContainer. ExistentialTypeMetadata just calls these
methods now instead of implementing the code inline.
2018-02-23 23:03:55 -08:00
John McCall
23fa44e56c Render TypeContextDescriptor into a proper hierarchy; NFC.
The purpose here is to make it easier to add type-specific fields to the
descriptor.
2018-02-20 15:20:32 -05:00
Doug Gregor
800e3818b7 [Type metadata] @objc protocol conformance requirements don't have arguments.
@objc protocols don't have witness tables. However, both type metadata
(in the nominal type descriptors) and the runtime code to demangle
type names into metadata weren't acknowledging this. Fix type metadata
emission to not count an "extra argument" for @objc protocol
conformance requirements, and teach the runtime code to properly look
for conformances to @objc protocols (through the Objective-C runtime)
and not record witness tables for them.
2018-02-15 14:07:46 -08:00