[llvm] 3f954f5 - Correct mismatched allocation/deallocation calls

Aaron Ballman via llvm-commits llvm-commits at lists.llvm.org
Wed May 15 09:36:32 PDT 2024


Author: Aaron Ballman
Date: 2024-05-15T12:36:19-04:00
New Revision: 3f954f575156bce8ac81d6b4d94de443786befed

URL: https://github.com/llvm/llvm-project/commit/3f954f575156bce8ac81d6b4d94de443786befed
DIFF: https://github.com/llvm/llvm-project/commit/3f954f575156bce8ac81d6b4d94de443786befed.diff

LOG: Correct mismatched allocation/deallocation calls

This amends dceaa0f4491ebe30c0b0f1bc7fa5ec365b60ced6 because ASAN
caught an issue where the allocation and deallocation were not properly
paired: https://lab.llvm.org/buildbot/#/builders/239/builds/7001

Use malloc and free throughout this file to ensure that all kinds of
memory buffers use the proper pairing.

Added: 
    

Modified: 
    llvm/lib/Support/MemoryBuffer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp
index 50308bd2bf4a3..fb7e804fd7e84 100644
--- a/llvm/lib/Support/MemoryBuffer.cpp
+++ b/llvm/lib/Support/MemoryBuffer.cpp
@@ -79,8 +79,16 @@ void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
   SmallString<256> NameBuf;
   StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
 
-  char *Mem = static_cast<char *>(operator new(N + sizeof(size_t) +
-                                               NameRef.size() + 1));
+  // We use malloc() and manually handle it returning null instead of calling
+  // operator new because we need all uses of NamedBufferAlloc to be
+  // deallocated with a call to free() due to needing to use malloc() in
+  // WritableMemoryBuffer::getNewUninitMemBuffer() to work around the out-of-
+  // memory handler installed by default in LLVM. See operator delete() member
+  // functions within this file for the paired call to free().
+  char *Mem =
+      static_cast<char *>(std::malloc(N + sizeof(size_t) + NameRef.size() + 1));
+  if (!Mem)
+    llvm::report_bad_alloc_error("Allocation failed");
   *reinterpret_cast<size_t *>(Mem + N) = NameRef.size();
   CopyStringRef(Mem + N + sizeof(size_t), NameRef);
   return Mem;
@@ -225,7 +233,7 @@ class MemoryBufferMMapFile : public MB {
 
   /// Disable sized deallocation for MemoryBufferMMapFile, because it has
   /// tail-allocated data.
-  void operator delete(void *p) { ::operator delete(p); }
+  void operator delete(void *p) { std::free(p); }
 
   StringRef getBufferIdentifier() const override {
     // The name is stored after the class itself.


        


More information about the llvm-commits mailing list