diff options
| author | Vedant Kumar <vsk@apple.com> | 2018-05-31 22:08:59 +0000 |
|---|---|---|
| committer | Vedant Kumar <vsk@apple.com> | 2018-05-31 22:08:59 +0000 |
| commit | 5b71e75ed34120a6cc0cd5b045558b6900df356b (patch) | |
| tree | bbb64f13ec92824c0a88a415dc48b37b6621e5d0 /lldb/source/Expression | |
| parent | 99d60e0dabcf20f4db683da83cde905b7a1373de (diff) | |
| download | bcm5719-llvm-5b71e75ed34120a6cc0cd5b045558b6900df356b.tar.gz bcm5719-llvm-5b71e75ed34120a6cc0cd5b045558b6900df356b.zip | |
[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
llvm-svn: 333697
Diffstat (limited to 'lldb/source/Expression')
| -rw-r--r-- | lldb/source/Expression/IRMemoryMap.cpp | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/lldb/source/Expression/IRMemoryMap.cpp b/lldb/source/Expression/IRMemoryMap.cpp index 8fc2b4a4520..1953e80852f 100644 --- a/lldb/source/Expression/IRMemoryMap.cpp +++ b/lldb/source/Expression/IRMemoryMap.cpp @@ -301,15 +301,21 @@ lldb::addr_t IRMemoryMap::Malloc(size_t size, uint8_t alignment, 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: |

