Skip to content

Commit c667a0a

Browse files
committed
[lldb] Do not deallocate memory after exec
After an exec has occured, resources used to manage the state of a Process are cleaned up. One such resource is the AllocatedMemoryCache which keeps track of memory allocations made in the process for things like expression evaluation. After an exec is performed, the allocated memory regions in the process are gone, so it does not make sense to try to deallocate those regions. rdar://103188106 Differential Revision: https://reviews.llvm.org/D140249 (cherry picked from commit f2f3b1a)
1 parent 0a8c0dd commit c667a0a

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

lldb/include/lldb/Target/Memory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class AllocatedMemoryCache {
118118

119119
~AllocatedMemoryCache();
120120

121-
void Clear();
121+
void Clear(bool deallocate_memory);
122122

123123
lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions,
124124
Status &error);

lldb/source/Target/Memory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ AllocatedMemoryCache::AllocatedMemoryCache(Process &process)
346346

347347
AllocatedMemoryCache::~AllocatedMemoryCache() = default;
348348

349-
void AllocatedMemoryCache::Clear() {
349+
void AllocatedMemoryCache::Clear(bool deallocate_memory) {
350350
std::lock_guard<std::recursive_mutex> guard(m_mutex);
351-
if (m_process.IsAlive()) {
351+
if (m_process.IsAlive() && deallocate_memory) {
352352
PermissionsToBlockMap::iterator pos, end = m_memory_map.end();
353353
for (pos = m_memory_map.begin(); pos != end; ++pos)
354354
m_process.DoDeallocateMemory(pos->second->GetBaseAddress());

lldb/source/Target/Process.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ void Process::Finalize() {
555555
m_notifications.swap(empty_notifications);
556556
m_image_tokens.clear();
557557
m_memory_cache.Clear();
558-
m_allocated_memory_cache.Clear();
558+
m_allocated_memory_cache.Clear(/*deallocate_memory=*/true);
559559
{
560560
std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex);
561561
m_language_runtimes.clear();
@@ -5838,7 +5838,9 @@ void Process::DidExec() {
58385838
m_dyld_up.reset();
58395839
m_jit_loaders_up.reset();
58405840
m_image_tokens.clear();
5841-
m_allocated_memory_cache.Clear();
5841+
// After an exec, the inferior is a new process and these memory regions are
5842+
// no longer allocated.
5843+
m_allocated_memory_cache.Clear(/*deallocte_memory=*/false);
58425844
{
58435845
std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex);
58445846
m_language_runtimes.clear();

0 commit comments

Comments
 (0)