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

via llvm-commits llvm-commits at lists.llvm.org
Thu May 9 16:45:52 PDT 2024


https://github.com/alx32 created https://github.com/llvm/llvm-project/pull/91680

FIxing the address sanitizer issue reported in https://github.com/llvm/llvm-project/pull/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. 

>From b46e22f8ec43c9e4fab57e332ef4cdacb909781e Mon Sep 17 00:00:00 2001
From: Alex B <alexborcan at meta.com>
Date: Thu, 9 May 2024 16:34:06 -0700
Subject: [PATCH] [lld-macho] Fix address sanitizer for category merging

---
 lld/MachO/ObjC.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

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);



More information about the llvm-commits mailing list