@@ -153,6 +153,8 @@ class ReflectionContext
153
153
auto CmdBuf = this ->getReader ().readBytes (
154
154
RemoteAddress (CmdStartAddress.getAddressData () + Offset),
155
155
SegmentCmdHdrSize);
156
+ if (!CmdBuf)
157
+ return false ;
156
158
auto CmdHdr = reinterpret_cast <typename T::SegmentCmd *>(CmdBuf.get ());
157
159
if (strncmp (CmdHdr->segname , " __TEXT" , sizeof (CmdHdr->segname )) == 0 ) {
158
160
Command = CmdHdr;
@@ -173,6 +175,8 @@ class ReflectionContext
173
175
auto LoadCmdAddress = reinterpret_cast <const char *>(loadCmdOffset);
174
176
auto LoadCmdBuf = this ->getReader ().readBytes (
175
177
RemoteAddress (LoadCmdAddress), sizeof (typename T::SegmentCmd));
178
+ if (!LoadCmdBuf)
179
+ return false ;
176
180
auto LoadCmd = reinterpret_cast <typename T::SegmentCmd *>(LoadCmdBuf.get ());
177
181
178
182
// The sections start immediately after the load command.
@@ -181,6 +185,8 @@ class ReflectionContext
181
185
sizeof (typename T::SegmentCmd);
182
186
auto Sections = this ->getReader ().readBytes (
183
187
RemoteAddress (SectAddress), NumSect * sizeof (typename T::Section));
188
+ if (!Sections)
189
+ return false ;
184
190
185
191
auto Slide = ImageStart.getAddressData () - Command->vmaddr ;
186
192
std::string Prefix = " __swift5" ;
@@ -211,6 +217,8 @@ class ReflectionContext
211
217
212
218
auto SectBuf = this ->getReader ().readBytes (RemoteAddress (RangeStart),
213
219
RangeEnd - RangeStart);
220
+ if (!SectBuf)
221
+ return false ;
214
222
215
223
auto findMachOSectionByName = [&](llvm::StringRef Name)
216
224
-> std::pair<RemoteRef<void >, uint64_t > {
@@ -267,6 +275,8 @@ class ReflectionContext
267
275
auto CmdBuf = this ->getReader ().readBytes (
268
276
RemoteAddress (CmdStartAddress.getAddressData () + Offset),
269
277
SegmentCmdHdrSize);
278
+ if (!CmdBuf)
279
+ return false ;
270
280
auto CmdHdr = reinterpret_cast <typename T::SegmentCmd *>(CmdBuf.get ());
271
281
if (strncmp (CmdHdr->segname , " __DATA" , sizeof (CmdHdr->segname )) == 0 ) {
272
282
auto DataSegmentEnd =
@@ -289,6 +299,8 @@ class ReflectionContext
289
299
bool readPECOFFSections (RemoteAddress ImageStart) {
290
300
auto DOSHdrBuf = this ->getReader ().readBytes (
291
301
ImageStart, sizeof (llvm::object::dos_header));
302
+ if (!DOSHdrBuf)
303
+ return false ;
292
304
auto DOSHdr =
293
305
reinterpret_cast <const llvm::object::dos_header *>(DOSHdrBuf.get ());
294
306
auto COFFFileHdrAddr = ImageStart.getAddressData () +
@@ -297,6 +309,8 @@ class ReflectionContext
297
309
298
310
auto COFFFileHdrBuf = this ->getReader ().readBytes (
299
311
RemoteAddress (COFFFileHdrAddr), sizeof (llvm::object::coff_file_header));
312
+ if (!COFFFileHdrBuf)
313
+ return false ;
300
314
auto COFFFileHdr = reinterpret_cast <const llvm::object::coff_file_header *>(
301
315
COFFFileHdrBuf.get ());
302
316
@@ -306,9 +320,11 @@ class ReflectionContext
306
320
auto SectionTableBuf = this ->getReader ().readBytes (
307
321
RemoteAddress (SectionTableAddr),
308
322
sizeof (llvm::object::coff_section) * COFFFileHdr->NumberOfSections );
323
+ if (!SectionTableBuf)
324
+ return false ;
309
325
310
- auto findCOFFSectionByName = [&](llvm::StringRef Name)
311
- -> std::pair<RemoteRef<void >, uint64_t > {
326
+ auto findCOFFSectionByName =
327
+ [&](llvm::StringRef Name) -> std::pair<RemoteRef<void >, uint64_t > {
312
328
for (size_t i = 0 ; i < COFFFileHdr->NumberOfSections ; ++i) {
313
329
const llvm::object::coff_section *COFFSec =
314
330
reinterpret_cast <const llvm::object::coff_section *>(
@@ -323,6 +339,8 @@ class ReflectionContext
323
339
auto Addr = ImageStart.getAddressData () + COFFSec->VirtualAddress ;
324
340
auto Buf = this ->getReader ().readBytes (RemoteAddress (Addr),
325
341
COFFSec->VirtualSize );
342
+ if (!Buf)
343
+ return {nullptr , 0 };
326
344
auto BufStart = Buf.get ();
327
345
savedBuffers.push_back (std::move (Buf));
328
346
@@ -508,6 +526,8 @@ class ReflectionContext
508
526
} else {
509
527
SecBuf = this ->getReader ().readBytes (SecStart, SecSize);
510
528
}
529
+ if (!SecBuf)
530
+ return {nullptr , 0 };
511
531
auto SecContents =
512
532
RemoteRef<void >(SecStart.getAddressData (), SecBuf.get ());
513
533
savedBuffers.push_back (std::move (SecBuf));
@@ -576,6 +596,8 @@ class ReflectionContext
576
596
bool readELF (RemoteAddress ImageStart, llvm::Optional<llvm::sys::MemoryBlock> FileBuffer) {
577
597
auto Buf =
578
598
this ->getReader ().readBytes (ImageStart, sizeof (llvm::ELF::Elf64_Ehdr));
599
+ if (!Buf)
600
+ return false ;
579
601
580
602
// Read the header.
581
603
auto Hdr = reinterpret_cast <const llvm::ELF::Elf64_Ehdr *>(Buf.get ());
@@ -887,10 +909,10 @@ class ReflectionContext
887
909
return ;
888
910
auto NodeBytes = getReader ().readBytes (RemoteAddress (NodePtr),
889
911
sizeof (ConformanceNode<Runtime>));
912
+ if (!NodeBytes)
913
+ return ;
890
914
auto NodeData =
891
915
reinterpret_cast <const ConformanceNode<Runtime> *>(NodeBytes.get ());
892
- if (!NodeData)
893
- return ;
894
916
Call (NodeData->Type , NodeData->Proto );
895
917
iterateConformanceTree (NodeData->Left , Call);
896
918
iterateConformanceTree (NodeData->Right , Call);
@@ -901,21 +923,21 @@ class ReflectionContext
901
923
std::function<void (StoredPointer Type, StoredPointer Proto)> Call) {
902
924
auto MapBytes = getReader ().readBytes (RemoteAddress (ConformancesPtr),
903
925
sizeof (ConcurrentHashMap<Runtime>));
926
+ if (!MapBytes)
927
+ return ;
904
928
auto MapData =
905
929
reinterpret_cast <const ConcurrentHashMap<Runtime> *>(MapBytes.get ());
906
- if (!MapData)
907
- return ;
908
930
909
931
auto Count = MapData->ElementCount ;
910
932
auto Size = Count * sizeof (ConformanceCacheEntry<Runtime>);
911
933
912
934
auto ElementsBytes =
913
935
getReader ().readBytes (RemoteAddress (MapData->Elements ), Size);
936
+ if (!ElementsBytes)
937
+ return ;
914
938
auto ElementsData =
915
939
reinterpret_cast <const ConformanceCacheEntry<Runtime> *>(
916
940
ElementsBytes.get ());
917
- if (!ElementsData)
918
- return ;
919
941
920
942
for (StoredSize i = 0 ; i < Count; i++) {
921
943
auto &Element = ElementsData[i];
@@ -983,10 +1005,10 @@ class ReflectionContext
983
1005
auto AllocationBytes =
984
1006
getReader ().readBytes (RemoteAddress (Allocation.Ptr ),
985
1007
Allocation.Size );
1008
+ if (!AllocationBytes)
1009
+ return 0 ;
986
1010
auto Entry = reinterpret_cast <const GenericMetadataCacheEntry *>(
987
1011
AllocationBytes.get ());
988
- if (!Entry)
989
- return 0 ;
990
1012
return Entry->Value ;
991
1013
}
992
1014
return 0 ;
@@ -1023,10 +1045,10 @@ class ReflectionContext
1023
1045
case GenericWitnessTableCacheTag: {
1024
1046
auto NodeBytes = getReader ().readBytes (
1025
1047
RemoteAddress (Allocation.Ptr ), sizeof (MetadataCacheNode<Runtime>));
1048
+ if (!NodeBytes)
1049
+ return llvm::None;
1026
1050
auto Node =
1027
1051
reinterpret_cast <const MetadataCacheNode<Runtime> *>(NodeBytes.get ());
1028
- if (!Node)
1029
- return llvm::None;
1030
1052
return *Node;
1031
1053
}
1032
1054
default :
@@ -1079,23 +1101,23 @@ class ReflectionContext
1079
1101
1080
1102
auto PoolBytes = getReader ()
1081
1103
.readBytes (AllocationPoolAddr->getResolvedAddress (), sizeof (PoolRange));
1082
- auto Pool = reinterpret_cast <const PoolRange *>(PoolBytes.get ());
1083
- if (!Pool)
1104
+ if (!PoolBytes)
1084
1105
return std::string (" failure reading allocation pool contents" );
1106
+ auto Pool = reinterpret_cast <const PoolRange *>(PoolBytes.get ());
1085
1107
1086
1108
auto TrailerPtr = Pool->Begin + Pool->Remaining ;
1087
1109
while (TrailerPtr) {
1088
1110
auto TrailerBytes = getReader ()
1089
1111
.readBytes (RemoteAddress (TrailerPtr), sizeof (PoolTrailer));
1090
- auto Trailer = reinterpret_cast <const PoolTrailer *>(TrailerBytes.get ());
1091
- if (!Trailer)
1112
+ if (!TrailerBytes)
1092
1113
break ;
1114
+ auto Trailer = reinterpret_cast <const PoolTrailer *>(TrailerBytes.get ());
1093
1115
auto PoolStart = TrailerPtr - Trailer->PoolSize ;
1094
1116
auto PoolBytes = getReader ()
1095
1117
.readBytes (RemoteAddress (PoolStart), Trailer->PoolSize );
1096
- auto PoolPtr = (const char *)PoolBytes.get ();
1097
- if (!PoolPtr)
1118
+ if (!PoolBytes)
1098
1119
break ;
1120
+ auto PoolPtr = (const char *)PoolBytes.get ();
1099
1121
1100
1122
uintptr_t Offset = 0 ;
1101
1123
while (Offset < Trailer->PoolSize ) {
@@ -1137,10 +1159,7 @@ class ReflectionContext
1137
1159
auto HeaderBytes = getReader ().readBytes (
1138
1160
RemoteAddress (BacktraceListNext),
1139
1161
sizeof (MetadataAllocationBacktraceHeader<Runtime>));
1140
- auto HeaderPtr =
1141
- reinterpret_cast <const MetadataAllocationBacktraceHeader<Runtime> *>(
1142
- HeaderBytes.get ());
1143
- if (HeaderPtr == nullptr ) {
1162
+ if (!HeaderBytes) {
1144
1163
// FIXME: std::stringstream would be better, but LLVM's standard library
1145
1164
// introduces a vtable and we don't want that.
1146
1165
char result[128 ];
@@ -1149,6 +1168,9 @@ class ReflectionContext
1149
1168
BacktraceListNext.getAddressData ());
1150
1169
return std::string (result);
1151
1170
}
1171
+ auto HeaderPtr =
1172
+ reinterpret_cast <const MetadataAllocationBacktraceHeader<Runtime> *>(
1173
+ HeaderBytes.get ());
1152
1174
auto BacktraceAddrPtr =
1153
1175
BacktraceListNext +
1154
1176
sizeof (MetadataAllocationBacktraceHeader<Runtime>);
0 commit comments