-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[llvm-21][MC] Fix fragments for sections bigger than 4G #169121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-llvm-mc Author: Lydia Kim (lydkim) ChangesAn OOM situation occurs in llvm-dwp when running big builds where sections are >4 GB. The problem is addressed in this post: #168923 Problem: To address the OOM issue while keeping MCEncodedFragment size minimal, this solution transitions from a begin/end representation ( This problem only exists in the llvm-21 branch. The recent refactoring in main branch makes this unnecessary there. Fixes #168923 Full diff: https://github.com/llvm/llvm-project/pull/169121.diff 2 Files Affected:
diff --git a/llvm/include/llvm/MC/MCSection.h b/llvm/include/llvm/MC/MCSection.h
index 64b13972bfca1..9daaebf7e7935 100644
--- a/llvm/include/llvm/MC/MCSection.h
+++ b/llvm/include/llvm/MC/MCSection.h
@@ -298,8 +298,8 @@ class MCFragment {
/// data.
class MCEncodedFragment : public MCFragment {
uint8_t BundlePadding = 0;
- uint32_t ContentStart = 0;
- uint32_t ContentEnd = 0;
+ uint32_t ContentSize = 0;
+ uint64_t ContentStart = 0;
uint32_t FixupStart = 0;
uint32_t FixupEnd = 0;
@@ -360,22 +360,23 @@ class MCEncodedFragment : public MCFragment {
// Content-related functions manage parent's storage using ContentStart and
// ContentSize.
- void clearContents() { ContentEnd = ContentStart; }
+ void clearContents() { ContentSize = 0; }
// Get a SmallVector reference. The caller should call doneAppending to update
- // `ContentEnd`.
+ // `ContentSize`.
SmallVectorImpl<char> &getContentsForAppending() {
SmallVectorImpl<char> &S = getParent()->ContentStorage;
- if (LLVM_UNLIKELY(ContentEnd != S.size())) {
+ if (LLVM_UNLIKELY(ContentStart + ContentSize != S.size())) {
// Move the elements to the end. Reserve space to avoid invalidating
// S.begin()+I for `append`.
- auto Size = ContentEnd - ContentStart;
auto I = std::exchange(ContentStart, S.size());
- S.reserve(S.size() + Size);
- S.append(S.begin() + I, S.begin() + I + Size);
+ S.reserve(S.size() + ContentSize);
+ S.append(S.begin() + I, S.begin() + I + ContentSize);
}
return S;
}
- void doneAppending() { ContentEnd = getParent()->ContentStorage.size(); }
+ void doneAppending() {
+ ContentSize = getParent()->ContentStorage.size() - ContentStart;
+ }
void appendContents(ArrayRef<char> Contents) {
getContentsForAppending().append(Contents.begin(), Contents.end());
doneAppending();
@@ -387,11 +388,11 @@ class MCEncodedFragment : public MCFragment {
LLVM_ABI void setContents(ArrayRef<char> Contents);
MutableArrayRef<char> getContents() {
return MutableArrayRef(getParent()->ContentStorage)
- .slice(ContentStart, ContentEnd - ContentStart);
+ .slice(ContentStart, ContentSize);
}
ArrayRef<char> getContents() const {
return ArrayRef(getParent()->ContentStorage)
- .slice(ContentStart, ContentEnd - ContentStart);
+ .slice(ContentStart, ContentSize);
}
// Fixup-related functions manage parent's storage using FixupStart and
@@ -409,7 +410,7 @@ class MCEncodedFragment : public MCFragment {
.slice(FixupStart, FixupEnd - FixupStart);
}
- size_t getSize() const { return ContentEnd - ContentStart; }
+ size_t getSize() const { return ContentSize; }
};
/// Fragment for data and encoded instructions.
diff --git a/llvm/lib/MC/MCSection.cpp b/llvm/lib/MC/MCSection.cpp
index a7330692571de..97f591fbf0e28 100644
--- a/llvm/lib/MC/MCSection.cpp
+++ b/llvm/lib/MC/MCSection.cpp
@@ -84,11 +84,11 @@ LLVM_DUMP_METHOD void MCSection::dump(
void MCEncodedFragment::setContents(ArrayRef<char> Contents) {
auto &S = getParent()->ContentStorage;
- if (ContentStart + Contents.size() > ContentEnd) {
+ if (Contents.size() > ContentSize) {
ContentStart = S.size();
S.resize_for_overwrite(S.size() + Contents.size());
}
- ContentEnd = ContentStart + Contents.size();
+ ContentSize = Contents.size();
llvm::copy(Contents, S.begin() + ContentStart);
}
|
|
Shouldn't this land in main first and then be cherry-picked to the release branch or is this a specific thing for the release branch? |
|
On main, this problem doesn't exist anymore due to more recent refactorings. |
I see - that makes sense. |
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D87662897
|
@lydkim Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
|
@lydkim (or anyone else). If you would like to add a note about this fix in the release notes (completely optional). Please reply to this comment with a one or two sentence description of the fix. When you are done, please add the release:note label to this PR. |
An OOM situation occurs in llvm-dwp when running big builds where sections are >4 GB. The problem is addressed in this post: #168923
Problem:
When
ContentStorageexceeds 4GB, theuint32_t ContentEndfield overflows when assigned indoneAppending(). Later ingetContentsForAppending(), the calculationSize = ContentEnd - ContentStartcauses unsigned integer underflow, producing large arbitrary memory allocations.To address the OOM issue while keeping MCEncodedFragment size minimal, this solution transitions from a begin/end representation (
uint32_t ContentStart,uint32_t ContentEnd) to a begin/size representation (uint64_t ContentStart,uint32_t ContentSize). The new approach changesContentStartto 64-bit to support positions beyond 4GB, whileContentSizeremains 32-bit, limiting individual fragments to 4GB.This problem only exists in the llvm-21 branch. The recent refactoring in main branch makes this unnecessary there.
Fixes #168923