Commit Graph

138 Commits

Author SHA1 Message Date
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
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
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
Saleem Abdulrasool
f13031f61d Reflection: remove unnecessary offset for MachO
Don't bother passing the slide for the MachO image as it is always 0.
2019-04-08 18:52:48 -07:00
Saleem Abdulrasool
f2986035ad Reflection: remove unnecessary offset for ELF
Don't bother passing the slide for the ELF image as it is always 0.
2019-04-08 18:52:48 -07:00
Saleem Abdulrasool
5b2e95d01f Reflection: remove unnecessary offset for COFF
Don't bother passing the slide for the COFF image as it is always 0.
2019-04-08 18:52:48 -07:00
Saleem Abdulrasool
8bf851064a Reflection: load the builtin section on Windows
This loads the builtin section metadata on Windows which is needed for
the tests.
2019-04-03 11:18:50 -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
0cf91f343e Reflection: add PE/COFF magic checking for inspection
When we inspect a binary, verify that it is a PE/COFF binary before
trying to interpret it as a PE/COFF binary.  This prepares the code
for extraction of the file inspection and will permit cross-platform
builds to introspect foreign binaries.
2019-02-28 09:03:04 -08:00
Daniel Rodríguez Troitiño
3f76e63b6e Merge pull request #22899 from drodriguez/reflection-context-fix-2
Reflection: try holding 32/64 address offset in target machine pointers.
2019-02-27 11:34:36 -08:00
Alexander Shaposhnikov
848b8efd34 [Reflection] Add first bits to support COFF
In this diff we add first bits to make reflection work on Windows,
in particular, properly extract the metadata from the corresponding sections.
With this diff box_descriptors.sil and capture_descriptors.sil start passing on Windows
after some minor tweaks to the lit testing infrastructure.
2019-02-26 17:12:53 -08:00
Daniel Rodríguez Troitiño
8bee95d232 Reflection: try holding 32/64 address offset in type big enough for all architectures.
When compiling for a 32 bit machine, uintptr_t from ReflectionInfo will
be the integer sized to hold a 32 bit pointer, so a 64 bit pointer might
not fit.

This commit removes the solution in
0f20c486e0 and does a runtime check that
the calculated offset will fit into the target machine uintptr_t, which
might not be true for 32 bits machines trying to read 64 bits images,
which should not be that common (and those images have to have offsets
bigger than what a 32 bits number can hold).
2019-02-26 13:31:35 -08:00
Saleem Abdulrasool
0f20c486e0 Reflection: prevent address truncation on ELF64
Ensure that we use the address type as specified by ELF rather than
truncate to a 32-bit value.
2019-02-24 15:40:48 -08:00
Daniel Rodríguez Troitiño
ec86064209 [swift-reflection-dump] Support for ELF32.
Split the addImage method for ELF into two branches for ELF32 and for
ELF64 depending on the value stored in the identifier of the image.

Most of the code of the previous function moves into readELFSections,
which is build similar to the already existing readMachOSections. The
code is only modified to use the types from the template parameter.

This fixes a couple of reflection tests in Android armv7 (and I suppose
it should also fix the same problem in other 32 bits platforms which use
ELF).
2019-02-19 13:06:10 -08: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
Davide Italiano
29a5605c13 [ReflectionContext] Save buffers and set the correct addresses. 2018-12-11 09:55:20 -08:00
Davide Italiano
32fc8f14d1 [ReflectionContext] Take slide in account. 2018-12-10 18:19:45 -08:00
Davide Italiano
fcc74e82de [Reflection] Try to read only the reflection sections.
(instead of the whole object).
2018-12-10 15:41:14 -08:00
Slava Pestov
00c1279dbb Reflection: Compute if types are bitwise takable
Bitwise takability is now part of the layout of a type, because
non-bitwise takable types are never stored inline in an
existential or resilient global's buffer, even if they would
fit.

The basic rule is that weak references, unknown-refcounted
unowned references, and aggregates that contain them, are not
bitwise takable, whereas everything else is bitwise takable.

Also, since the bitwise takable for an unowned reference
depends on the reference counting style, we have to record the
superclass of a protocol, if any, to correctly determine the
reference counting style of the protocol existential.
2018-11-07 00:32:12 -05:00
Mike Ash
8624325713 [RemoteMirrors] Fix the computation of the section start address in readMachOSections.
rdar://problem/45271231
2018-10-16 17:16:34 -04:00
Davide Italiano
fdf2ba19ac [ReflectionContext] Remove dead code. NFCI.
This is untested, and unused. I originally planned to use it
in the debugger, but I changed my mind.
2018-08-06 18:45:06 -07:00
Davide Italiano
5dc41566fb [Reflection] Hoist computation of error existentials to the reader.
<rdar://problem/41546568>
2018-07-26 10:23:31 -07:00
Davide Italiano
055c789915 [Reflection] Hoist computation of metadata/value for opaque existential to the reader.
This will allow re-use of this code in RemoteAST (and, transitively,
in lldb).

<rdar://problem/41546568>
2018-07-25 16:02:46 -07:00
Davide Italiano
e9d8e0a8ba [ELF] Remove a wrong comment and clarify another one.
<rdar://problem/41901725>
2018-07-19 15:23:58 -07:00
Davide Italiano
a907398872 [RemoteMirrors] Add support for reading ELF.
<rdar://problem/41901725>
2018-07-19 15:21:42 -07:00
Davide Italiano
78f23cc10f [Reflection] Locate __DATA and push into imageRanges.
This brings the current support to parity to the old one.
Thanks to Mike Ash for noticing this.

<rdar://problem/42306551>
2018-07-19 11:30:09 -07:00
Davide Italiano
382e099e60 [Reflection] Remove now dead code. NFCI.
<rdar://problem/42306551>
2018-07-18 15:24:31 -07:00
Davide Italiano
29f45b10cd [Reflection] Fix lifetime issues and out-of-process mapping.
This comes back to the older approach of mapping all __TEXT, as
taking in account the delta between sections seems a little too
complicated for now. Hopefully we'll be back optimizing this
once we're done with debugger integration.

<rdar://problem/42306551>
2018-07-18 14:53:36 -07:00
Davide Italiano
64a1e98634 [Reflection] Be less aggressive with unknown objects.
Instead of asserting, just return false.
2018-07-17 16:20:56 -07:00
Davide Italiano
da448fefc5 [ReflectionContext] Rewrite the Mach reader path.
There are several problems fixed by this commit.
-> It removes the dependency on a system header.
-> It works when the pointer size for host and target don't match
(i.e. 32-bits/64-bits and viceversa)
-> It reads the file lazily, mapping only the sections that are
needed. The old approach mapped the whole __TEXT segment, causing
significant slowdowns when trying to integrate this API in the
debugger. Now it's almost instant (10 seconds vs milliseconds).

<rdar://problem/42306551>
2018-07-17 15:35:40 -07:00
Davide Italiano
52531eef00 [Reflection] Fix a member function so that it actually compiles.
Nobody's using it, so nobody noticed. I plan to use this in the
debugger.
2018-05-31 17:02:32 -07:00
Arnold Schwaighofer
cc7f207cec Rename swift sections from swift4 to swift5
rdar://38465220
2018-05-23 12:18:08 -07:00
Sho Ikeda
e71a065e97 [gardening][Reflection][Remote] Replace typedef with using 2018-04-02 13:43:39 +09:00
Mike Ash
979b75699a Merge branch 'master' into remotemirror-hide-reflection-sections 2018-03-16 16:24:57 -04:00
Arnold Schwaighofer
0c11e1a0a3 Mark swift sections as swift4 for Swift 4.2
rdar://36363251
2018-03-14 10:27:28 -07:00
Mike Ash
ac026128d1 [RemoteMirrors] Provide interop function for converting a raw metadata pointer into a swift_metadata_interop_t.
rdar://problem/37538580
2018-03-08 13:25:09 -05:00
Mike Ash
9a27add5f8 [RemoteMirrors] Fix ReflectionContext.h cast on 32-bit. 2018-03-02 13:53:44 -05:00
Mike Ash
553ef86a4b [RemoteMirrors] Change MemoryReader::readBytes to return a unique_ptr instead of a tuple so that the destructor handles freeing the memory. 2018-03-02 12:05:36 -05:00
Mike Ash
1434fce417 [RemoteMirrors] Switch #ifdef __APPLE__ to #if defined(__APPLE__) && defined(__MACH__) to be more specific about the fact that this code is for MachO. 2018-03-02 11:03:45 -05:00
Mike Ash
7a4ca828d8 [RemoteMirrors] Wrap MachO code in #ifdef __APPLE__. 2018-02-27 15:54:48 -05:00
Mike Ash
e4564087af [RemoteMirrors] Move ownsObject into ReflectionContext. 2018-02-27 10:32:32 -05:00