Skip to content

Commit 8bee95d

Browse files
committed
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 0f20c48 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).
1 parent fb4038c commit 8bee95d

File tree

4 files changed

+53
-55
lines changed

4 files changed

+53
-55
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ template <> struct ELFTraits<llvm::ELF::ELFCLASS32> {
6060
using Section = const struct llvm::ELF::Elf32_Shdr;
6161
using Offset = llvm::ELF::Elf32_Off;
6262
using Size = llvm::ELF::Elf32_Word;
63-
using Address = llvm::ELF::Elf32_Addr;
6463
static constexpr unsigned char ELFClass = llvm::ELF::ELFCLASS32;
6564
};
6665

@@ -69,7 +68,6 @@ template <> struct ELFTraits<llvm::ELF::ELFCLASS64> {
6968
using Section = const struct llvm::ELF::Elf64_Shdr;
7069
using Offset = llvm::ELF::Elf64_Off;
7170
using Size = llvm::ELF::Elf64_Xword;
72-
using Address = llvm::ELF::Elf64_Addr;
7371
static constexpr unsigned char ELFClass = llvm::ELF::ELFCLASS64;
7472
};
7573

@@ -202,7 +200,7 @@ class ReflectionContext
202200
RangeEnd - RangeStart);
203201

204202
auto findMachOSectionByName = [&](std::string Name)
205-
-> std::pair<std::pair<const char *, const char *>, uint32_t> {
203+
-> std::pair<std::pair<const char *, const char *>, uint64_t> {
206204
for (unsigned I = 0; I < NumSect; ++I) {
207205
auto S = reinterpret_cast<typename T::Section *>(
208206
SectionsBuf + (I * sizeof(typename T::Section)));
@@ -233,8 +231,8 @@ class ReflectionContext
233231
ReflStrMdSec.first.first == nullptr)
234232
return false;
235233

236-
auto LocalStartAddress = reinterpret_cast<uintptr_t>(SectBuf.get());
237-
auto RemoteStartAddress = static_cast<uintptr_t>(RangeStart);
234+
auto LocalStartAddress = reinterpret_cast<uint64_t>(SectBuf.get());
235+
auto RemoteStartAddress = static_cast<uint64_t>(RangeStart);
238236

239237
ReflectionInfo info = {
240238
{{FieldMdSec.first.first, FieldMdSec.first.second}, 0},
@@ -332,7 +330,7 @@ class ReflectionContext
332330
auto StrTab = reinterpret_cast<const char *>(StrTabBuf.get());
333331

334332
auto findELFSectionByName = [&](std::string Name)
335-
-> std::pair<std::pair<const char *, const char *>, typename T::Address> {
333+
-> std::pair<std::pair<const char *, const char *>, uint64_t> {
336334
// Now for all the sections, find their name.
337335
for (const typename T::Section *Hdr : SecHdrVec) {
338336
uint32_t Offset = Hdr->sh_name;
@@ -367,9 +365,9 @@ class ReflectionContext
367365
ReflStrMdSec.first.first == nullptr)
368366
return false;
369367

370-
auto LocalStartAddress = reinterpret_cast<uintptr_t>(Buf.get());
368+
auto LocalStartAddress = reinterpret_cast<uint64_t>(Buf.get());
371369
auto RemoteStartAddress =
372-
static_cast<uintptr_t>(ImageStart.getAddressData());
370+
static_cast<uint64_t>(ImageStart.getAddressData());
373371

374372
ReflectionInfo info = {
375373
{{FieldMdSec.first.first, FieldMdSec.first.second}, FieldMdSec.second},

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,36 +82,36 @@ using GenericSection = ReflectionSection<const void *>;
8282
struct ReflectionInfo {
8383
struct {
8484
FieldSection Metadata;
85-
uintptr_t SectionOffset;
85+
uint64_t SectionOffset;
8686
} Field;
8787

8888
struct {
8989
AssociatedTypeSection Metadata;
90-
uintptr_t SectionOffset;
90+
uint64_t SectionOffset;
9191
} AssociatedType;
9292

9393
struct {
9494
BuiltinTypeSection Metadata;
95-
uintptr_t SectionOffset;
95+
uint64_t SectionOffset;
9696
} Builtin;
9797

9898
struct {
9999
CaptureSection Metadata;
100-
uintptr_t SectionOffset;
100+
uint64_t SectionOffset;
101101
} Capture;
102102

103103
struct {
104104
GenericSection Metadata;
105-
uintptr_t SectionOffset;
105+
uint64_t SectionOffset;
106106
} TypeReference;
107107

108108
struct {
109109
GenericSection Metadata;
110-
uintptr_t SectionOffset;
110+
uint64_t SectionOffset;
111111
} ReflectionString;
112112

113-
uintptr_t LocalStartAddress;
114-
uintptr_t RemoteStartAddress;
113+
uint64_t LocalStartAddress;
114+
uint64_t RemoteStartAddress;
115115
};
116116

117117
struct ClosureContextInfo {
@@ -506,11 +506,11 @@ class TypeRefBuilder {
506506

507507
/// Get the raw capture descriptor for a remote capture descriptor
508508
/// address.
509-
const CaptureDescriptor *getCaptureDescriptor(uintptr_t RemoteAddress);
509+
const CaptureDescriptor *getCaptureDescriptor(uint64_t RemoteAddress);
510510

511511
/// Get the unsubstituted capture types for a closure context.
512512
ClosureContextInfo getClosureContextInfo(const CaptureDescriptor &CD,
513-
uintptr_t Offset);
513+
uint64_t Offset);
514514

515515
///
516516
/// Dumping typerefs, field declarations, associated types

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorTypes.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
extern "C" {
2525
#endif
2626

27-
typedef uintptr_t swift_typeref_t;
27+
typedef uint64_t swift_typeref_t;
2828

2929
/// Represents one of the Swift reflection sections of an image.
3030
typedef struct swift_reflection_section {
@@ -37,37 +37,37 @@ typedef struct swift_reflection_section {
3737
typedef struct swift_reflection_info {
3838
struct {
3939
swift_reflection_section_t section;
40-
uintptr_t offset;
40+
uint64_t offset;
4141
} field;
4242

4343
struct {
4444
swift_reflection_section_t section;
45-
uintptr_t offset;
45+
uint64_t offset;
4646
} associated_types;
4747

4848
struct {
4949
swift_reflection_section_t section;
50-
uintptr_t offset;
50+
uint64_t offset;
5151
} builtin_types;
5252

5353
struct {
5454
swift_reflection_section_t section;
55-
uintptr_t offset;
55+
uint64_t offset;
5656
} capture;
5757

5858
struct {
5959
swift_reflection_section_t section;
60-
uintptr_t offset;
60+
uint64_t offset;
6161
} type_references;
6262

6363
struct {
6464
swift_reflection_section_t section;
65-
uintptr_t offset;
65+
uint64_t offset;
6666
} reflection_strings;
6767

6868
// Start address in local and remote address spaces.
69-
uintptr_t LocalStartAddress;
70-
uintptr_t RemoteStartAddress;
69+
uint64_t LocalStartAddress;
70+
uint64_t RemoteStartAddress;
7171
} swift_reflection_info_t;
7272

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

stdlib/public/Reflection/TypeRefBuilder.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ TypeRefBuilder::getRemoteAddrOfTypeRefPointer(const void *pointer) {
3131
// Find what type ref section the pointer resides in, if any.
3232
const ReflectionInfo *containingInfo = nullptr;
3333
for (auto &info : ReflectionInfos) {
34-
auto start = (uintptr_t)info.TypeReference.Metadata.startAddress();
35-
auto size = (uintptr_t)info.TypeReference.Metadata.size();
36-
if (start <= (uintptr_t)pointer && (uintptr_t)pointer < start + size) {
34+
auto start = (uint64_t)info.TypeReference.Metadata.startAddress();
35+
auto size = (uint64_t)info.TypeReference.Metadata.size();
36+
if (start <= (uint64_t)pointer && (uint64_t)pointer < start + size) {
3737
containingInfo = &info;
3838
break;
3939
}
4040
}
4141

4242
if (!containingInfo)
4343
return 0;
44-
45-
return (uintptr_t)pointer
44+
45+
return (uint64_t)pointer
4646
+ containingInfo->RemoteStartAddress
4747
- containingInfo->LocalStartAddress
4848
+ containingInfo->TypeReference.SectionOffset;
@@ -86,10 +86,10 @@ lookupTypeWitness(const std::string &MangledTypeName,
8686
// Cache missed - we need to look through all of the assocty sections
8787
// for all images that we've been notified about.
8888
for (auto &Info : ReflectionInfos) {
89-
uintptr_t TypeRefOffset = Info.AssociatedType.SectionOffset
90-
- Info.TypeReference.SectionOffset;
91-
uintptr_t NameOffset = Info.AssociatedType.SectionOffset
92-
- Info.ReflectionString.SectionOffset;
89+
uint64_t TypeRefOffset = Info.AssociatedType.SectionOffset
90+
- Info.TypeReference.SectionOffset;
91+
uint64_t NameOffset = Info.AssociatedType.SectionOffset
92+
- Info.ReflectionString.SectionOffset;
9393
for (const auto &AssocTyDescriptor : Info.AssociatedType.Metadata) {
9494
if (!reflectionNameMatches(Dem,
9595
AssocTyDescriptor.getMangledConformingTypeName(TypeRefOffset),
@@ -158,8 +158,8 @@ TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
158158
// On failure, fill out the cache with everything we know about.
159159
std::vector<std::pair<std::string, const TypeRef *>> Fields;
160160
for (auto &Info : ReflectionInfos) {
161-
uintptr_t TypeRefOffset = Info.Field.SectionOffset
162-
- Info.TypeReference.SectionOffset;
161+
uint64_t TypeRefOffset = Info.Field.SectionOffset
162+
- Info.TypeReference.SectionOffset;
163163
for (auto &FD : Info.Field.Metadata) {
164164
if (!FD.hasMangledTypeName())
165165
continue;
@@ -232,8 +232,8 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
232232
return nullptr;
233233

234234
for (auto Info : ReflectionInfos) {
235-
uintptr_t TypeRefOffset = Info.Builtin.SectionOffset
236-
- Info.TypeReference.SectionOffset;
235+
uint64_t TypeRefOffset = Info.Builtin.SectionOffset
236+
- Info.TypeReference.SectionOffset;
237237
for (auto &BuiltinTypeDescriptor : Info.Builtin.Metadata) {
238238
assert(BuiltinTypeDescriptor.Size > 0);
239239
assert(BuiltinTypeDescriptor.getAlignment() > 0);
@@ -252,10 +252,10 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
252252
}
253253

254254
const CaptureDescriptor *
255-
TypeRefBuilder::getCaptureDescriptor(uintptr_t RemoteAddress) {
255+
TypeRefBuilder::getCaptureDescriptor(uint64_t RemoteAddress) {
256256
for (auto Info : ReflectionInfos) {
257257
for (auto &CD : Info.Capture.Metadata) {
258-
auto OtherAddr = (reinterpret_cast<uintptr_t>(&CD) -
258+
auto OtherAddr = (reinterpret_cast<uint64_t>(&CD) -
259259
Info.LocalStartAddress + Info.RemoteStartAddress);
260260
if (OtherAddr == RemoteAddress)
261261
return &CD;
@@ -268,7 +268,7 @@ TypeRefBuilder::getCaptureDescriptor(uintptr_t RemoteAddress) {
268268
/// Get the unsubstituted capture types for a closure context.
269269
ClosureContextInfo
270270
TypeRefBuilder::getClosureContextInfo(const CaptureDescriptor &CD,
271-
uintptr_t TypeRefOffset) {
271+
uint64_t TypeRefOffset) {
272272
ClosureContextInfo Info;
273273

274274
for (auto i = CD.capture_begin(), e = CD.capture_end(); i != e; ++i) {
@@ -326,10 +326,10 @@ TypeRefBuilder::dumpTypeRef(StringRef MangledName,
326326

327327
void TypeRefBuilder::dumpFieldSection(std::ostream &OS) {
328328
for (const auto &sections : ReflectionInfos) {
329-
uintptr_t TypeRefOffset = sections.Field.SectionOffset
330-
- sections.TypeReference.SectionOffset;
331-
uintptr_t NameOffset = sections.Field.SectionOffset
332-
- sections.ReflectionString.SectionOffset;
329+
uint64_t TypeRefOffset = sections.Field.SectionOffset
330+
- sections.TypeReference.SectionOffset;
331+
uint64_t NameOffset = sections.Field.SectionOffset
332+
- sections.ReflectionString.SectionOffset;
333333
for (const auto &descriptor : sections.Field.Metadata) {
334334
auto TypeDemangling = Dem.demangleType(
335335
dropSwiftManglingPrefix(descriptor.getMangledTypeName(TypeRefOffset)));
@@ -354,10 +354,10 @@ void TypeRefBuilder::dumpFieldSection(std::ostream &OS) {
354354

355355
void TypeRefBuilder::dumpAssociatedTypeSection(std::ostream &OS) {
356356
for (const auto &sections : ReflectionInfos) {
357-
uintptr_t TypeRefOffset = sections.AssociatedType.SectionOffset
358-
- sections.TypeReference.SectionOffset;
359-
uintptr_t NameOffset = sections.AssociatedType.SectionOffset
360-
- sections.ReflectionString.SectionOffset;
357+
uint64_t TypeRefOffset = sections.AssociatedType.SectionOffset
358+
- sections.TypeReference.SectionOffset;
359+
uint64_t NameOffset = sections.AssociatedType.SectionOffset
360+
- sections.ReflectionString.SectionOffset;
361361
for (const auto &descriptor : sections.AssociatedType.Metadata) {
362362
auto conformingTypeNode = Dem.demangleType(
363363
descriptor.getMangledConformingTypeName(TypeRefOffset));
@@ -381,8 +381,8 @@ void TypeRefBuilder::dumpAssociatedTypeSection(std::ostream &OS) {
381381

382382
void TypeRefBuilder::dumpBuiltinTypeSection(std::ostream &OS) {
383383
for (const auto &sections : ReflectionInfos) {
384-
uintptr_t TypeRefOffset = sections.Builtin.SectionOffset
385-
- sections.TypeReference.SectionOffset;
384+
uint64_t TypeRefOffset = sections.Builtin.SectionOffset
385+
- sections.TypeReference.SectionOffset;
386386
for (const auto &descriptor : sections.Builtin.Metadata) {
387387
auto typeName =
388388
Demangle::demangleTypeAsString(
@@ -426,8 +426,8 @@ void ClosureContextInfo::dump(std::ostream &OS) const {
426426

427427
void TypeRefBuilder::dumpCaptureSection(std::ostream &OS) {
428428
for (const auto &sections : ReflectionInfos) {
429-
uintptr_t TypeRefOffset = sections.Capture.SectionOffset
430-
- sections.TypeReference.SectionOffset;
429+
uint64_t TypeRefOffset = sections.Capture.SectionOffset
430+
- sections.TypeReference.SectionOffset;
431431
for (const auto &descriptor : sections.Capture.Metadata) {
432432
auto info = getClosureContextInfo(descriptor, TypeRefOffset);
433433
info.dump(OS);

0 commit comments

Comments
 (0)