Skip to content

Commit 233e674

Browse files
author
Lydia Kim
committed
[server-llvm-21][MC] Fixing vector overflow
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D87662897
1 parent 33e1a55 commit 233e674

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

llvm/include/llvm/MC/MCSection.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ class MCFragment {
298298
/// data.
299299
class MCEncodedFragment : public MCFragment {
300300
uint8_t BundlePadding = 0;
301-
uint32_t ContentStart = 0;
302-
uint32_t ContentEnd = 0;
301+
uint32_t ContentSize = 0;
302+
uint64_t ContentStart = 0;
303303
uint32_t FixupStart = 0;
304304
uint32_t FixupEnd = 0;
305305

@@ -360,22 +360,23 @@ class MCEncodedFragment : public MCFragment {
360360

361361
// Content-related functions manage parent's storage using ContentStart and
362362
// ContentSize.
363-
void clearContents() { ContentEnd = ContentStart; }
363+
void clearContents() { ContentSize = 0; }
364364
// Get a SmallVector reference. The caller should call doneAppending to update
365-
// `ContentEnd`.
365+
// `ContentSize`.
366366
SmallVectorImpl<char> &getContentsForAppending() {
367367
SmallVectorImpl<char> &S = getParent()->ContentStorage;
368-
if (LLVM_UNLIKELY(ContentEnd != S.size())) {
368+
if (LLVM_UNLIKELY(ContentStart + ContentSize != S.size())) {
369369
// Move the elements to the end. Reserve space to avoid invalidating
370370
// S.begin()+I for `append`.
371-
auto Size = ContentEnd - ContentStart;
372371
auto I = std::exchange(ContentStart, S.size());
373-
S.reserve(S.size() + Size);
374-
S.append(S.begin() + I, S.begin() + I + Size);
372+
S.reserve(S.size() + ContentSize);
373+
S.append(S.begin() + I, S.begin() + I + ContentSize);
375374
}
376375
return S;
377376
}
378-
void doneAppending() { ContentEnd = getParent()->ContentStorage.size(); }
377+
void doneAppending() {
378+
ContentSize = getParent()->ContentStorage.size() - ContentStart;
379+
}
379380
void appendContents(ArrayRef<char> Contents) {
380381
getContentsForAppending().append(Contents.begin(), Contents.end());
381382
doneAppending();
@@ -387,11 +388,11 @@ class MCEncodedFragment : public MCFragment {
387388
LLVM_ABI void setContents(ArrayRef<char> Contents);
388389
MutableArrayRef<char> getContents() {
389390
return MutableArrayRef(getParent()->ContentStorage)
390-
.slice(ContentStart, ContentEnd - ContentStart);
391+
.slice(ContentStart, ContentSize);
391392
}
392393
ArrayRef<char> getContents() const {
393394
return ArrayRef(getParent()->ContentStorage)
394-
.slice(ContentStart, ContentEnd - ContentStart);
395+
.slice(ContentStart, ContentSize);
395396
}
396397

397398
// Fixup-related functions manage parent's storage using FixupStart and
@@ -409,7 +410,7 @@ class MCEncodedFragment : public MCFragment {
409410
.slice(FixupStart, FixupEnd - FixupStart);
410411
}
411412

412-
size_t getSize() const { return ContentEnd - ContentStart; }
413+
size_t getSize() const { return ContentSize; }
413414
};
414415

415416
/// Fragment for data and encoded instructions.

llvm/lib/MC/MCSection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ LLVM_DUMP_METHOD void MCSection::dump(
8484

8585
void MCEncodedFragment::setContents(ArrayRef<char> Contents) {
8686
auto &S = getParent()->ContentStorage;
87-
if (ContentStart + Contents.size() > ContentEnd) {
87+
if (Contents.size() > ContentSize) {
8888
ContentStart = S.size();
8989
S.resize_for_overwrite(S.size() + Contents.size());
9090
}
91-
ContentEnd = ContentStart + Contents.size();
91+
ContentSize = Contents.size();
9292
llvm::copy(Contents, S.begin() + ContentStart);
9393
}
9494

0 commit comments

Comments
 (0)