This patch adds parsing and extracting of the Swift reflection
metadata data segments from within the WebAssembly DATA section and
tests it using swift-reflection-dump. This is needed to allow LLDB to
acces Swift reflection metadata when attached to WebAssembly
processes.
rdar://159217213
When parsing ELF, reading the number of sections needs special handling.
From the elf man page:
If the number of entries in the section header table is
larger than or equal to SHN_LORESERVE (0xff00), e_shnum
holds the value zero and the real number of entries in the
section header table is held in the sh_size member of the
initial entry in section header table. Otherwise, the
sh_size member of the initial entry in the section header
table holds the value zero.
Adds an `IsSuspended` field to `AsyncTaskInfo`, which is the same value
as `HasTaskDependency`, but uses the `Is<State>` naming to match
`IsEnqueued`, `IsRunning`, and `IsComplete`.
Based on the docs in Task.h:
```cpp
/// A task can have the following states:
/// * suspended: In this state, a task is considered not runnable
/// * enqueued: In this state, a task is considered runnable
/// * running on a thread
/// * completed
```
rdar://148663671
Add an extra opaque field to AddressSpace, which can be used by clients
of RemoteInspection to distinguish between different address spaces.
LLDB employs an optimization where it reads memory from files instead of
the running process whenever it can to speed up memory reads (these can
be slow when debugging something over a network). To do this, it needs
to keep track whether an address originated from a process or a file. It
currently distinguishes addresses by setting an unused high bit on the
address, but because of pointer authentication this is not a reliable
solution. In order to keep this optimization working, this patch adds an
extra opaque AddressSpace field to RemoteAddress, which LLDB can use on
its own implementation of MemoryReader to distinguish between addresses.
This patch is NFC for the other RemoteInspection clients, as it adds
extra information to RemoteAddress, which is entirely optional and if
unused should not change the behavior of the library.
Although this patch is quite big the changes are largely mechanical,
replacing threading StoredPointer with RemoteAddress.
rdar://148361743
ELF section headers are allowed to be left uninitialized when the
section is empty and unused. LLD is a tad more aggressive about this.
The ELF reader in the Swift runtime was a bit aggressive about
converting the section headers to names and would not skip over these
unused sections headers resulting in crashes due to operating on
uninitialized memory in the `sh_name` field.
This patch teaches the ELF reader to skip over unused section header
table entries.
This patch changes RemoteAbsolutePointer to store both the symbol and
the resolved address. This allows us to retire some ugly workarounds
to deal with non-symbolic addresses and it fixes code paths that would
need these workarounds, but haven't implemented them yet (i.e., the
pack shape handling in the symbolicReferenceResolver in MetadatyaReader.
Addresses parts of rdar://146273066.
rdar://153687085
Reflection metadata lookup failures are notoriously difficult to debug
because there is no error handling in TypeLowering outside of
compile-time #ifdef'd fprintf(stderr) calls. The nicest thing to do
would be to adopt llvm::Expected<> but TypeLowering is also included
in the standard library, which only has access to a tiny subset of the
LLVM Support library. This patch adds a place to store a pointer to
the first encountered error, which can then be converted to an
llvm::Error at the API level.
When the RunJob pointer is set to adapters, we try to get the "real" run pointer from the context. However, there are cases where the context can be a dangling pointer, when the task has finished with it but hasn't reset the pointer to anything else. For cases where that can happen, the context is legitimate only when there's a dependency record. Check for a dependency record before trying to read the context in those cases.
In most uses this will fail gracefully or return a garbage run pointer, but swift-reflection-test uses an in-process memory reader which can crash when trying to chase this pointer, resulting in test failures.
rdar://149252404
Iterating child tasks depends on knowing the size of AsyncTask, and changing the size of the task broke it. Instead of relying on mirroring the full structure in our out-of-process definitions, add a debug variable to libswift_Concurrency that contains the size of AsyncTask.
While we're there, add some more validation to child task enumeration. Check each child task's metadata pointer to make sure that it actually points to the AsyncTask metadata, and have the inner loop also increment and check ChildTaskLoopCount to stop runaway iteration in that loop.
This will be used by lldb. I'd like to add it to swift-inspect too, but that will require figuring out how to evolve the swift runtime's ABI.
rdar://147448235
When building the runtime we define `SWIFT_RUNTIME`. `swiftCore_EXPORTS`
is only defined when building swiftCore as a shared library. This would
thus allow the ODR violations to appear in a static library form of the
standard library. This was uncovered with the new runtime build system.
Expose a `Task`'s resume context via the `AsyncTaskInfo` struct.
This will be used by lldb, but since this data is not specific to lldb it seems reasonable to include it generally.
This change is part of a fix for a regression that wasn't noticed because lldb has a
fallback to using ASTContexts when type metadata resolution fails.
For any Swift class that directly or indirectly inherits from an ObjC class (such as
`NSObject`, `NSView`, etc), the function `computeUnalignedFieldStartOffset` will fail
to compute the start offset. To compute the start offset, it traverses the parent
classes and uses their sizes. Once the traversal reaches an ObjC class, the
RemoteInspection does not know the size. The fix here is to get the size from the
`TypeInfo` returned by the external `TypeInfoProvider` (which in LLDB is
`LLDBTypeInforProvider`).
Depended on by https://github.com/swiftlang/llvm-project/pull/9320
without relying on spare bit information in the reflection metadata
(which was added in #40906). As a result, we can remove the
code from #40906.
This is the first step in such removal. It removes the RemoteMirror
code for looking up such metadata. It leaves behind:
* Sufficient stubs for LLDB to continue to build. Once LLDB is updated, these stubs can be removed as well.
* The compiler code to emit such metadata. This allows new binaries to still reflect MPEs on older runtimes. This will need to be kept for a transitional period.
Pass in a TypeInfoProvider parameter to computeUnalignedFieldStartOffset
because it calls getClassInstanceTypeInfo which might need the external
type info provider to find clang types.
rdar://128141491
There are a number of instances in `ReflectionContext.h` where we
are doing `return false` with an `std::optional<...>` where it seems
we really mean to return an empty optional instead.
(The way to do this is either `return {}` or `return std::nullopt`.)
rdar://123504095
When linking with `gold`, we can end up with two separate sections
for each of the reflection sections; this happens because `swiftrt.o`
declares them with `SHF_GNU_RETAIN` set (to stop them from vanishing
when section GC is enabled), but if the compiler is set to allow the
reflection information to be stripped then *it* will generate them
*without* `SHF_GNU_RETAIN`.
The other Linux linkers will coalesce the two sections and leave the
`SHF_GNU_RETAIN` flag set, but `gold` does not, and adds both of them
separately to the output.
The upshot is that we want `ReflectionContext` to look for both the
retained and non-retained sections and read the data from both of
them.
rdar://123504095
We'd crash on a call to getKind(). Ignore them instead by returning false from isMetadataSourceReady. If this causes getClosureContextInfo to fail to make forward progress, then it will in turn fail and return nullptr to its caller.
rdar://126866747
This change is analogous to the TypeInfo cache in TypeLowering, which does the
exact same thing. In LLDB the ExternalTypeInfoProvider may gain new information
throughout the life of a process, and more importantly, it may return
context-dependent results.
rdar://122432501
Implement computeUnalignedFieldStartOffset as an alternative way to
finding out the start of the fields that belong to the instance when
reading the field's start from binary is not possible (for example,
embedded Swift doesn't emit any reflection metadata on the binary).
Currently, TypeRefBuilder knows how to parse type descriptors from
reflection metadata. We aim to store the same information that lives
in type descriptors externally, as an effort to support embedded Swift
debugging. In order to take advantage of all of the existing type
information parsing code that exists today in remote mirrors, add an
interface for external lookup of type descriptors, and replace all the
usages of type descriptors with their generic counterpart (this patch
only adds support for builtin descriptors, but follow up patches will
add support for other descriptor types).
Separately track text and data segments for ownsAddress. We were previously tracking one range per image, encompassing the range from the start of the image through the end of the data segment. This ends up including a lot of unwanted address space if the two aren't adjacent, as is the case for libraries in the shared cache on Darwin.
This makes metadataIsActor a lot more reliable, as it was previously identifying a lot of garbage as actor metadata due to the supposed descriptor pointer falling in this range.
rdar://113417637
ReflectionContext::allocationMetadataPointer() was reading the
metadata pointer from a wrong offset because of the out-of-sync struct
layouts and dump-generic-metadata was not working correctly.
This change resync's the layouts and adds a static_assert to verify
that the offsets match between GenericMetadataCacheEntry and
GenericCacheEntry.