Skip to content

Commit 12bf25e

Browse files
authored
Merge pull request #27388 from jckarter/reflection-remove-start-addresses
swift-reflection-dump: Virtualize logical-to-physical address mapping.
2 parents 9182051 + 2fe6892 commit 12bf25e

File tree

6 files changed

+56
-54
lines changed

6 files changed

+56
-54
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,13 @@ class ReflectionContext
237237
ReflStrMdSec.first == nullptr)
238238
return false;
239239

240-
auto LocalStartAddress = reinterpret_cast<uint64_t>(SectBuf.get());
241-
auto RemoteStartAddress = static_cast<uint64_t>(RangeStart);
242-
243240
ReflectionInfo info = {
244241
{FieldMdSec.first, FieldMdSec.second},
245242
{AssocTySec.first, AssocTySec.second},
246243
{BuiltinTySec.first, BuiltinTySec.second},
247244
{CaptureSec.first, CaptureSec.second},
248245
{TypeRefMdSec.first, TypeRefMdSec.second},
249-
{ReflStrMdSec.first, ReflStrMdSec.second},
250-
LocalStartAddress,
251-
RemoteStartAddress};
246+
{ReflStrMdSec.first, ReflStrMdSec.second}};
252247

253248
this->addReflectionInfo(info);
254249

@@ -346,19 +341,13 @@ class ReflectionContext
346341
ReflStrMdSec.first == nullptr)
347342
return false;
348343

349-
auto LocalStartAddress = reinterpret_cast<uintptr_t>(DOSHdrBuf.get());
350-
auto RemoteStartAddress =
351-
static_cast<uintptr_t>(ImageStart.getAddressData());
352-
353344
ReflectionInfo Info = {
354345
{FieldMdSec.first, FieldMdSec.second},
355346
{AssocTySec.first, AssocTySec.second},
356347
{BuiltinTySec.first, BuiltinTySec.second},
357348
{CaptureSec.first, CaptureSec.second},
358349
{TypeRefMdSec.first, TypeRefMdSec.second},
359-
{ReflStrMdSec.first, ReflStrMdSec.second},
360-
LocalStartAddress,
361-
RemoteStartAddress};
350+
{ReflStrMdSec.first, ReflStrMdSec.second}};
362351
this->addReflectionInfo(Info);
363352
return true;
364353
}
@@ -466,19 +455,13 @@ class ReflectionContext
466455
ReflStrMdSec.first == nullptr)
467456
return false;
468457

469-
auto LocalStartAddress = reinterpret_cast<uint64_t>(Buf.get());
470-
auto RemoteStartAddress =
471-
static_cast<uint64_t>(ImageStart.getAddressData());
472-
473458
ReflectionInfo info = {
474459
{FieldMdSec.first, FieldMdSec.second},
475460
{AssocTySec.first, AssocTySec.second},
476461
{BuiltinTySec.first, BuiltinTySec.second},
477462
{CaptureSec.first, CaptureSec.second},
478463
{TypeRefMdSec.first, TypeRefMdSec.second},
479-
{ReflStrMdSec.first, ReflStrMdSec.second},
480-
LocalStartAddress,
481-
RemoteStartAddress};
464+
{ReflStrMdSec.first, ReflStrMdSec.second}};
482465

483466
this->addReflectionInfo(info);
484467

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,6 @@ struct ReflectionInfo {
203203
CaptureSection Capture;
204204
GenericSection TypeReference;
205205
GenericSection ReflectionString;
206-
207-
uint64_t LocalStartAddress;
208-
uint64_t RemoteStartAddress;
209206
};
210207

211208
struct ClosureContextInfo {

lib/Demangling/NodePrinter.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,18 @@ static DemanglerPrinter &operator<<(DemanglerPrinter &printer,
7979
case '\n': printer << "\\n"; break;
8080
case '\r': printer << "\\r"; break;
8181
case '"': printer << "\\\""; break;
82-
case '\'': printer << '\''; break; // no need to escape these
8382
case '\0': printer << "\\0"; break;
8483
default:
85-
auto c = static_cast<char>(C);
86-
// Other ASCII control characters should get escaped.
87-
if (c < 0x20 || c == 0x7F) {
84+
auto c = static_cast<unsigned char>(C);
85+
// Other control or high-bit characters should get escaped.
86+
if (c < 0x20 || c >= 0x7F) {
8887
static const char Hexdigit[] = {
8988
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
9089
'A', 'B', 'C', 'D', 'E', 'F'
9190
};
9291
printer << "\\x" << Hexdigit[c >> 4] << Hexdigit[c & 0xF];
9392
} else {
94-
printer << c;
93+
printer << (char)c;
9594
}
9695
break;
9796
}

stdlib/public/Reflection/TypeRefBuilder.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,13 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
268268
return nullptr;
269269
}
270270

271-
// TODO: This can probably be eliminated.
272271
RemoteRef<CaptureDescriptor>
273272
TypeRefBuilder::getCaptureDescriptor(uint64_t RemoteAddress) {
274273
for (auto Info : ReflectionInfos) {
275274
for (auto CD : Info.Capture) {
276-
auto OtherAddr = (reinterpret_cast<uint64_t>(CD.getLocalBuffer()) -
277-
Info.LocalStartAddress + Info.RemoteStartAddress);
278-
if (OtherAddr == RemoteAddress)
275+
if (RemoteAddress == CD.getAddressData()) {
279276
return CD;
277+
}
280278
}
281279
}
282280

stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,7 @@ swift_reflection_addReflectionInfo(SwiftReflectionContextRef ContextRef,
149149
sectionFromInfo<BuiltinTypeDescriptorIterator>(Info, Info.builtin_types),
150150
sectionFromInfo<CaptureDescriptorIterator>(Info, Info.capture),
151151
sectionFromInfo<const void *>(Info, Info.type_references),
152-
sectionFromInfo<const void *>(Info, Info.reflection_strings),
153-
Info.LocalStartAddress,
154-
Info.RemoteStartAddress
155-
};
152+
sectionFromInfo<const void *>(Info, Info.reflection_strings)};
156153

157154
Context->addReflectionInfo(ContextInfo);
158155
}

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

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,44 +122,72 @@ static bool needToRelocate(SectionRef S) {
122122

123123

124124
class Image {
125-
std::vector<char> Memory;
125+
const ObjectFile *O;
126+
uint64_t VASize;
126127

127-
public:
128-
explicit Image(const ObjectFile *O) {
129-
uint64_t VASize = O->getData().size();
130-
for (SectionRef S : O->sections()) {
131-
if (auto SectionAddr = getSectionAddress(S))
132-
VASize = std::max(VASize, SectionAddr + S.getSize());
133-
}
134-
Memory.resize(VASize);
135-
std::memcpy(&Memory[0], O->getData().data(), O->getData().size());
128+
struct RelocatedRegion {
129+
uint64_t Start, Size;
130+
const char *Base;
131+
};
136132

133+
std::vector<RelocatedRegion> RelocatedRegions;
134+
135+
public:
136+
explicit Image(const ObjectFile *O) : O(O), VASize(O->getData().size()) {
137137
for (SectionRef S : O->sections()) {
138138
if (!needToRelocate(S))
139139
continue;
140140
StringRef Content;
141+
auto SectionAddr = getSectionAddress(S);
142+
if (SectionAddr)
143+
VASize = std::max(VASize, SectionAddr + S.getSize());
144+
141145
if (auto EC = S.getContents(Content))
142146
reportError(EC);
143-
std::memcpy(&Memory[getSectionAddress(S)], Content.data(),
144-
Content.size());
147+
148+
auto PhysOffset = (uintptr_t)Content.data() - (uintptr_t)O->getData().data();
149+
150+
if (PhysOffset == SectionAddr) {
151+
continue;
152+
}
153+
154+
RelocatedRegions.push_back(RelocatedRegion{
155+
SectionAddr,
156+
Content.size(),
157+
Content.data()});
145158
}
146159
}
147160

148161
RemoteAddress getStartAddress() const {
149-
return RemoteAddress((uintptr_t)Memory.data());
162+
return RemoteAddress((uintptr_t)O->getData().data());
150163
}
151164

152165
bool isAddressValid(RemoteAddress Addr, uint64_t Size) const {
153-
return (uintptr_t)Memory.data() <= Addr.getAddressData() &&
154-
Addr.getAddressData() + Size <=
155-
(uintptr_t)Memory.data() + Memory.size();
166+
auto start = getStartAddress().getAddressData();
167+
return start <= Addr.getAddressData()
168+
&& Addr.getAddressData() + Size <= start + VASize;
156169
}
157170

158171
ReadBytesResult readBytes(RemoteAddress Addr, uint64_t Size) {
159172
if (!isAddressValid(Addr, Size))
160173
return ReadBytesResult(nullptr, [](const void *) {});
161-
return ReadBytesResult((const void *)(Addr.getAddressData()),
162-
[](const void *) {});
174+
175+
auto addrValue = Addr.getAddressData();
176+
auto base = O->getData().data();
177+
auto offset = addrValue - (uint64_t)base;
178+
for (auto &region : RelocatedRegions) {
179+
if (region.Start <= offset && offset < region.Start + region.Size) {
180+
// Read shouldn't need to straddle section boundaries.
181+
if (offset + Size > region.Start + region.Size)
182+
return ReadBytesResult(nullptr, [](const void *) {});
183+
184+
offset -= region.Start;
185+
base = region.Base;
186+
break;
187+
}
188+
}
189+
190+
return ReadBytesResult(base + offset, [](const void *) {});
163191
}
164192
};
165193

0 commit comments

Comments
 (0)