[Lldb-commits] [lldb] r333697 - [IRMemoryMap] Fix the alignment adjustment in Malloc

Vedant Kumar via lldb-commits lldb-commits at lists.llvm.org
Thu May 31 15:08:59 PDT 2018


Author: vedantk
Date: Thu May 31 15:08:59 2018
New Revision: 333697

URL: http://llvm.org/viewvc/llvm-project?rev=333697&view=rev
Log:
[IRMemoryMap] Fix the alignment adjustment in Malloc

This prevents Malloc from allocating the same chunk of memory twice, as
a byproduct of an alignment adjustment which gave the client access to
unallocated memory.

Prior to this patch, the newly-added test failed with:

$ lldb-test ir-memory-map ... ir-memory-map-overlap1.test
...
Command: malloc(size=64, alignment=32)
Malloc: address = 0x1000cd080
Command: malloc(size=64, alignment=8)
Malloc: address = 0x1000cd0b0
Malloc error: overlapping allocation detected, previous allocation at [0x1000cd080, 0x1000cd0c0)

Differential Revision: https://reviews.llvm.org/D47551

Added:
    lldb/trunk/lit/Expr/Inputs/ir-memory-map-basic.test
      - copied, changed from r333690, lldb/trunk/lit/Expr/TestIRMemoryMap.test
    lldb/trunk/lit/Expr/Inputs/ir-memory-map-overlap1.test
Modified:
    lldb/trunk/lit/Expr/TestIRMemoryMap.test
    lldb/trunk/source/Expression/IRMemoryMap.cpp

Copied: lldb/trunk/lit/Expr/Inputs/ir-memory-map-basic.test (from r333690, lldb/trunk/lit/Expr/TestIRMemoryMap.test)
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Expr/Inputs/ir-memory-map-basic.test?p2=lldb/trunk/lit/Expr/Inputs/ir-memory-map-basic.test&p1=lldb/trunk/lit/Expr/TestIRMemoryMap.test&r1=333690&r2=333697&rev=333697&view=diff
==============================================================================
--- lldb/trunk/lit/Expr/TestIRMemoryMap.test (original)
+++ lldb/trunk/lit/Expr/Inputs/ir-memory-map-basic.test Thu May 31 15:08:59 2018
@@ -1,6 +1,3 @@
-# RUN: %cxx %p/Inputs/call-function.cpp -g -o %t
-# RUN: lldb-test ir-memory-map %t %s
-
 malloc 0 1
 malloc 1 1
 

Added: lldb/trunk/lit/Expr/Inputs/ir-memory-map-overlap1.test
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Expr/Inputs/ir-memory-map-overlap1.test?rev=333697&view=auto
==============================================================================
--- lldb/trunk/lit/Expr/Inputs/ir-memory-map-overlap1.test (added)
+++ lldb/trunk/lit/Expr/Inputs/ir-memory-map-overlap1.test Thu May 31 15:08:59 2018
@@ -0,0 +1,10 @@
+malloc 8 16
+malloc 16 8
+malloc 64 32
+malloc 1 8
+malloc 64 32
+malloc 64 8
+malloc 1024 32
+malloc 1 16
+malloc 8 16
+malloc 1024 16
\ No newline at end of file

Modified: lldb/trunk/lit/Expr/TestIRMemoryMap.test
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Expr/TestIRMemoryMap.test?rev=333697&r1=333696&r2=333697&view=diff
==============================================================================
--- lldb/trunk/lit/Expr/TestIRMemoryMap.test (original)
+++ lldb/trunk/lit/Expr/TestIRMemoryMap.test Thu May 31 15:08:59 2018
@@ -1,28 +1,3 @@
 # RUN: %cxx %p/Inputs/call-function.cpp -g -o %t
-# RUN: lldb-test ir-memory-map %t %s
-
-malloc 0 1
-malloc 1 1
-
-malloc 2 1
-malloc 2 2
-malloc 2 4
-
-malloc 3 1
-malloc 3 2
-malloc 3 4
-
-malloc 128 1
-malloc 128 2
-malloc 128 4
-malloc 128 128
-
-malloc 2048 1
-malloc 2048 2
-malloc 2048 4
-
-malloc 3968 1
-malloc 3968 2
-malloc 3968 4
-
-malloc 0 1
+# RUN: lldb-test ir-memory-map %t %S/Inputs/ir-memory-map-basic.test
+# RUN: lldb-test ir-memory-map %t %S/Inputs/ir-memory-map-overlap1.test

Modified: lldb/trunk/source/Expression/IRMemoryMap.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/IRMemoryMap.cpp?rev=333697&r1=333696&r2=333697&view=diff
==============================================================================
--- lldb/trunk/source/Expression/IRMemoryMap.cpp (original)
+++ lldb/trunk/source/Expression/IRMemoryMap.cpp Thu May 31 15:08:59 2018
@@ -301,15 +301,21 @@ lldb::addr_t IRMemoryMap::Malloc(size_t
   lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
   lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
 
-  size_t alignment_mask = alignment - 1;
   size_t allocation_size;
 
-  if (size == 0)
+  if (size == 0) {
+    // FIXME: Malloc(0) should either return an invalid address or assert, in
+    // order to cut down on unnecessary allocations.
     allocation_size = alignment;
-  else
-    allocation_size = (size & alignment_mask)
-                          ? ((size + alignment) & (~alignment_mask))
-                          : size;
+  } else {
+    // Round up the requested size to an aligned value.
+    allocation_size = llvm::alignTo(size, alignment);
+
+    // The process page cache does not see the requested alignment. We can't
+    // assume its result will be any more than 1-byte aligned. To work around
+    // this, request `alignment - 1` additional bytes.
+    allocation_size += alignment - 1;
+  }
 
   switch (policy) {
   default:




More information about the lldb-commits mailing list