Commit Graph

263 Commits

Author SHA1 Message Date
Mike Ash
5decec24e7 [Runtime] Fix gatherWrittenGenericArgs crashing when description is NULL.
rdar://problem/54775582
SR-11374
2019-09-09 10:58:22 -04:00
Joe Groff
a49e0d5a2e Runtime: Hook the ObjC runtime with an untrusted demangler.
We don't want objc_getClass and NSClassFromString to be able to feed arbitrary symbolic reference
pointers into the Swift runtime. Fixes rdar://problem/54724618.
2019-08-28 11:56:45 -07:00
pschuh
37c6414e77 Fix usage of uninitialized memory in swift_addNewDSOImage. (#26275)
This should address https://bugs.swift.org/browse/SR-6848.

Couldn't transform the unsafeGet to get because it would produce an
infinite loop.
2019-08-13 13:23:33 -07:00
Doug Gregor
b7e245c234 [Runtime] Mark hash_value inline 2019-08-09 11:54:27 -07:00
Doug Gregor
ae477280b4 [Runtime] Make hash_value function inline. 2019-08-09 11:53:17 -07:00
Doug Gregor
e7cedae5fc [Runtime] Fail in +Asserts builds if the context descriptor cache is wrong. 2019-08-09 11:21:21 -07:00
Mike Ash
4bc794f407 Change the context descriptor cache to be a SmallDenseSet instead of a TinyPtrVector to avoid duplicates. 2019-08-09 11:19:12 -07:00
Mike Ash
a2ed459024 Fix _forEach*SectionAfter to update the start parameter. 2019-08-09 11:19:07 -07:00
Doug Gregor
bf7aad2ac9 Populate context descriptor cache from type metadata sections 2019-08-09 11:19:03 -07:00
Doug Gregor
961842def2 Eliminate deadlock in context descriptor cache 2019-08-09 11:18:50 -07:00
Mike Ash
c09adf0394 Add caching based on ABI name to _findContextDescriptor.
This is a one-to-many cache that's more speculative than the cache mapping mangled names to context descriptors. Entries found in the cache need to be verified for a match before they can be returned. However, this allows scanning conformance records up front and building up the cache in one scan rather than performing an expensive scan of all conformance records every time the mangled name cache misses.

rdar://problem/53560010
2019-08-09 11:18:43 -07:00
Doug Gregor
17b917fef9 [Runtime] Short-circuit search for context descriptor for protocol extensions
When looking for a context descriptor for a protocol extension, we
search based on the mangled name--"x", a generic type parameter with
depth and index 0--and are guaranteed to fail because there is no such
concrete type. However, _findContextDescriptor will fail very slowly,
spinning through all of the types and conformances in all of the
loaded images. Moreover, negative results aren't cached, so this can
happen repeatedly.

Short-circuit _findContextDescriptor when it receives a dependent
generic type parameter type, avoiding the expensive search when it
will find nothing.

Potential fix for rdar://problem/53560010.
2019-08-05 22:05:53 -07:00
Jordan Rose
4b8068d1df [runtime] Handle same-type constraints when resolving generic params
Generic parameters for a context are normally classified as "key",
meaning they have actual metadata provided at runtime, or non-key,
meaning they're derivable from somewhere else. However, a nested
context or constrained extension can take what would be a "key"
parameter in a parent context and make it non-key in a child context.
This messes with the mapping between the (depth, index) representation
of generic parameters and the flat list of generic arguments. Fix this
by (1) consistently substituting out extension contexts with the
contexts of the extended types, and (2) using the most nested context
to decide which parameters are key, instead of the context a parameter
was originally introduced in.

Note that (1) may have problems if/when extensions start introducing
their /own/ generic parameters. For now I tried to be consistent with
what was there.

rdar://problem/52364601
2019-07-08 17:46:14 -07:00
Joe Groff
85c44472e0 Merge pull request #25811 from jckarter/remote-mirror-opaque-types
Reflection: Look through opaque type descriptors.
2019-06-26 21:06:55 -07:00
Joe Groff
4e493b0051 Reflection: Look through opaque type descriptors.
Turn an opaque type reference in a mangled name into its underlying type, if we can.
rdar://problem/46140707
2019-06-26 17:58:52 -07:00
Brent Royal-Gordon
4149c8f97d [Runtime] Fix private generic type witness demangling failure (#25611)
This one’s screwily specific--if you have:

* a private generic type,
* with a nested generic type in it,
* and the nested type conforms to a protocol with an associated type,
* and that associated type’s witness is a generic type,
* and some of the witness type’s generic parameters are generic parameters of the nested type,

demangling would fail. The problem is that the substitution machinery in the runtime would consider there to be three, not two, generic context depths involved. Depth 1, which should correspond to the nested type, would instead have no generic parameters. The fix is to skip over depths with zero generic parameters.

Fixes <rdar://problem/47773183>.
2019-06-20 12:02:01 -07:00
Arnold Schwaighofer
fb08153de0 Fix dynamic replacement runtime when generating replacements from multiple object files
The sections with the replacements get merged and we need to consider all replacements.

SR-10947
rdar://51913012
2019-06-19 15:11:33 -07:00
Arnold Schwaighofer
5c3a4d329b Fix dynamic replacement of some type when used with associated types
rdar://50638228
2019-05-18 10:34:52 -07:00
Joe Groff
9c6bc9ccd7 Runtime: Generalize TypeMetadataRecords to non-type contexts.
We could introduce non-nominal-type context descriptors, such as those for opaque declarations,
which are also interesting to be able to look up for reflection or remote purposes. This should be
a backward compatible change with old runtimes, which always ignore any context descriptor kind
they don't know about.
2019-04-29 11:30:11 -07:00
Joe Groff
7bab318238 Merge pull request #24159 from jckarter/protocol-extension-context-lookup
MetadataLookup: Use extension's generic context for non-nominal extensions.
2019-04-19 13:37:48 -07:00
Joe Groff
9a411bea6a MetadataLookup: Use extension's generic context for non-nominal extensions
In protocol extensions, and in the future parameterized extensions, have their own generic arguments
independent of an originating nominal type's formal generic parameters. Instead of crashing, handle
this gracefully. rdar://problem/50038754
2019-04-19 10:30:43 -07:00
Joe Groff
95c43f4e18 Decode opaque types in the runtime demangler. 2019-04-17 14:44:40 -07:00
Joe Groff
a3c17bd080 Runtime: Implement entry points for opaque type support 2019-04-17 14:44:40 -07:00
Joe Groff
05dfec0cda Push the conformance accessor hack down into getTypeByMangledName. 2019-04-17 14:44:40 -07:00
Harlan Haskins
76dd00beb6 [Demangle] Check for old-style mangling in getObjCClassByMangledName
This caused an issue where the runtime was unable to find subclasses of
resilient subclasses of NSObject until they were first registered by their
sugared name with NSClassFromString or were instantiated directly.

rdar://48892003
2019-03-15 19:03:39 -07:00
Erik Eckstein
abccbd8c8c runtime: make the old remangler allocation free.
Extract common code from the old and new remangler into a common base class.
This lets the old remangler benefit from the changes I did recently in the new remangler.
2019-03-13 11:49:58 -07:00
Erik Eckstein
0dd2495815 runtime: bail in createBoundGenericType if the number of generic parameters and arguments don't match.
This can happen if _typeByName() is called with an invalid mangled type name.
2019-03-06 14:37:03 -08:00
Erik Eckstein
d7fd45d74a Remangler: Use a bump-pointer allocated string instead of std::string
Done by replacing DemanglerPrinter with a bump-pointer allocated CharVector buffer.
This avoids malloc calls.

SR-10028
rdar://problem/48575729
2019-03-06 14:37:03 -08:00
Erik Eckstein
a60086f1dd Runtime: use lambdas to avoid allocations in std::function
Instead of capturing SubstGenericParametersFromMetadata and SubstGenericParametersFromWrittenArgs by value, capture by reference.
This avoids those instances to be copied and thus avoids a lot of mallocs.

SR-10028
rdar://problem/48575729
2019-03-06 14:37:03 -08:00
Erik Eckstein
3bd7f027f8 Runtime: use SmallVector instead of std::vector to avoid memory allocations in most cases.
This dramatically reduces the number of needed malloc calls.
Unfortunately I had to add the implementation of SmallVectorBase::grow_pod to the runtime, as we don't link LLVM. This is a bad hack, but better than re-inventing a new SmallVector implementation.

SR-10028
rdar://problem/48575729
2019-03-06 14:37:03 -08:00
Erik Eckstein
80e86fb5c3 Runtime: make the demangler use stack allocated memory.
This reduces the amount of mallocs significantly.
2019-02-15 09:29:49 -08:00
Erik Eckstein
58f2d373d4 Demangler: Reduce sizeof(Node) from 48 bytes to 24 bytes
This is done by disallowing nodes with children to also have index or text payloads.
In some cases those payloads were not needed anyway, because the information can be derived later.
In other cases the fix was to insert an additional child node with the index/text payload.

Also, implement single or double children as "inline" children, which avoids needing a separate node vector for children.

All this reduces the needed size for node trees by over 2x.
2019-02-15 09:29:49 -08:00
Slava Pestov
2c015164cf Stop passing NodePointer by reference 2019-02-13 21:51:28 -05:00
Slava Pestov
d0c7b1547e ASTMangler: Mangle sugared types for the debugger
Fixes <rdar://problem/48004306>.
2019-02-13 19:17:25 -05:00
John McCall
d07efd37e0 Requestify the mangling-to-metadata APIs.
Note that I've called out a couple of suspicious places where we
are requesting abstract metadata for superclasses but probably
need to be requesting something more complete.
2019-02-05 16:20:48 -05:00
Slava Pestov
36d3f47388 ASTDemangler: Preserve generic arguments for bound generic Objective-C classes
MetadataLookup gives special treatment to imported Objective-C classes,
since there's no nominal type descriptor and metadata is obtained
directly by calling into the Objective-C runtime.

Remote reflection also gives special treatment to imported Objective-C
classes; they don't have field descriptors.

However, the ASTDemangler needs to treat them like ordinary classes,
in particular it wants to preserve the generic arguments here so that
we can round-trip debug info.
2019-01-30 01:28:48 -05:00
Slava Pestov
4a74bf4ba1 ASTDemangler: Add support for lowered function types 2019-01-29 21:55:57 -05:00
Slava Pestov
13a50c2d2d ASTDemangler: Add support for lowered metatypes 2019-01-29 19:15:17 -05:00
Slava Pestov
fce933910d ASTDemangler: Implement types in generic local context
If we nest a type inside a local context inside a generic type,
we have to look through the local context(s) to find the outer
generic type when stripping off generic arguments.

We don't support nominal types inside generic local context
right now, but this can happen with type aliases.
2019-01-29 19:15:17 -05:00
Arnold Schwaighofer
bcc7588db9 Merge pull request #22125 from aschwaighofer/workaround_dynamic_replacement_failure
DynamicReplacement: Don't fail when a library is closed and reloaded
2019-01-26 07:57:06 -08:00
Slava Pestov
b6951932b4 ASTDemangler: Implement type alias types
Debug info uses a special mangling where type aliases can be
represented without being desugared; attempt to reconstruct
the TypeAliasType in this case.
2019-01-25 21:59:48 -05:00
Slava Pestov
55605ceab9 ASTDemangler: Implement DynamicSelfType 2019-01-25 21:44:02 -05:00
Slava Pestov
bbe6a56e22 ASTDemangler: Implement builtin types 2019-01-25 21:44:02 -05:00
Slava Pestov
2d21233378 ASTDemangler: Implement unresolved DependentMemberType
The debug mangling emits these when the type parameter only
conforms to one protocol having an associated type with this
name.
2019-01-25 21:44:02 -05:00
Arnold Schwaighofer
8394fc525e DynamicReplacement: Don't fail when a library is closed and reloaded
rdar://47560273
2019-01-25 13:50:27 -08:00
Doug Gregor
f223d0b886 [Runtime] Eliminate a use-after-free when comparing @objc type names.
We were creating a local Demangler instance, demangling a type name
using it, and then returning one of the resulting nodes to the caller.

Fixes rdar://problem/46817009.
2018-12-20 20:28:23 -08:00
Joe Groff
85eb634191 stdlib: Make _typeByName Foundation SPI reject symbolic references.
It's used for recovering type metadata from deserialized mangled names, which should never have symbolic
references in them.
2018-12-10 20:22:03 -08:00
Joe Groff
021053d499 Runtime: Make getTypeByMangledNameIn(Context|Environment) a public entry point.
This can be used by compiler-generated code as a size optimization for metadata access, using a
mangled name instead of possibly many open-coded metadata calls. It can also allow reflection
libraries outside of the standard library to turn type reference strings into in-process metadata
pointers in a robust way. rdar://problem/46451849
2018-12-10 14:22:41 -08:00
Greg Parker
70bbeaab78 [runtime] Register a hook for class name lookup from libobjc (#20650)
libobjc needs to look up classes by name. Some Swift classes, such as
instantiated generics and their subclasses, are created only on demand.
Now a by-name lookup from libobjc counts as a demand for those classes.

rdar://problem/27808571
2018-11-29 17:06:28 -08:00
Doug Gregor
d4fe6e9ff7 [Runtime] Fix a bug in handling multi-level generic environment substitutions. 2018-11-16 10:39:19 -08:00