@@ -169,6 +169,8 @@ class ReflectionContext
169
169
auto CmdBuf = this ->getReader ().readBytes (
170
170
RemoteAddress (CmdStartAddress.getAddressData () + Offset),
171
171
SegmentCmdHdrSize);
172
+ if (!CmdBuf)
173
+ return false ;
172
174
auto CmdHdr = reinterpret_cast <typename T::SegmentCmd *>(CmdBuf.get ());
173
175
if (strncmp (CmdHdr->segname , " __TEXT" , sizeof (CmdHdr->segname )) == 0 ) {
174
176
Command = CmdHdr;
@@ -189,6 +191,8 @@ class ReflectionContext
189
191
auto LoadCmdAddress = reinterpret_cast <const char *>(loadCmdOffset);
190
192
auto LoadCmdBuf = this ->getReader ().readBytes (
191
193
RemoteAddress (LoadCmdAddress), sizeof (typename T::SegmentCmd));
194
+ if (!LoadCmdBuf)
195
+ return false ;
192
196
auto LoadCmd = reinterpret_cast <typename T::SegmentCmd *>(LoadCmdBuf.get ());
193
197
194
198
// The sections start immediately after the load command.
@@ -197,6 +201,8 @@ class ReflectionContext
197
201
sizeof (typename T::SegmentCmd);
198
202
auto Sections = this ->getReader ().readBytes (
199
203
RemoteAddress (SectAddress), NumSect * sizeof (typename T::Section));
204
+ if (!Sections)
205
+ return false ;
200
206
201
207
auto Slide = ImageStart.getAddressData () - Command->vmaddr ;
202
208
std::string Prefix = " __swift5" ;
@@ -227,6 +233,8 @@ class ReflectionContext
227
233
228
234
auto SectBuf = this ->getReader ().readBytes (RemoteAddress (RangeStart),
229
235
RangeEnd - RangeStart);
236
+ if (!SectBuf)
237
+ return false ;
230
238
231
239
auto findMachOSectionByName = [&](llvm::StringRef Name)
232
240
-> std::pair<RemoteRef<void >, uint64_t > {
@@ -283,6 +291,8 @@ class ReflectionContext
283
291
auto CmdBuf = this ->getReader ().readBytes (
284
292
RemoteAddress (CmdStartAddress.getAddressData () + Offset),
285
293
SegmentCmdHdrSize);
294
+ if (!CmdBuf)
295
+ return false ;
286
296
auto CmdHdr = reinterpret_cast <typename T::SegmentCmd *>(CmdBuf.get ());
287
297
if (strncmp (CmdHdr->segname , " __DATA" , sizeof (CmdHdr->segname )) == 0 ) {
288
298
auto DataSegmentEnd =
@@ -305,6 +315,8 @@ class ReflectionContext
305
315
bool readPECOFFSections (RemoteAddress ImageStart) {
306
316
auto DOSHdrBuf = this ->getReader ().readBytes (
307
317
ImageStart, sizeof (llvm::object::dos_header));
318
+ if (!DOSHdrBuf)
319
+ return false ;
308
320
auto DOSHdr =
309
321
reinterpret_cast <const llvm::object::dos_header *>(DOSHdrBuf.get ());
310
322
auto COFFFileHdrAddr = ImageStart.getAddressData () +
@@ -313,6 +325,8 @@ class ReflectionContext
313
325
314
326
auto COFFFileHdrBuf = this ->getReader ().readBytes (
315
327
RemoteAddress (COFFFileHdrAddr), sizeof (llvm::object::coff_file_header));
328
+ if (!COFFFileHdrBuf)
329
+ return false ;
316
330
auto COFFFileHdr = reinterpret_cast <const llvm::object::coff_file_header *>(
317
331
COFFFileHdrBuf.get ());
318
332
@@ -322,9 +336,11 @@ class ReflectionContext
322
336
auto SectionTableBuf = this ->getReader ().readBytes (
323
337
RemoteAddress (SectionTableAddr),
324
338
sizeof (llvm::object::coff_section) * COFFFileHdr->NumberOfSections );
339
+ if (!SectionTableBuf)
340
+ return false ;
325
341
326
- auto findCOFFSectionByName = [&](llvm::StringRef Name)
327
- -> std::pair<RemoteRef<void >, uint64_t > {
342
+ auto findCOFFSectionByName =
343
+ [&](llvm::StringRef Name) -> std::pair<RemoteRef<void >, uint64_t > {
328
344
for (size_t i = 0 ; i < COFFFileHdr->NumberOfSections ; ++i) {
329
345
const llvm::object::coff_section *COFFSec =
330
346
reinterpret_cast <const llvm::object::coff_section *>(
@@ -339,6 +355,8 @@ class ReflectionContext
339
355
auto Addr = ImageStart.getAddressData () + COFFSec->VirtualAddress ;
340
356
auto Buf = this ->getReader ().readBytes (RemoteAddress (Addr),
341
357
COFFSec->VirtualSize );
358
+ if (!Buf)
359
+ return {nullptr , 0 };
342
360
auto BufStart = Buf.get ();
343
361
savedBuffers.push_back (std::move (Buf));
344
362
@@ -524,6 +542,8 @@ class ReflectionContext
524
542
} else {
525
543
SecBuf = this ->getReader ().readBytes (SecStart, SecSize);
526
544
}
545
+ if (!SecBuf)
546
+ return {nullptr , 0 };
527
547
auto SecContents =
528
548
RemoteRef<void >(SecStart.getAddressData (), SecBuf.get ());
529
549
savedBuffers.push_back (std::move (SecBuf));
@@ -592,6 +612,8 @@ class ReflectionContext
592
612
bool readELF (RemoteAddress ImageStart, llvm::Optional<llvm::sys::MemoryBlock> FileBuffer) {
593
613
auto Buf =
594
614
this ->getReader ().readBytes (ImageStart, sizeof (llvm::ELF::Elf64_Ehdr));
615
+ if (!Buf)
616
+ return false ;
595
617
596
618
// Read the header.
597
619
auto Hdr = reinterpret_cast <const llvm::ELF::Elf64_Ehdr *>(Buf.get ());
@@ -903,10 +925,10 @@ class ReflectionContext
903
925
return ;
904
926
auto NodeBytes = getReader ().readBytes (RemoteAddress (NodePtr),
905
927
sizeof (ConformanceNode<Runtime>));
928
+ if (!NodeBytes)
929
+ return ;
906
930
auto NodeData =
907
931
reinterpret_cast <const ConformanceNode<Runtime> *>(NodeBytes.get ());
908
- if (!NodeData)
909
- return ;
910
932
Call (NodeData->Type , NodeData->Proto );
911
933
iterateConformanceTree (NodeData->Left , Call);
912
934
iterateConformanceTree (NodeData->Right , Call);
@@ -917,21 +939,21 @@ class ReflectionContext
917
939
std::function<void (StoredPointer Type, StoredPointer Proto)> Call) {
918
940
auto MapBytes = getReader ().readBytes (RemoteAddress (ConformancesPtr),
919
941
sizeof (ConcurrentHashMap<Runtime>));
942
+ if (!MapBytes)
943
+ return ;
920
944
auto MapData =
921
945
reinterpret_cast <const ConcurrentHashMap<Runtime> *>(MapBytes.get ());
922
- if (!MapData)
923
- return ;
924
946
925
947
auto Count = MapData->ElementCount ;
926
948
auto Size = Count * sizeof (ConformanceCacheEntry<Runtime>);
927
949
928
950
auto ElementsBytes =
929
951
getReader ().readBytes (RemoteAddress (MapData->Elements ), Size);
952
+ if (!ElementsBytes)
953
+ return ;
930
954
auto ElementsData =
931
955
reinterpret_cast <const ConformanceCacheEntry<Runtime> *>(
932
956
ElementsBytes.get ());
933
- if (!ElementsData)
934
- return ;
935
957
936
958
for (StoredSize i = 0 ; i < Count; i++) {
937
959
auto &Element = ElementsData[i];
@@ -999,10 +1021,10 @@ class ReflectionContext
999
1021
auto AllocationBytes =
1000
1022
getReader ().readBytes (RemoteAddress (Allocation.Ptr ),
1001
1023
Allocation.Size );
1024
+ if (!AllocationBytes)
1025
+ return 0 ;
1002
1026
auto Entry = reinterpret_cast <const GenericMetadataCacheEntry *>(
1003
1027
AllocationBytes.get ());
1004
- if (!Entry)
1005
- return 0 ;
1006
1028
return Entry->Value ;
1007
1029
}
1008
1030
return 0 ;
@@ -1039,10 +1061,10 @@ class ReflectionContext
1039
1061
case GenericWitnessTableCacheTag: {
1040
1062
auto NodeBytes = getReader ().readBytes (
1041
1063
RemoteAddress (Allocation.Ptr ), sizeof (MetadataCacheNode<Runtime>));
1064
+ if (!NodeBytes)
1065
+ return llvm::None;
1042
1066
auto Node =
1043
1067
reinterpret_cast <const MetadataCacheNode<Runtime> *>(NodeBytes.get ());
1044
- if (!Node)
1045
- return llvm::None;
1046
1068
return *Node;
1047
1069
}
1048
1070
default :
@@ -1095,23 +1117,23 @@ class ReflectionContext
1095
1117
1096
1118
auto PoolBytes = getReader ()
1097
1119
.readBytes (AllocationPoolAddr->getResolvedAddress (), sizeof (PoolRange));
1098
- auto Pool = reinterpret_cast <const PoolRange *>(PoolBytes.get ());
1099
- if (!Pool)
1120
+ if (!PoolBytes)
1100
1121
return std::string (" failure reading allocation pool contents" );
1122
+ auto Pool = reinterpret_cast <const PoolRange *>(PoolBytes.get ());
1101
1123
1102
1124
auto TrailerPtr = Pool->Begin + Pool->Remaining ;
1103
1125
while (TrailerPtr) {
1104
1126
auto TrailerBytes = getReader ()
1105
1127
.readBytes (RemoteAddress (TrailerPtr), sizeof (PoolTrailer));
1106
- auto Trailer = reinterpret_cast <const PoolTrailer *>(TrailerBytes.get ());
1107
- if (!Trailer)
1128
+ if (!TrailerBytes)
1108
1129
break ;
1130
+ auto Trailer = reinterpret_cast <const PoolTrailer *>(TrailerBytes.get ());
1109
1131
auto PoolStart = TrailerPtr - Trailer->PoolSize ;
1110
1132
auto PoolBytes = getReader ()
1111
1133
.readBytes (RemoteAddress (PoolStart), Trailer->PoolSize );
1112
- auto PoolPtr = (const char *)PoolBytes.get ();
1113
- if (!PoolPtr)
1134
+ if (!PoolBytes)
1114
1135
break ;
1136
+ auto PoolPtr = (const char *)PoolBytes.get ();
1115
1137
1116
1138
uintptr_t Offset = 0 ;
1117
1139
while (Offset < Trailer->PoolSize ) {
@@ -1153,10 +1175,7 @@ class ReflectionContext
1153
1175
auto HeaderBytes = getReader ().readBytes (
1154
1176
RemoteAddress (BacktraceListNext),
1155
1177
sizeof (MetadataAllocationBacktraceHeader<Runtime>));
1156
- auto HeaderPtr =
1157
- reinterpret_cast <const MetadataAllocationBacktraceHeader<Runtime> *>(
1158
- HeaderBytes.get ());
1159
- if (HeaderPtr == nullptr ) {
1178
+ if (!HeaderBytes) {
1160
1179
// FIXME: std::stringstream would be better, but LLVM's standard library
1161
1180
// introduces a vtable and we don't want that.
1162
1181
char result[128 ];
@@ -1165,6 +1184,9 @@ class ReflectionContext
1165
1184
BacktraceListNext.getAddressData ());
1166
1185
return std::string (result);
1167
1186
}
1187
+ auto HeaderPtr =
1188
+ reinterpret_cast <const MetadataAllocationBacktraceHeader<Runtime> *>(
1189
+ HeaderBytes.get ());
1168
1190
auto BacktraceAddrPtr =
1169
1191
BacktraceListNext +
1170
1192
sizeof (MetadataAllocationBacktraceHeader<Runtime>);
0 commit comments