Commit Graph

179 Commits

Author SHA1 Message Date
Mike Ash
31f68b1687 [RemoteMirror] Add a call for iterating over the allocations belonging to an AsyncTask.
rdar://72907056
2021-02-11 17:14:38 -05: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
Adrian Prantl
4b9cf31ba0 Add a callback to swift::reflection::MemoryReader that allows LLDB to provide
swift::reflection::TypeInfo for (Clang-)imported non-Objective-C types. This is
needed to reflect on the size mixed Swift / Clang types, when no type metadata
is available for the C types.

This is a necessary ingredient for the TypeRef-based Swift context in
LLDB. Because we do not have reflection metadata for pure C types in Swift,
reflection cannot compute TypeInfo for NominalTypeRefs for those types. By
providing this callback, LLDB can supply this information for DWARF, and
reflection can compute TypeInfos for mixed Swift/C types.
2020-08-25 15:19:49 -07:00
Mike Ash
ecd6d4ddec Add a new ConcurrentReadableHashMap type. Switch the protocol conformance cache
to use it.

ConcurrentReadableHashMap is lock-free for readers, with writers using a lock to
ensure mutual exclusion amongst each other. The intent is to eventually replace
all uses ConcurrentMap with ConcurrentReadableHashMap.

ConcurrentReadableHashMap provides for relatively quick lookups by using a hash
table. Rearders perform an atomic increment/decrement in order to inform writers
that there are active readers. The design attempts to minimize wasted memory by
storing the actual elements out-of-line, and having the table store indices into
a separate array of elements.

The protocol conformance cache now uses ConcurrentReadableHashMap, which
provides faster lookups and less memory use than the previous ConcurrentMap
implementation. The previous implementation caches
ProtocolConformanceDescriptors and extracts the WitnessTable after the cache
lookup. The new implementation directly caches the WitnessTable, removing an
extra step (potentially a quite slow one) from the fast path.

The previous implementation used a generational scheme to detect when negative
cache entries became obsolete due to new dynamic libraries being loaded, and
update them in place. The new implementation just clears the entire cache when
libraries are loaded, greatly simplifying the code and saving the memory needed
to track the current generation in each negative cache entry. This means we need
to re-cache all requested conformances after loading a dynamic library, but
loading libraries at runtime is rare and slow anyway.

rdar://problem/67268325
2020-08-20 13:05:30 -04:00
Augusto Noronha
d2e162676d Fix reading elf section from file buffer 2020-08-13 08:26:11 -03:00
Augusto Noronha
7cd920403d Fix reading metadata from ELF sections 2020-08-11 20:45:23 -03:00
adrian-prantl
722da4bbfa Merge pull request #33358 from augusto2112/reflection-section-constants
Add functionality that returns correct reflection section name for different object files (Mach-O, ELF, COFF)
2020-08-11 16:18:00 -07:00
Augusto Noronha
34c55e2d99 Add functionality that returns correct reflection section name for different object files 2020-08-10 17:21:08 -03:00
Mike Ash
b65d2b5f99 [Reflection][swift-inspect] Add a command for dumping metadata cache nodes. 2020-07-15 14:01:49 -04:00
Mike Ash
5eb226557b [swift-inspect] Support DLQ_GetPtrAuthMask. While we're in there, fix an snprintf warning in ReflectionContext.h 2020-07-10 12:34:25 -04:00
Augusto Noronha
5cee6f3d6a Unify 'MetadataSections' data structure, as well as common ELF and COFF procedures 2020-06-29 19:35:44 -03:00
Mike Ash
c925ce502b Merge pull request #32388 from mikeash/swift-inspect-tag-names
[Reflection][swift-inspect] Show tag names when dumping raw metadata allocations.
2020-06-17 16:14:20 -04:00
Mike Ash
3fcf86ac01 [Reflection][swift-inspect] Show tag names when dumping raw metadata allocations. 2020-06-17 11:31:12 -04:00
David Zarzycki
bf8f50f6eb [Reflection] NFC: Workaround LLVM C++ standard library weirdness
Pull request #32244 introduced the use of `std::stringstream` but that
causes vtables to be generated and we don't want that.
2020-06-16 08:08:34 -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
aea7d03063 [Reflection] Remove useless TaggedUnion.h include. Comment swift_metadata_allocation_tag. 2020-05-29 08:31:04 -07:00
Mike Ash
88046363f1 Minor cleanup of initial swiftdt work. 2020-05-29 08:31:03 -07:00
Mike Ash
7ca1392678 [Reflection] Clean up the ReflectionContext additions.
Move the internal runtime structures into RuntimeInternals.h. Remove leftover methods. Comment the new methods.

rdar://problem/55481578
2020-05-29 08:31:03 -07:00
Mike Ash
ad09919891 [Runtime] Expose the protocol conformance state as a _swift_debug variable.
Have Remote Mirror look that up instead of the C++ mangled name.

rdar://problem/55481578
2020-05-29 08:31:03 -07:00
Mike Ash
b73b325e10 [Runtime] Only enable metadata allocation tracking when requested with an environment variable.
Expose _swift_debug variables for Remote Mirror to use for tracking metadata allocation, and adopt them in Remote Mirror.

rdar://problem/55481578
2020-05-29 08:31:03 -07:00
Mike Ash
d9805ca4eb [Reflection] Add error messages to the new Remote Mirror calls and plumb them through swiftdt.
rdar://problem/55481578
2020-05-29 08:31:03 -07:00
Mike Ash
df5580bcfc [Tools] Move metadata dumping into swiftdt, have Remote Mirror just iterate and inspect.
rdar://problem/55481578
2020-05-29 08:31:03 -07:00
Mike Ash
48e5473d9a [Tools] Simplify swiftdt conformance cache dumping since the conformance cache keys are always metadata pointers. 2020-05-29 08:31:03 -07:00
Mike Ash
2c7f09cef7 [Tools] Add some metadata printing to swiftdt.
rdar://problem/55481578
2020-05-29 08:31:03 -07:00
Mike Ash
22cfe461ec [Tools] Super rough draft of swiftdt dumping MetadataAllocator contents.
rdar://problem/55481578
2020-05-29 08:31:03 -07:00
Mike Ash
0ddbd84841 [Tools] Teach swiftdt how to retrieve ObjC class names.
rdar://problem/55481578
2020-05-29 08:31:03 -07:00
Mike Ash
98c2a35d77 [Tools] Get a first pass of swiftdt conformance cache dumping up and running.
rdar://problem/55481578
2020-05-29 08:31:03 -07:00
Mike Ash
a317b8737a Merge pull request #30739 from mikeash/remote-mirror-fix-ownsaddress
[Reflection] Fix the ownsAddress function to claim heap-allocated Metadatas
2020-04-01 14:46:49 -04:00
tbkka
3c8fde7885 Implement MultiPayloadEnum support for projectEnumValue (#30635)
This code rearchitects and simplifies the projectEnumValue support by
introducing a new `TypeInfo` subclass for each kind of enum, including trivial,
no-payload, single-payload, and three different classes for multi-payload enums:

* "UnsupportedEnum" that we don't understand.  This returns "don't know" answers for all requests in cases where the runtime lacks enough information to accurately handle a particular enum.

* MP Enums that only use a separate tag value.  This includes generic enums and other dynamic layouts, as well as enums whose payloads have no spare bits.

* MP Enums that use spare bits, possibly in addition to a separate tag.  This logic can only be used, of course, if we can in fact compute a spare bit mask that agrees with the compiler.

The final challenge is to choose one of the above three handlings for every MPE.  Currently, we do not have an accurate source of information for the spare bit mask, so we never choose the third option above.  We use the second option for dynamic MPE layouts (including generics) and the first for everything else.

TODO: Once we can arrange for the compiler to expose spare bit mask data, we'll be able to use that to drive more MPE cases.
2020-03-31 15:12:44 -07:00
Mike Ash
5da118eef2 [Reflection] Fix the ownsAddress function to claim heap-allocated Metadatas.
ownsAddress was a simple range check on images, but that won't find Metadatas that get allocated on the heap. If an address isn't found, try reading it as a Metadata and doing a range check on the type context descriptor too.

rdar://problem/60981575
2020-03-31 16:49:56 -04:00
tbkka
94a09104fe Don't try to look up info for a null TypeRef (#30640)
This indirectly hardens the `swift_reflection_infoForTypeRef` API
against being invoked with a null TypeRef pointer.  The API already
handles a nullptr being returned from `TypeConverter::getTypeInfo`
by converting it into a standard "UNKNOWN" type info descriptor.

Resolves rdar://60633988
2020-03-25 18:27:59 -07: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
Mike Ash
b80e33b514 Fix reuse of the payload area when the discriminator != 0 2020-03-09 11:58:54 -07: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
Ivan Smetanin
dd7d173c97 [stdlib] [reflection] Excise iostream from reflection 2019-10-14 20:28:20 +03:00
Joe Groff
5d3ee68f9c swift-reflection-dump: Base ObjectMemoryReader address space on image vm address.
As the base of the "remote" address space ObjectMemoryReader presents for an image, use the
image's own preferred VM address mappings. If there are multiple images loaded, differentiate
them by using the top 16 bits of the remote address space as an index into the array of images.

This should make it so that absolute pointers in the file Just Work without sliding in most
cases; we'd only need to mix in the image index in order to have a value that is also a valid
remote address.
2019-10-01 12:12:25 -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
12bf25e75d Merge pull request #27388 from jckarter/reflection-remove-start-addresses
swift-reflection-dump: Virtualize logical-to-physical address mapping.
2019-09-27 14:09:22 -07:00
Saleem Abdulrasool
8db900634d Revert "Fix parsing of ELF images"
This reverts commit e2b6a3d982.  This
broke on the handling of x86_64 relocation as well as the Windows build
process.
2019-09-27 08:14:32 -07:00
Jordan Rose
a6dd630ca3 Eliminate Builtin.UnknownObject as an AST type (#27378)
This removes it from the AST and largely replaces it with AnyObject
at the SIL and IRGen layers. Some notes:

- Reflection still uses the notion of "unknown object" to mean an
  object with unknown refcounting. There's no real reason to make
  this different from AnyObject (an existential containing a
  single object with unknown refcounting), but this way nothing
  changes for clients of Reflection, and it's consistent with how
  native objects are represented.

- The value witness table and reflection descriptor for AnyObject
  use the mangling "BO" instead of "yXl".

- The demangler and remangler continue to support "BO" because it's
  still in use as a type encoding, even if it's not an AST-level
  Type anymore.

- Type-based alias analysis for Builtin.UnknownObject was incorrect,
  so it's a good thing we weren't using it.

- Same with enum layout. (This one assumed UnknownObject never
  referred to an Objective-C tagged pointer. That certainly wasn't how
  we were using it!)
2019-09-26 17:48:04 -07:00
Alexander Shaposhnikov
e2b6a3d982 Fix parsing of ELF images 2019-09-26 14:37:36 -07:00
Joe Groff
14453147d6 Reflection: Remove StartAddress from ReflectionInfo.
We now get the local/remote mapping from RemoteRef when we need it.
2019-09-26 12:09:52 -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
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
83b34ab8d3 Merge pull request #27306 from jckarter/remove-reflection-offsets
Reflection: Remove vestigial Offset fields from ReflectionInfo
2019-09-24 10:04:20 -07:00
Joe Groff
bc3795eb53 Reflection: Remove vestigial Offset fields from ReflectionInfo
These are now always zero, because memory readers handle virtual address mapping.
The `swift_reflection_info_t` structure used by the C RemoteMirror API keeps
its offset fields because it's supposed to be a stable API, but we now assert that
the values are always zero.
2019-09-23 14:32:26 -07:00
Joe Groff
761b02949a ReflectionContext: Remove platform #ifs for image format checks.
In principle, swift-reflection-* ought to work with cross-compiled binaries. Dispatch out to reading
MachO, PE, or ELF section metadata based on the magic of an image passed to `addImage` instead of
using #ifs to pick an implementation based on the host platform. (This still doesn't fully address
other host/target differences like word size or endianness, but is progress toward making the tool
target-agnostic.)
2019-09-18 11:43:35 -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
33e7ed74bc ReflectionContext: Keep read range properly-aligned.
If a Mach-O image got emitted in just the wrong way, the range of `__TEXT,__swift*` sections to
read could end up starting at an unaligned address (because things like type refs have only one
byte alignment), and this would cause the reflection context to read an unaligned chunk of the
remote memory, causing alignment errors when addresses are mapped to the local copy. Keep the ranges
at least 8-byte-aligned to stave off the alignment issues we might run into with any metadata
structures, which are generally at most pointer aligned. Fixes rdar://problem/54556791
2019-08-26 16:24:26 -07:00