Commit Graph

190 Commits

Author SHA1 Message Date
Mike Ash
cf3c131e7c [Reflection] Add API for inspecting async task allocation slabs.
We remove the existing `swift_reflection_iterateAsyncTaskAllocations` API that attempts to provide all necessary information about a tasks's allocations starting from the task. Instead, we split it into two pieces: `swift_reflection_asyncTaskSlabPointer` to get the first slab for a task, and `+swift_reflection_asyncTaskSlabAllocations` to get the allocations in a slab, and a pointer to the next slab.

We also add a dummy metadata pointer to the beginning of each slab. This allows tools to identify slab allocations on the heap without needing to locate every single async task object. They can then use `swift_reflection_asyncTaskSlabAllocations` on such allocations to find out about the contents.

rdar://82549631
2021-11-18 14:15:25 -05:00
Mike Ash
89d93a000d [Runtime] Replace std::max_align_t with MaximumAlignment from MetadataValues.h.
On Windows, std::max_align_t is only 8-byte aligned, but Swift assumes 16-byte alignment. MaximumAlignment is our notion of the maximum alignment of a type, so use that instead.
2021-11-18 13:56:05 -05:00
Saleem Abdulrasool
3c9c564eba Revert "[Reflection] Add API for inspecting async task allocation slabs." 2021-11-17 18:47:13 -08:00
Mike Ash
7c7dc5d5b3 [Reflection] Add API for inspecting async task allocation slabs.
We remove the existing `swift_reflection_iterateAsyncTaskAllocations` API that attempts to provide all necessary information about a tasks's allocations starting from the task. Instead, we split it into two pieces: `swift_reflection_asyncTaskSlabPointer` to get the first slab for a task, and `+swift_reflection_asyncTaskSlabAllocations` to get the allocations in a slab, and a pointer to the next slab.

We also add a dummy metadata pointer to the beginning of each slab. This allows tools to identify slab allocations on the heap without needing to locate every single async task object. They can then use `swift_reflection_asyncTaskSlabAllocations` on such allocations to find out about the contents.

rdar://82549631
2021-11-11 16:58:14 -05:00
Mike Ash
2e4754344a [Reflection] Fix ReflectionContext::allocationMetadataPointer.
The definition of GenericMetadataCacheEntry was wrong, causing the function to return garbage.
2021-10-15 15:17:32 -04:00
Mike Ash
c0e6e1d878 [Reflection] Fix iterateAsyncTaskAllocations.
Fix the declaration of AsyncTask and add a test for iterateAsyncTaskAllocations. Reflection's declaration of AsyncTask had fallen out of sync with the real thing. The test that was supposed to catch this was never actually committed, oops.

Add a swift_reflection_libraryVersion variable to Remote Mirror to indicate the presence of this fix. In the future, the value can be incremented to signal the presence of other changes that can't otherwise be detected.

rdar://80035307
2021-07-02 17:33:20 -04:00
Augusto Noronha
0a67868a13 Implement ReflectionContext::addImage with section finder
Implement a version of addImage that takes in a closure responsible
for finding the sections of interest. This allows for the registration
of metadata even in situations where the object file is not complete
(for example, when generated by the JIT).
2021-06-24 17:35:57 -03:00
Alastair Houghton
eb68120112 [RemoteMirror] Add new API for getting the type descriptor address.
This will allow the heap tool to work out which binary a dynamically allocated
class comes from, by looking up its nominal type descriptor address and then
seeing which binary contains that.

Fixes rdar://65742351.
2021-06-17 10:50:21 +01:00
Augusto Noronha
f41d192b48 Implement projectExistentialAndUnwrapClass
Implement a version of projectExistential tailored for LLDB. There are 2
differences when projecting existentials for LLDB:

1 - When it comes to existentials, LLDB stores the address of the error
    pointer, which must be dereferenced.
2 - When the existential wraps a class type, LLDB expects the address
    returned is the class instance itself and not the address of the
    reference.

This patch also adapts the swift reflection test machinery to test
projectExistentialAndUnwrapClass as well. This is done by exposing
the new functionality from swift reflection test.  It is tested in
existentials.swift, and ensures that the typeref information is
exactly the same as what is expected from projectExistential,
except the out address.

(cherry picked from commit 55e971e06750c3ba29722d558cc5400298f6bdaf)
2021-06-16 08:54:50 -03:00
Augusto Noronha
b54d42653e Read machO sections section by section
This changes how ReflectionContext reads machO reflection sections by reading them individually instead of as one big memory block spanning from the first to the last section (and including whatever else is in between). This change will enable an optimization on LLDB's side where, if we're reading read-only data, we read from the file-cache instead of the child process, which should speed up debugging when working with remote processes.
2021-04-23 14:16:50 -03:00
Adrian Prantl
38148be26f Add missing error handling to ReflectionContext
We found crashes deep in TypeRefBuilder that could traced back to a likely
nullptr RemoteRef<> section address. It is very plausible that this is
connected to a failed MemoryReader::getBytes() call, which can fail but
isn't checked.

This patch adds missing error checks to every call to readBytes().

rdar://74445486
(cherry picked from commit 714cefbba4)
2021-02-24 15:09:14 -08:00
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