Commit Graph

304 Commits

Author SHA1 Message Date
Fred Riss
259d78a350 Adapt to llvm.org StringRef API change 2020-03-13 19:08:22 +01:00
tbkka
ee36c1cc3e RemoteMirror: Project some multi-payload enums (#30357)
* First part of multi-payload enum support

This handles multi-payload enums with fixed
layouts that don't use spare payload bits.
It includes XI calculations that allow us to
handle single-payload enums where the payload
ultimately includes a multi-payload enum
(For example, on 32-bit platforms, String uses
a multi-payload enum, so this now supports single-payload
enums carrying Strings.)
2020-03-11 18:48:39 -07:00
Kuba (Brecka) Mracek
ab6533a40f Merge branch 'master' into mracek/arm64e 2020-03-06 15:07:01 -08:00
tbkka
0d361bd3ea Teach RemoteMirror how to project enum values (#30161)
Teach RemoteMirror how to project enum values

This adds two new functions to the SwiftRemoteMirror
facility that support inspecting enum values.

Currently, these support non-payload enums and
single-payload enums, including nested enums and
payloads with struct, tuple, and reference payloads.
In particular, it handles nested `Optional` types.

TODO: Multi-payload enums use different strategies for
encoding the cases that aren't yet supported by this
code.

Note: This relies on information from dataLayoutQuery
to correctly decode invalid pointer values that are used
to encode enums.  Existing clients will need to augment
their DLQ functions before using these new APIs.

Resolves rdar://59961527

```
/// Projects the value of an enum.
///
/// Takes the address and typeref for an enum and determines the
/// index of the currently-selected case within the enum.
///
/// Returns true iff the enum case could be successfully determined.
/// In particular, note that this code may fail for valid in-memory data
/// if the compiler is using a strategy we do not yet understand.
SWIFT_REMOTE_MIRROR_LINKAGE
int swift_reflection_projectEnumValue(SwiftReflectionContextRef ContextRef,
                                      swift_addr_t EnumAddress,
                                      swift_typeref_t EnumTypeRef,
                                      uint64_t *CaseIndex);

/// Finds information about a particular enum case.
///
/// Given an enum typeref and index of a case, returns:
/// * Typeref of the associated payload or zero if there is no payload
/// * Name of the case if known.
///
/// The Name points to a freshly-allocated C string on the heap.  You
/// are responsible for freeing the string (via `free()`) when you are finished.
SWIFT_REMOTE_MIRROR_LINKAGE
int swift_reflection_getEnumCaseTypeRef(SwiftReflectionContextRef ContextRef,
                                        swift_typeref_t EnumTypeRef,
                                        unsigned CaseIndex,
                                        char **CaseName,
                                        swift_typeref_t *PayloadTypeRef);
```


Co-authored-by: Mike Ash <mikeash@apple.com>
2020-03-06 13:17:40 -08:00
Kuba Mracek
84c4864911 [arm64e] Add Swift compiler support for arm64e pointer authentication 2020-02-27 16:10:31 -08:00
Fred Riss
20573bdbb1 [MetadataReader] Adapt to newer Objective-C class_rw_t
Newer Objective-C runtimes implement a size optimization in class_rw_t
which requires an additional indirection to get to the class_ro_t pointer.

Thanks to Davide for getting to the bottom of this!
2019-10-21 20:59:05 -07:00
Joe Groff
263bbbb839 RemoteMirror: Work around flaw in C interface
The CMemoryReader interface relies on a `GetStringLength` callback, which
returns zero either if the address is invalid or if a valid zero-length
string exists at the given address. We don't want to break ABI with
RemoteMirror, but we can work around this by issuing a one-byte read
at the address and confirming that a null terminator exists there.
2019-10-01 12:36:49 -07:00
Joe Groff
40e40103a8 Merge pull request #27418 from jckarter/reflection-symbolic-absolute-pointer
MetadataReader: Add an API for reading absolute pointers.
2019-09-30 08:48:32 -07:00
Joe Groff
4012a207c8 MetadataReader: Add an API for reading absolute pointers.
Pointer data in some remote reflection targets may required relocation, or may not be
fully resolvable, such as when we're dumping info from a single image on disk that
references other dynamic libraries. Add a `RemoteAbsolutePointer` type that can hold a
symbol, offset, or combination of both, and add APIs to `MemoryReader` and `MetadataReader`
for reading pointers that can get unresolved relocation info from an image, or apply
relocations to pointer information. MetadataReader can use the symbol name information to
fill in demanglings of symbolic-reference-bearing mangled names by using the information
from the symbol name to fill in the name even though the context descriptors are not
available.

For now, this is NFC (MemoryReader::resolvePointer just forwards the pointer data), but
lays the groundwork for implementation of relocation in ObjectMemoryReader.
2019-09-28 16:53:34 -07:00
Joe Groff
e5b31c4e1e Merge pull request #27383 from jckarter/type-ref-builder-remote-metadata-reader
Reflection: Share demangler with MetadataReader.
2019-09-26 13:58:16 -07:00
Joe Groff
633471c092 Reflection: Share demangler with MetadataReader.
TypeRefBuilder and MetadataReader had nearly identical symbolic reference resolvers,
but diverged because TypeRefBuilder had its own local/remote address management mechanism,
and because TypeRefBuilder tries to resolve opaque types to their underlying types, whereas
other MetadataReader clients want to preserve them as written in source. The first problem
has been addressed by making TypeRefBuilder use `RemoteRef` everywhere, and the second
can be handled with a flag (and might be able to be handled more elegantly with some more
refactoring of general opaque type handling in MetadataReader).
2019-09-26 11:28:00 -07:00
Joe Groff
02b5e56327 Merge pull request #27369 from jckarter/type-ref-builder-remote-ref
Reflection: Traffic in RemoteRefs.
2019-09-26 09:22:57 -07:00
Joe Groff
8c4df3b4c2 Reflection: Traffic in RemoteRefs.
Instead of passing around raw local pointers and references, and spreading
tricky offset arithmetic around with the Local/RemoteAddress fields in
ReflectionInfo, have the TypeRefBuilder code use RemoteRefs everywhere,
which keep the remote/local mapping together in one unit and provide
centralized API for this logic.

This doesn't yet change how code uses the RemoteRef address data to
follow pointers across objects, for things like reading type refs, but
that should be much easier to do after this lands.
2019-09-25 20:28:03 -07:00
Joe Groff
bacbdb8164 Merge pull request #27368 from jckarter/metadata-reader-anonymous-context-size
MetadataReader: Correct size measurement reading AnonymousContextDescriptors.
2019-09-25 17:09:01 -07:00
Joe Groff
a37f81880e MetadataReader: Correct size measurement reading AnonymousContextDescriptors.
By including the trailing mangled name reference in the baseSize, we computed the wrong offset for
the generic parameter header, and then miscomputed the size of the trailing generic context info.
This would lead to accesses into the context sometimes reading from uninitialized memory.
Fixes rdar://problem/55711107
2019-09-25 14:29:47 -07:00
Joe Groff
31212b8b25 Fix handling of null relative references in RemoteRef. 2019-09-23 20:08:48 -07:00
Joe Groff
88a833f19c Reflection: Move resolveRelativeField functionality onto RemoteRef
Resolving a direct relative reference given a RemoteRef doesn't need the MetadataReader,
since the offset should already be in the local buffer; we can add it to RemoteRef's
saved remote address and get a new remote address. Refactor the API to make as much as
possible of it available directly on RemoteRef.
2019-09-20 15:01:57 -07:00
Joe Groff
f1cb0d11ef Reflection: Make RemoteRef template not depend on Runtime.
The only thing the Runtime affects is the width of the StoredPointer for the remote address, for
which storing a uint64_t ought to be enough for anyone we care about so far. This will make it
easier to store and use RemoteRefs in code that isn't or shouldn't ideally be templatized on
Runtime (such as TypeRefBuilder, and ultimately ReflectionContext, from the Reflection library.)
2019-09-20 12:43:51 -07: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
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
Joe Groff
17511c06bd MetadataReader: Build demanglings for opaque type contexts.
When building for debug, the opaque return type context is nested under an anonymous context for
the defining function. Demangle the anonymous context name to reconstruct the mangling for the
opaque type.
2019-05-02 17:27:59 -07:00
Joe Groff
fc8be62895 RemoteAST: Add a request to get the underlying type from an opaque type descriptor. 2019-05-01 15:42:57 -07:00
swift-ci
3aa9158940 Merge pull request #24130 from mikeash/readMetadataAndValueErrorExistential-null-check 2019-04-18 16:35:28 -07:00
Mike Ash
55cf559adc [RemoteMirror] Add a missing NULL check when reading metadata in readMetadataAndValueErrorExistential.
rdar://problem/49246601
2019-04-18 16:44:45 -04:00
Joe Groff
05dfec0cda Push the conformance accessor hack down into getTypeByMangledName. 2019-04-17 14:44:40 -07:00
Adrian Prantl
0b8fc3d405 Remove Resilience workarounds and describe fixed-size global resilient
variables in DWARF.

rdar://problem/48409386
2019-03-27 10:51:16 -07:00
Mike Ash
78e7106c57 [Reflection] Gracefully handle the absence of objc_debug_taggedpointer_ext_* variables.
rdar://problem/48483373
2019-02-28 17:51:54 -05:00
Slava Pestov
c8343525c9 Merge pull request #22975 from slavapestov/remoteast-error-existential-linux
RemoteAST: Fix Error existential introspection on Linux
2019-02-28 15:04:55 -05:00
Slava Pestov
31992143ee RemoteAST: Fix Error existential introspection on Linux
We don't have tests that look at the instance address
on the Swift side, but lldb is going to use this API
shortly.
2019-02-28 00:00:36 -05:00
Mike Ash
f85d87f764 [Reflection] Fail gracefully when objc_debug_taggedpointer_obfuscator isn't present.
Older OSes don't have this variable. We can treat it as 0 when it's not present.

rdar://problem/48411483
2019-02-27 14:37:18 -05:00
Saleem Abdulrasool
40c3655838 Remote: loosen a cast
cl objects to the use of the reinterpret_cast for the casting between
integral types.  Loosen to a static_cast.
2019-02-25 11:19:47 -08:00
Slava Pestov
86fe7fde77 RemoteAST: Fix error existential introspection on Linux 2019-02-18 18:44:02 -05: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
905a6def79 RemoteAST: Clean up use of std::pair and std::tuple for opened existentials with named structs 2019-02-12 15:18:03 -05:00
Slava Pestov
76ebaee078 MetadataReader: Add support for NSError toll-free bridging
An Error existential value can directly store a
reference to an NSError instance without wrapping
it in an Error container.

When "projecting" such an existential, the dynamic type
is the NSError's isa pointer, and the payload is the
address of the instance itself.
2019-02-12 14:59:57 -05:00
Slava Pestov
5824b0f475 MetadataReader: Factor out readNominalTypeFromClassMetadata() to clean up the code flow 2019-02-12 14:59:57 -05:00
Slava Pestov
94a6ca3094 MetadataReader: Fix reading obfuscated tagged pointers
Turns out the tags are shuffled around by XORing with a
per-process hash, and we have to deobfuscate the tag
before checking if its an extended tag.

There's no test for this; just running the existing tests
several times in a row is sufficient to trigger the problem.
2019-02-11 19:12:02 -05:00
Slava Pestov
28f305bf75 MetadataReader: Fix caching of Objective C class metadata
If resolving the type of an instance produces a class metadata for
which we cannot build a type (for example, a special class like
__NSCFNumber, which the ClangImporter does not produce a ClassDecl
for), we try the superclass.

The caching logic was broken in this case, so subsequent calls
would return an empty type.
2019-02-11 19:12:02 -05:00
Slava Pestov
dc3abd14c3 MetadataReader: Add support for Objective-C tagged pointers (#22498)
This allows Remote Mirrors and RemoteAST to inspect existentials
containing instances of imported classes.
2019-02-10 12:23:37 -05:00
Saleem Abdulrasool
9bd31f9d04 Remote: fully qualify NodePointer for Win32
This adjusts the uses of `NodePointer` to an elaborated type to resolve
ambiguity when building for Windows.
2019-01-28 19:55:27 -08:00
Saleem Abdulrasool
b422fdcaf7 Merge pull request #22155 from compnerd/NodePointer
Remote: use the qualified name for `NodePointer`
2019-01-28 10:56:56 -08:00
Doug Gregor
21e5a7e8a2 [Metadata reader] Save/restore symbolic reference resolver
… rather than clearing it out completely. This is better hygiene, although it
doesn’t matter right now.
2019-01-27 21:58:09 -08:00
Doug Gregor
23886ba579 [Metadata reader] Form demangle trees for generic extension contexts.
Translate the metadata for the generic requirements of an extension context
into a demangle tree that is associated with the demangling of an extension.

Teach the ASTDemangler how to handle class layout constraints as well.

With this, RemoteAST can resolve types nested within most constrained
extensions.
2019-01-26 23:25:51 -08:00
Doug Gregor
ff687967cc [Metadata reader] Handle symbolic references in mangled names.
When reading a mangled name, make sure to cope with embedded null bytes that
show up in symbolic references. When demangling such a name, handle symbolic
references.
2019-01-26 23:23:37 -08:00
Doug Gregor
850a2a753d [Metadata reader] Factor out reading metadata for a protocol reference.
Protocol references are interesting because we have to deal with the
low bit indicating whether we have a reference to an Objective-C protocol.
Factor out this logic for later re-use.
2019-01-26 23:08:38 -08:00
Saleem Abdulrasool
e8c6e842ce Remote: use the qualified name for NodePointer
Use the qualified name for the `NodePointer`.  The unqualified name
causes ambiguity when building on Windows.  This repairs the Windows
build.
2019-01-26 11:30:00 -08:00
Doug Gregor
cc360908cc [Remote AST] Resolve extension context descriptors to demangle trees.
Read the extended context mangled name from an extension context descriptor
so we can form a proper demangle tree for extensions. For example, this allows
types nested within extensions of types from different modules to be found.
2019-01-25 21:18:43 -08:00
Doug Gregor
9e7826b29f [Remote AST] Resolve local types using anonymous context descriptor mangling.
When the mangled name is available within an anonymous context descriptor
for a local type, use that mangled name to help RemoteAST resolve lookups
based on local type metadata.
2019-01-25 20:17:50 -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