Skip to content

[lld-macho] Fix address sanitizer for category merging #91680

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

Merged
merged 1 commit into from
May 10, 2024

Conversation

alx32
Copy link
Contributor

@alx32 alx32 commented May 9, 2024

FIxing the address sanitizer issue reported in #91548 .
The problem comes from the assignment auto bodyData = newSectionData which defaults to SmallVector<uint8_t> data = newSectionData - which actually creates a copy of the data, placed on the stack.
By explicitly using ArrayRef instead, we make sure that the original copy is used.
We also change the assignment in ObjcCategoryMerger::newStringData from auto to SmallVector<uint8_t> & to make it explicit.

@llvmbot
Copy link
Member

llvmbot commented May 9, 2024

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-macho

Author: None (alx32)

Changes

FIxing the address sanitizer issue reported in #91548 .
The problem comes from the assignment auto bodyData = newSectionData which defaults to SmallVector&lt;uint8_t&gt; data = newSectionData - which actually creates a copy of the data, placed on the stack.
By explicitly using ArrayRef instead, we make sure that the original copy is used.
We also change the assignment in ObjcCategoryMerger::newStringData from auto to SmallVector&lt;uint8_t&gt; &amp; to make it explicit.


Full diff: https://github.com/llvm/llvm-project/pull/91680.diff

1 Files Affected:

  • (modified) lld/MachO/ObjC.cpp (+2-2)
diff --git a/lld/MachO/ObjC.cpp b/lld/MachO/ObjC.cpp
index 96ec646095be8..9d1612beae872 100644
--- a/lld/MachO/ObjC.cpp
+++ b/lld/MachO/ObjC.cpp
@@ -1148,7 +1148,7 @@ void ObjcCategoryMerger::generateCatListForNonErasedCategories(
       assert(nonErasedCatBody && "Failed to relocate non-deleted category");
 
       // Allocate data for the new __objc_catlist slot
-      auto bodyData = newSectionData(target->wordSize);
+      llvm::ArrayRef<uint8_t> bodyData = newSectionData(target->wordSize);
 
       // We mark the __objc_catlist slot as belonging to the same file as the
       // category
@@ -1279,7 +1279,7 @@ void ObjcCategoryMerger::doCleanup() { generatedSectionData.clear(); }
 StringRef ObjcCategoryMerger::newStringData(const char *str) {
   uint32_t len = strlen(str);
   uint32_t bufSize = len + 1;
-  auto &data = newSectionData(bufSize);
+  SmallVector<uint8_t> &data = newSectionData(bufSize);
   char *strData = reinterpret_cast<char *>(data.data());
   // Copy the string chars and null-terminator
   memcpy(strData, str, bufSize);

@@ -1148,7 +1148,7 @@ void ObjcCategoryMerger::generateCatListForNonErasedCategories(
assert(nonErasedCatBody && "Failed to relocate non-deleted category");

// Allocate data for the new __objc_catlist slot
auto bodyData = newSectionData(target->wordSize);
llvm::ArrayRef<uint8_t> bodyData = newSectionData(target->wordSize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using auto & would have also been ok. But since this is a constant, it is preferred to use ArrayRef<> over const SmallVector<> &.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand, it is using ArrayRef<>, or what is the suggested change ?

@alx32 alx32 merged commit db78ee0 into llvm:main May 10, 2024
@alx32 alx32 deleted the 11_fix_sanitizer_catslot branch July 11, 2024 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants