Skip to content

Commit 5cc4ce1

Browse files
committed
Reflection: Looking up CaptureDescriptors by remote address
Remote metadata for closure contexts points to a capture descriptor. We have a local copy of all capture descriptors. Translate the address by recording the local and remote start address of reflection metadata.
1 parent 2abcd97 commit 5cc4ce1

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,17 @@ struct ReflectionInfo {
7979
CaptureSection capture;
8080
GenericSection typeref;
8181
GenericSection reflstr;
82+
uintptr_t LocalStartAddress;
83+
uintptr_t RemoteStartAddress;
8284
};
8385

8486
struct ClosureContextInfo {
8587
std::vector<const TypeRef *> CaptureTypes;
8688
std::vector<std::pair<const TypeRef *, const MetadataSource *>> MetadataSources;
8789
unsigned NumBindings = 0;
8890

89-
void dump(std::ostream &OS);
91+
void dump() const;
92+
void dump(std::ostream &OS) const;
9093
};
9194

9295
/// An implementation of MetadataReader's BuilderType concept for
@@ -289,6 +292,10 @@ class TypeRefBuilder {
289292
/// Get the primitive type lowering for a builtin type.
290293
const BuiltinTypeDescriptor *getBuiltinTypeInfo(const TypeRef *TR);
291294

295+
/// Get the raw capture descriptor for a remote capture descriptor
296+
/// address.
297+
const CaptureDescriptor *getCaptureDescriptor(uintptr_t RemoteAddress);
298+
292299
/// Get the unsubstituted capture types for a closure context.
293300
ClosureContextInfo getClosureContextInfo(const CaptureDescriptor &CD);
294301

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorTypes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ typedef struct swift_reflection_info {
4141
swift_reflection_section_t capture;
4242
swift_reflection_section_t typeref;
4343
swift_reflection_section_t reflstr;
44+
45+
// Start address in local and remote address spaces.
46+
uintptr_t LocalStartAddress;
47+
uintptr_t RemoteStartAddress;
4448
} swift_reflection_info_t;
4549

4650
/// The layout kind of a Swift type.

stdlib/public/Reflection/TypeRefBuilder.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
152152
return nullptr;
153153
}
154154

155+
const CaptureDescriptor *
156+
TypeRefBuilder::getCaptureDescriptor(uintptr_t RemoteAddress) {
157+
for (auto Info : ReflectionInfos) {
158+
for (auto &CD : Info.capture) {
159+
auto OtherAddr = ((uintptr_t) &CD -
160+
Info.LocalStartAddress +
161+
Info.RemoteStartAddress);
162+
if (OtherAddr == RemoteAddress)
163+
return &CD;
164+
}
165+
}
166+
167+
return nullptr;
168+
}
169+
155170
/// Get the unsubstituted capture types for a closure context.
156171
ClosureContextInfo
157172
TypeRefBuilder::getClosureContextInfo(const CaptureDescriptor &CD) {
@@ -265,7 +280,11 @@ void TypeRefBuilder::dumpBuiltinTypeSection(std::ostream &OS) {
265280
}
266281
}
267282

268-
void ClosureContextInfo::dump(std::ostream &OS) {
283+
void ClosureContextInfo::dump() const {
284+
dump(std::cerr);
285+
}
286+
287+
void ClosureContextInfo::dump(std::ostream &OS) const {
269288
OS << "- Capture types:\n";
270289
for (auto *TR : CaptureTypes) {
271290
if (TR == nullptr)

tools/swift-reflection-dump/swift-reflection-dump.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ static ReflectionInfo findReflectionInfo(const ObjectFile *objectFile) {
127127
captureSection,
128128
typeRefSection,
129129
reflectionStringsSection,
130+
/*LocalStartAddress*/ 0,
131+
/*RemoteStartAddress*/ 0,
130132
};
131133
}
132134

tools/swift-reflection-test/swift-reflection-test.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ PipeMemoryReader_receiveReflectionInfo(SwiftReflectionContextRef RC,
293293
makeLocalSection(Buffer, RemoteInfo.builtin, RemoteInfo),
294294
makeLocalSection(Buffer, RemoteInfo.capture, RemoteInfo),
295295
makeLocalSection(Buffer, RemoteInfo.typeref, RemoteInfo),
296-
makeLocalSection(Buffer, RemoteInfo.reflstr, RemoteInfo)
296+
makeLocalSection(Buffer, RemoteInfo.reflstr, RemoteInfo),
297+
/*LocalStartAddress*/ (uintptr_t) Buffer,
298+
/*RemoteStartAddress*/ RemoteInfo.StartAddress,
297299
};
298300
swift_reflection_addReflectionInfo(RC, Info);
299301
}

0 commit comments

Comments
 (0)