Skip to content

Commit bb7bf1f

Browse files
committed
Drop RVA property and index and instead pop off the front of the arrayrefs, remove expected method and use range loop
1 parent e5794ae commit bb7bf1f

File tree

4 files changed

+69
-64
lines changed

4 files changed

+69
-64
lines changed

llvm/include/llvm/Object/Minidump.h

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,12 @@ class MinidumpFile : public Binary {
146146
public:
147147
static Memory64Iterator
148148
begin(ArrayRef<uint8_t> Storage,
149-
ArrayRef<minidump::MemoryDescriptor_64> Descriptors,
150-
uint64_t BaseRVA) {
151-
return Memory64Iterator(Storage, Descriptors, BaseRVA);
149+
ArrayRef<minidump::MemoryDescriptor_64> Descriptors) {
150+
return Memory64Iterator(Storage, Descriptors);
152151
}
153152

154153
static Memory64Iterator end() { return Memory64Iterator(); }
155154

156-
std::pair<minidump::MemoryDescriptor_64, ArrayRef<uint8_t>> Current;
157-
158155
bool operator==(const Memory64Iterator &R) const {
159156
return IsEnd == R.IsEnd;
160157
}
@@ -167,29 +164,33 @@ class MinidumpFile : public Binary {
167164
}
168165

169166
const std::pair<minidump::MemoryDescriptor_64, ArrayRef<uint8_t>> *
170-
operator&() {
167+
operator->() {
171168
return &Current;
172169
}
173170

174171
Error inc() {
175-
if (Storage.size() == 0 || Descriptors.size() == 0)
176-
return make_error<GenericBinaryError>("No Memory64List Stream",
177-
object_error::parse_failed);
178-
179-
if (Index >= Descriptors.size())
180-
return createError("Can't read past of Memory64List Stream");
172+
if (Storage.size() == 0 || Descriptors.size() == 0) {
173+
IsEnd = true;
174+
return Error::success();
175+
}
181176

182-
const minidump::MemoryDescriptor_64 &Descriptor = Descriptors[Index];
183-
if (RVA + Descriptor.DataSize > Storage.size())
177+
// Drop front gives us an array ref, so we need to call .front() as well.
178+
const minidump::MemoryDescriptor_64 &Descriptor = Descriptors.front();
179+
if (Descriptor.DataSize > Storage.size()) {
180+
IsEnd = true;
184181
return make_error<GenericBinaryError>(
185182
"Memory64 Descriptor exceeds end of file.",
186183
object_error::unexpected_eof);
184+
}
187185

188-
ArrayRef<uint8_t> Content = Storage.slice(RVA, Descriptor.DataSize);
186+
187+
ArrayRef<uint8_t> Content = Storage.take_front(Descriptor.DataSize);
189188
Current = std::make_pair(Descriptor, Content);
190-
Index++;
191-
RVA += Descriptor.DataSize;
192-
if (Index >= Descriptors.size())
189+
190+
Storage = Storage.drop_front(Descriptor.DataSize);
191+
Descriptors = Descriptors.drop_front();
192+
193+
if (Descriptors.empty())
193194
IsEnd = true;
194195
return Error::success();
195196
}
@@ -198,23 +199,22 @@ class MinidumpFile : public Binary {
198199
// This constructor will only happen after a validation check to see
199200
// if the first descriptor is readable.
200201
Memory64Iterator(ArrayRef<uint8_t> Storage,
201-
ArrayRef<minidump::MemoryDescriptor_64> Descriptors,
202-
uint64_t BaseRVA)
203-
: RVA(BaseRVA), Storage(Storage), Descriptors(Descriptors),
202+
ArrayRef<minidump::MemoryDescriptor_64> Descriptors)
203+
: Storage(Storage), Descriptors(Descriptors),
204204
IsEnd(false) {
205-
assert(Descriptors.size() > 0);
206-
assert(Storage.size() >= BaseRVA + Descriptors.front().DataSize);
207-
Current =
208-
std::make_pair(Descriptors.front(),
209-
Storage.slice(BaseRVA, Descriptors.front().DataSize));
205+
assert(Descriptors.size() > 0 && Storage.size() >= Descriptors.front().DataSize);
206+
minidump::MemoryDescriptor_64 Descriptor = Descriptors.front();
207+
ArrayRef<uint8_t> Content = Storage.take_front(Descriptor.DataSize);
208+
Current = std::make_pair(Descriptor, Content);
209+
this->Descriptors = Descriptors.drop_front();
210+
this->Storage = Storage.drop_front(Descriptor.DataSize);
210211
}
211212

212213
Memory64Iterator()
213-
: RVA(0), Storage(ArrayRef<uint8_t>()),
214+
: Storage(ArrayRef<uint8_t>()),
214215
Descriptors(ArrayRef<minidump::MemoryDescriptor_64>()), IsEnd(true) {}
215216

216-
size_t Index = 0;
217-
uint64_t RVA;
217+
std::pair<minidump::MemoryDescriptor_64, ArrayRef<uint8_t>> Current;
218218
ArrayRef<uint8_t> Storage;
219219
ArrayRef<minidump::MemoryDescriptor_64> Descriptors;
220220
bool IsEnd;
@@ -226,7 +226,7 @@ class MinidumpFile : public Binary {
226226
/// content from the Memory64List stream. An error is returned if the file
227227
/// does not contain a Memory64List stream, or if the descriptor data is
228228
/// unreadable.
229-
Expected<iterator_range<FallibleMemory64Iterator>>
229+
iterator_range<FallibleMemory64Iterator>
230230
getMemory64List(Error &Err) const;
231231

232232
/// Returns the list of descriptors embedded in the MemoryInfoList stream. The

llvm/lib/Object/Minidump.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,45 @@ MinidumpFile::create(MemoryBufferRef Source) {
156156
new MinidumpFile(Source, Hdr, *ExpectedStreams, std::move(StreamMap)));
157157
}
158158

159-
Expected<iterator_range<MinidumpFile::FallibleMemory64Iterator>>
159+
static iterator_range<MinidumpFile::FallibleMemory64Iterator> makeEmptyRange(Error &Err) {
160+
return make_range(llvm::object::MinidumpFile::FallibleMemory64Iterator::itr(llvm::object::MinidumpFile::Memory64Iterator::end(), Err),
161+
llvm::object::MinidumpFile::FallibleMemory64Iterator::end(llvm::object::MinidumpFile::Memory64Iterator::end()));
162+
}
163+
164+
iterator_range<MinidumpFile::FallibleMemory64Iterator>
160165
MinidumpFile::getMemory64List(Error &Err) const {
161166
Expected<minidump::Memory64ListHeader> ListHeader = getMemoryList64Header();
162-
if (!ListHeader)
163-
return ListHeader.takeError();
167+
if (!ListHeader) {
168+
Err = ListHeader.takeError();
169+
return makeEmptyRange(Err);
170+
}
164171

165172
std::optional<ArrayRef<uint8_t>> Stream =
166173
getRawStream(StreamType::Memory64List);
167-
if (!Stream)
168-
return createError("No such stream");
174+
if (!Stream) {
175+
Err = createError("No such stream");
176+
return makeEmptyRange(Err);
177+
}
169178

170179
Expected<ArrayRef<minidump::MemoryDescriptor_64>> Descriptors =
171180
getDataSliceAs<minidump::MemoryDescriptor_64>(
172181
*Stream, sizeof(Memory64ListHeader),
173182
ListHeader->NumberOfMemoryRanges);
174183

175-
if (!Descriptors)
176-
return Descriptors.takeError();
184+
if (!Descriptors) {
185+
Err = Descriptors.takeError();
186+
return makeEmptyRange(Err);
187+
}
177188

178189
if (!Descriptors->empty() &&
179-
ListHeader->BaseRVA + Descriptors->front().DataSize > getData().size())
180-
return createEOFError();
190+
ListHeader->BaseRVA + Descriptors->front().DataSize > getData().size()) {
191+
Err = createError("Memory64List header RVA out of range");
192+
return makeEmptyRange(Err);
193+
}
181194

182195
return make_range(
183196
FallibleMemory64Iterator::itr(
184-
Memory64Iterator::begin(getData(), *Descriptors, ListHeader->BaseRVA),
197+
Memory64Iterator::begin(getData().slice(ListHeader->BaseRVA), *Descriptors),
185198
Err),
186199
FallibleMemory64Iterator::end(Memory64Iterator::end()));
187200
}

llvm/lib/ObjectYAML/MinidumpYAML.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -529,22 +529,13 @@ Stream::create(const Directory &StreamDesc, const object::MinidumpFile &File) {
529529
return std::make_unique<MemoryListStream>(std::move(Ranges));
530530
}
531531
case StreamKind::Memory64List: {
532-
// Error, unlike expected is true in failure state
533532
Error Err = Error::success();
534-
// Explicit check on Err so that if we return due to getmemory64list
535-
// getting an error, it's not destructed when unchecked.
533+
auto Memory64List = File.getMemory64List(Err);
536534
if (Err)
537535
return Err;
538-
auto ExpectedList = File.getMemory64List(Err);
539-
if (!ExpectedList)
540-
return ExpectedList.takeError();
541536
std::vector<Memory64ListStream::entry_type> Ranges;
542-
for (auto It = ExpectedList->begin(); It != ExpectedList->end(); ++It) {
543-
const auto Pair = *It;
544-
if (!Err)
537+
for (const auto &Pair : Memory64List) {
545538
Ranges.push_back({Pair.first, Pair.second});
546-
else
547-
break;
548539
}
549540

550541
// If we don't have an error, or if any of the reads succeed, return ranges

llvm/unittests/ObjectYAML/MinidumpYAMLTest.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ TEST(MinidumpYAML, MemoryRegion_64bit) {
353353
ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
354354
object::MinidumpFile &File = **ExpectedFile;
355355

356-
ASSERT_THAT(1u, File.streams().size());
356+
ASSERT_THAT(File.streams().size(), 1u);
357357

358358
Error Err = Error::success();
359359
// Explicit Err check
@@ -367,21 +367,22 @@ TEST(MinidumpYAML, MemoryRegion_64bit) {
367367
*ExpectedMemoryList;
368368
auto Iterator = MemoryList.begin();
369369

370-
++Iterator;
371-
ASSERT_FALSE(Err);
370+
372371

373372
auto DescOnePair = *Iterator;
374373
const minidump::MemoryDescriptor_64 &DescOne = DescOnePair.first;
375-
ASSERT_THAT(0x7FFFFFCF0818283u, DescOne.StartOfMemoryRange);
376-
ASSERT_THAT(5u, DescOne.DataSize);
377-
374+
ASSERT_THAT(DescOne.StartOfMemoryRange, 0x7FFFFFCF0818283u);
375+
ASSERT_THAT(DescOne.DataSize, 5u);
378376
++Iterator;
379377
ASSERT_FALSE(Err);
380378

381379
auto DescTwoPair = *Iterator;
382380
const minidump::MemoryDescriptor_64 &DescTwo = DescTwoPair.first;
383-
ASSERT_THAT(0x7FFFFFFF0818283u, DescTwo.StartOfMemoryRange);
384-
ASSERT_THAT(5u, DescTwo.DataSize);
381+
ASSERT_THAT(DescTwo.StartOfMemoryRange, 0x7FFFFFFF0818283u);
382+
ASSERT_THAT(DescTwo.DataSize, 5u);
383+
++Iterator;
384+
ASSERT_FALSE(Err);
385+
385386
const std::optional<ArrayRef<uint8_t>> ExpectedContent =
386387
File.getRawStream(StreamType::Memory64List);
387388
ASSERT_TRUE(ExpectedContent);
@@ -396,14 +397,14 @@ TEST(MinidumpYAML, MemoryRegion_64bit) {
396397

397398
Expected<ArrayRef<uint8_t>> DescOneExpectedContentSlice = DescOnePair.second;
398399
ASSERT_THAT_EXPECTED(DescOneExpectedContentSlice, Succeeded());
399-
ASSERT_THAT(5u, DescOneExpectedContentSlice->size());
400-
ASSERT_THAT(arrayRefFromStringRef("hello"), *DescOneExpectedContentSlice);
400+
ASSERT_THAT(DescOneExpectedContentSlice->size(), 5u);
401+
ASSERT_THAT(*DescOneExpectedContentSlice, arrayRefFromStringRef("hello"));
401402

402403
Expected<ArrayRef<uint8_t>> DescTwoExpectedContentSlice = DescTwoPair.second;
403404
ASSERT_THAT_EXPECTED(DescTwoExpectedContentSlice, Succeeded());
404-
ASSERT_THAT(arrayRefFromStringRef("world"), *DescTwoExpectedContentSlice);
405+
ASSERT_THAT(*DescTwoExpectedContentSlice, arrayRefFromStringRef("world"));
405406

406-
ASSERT_TRUE(Iterator == MemoryList.end());
407+
ASSERT_EQ(Iterator, MemoryList.end());
407408
}
408409

409410
TEST(MinidumpYAML, MemoryRegion_DataSize_TooSmall) {
@@ -414,7 +415,7 @@ TEST(MinidumpYAML, MemoryRegion_DataSize_TooSmall) {
414415
- Type: Memory64List
415416
Memory Ranges:
416417
- Start of Memory Range: 0x7FFFFFCF0818283
417-
Data Size: 4 1
418+
Data Size: 1
418419
Content: '68656c6c6f'
419420
- Start of Memory Range: 0x7FFFFFFF0818283
420421
Content: '776f726c64'

0 commit comments

Comments
 (0)