Skip to content

Commit 4012a20

Browse files
committed
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.
1 parent 11b2c29 commit 4012a20

File tree

6 files changed

+305
-95
lines changed

6 files changed

+305
-95
lines changed

include/swift/Basic/ExternalUnion.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,27 +433,27 @@ struct MembersHelper<> {
433433

434434
LLVM_ATTRIBUTE_ALWAYS_INLINE
435435
static void copyConstruct(void *self, int index, const void *other) {
436-
llvm_unreachable("bad index");
436+
assert(false && "bad index");
437437
}
438438

439439
LLVM_ATTRIBUTE_ALWAYS_INLINE
440440
static void moveConstruct(void *self, int index, void *other) {
441-
llvm_unreachable("bad index");
441+
assert(false && "bad index");
442442
}
443443

444444
LLVM_ATTRIBUTE_ALWAYS_INLINE
445445
static void copyAssignSame(int index, void *self, const void *other) {
446-
llvm_unreachable("bad index");
446+
assert(false && "bad index");
447447
}
448448

449449
LLVM_ATTRIBUTE_ALWAYS_INLINE
450450
static void moveAssignSame(int index, void *self, void *other) {
451-
llvm_unreachable("bad index");
451+
assert(false && "bad index");
452452
}
453453

454454
LLVM_ATTRIBUTE_ALWAYS_INLINE
455455
static void destruct(int index, void *self) {
456-
llvm_unreachable("bad index");
456+
assert(false && "bad index");
457457
}
458458
};
459459

include/swift/Reflection/ReflectionContext.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,14 +608,17 @@ class ReflectionContext
608608
auto CDAddr = this->readCaptureDescriptorFromMetadata(*MetadataAddress);
609609
if (!CDAddr)
610610
return nullptr;
611+
if (!CDAddr->isResolved())
612+
return nullptr;
611613

612614
// FIXME: Non-generic SIL boxes also use the HeapLocalVariable metadata
613615
// kind, but with a null capture descriptor right now (see
614616
// FixedBoxTypeInfoBase::allocate).
615617
//
616618
// Non-generic SIL boxes share metadata among types with compatible
617619
// layout, but we need some way to get an outgoing pointer map for them.
618-
auto CD = getBuilder().getCaptureDescriptor(*CDAddr);
620+
auto CD = getBuilder().getCaptureDescriptor(
621+
CDAddr->getResolvedAddress().getAddressData());
619622
if (CD == nullptr)
620623
return nullptr;
621624

include/swift/Remote/MemoryReader.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class MemoryReader {
6969
/// NOTE: subclasses MUST override at least one of the readBytes functions. The default
7070
/// implementation calls through to the other one.
7171
virtual ReadBytesResult
72-
readBytes(RemoteAddress address, uint64_t size) {
72+
readBytes(RemoteAddress address, uint64_t size) {
7373
auto *Buf = malloc(size);
7474
ReadBytesResult Result(Buf, [](const void *ptr) {
7575
free(const_cast<void *>(ptr));
@@ -96,6 +96,34 @@ class MemoryReader {
9696
memcpy(dest, Ptr.get(), size);
9797
return true;
9898
}
99+
100+
/// Attempts to resolve a pointer value read from the given remote address.
101+
virtual RemoteAbsolutePointer resolvePointer(RemoteAddress address,
102+
uint64_t readValue) {
103+
// Default implementation returns the read value as is.
104+
return RemoteAbsolutePointer("", readValue);
105+
}
106+
107+
/// Attempt to read and resolve a pointer value at the given remote address.
108+
llvm::Optional<RemoteAbsolutePointer> readPointer(RemoteAddress address,
109+
unsigned pointerSize) {
110+
auto result = readBytes(address, pointerSize);
111+
if (!result)
112+
return llvm::None;
113+
114+
uint64_t pointerData;
115+
if (pointerSize == 4) {
116+
uint32_t theData;
117+
memcpy(&theData, result.get(), 4);
118+
pointerData = theData;
119+
} else if (pointerSize == 8) {
120+
memcpy(&pointerData, result.get(), 8);
121+
} else {
122+
return llvm::None;
123+
}
124+
125+
return resolvePointer(address, pointerData);
126+
}
99127

100128
virtual ~MemoryReader() = default;
101129
};

0 commit comments

Comments
 (0)