Commit Graph

94 Commits

Author SHA1 Message Date
Adrian Prantl
75bee892be Add LLDB and swift-reflection-dump support for WebAssembly
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
2025-08-27 17:14:17 -07:00
Ben Barham
9779c18da3 Rename startswith to starts_with
LLVM is presumably moving towards `std::string_view` -
`StringRef::startswith` is deprecated on tip. `SmallString::startswith`
was just renamed there (maybe with some small deprecation inbetween, but
if so, we've missed it).

The `SmallString::startswith` references were moved to
`.str().starts_with()`, rather than adding the `starts_with` on
`stable/20230725` as we only had a few of them. Open to switching that
over if anyone feels strongly though.
2024-03-13 22:25:47 -07:00
Alejandro Alonso
382510fa50 Rename Reflection library to RemoteInspection (#62846) 2023-01-06 13:21:32 -05:00
swift-ci
0f48feb1e7 Merge pull request #61557 from adrian-prantl/refl-objc-interop
[swift-reflection-dump] Make ObjC interoperability configurable.
2022-10-14 14:46:34 -07:00
Adrian Prantl
33eb213e28 [swift-reflection-dump] Make ObjC interoperability configurable.
This addresses a FIXME in the code. This allows running swift-reflection-dump
on macOS to dump the contents of an ELF binary.
2022-10-13 17:21:39 -07:00
Adrian Prantl
75c42ed43b [swift-reflection-dump] Turn --binary-filename into a positional argument.
I mostly find it annoying to type and this makes the usage of the tool more
consistent with other similar tools like dwarfdump or otool.
2022-10-12 11:18:59 -07:00
Artem Chikin
458eccca5f [LibSwiftScan] Add entry-points for querying protocol conformances from the static mirror
This commit adds new entry-points to `libSwiftScan` that operate on the new BinaryScanningTool, which reads out Swift type information from object files, starting with a query of all protocol conformances.
2022-02-02 11:56:17 -08:00
Artem Chikin
5d17074222 [Static Mirror] Allow ObjCInteropKind to be specified by the client of conformanceReader, separately from how the client builds. 2022-02-01 10:56:58 -08:00
Artem Chikin
e934fe201a Add extraction protocol conformance descriptor extraction, as read from an object file image, to TypeRefBuilder. 2022-01-26 13:54:02 -08:00
Artem Chikin
f3d784fd0f Factor out Object File handling from swift-reflection-dump to separate CMake target: StaticMirror
This target will then be used to have a broader set of tools for reading metadata from object files.
2022-01-24 16:49:01 -08:00
Adrian Prantl
fede775269 Make Objective-C interoperability configurable in the runtime
In order to be able to debug, for example, a Linux process from a macOS host, we
need to be able to initialize a ReflectionContext without Objective-C
interoperability. This patch turns ObjCInterOp into another template trait, so
it's possible to instantiate a non-ObjC MetadataReader on a system built with
ObjC-interop (but not vice versa).

This patch changes the class hierarchy to

                          TargetMetadata<Runtime>
                                    |
                          TargetHeapMetadata<Runtime>
                                    |
                          TargetAnyClassMetadata<Runtime>
                                   /                \
                                  /               TargetAnyClassMetadataObjCInterop<Runtime>
                                 /                              \
TargetClassMetadata<Runtime, TargetAnyClassMetadata<Runtime>>    \
                                                                  \
                    TargetClassMetadata<Runtime, TargetAnyClassMetadataObjCInterop<Runtime>>

TargetAnyClassMetadataObjCInterop inherits from TargetAnyClassMetadata because
most of the implementation is the same. This choice makes TargetClassMetadata a
bit tricky. In this patch I went with templating the parent class.

rdar://87179578
2022-01-20 18:28:18 -08:00
Augusto Noronha
385d91aeb8 Modify reflection dumping to use std::ostream instead of FILE * 2021-12-07 16:05:20 -03:00
Kuba (Brecka) Mracek
223dbd445b Don't include <iostream> in MetadataSource.h because it's not actually needed (#39845) 2021-10-21 15:36:01 -07:00
Arnold Schwaighofer
a20a0cd81c Fix swift-reflection-dump.cpp for recent LLVM changes 2020-12-04 10:47:12 -08:00
Mike Ash
d3a1403ea3 [swift-reflection-dump] Fix resolve() call on branch. 2020-11-20 14:42:27 -05:00
Pavel Yaskevich
85a136aa7d Fix use of reference type as pointer in swift-reflection-dump.cpp 2020-09-15 13:44:49 -07:00
adrian-prantl
4247b6008e Merge pull request #33417 from adrian-prantl/55412920
Add a callback to swift::reflection::MemoryReader that allows LLDB to…
2020-09-08 09:56:21 -07:00
Mike Ash
fd6922f92d Add error reporting when looking up types by demangled name. 2020-08-28 14:43:51 -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
Mishal Shah
6336b14db4 [Apple Silicon] [In-process memory reader] Adjust low-bits computation for arm64 macOS 2020-07-02 18:32:23 -07:00
tbkka
efa1a01a5a Fix test crash in swift-reflection-dump rdar://60966825 (#30737)
Return a 32-bit or 64-bit ptrauth mask appropriate for the target.  This should correct a crash when a 64-bit swift-reflection-dump executable was used to inspect a 32-bit target binary.

Resolves rdar://60966825
2020-03-31 15:14:52 -07:00
Kuba Mracek
c0ab0c9c0a Fixup swift-reflection-test.c and swift-reflection-dump.cpp 2020-03-07 08:11:52 -08: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
1bd425da67 [arm64e] Test changes to support arm64e 2020-02-27 16:10:48 -08:00
Xi Ge
64c0680686 Merge remote-tracking branch 'apple/master' into master-next 2019-10-28 10:19:36 -07:00
Ivan Smetanin
dd7d173c97 [stdlib] [reflection] Excise iostream from reflection 2019-10-14 20:28:20 +03:00
Shoaib Meenai
84711260fd [swift-reflection-dump] Adjust for LLVM r372278
Use the new API for getting a Mach-O object.
2019-10-04 12:47:43 -07:00
Joe Groff
ce6de7e81d swift-reflection-dump: ELF ObjectMemoryReader support for relocations
Collect the relative and symbol relocations from ELF images in order to resolve pointer values
read from disk. This allows us to enable symbolic-referencing-all-the-things for ELF platforms.
2019-10-03 14:28:24 -07:00
Joe Groff
b93096c8c5 swift-reflection-dump: Use a 32-bit friendly multi-image VM mapping.
My previous attempt doesn't work well for 32-bit targets; 32-bit memory readers
reasonably assume that they only get 32-bit RemoteAddress values. When working
with multiple images, instead pack them all into a contiguous subset of the
address space.
2019-10-02 13:13:27 -07:00
Joe Groff
8821c6cf26 swift-reflection-dump: Create ReflectionContext with correct target pointer size
Necessary to make sure we read pointers as the right size, and use the correct object layouts
when using swift-reflection-dump for cross-platform dumps.
2019-10-02 12:41:59 -07:00
Joe Groff
bce163608f swift-reflection-dump: Mach-O binding list support
When resolving a pointer value in a Mach-O image, look through the binding list
to see if a symbol address will be added at a given location, and return
an unresolved `RemoteAbsolutePointer` with the symbol name if so.
2019-10-01 12:36:55 -07: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
0c1b5cbd9b fix bad merge 2019-09-27 15:22:00 -07:00
Joe Groff
6f8b2bf698 Merge remote-tracking branch 'origin/master' into master-rebranch 2019-09-27 14:37:21 -07:00
swift-ci
bba456904a Merge remote-tracking branch 'origin/master' into master-rebranch 2019-09-26 17:43:09 -07:00
Alexander Shaposhnikov
e2b6a3d982 Fix parsing of ELF images 2019-09-26 14:37:36 -07:00
Joe Groff
11b2c29125 swift-reflection-dump: Virtualize logical-to-physical address mapping.
Instead of copying the entire object file and moving around its sections to make an approximate
representation of its mapped state, use the memory-mapped buffer from `llvm::ObjectFile`, and
deal with non-contiguous logical mappings entirely in readBytes.
2019-09-26 14:28:43 -07:00
Xi Ge
5295ef7bde Revert "[reflection-dump] Teach reflection dump how to dump protocols from the __swift5_protos section of a .o" 2019-06-20 08:29:49 -07:00
Michael Gottesman
476a7de34b [reflection] Teach swift-reflection-dump how to dump protocols from the protocol section on Darwin.
NOTE: I used YAMLIO to simplify dumping this information which required me to
use llvm::outs() instead of std::cout like the rest of libReflection. We really
should remove std::cout from libReflection (iostream is a smell we try to avoid
generally). As a result, there is a little bit of finagling in the commit to
work around flushing issues with respect to use llvm::outs(), std::cout... but
it isn't too bad overall.
2019-06-19 20:27:34 -07:00
Michael Gottesman
b4ff6ef9b5 [reflection-dump] Extract NativeRuntime out into its own using directive.
When I add to swift-reflection-dump the ability to dump the names, I am going to
need to reuse this in the protocol dumping code.
2019-06-19 15:16:17 -07:00
Gwen Mittertreiner
67cfef2d60 SectionRef::getContents() now returns Expected
Updated uses of object::SectionRef::getContents() since it now returns
an Expected<StringRef> instead of modifying the one it's passed.

See also: git-svn-id:
https://llvm.org/svn/llvm-project/llvm/trunk@360892
91177308-0d34-0410-b5e6-96231b3b80d8
2019-05-20 16:02:41 -07:00
Alexander Shaposhnikov
33fa33ff0d [Reflection] Fix several issues for COFF and ELF
Previously when ReflectionContext was parsing the image of a binary
(for ELF or COFF) it was making some incorrect assumptions about the location
of sections in the memory of a remote process. In particular, it was using the offsets
rather than the virtual addresses and it was incorrectly calculating the references (relative pointers)
inside the metadata. In this diff we address these issues and adjust swift-reflection-dump
(used in tests) to emulate the runtime behavior more closely.
This diff has been extensively tested, the reflection tests are green on OSX and Linux,
on Windows it fixes 2 new tests:
    Reflection/typeref_decoding.swift
    stdlib/ReflectionHashing.swift
So now (with some minor fixes to the lit testsing infrastructure) the following reflection
tests pass:
    Reflection/box_descriptors.sil
    Reflection/capture_descriptors.sil
    Reflection/typeref_decoding.swift
    stdlib/ReflectionHashing.swift
2019-04-02 21:22:56 -07:00
Saleem Abdulrasool
a2ff73fd9a swift-reflection-dump: use elaborated type name
cl is unable to differentiate between the `swift::ReflectionContext` and
`swift::reflection::ReflectionContext`.  Use the elaborated typename to
uniquely identify the type.  This allows us to build
`swift-reflection-dump` with cl again.
2019-02-24 15:40:00 -08:00
Davide Italiano
862711616d [swift-reflection-dump] Garbage collect dead code.
Now that the API is fully transparent and works both with
ELF and MachO, clients don't need to worry about object format
details anymore.
2018-07-20 11:32:46 -07:00
Davide Italiano
a907398872 [RemoteMirrors] Add support for reading ELF.
<rdar://problem/41901725>
2018-07-19 15:21:42 -07:00
Davide Italiano
2f0577a967 [swift-reflection-dump] Use the new API for MachO.
<rdar://problem/42306551>
2018-07-17 15:45:32 -07:00
Arnold Schwaighofer
cc7f207cec Rename swift sections from swift4 to swift5
rdar://38465220
2018-05-23 12:18:08 -07:00
Kuba (Brecka) Mracek
84e71b8d7a Change the RemoteMirror API to have extensible data layout callback (#15291)
* Change the RemoteMirror API to have extensible data layout callback

* Use DLQ_Get prefix on DataLayoutQueryType enum values

* Simplify MemoryReaderImpl and synthesize minimalDataLayoutQueryFunction
2018-03-16 14:54:04 -07:00
Arnold Schwaighofer
0c11e1a0a3 Mark swift sections as swift4 for Swift 4.2
rdar://36363251
2018-03-14 10:27:28 -07:00