@@ -970,7 +970,10 @@ Status MinidumpFileBuilder::DumpDirectories() const {
970970}
971971
972972Status MinidumpFileBuilder::ReadWriteMemoryInChunks (
973+ const std::unique_ptr<lldb_private::DataBufferHeap> &data_up,
973974 const lldb_private::CoreFileMemoryRange &range, uint64_t *bytes_read) {
975+ if (!data_up)
976+ return Status::FromErrorString (" No buffer supplied to read memory." );
974977 Log *log = GetLog (LLDBLog::Object);
975978 const lldb::addr_t addr = range.range .start ();
976979 const lldb::addr_t size = range.range .size ();
@@ -982,8 +985,6 @@ Status MinidumpFileBuilder::ReadWriteMemoryInChunks(
982985
983986 uint64_t bytes_remaining = size;
984987 uint64_t total_bytes_read = 0 ;
985- auto data_up = std::make_unique<DataBufferHeap>(
986- std::min (bytes_remaining, MAX_WRITE_CHUNK_SIZE), 0 );
987988 Status error;
988989 while (bytes_remaining > 0 ) {
989990 // Get the next read chunk size as the minimum of the remaining bytes and
@@ -1041,19 +1042,19 @@ Status MinidumpFileBuilder::ReadWriteMemoryInChunks(
10411042 // report the accurate total.
10421043 if (bytes_read)
10431044 *bytes_read += bytes_read_for_chunk;
1044-
1045- // We clear the heap per loop, without checking if we
1046- // read the expected bytes this is so we don't allocate
1047- // more than the MAX_WRITE_CHUNK_SIZE. But we do check if
1048- // this is even worth clearing before we return and
1049- // destruct the heap.
1050- if (bytes_remaining > 0 )
1051- data_up->Clear ();
10521045 }
10531046
10541047 return error;
10551048}
10561049
1050+ static uint64_t
1051+ GetLargestRangeSize (const std::vector<CoreFileMemoryRange> &ranges) {
1052+ uint64_t max_size = 0 ;
1053+ for (const auto &core_range : ranges)
1054+ max_size = std::max (max_size, core_range.range .size ());
1055+ return max_size;
1056+ }
1057+
10571058Status
10581059MinidumpFileBuilder::AddMemoryList_32 (std::vector<CoreFileMemoryRange> &ranges,
10591060 Progress &progress) {
@@ -1064,6 +1065,8 @@ MinidumpFileBuilder::AddMemoryList_32(std::vector<CoreFileMemoryRange> &ranges,
10641065
10651066 Log *log = GetLog (LLDBLog::Object);
10661067 size_t region_index = 0 ;
1068+ auto data_up = std::make_unique<DataBufferHeap>(
1069+ std::min (GetLargestRangeSize (ranges), MAX_WRITE_CHUNK_SIZE), 0 );
10671070 for (const auto &core_range : ranges) {
10681071 // Take the offset before we write.
10691072 const offset_t offset_for_data = GetCurrentDataEndOffset ();
@@ -1079,7 +1082,7 @@ MinidumpFileBuilder::AddMemoryList_32(std::vector<CoreFileMemoryRange> &ranges,
10791082
10801083 progress.Increment (1 , " Adding Memory Range " + core_range.Dump ());
10811084 uint64_t bytes_read;
1082- error = ReadWriteMemoryInChunks (core_range, &bytes_read);
1085+ error = ReadWriteMemoryInChunks (data_up, core_range, &bytes_read);
10831086 if (error.Fail ())
10841087 return error;
10851088
@@ -1155,6 +1158,8 @@ MinidumpFileBuilder::AddMemoryList_64(std::vector<CoreFileMemoryRange> &ranges,
11551158 list_header.BaseRVA = memory_ranges_base_rva;
11561159 m_data.AppendData (&list_header, sizeof (llvm::minidump::Memory64ListHeader));
11571160
1161+ auto data_up = std::make_unique<DataBufferHeap>(
1162+ std::min (GetLargestRangeSize (ranges), MAX_WRITE_CHUNK_SIZE), 0 );
11581163 bool cleanup_required = false ;
11591164 std::vector<MemoryDescriptor_64> descriptors;
11601165 // Enumerate the ranges and create the memory descriptors so we can append
@@ -1186,7 +1191,7 @@ MinidumpFileBuilder::AddMemoryList_64(std::vector<CoreFileMemoryRange> &ranges,
11861191
11871192 progress.Increment (1 , " Adding Memory Range " + core_range.Dump ());
11881193 uint64_t bytes_read;
1189- error = ReadWriteMemoryInChunks (core_range, &bytes_read);
1194+ error = ReadWriteMemoryInChunks (data_up, core_range, &bytes_read);
11901195 if (error.Fail ())
11911196 return error;
11921197
0 commit comments