@@ -970,7 +970,10 @@ Status MinidumpFileBuilder::DumpDirectories() const {
970
970
}
971
971
972
972
Status MinidumpFileBuilder::ReadWriteMemoryInChunks (
973
+ const std::unique_ptr<lldb_private::DataBufferHeap> &data_up,
973
974
const lldb_private::CoreFileMemoryRange &range, uint64_t *bytes_read) {
975
+ if (!data_up)
976
+ return Status::FromErrorString (" No buffer supplied to read memory." );
974
977
Log *log = GetLog (LLDBLog::Object);
975
978
const lldb::addr_t addr = range.range .start ();
976
979
const lldb::addr_t size = range.range .size ();
@@ -982,8 +985,6 @@ Status MinidumpFileBuilder::ReadWriteMemoryInChunks(
982
985
983
986
uint64_t bytes_remaining = size;
984
987
uint64_t total_bytes_read = 0 ;
985
- auto data_up = std::make_unique<DataBufferHeap>(
986
- std::min (bytes_remaining, MAX_WRITE_CHUNK_SIZE), 0 );
987
988
Status error;
988
989
while (bytes_remaining > 0 ) {
989
990
// Get the next read chunk size as the minimum of the remaining bytes and
@@ -1041,19 +1042,19 @@ Status MinidumpFileBuilder::ReadWriteMemoryInChunks(
1041
1042
// report the accurate total.
1042
1043
if (bytes_read)
1043
1044
*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 ();
1052
1045
}
1053
1046
1054
1047
return error;
1055
1048
}
1056
1049
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
+
1057
1058
Status
1058
1059
MinidumpFileBuilder::AddMemoryList_32 (std::vector<CoreFileMemoryRange> &ranges,
1059
1060
Progress &progress) {
@@ -1064,6 +1065,8 @@ MinidumpFileBuilder::AddMemoryList_32(std::vector<CoreFileMemoryRange> &ranges,
1064
1065
1065
1066
Log *log = GetLog (LLDBLog::Object);
1066
1067
size_t region_index = 0 ;
1068
+ auto data_up = std::make_unique<DataBufferHeap>(
1069
+ std::min (GetLargestRangeSize (ranges), MAX_WRITE_CHUNK_SIZE), 0 );
1067
1070
for (const auto &core_range : ranges) {
1068
1071
// Take the offset before we write.
1069
1072
const offset_t offset_for_data = GetCurrentDataEndOffset ();
@@ -1079,7 +1082,7 @@ MinidumpFileBuilder::AddMemoryList_32(std::vector<CoreFileMemoryRange> &ranges,
1079
1082
1080
1083
progress.Increment (1 , " Adding Memory Range " + core_range.Dump ());
1081
1084
uint64_t bytes_read;
1082
- error = ReadWriteMemoryInChunks (core_range, &bytes_read);
1085
+ error = ReadWriteMemoryInChunks (data_up, core_range, &bytes_read);
1083
1086
if (error.Fail ())
1084
1087
return error;
1085
1088
@@ -1155,6 +1158,8 @@ MinidumpFileBuilder::AddMemoryList_64(std::vector<CoreFileMemoryRange> &ranges,
1155
1158
list_header.BaseRVA = memory_ranges_base_rva;
1156
1159
m_data.AppendData (&list_header, sizeof (llvm::minidump::Memory64ListHeader));
1157
1160
1161
+ auto data_up = std::make_unique<DataBufferHeap>(
1162
+ std::min (GetLargestRangeSize (ranges), MAX_WRITE_CHUNK_SIZE), 0 );
1158
1163
bool cleanup_required = false ;
1159
1164
std::vector<MemoryDescriptor_64> descriptors;
1160
1165
// Enumerate the ranges and create the memory descriptors so we can append
@@ -1186,7 +1191,7 @@ MinidumpFileBuilder::AddMemoryList_64(std::vector<CoreFileMemoryRange> &ranges,
1186
1191
1187
1192
progress.Increment (1 , " Adding Memory Range " + core_range.Dump ());
1188
1193
uint64_t bytes_read;
1189
- error = ReadWriteMemoryInChunks (core_range, &bytes_read);
1194
+ error = ReadWriteMemoryInChunks (data_up, core_range, &bytes_read);
1190
1195
if (error.Fail ())
1191
1196
return error;
1192
1197
0 commit comments