Fix leaking mach ports & memory after task_threads() (#70338)
authorIlia <ki.stfu@gmail.com>
Wed, 29 Jun 2022 10:26:09 +0000 (12:26 +0200)
committerGitHub <noreply@github.com>
Wed, 29 Jun 2022 10:26:09 +0000 (12:26 +0200)
src/coreclr/debug/createdump/crashinfomac.cpp
src/coreclr/gc/unix/gcenv.unix.cpp
src/coreclr/pal/src/exception/machmessage.cpp
src/coreclr/pal/src/thread/process.cpp

index ae77509..9231e5c 100644 (file)
@@ -75,6 +75,12 @@ CrashInfo::EnumerateAndSuspendThreads()
         m_threads.push_back(thread);
     }
 
+    result = ::vm_deallocate(mach_task_self(), reinterpret_cast<vm_address_t>(threadList), threadCount * sizeof(thread_act_t));
+    if (result != KERN_SUCCESS)
+    {
+        TRACE("vm_deallocate FAILED %x %s\n", result, mach_error_string(result));
+    }
+
     return true;
 }
 
index e70a112..a03edc3 100644 (file)
@@ -569,6 +569,9 @@ void GCToOSInterface::FlushProcessWriteBuffers()
             {
                 CHECK_MACH("thread_get_register_pointer_values()", machret);
             }
+
+            machret = mach_port_deallocate(mach_task_self(), pThreads[i]);
+            CHECK_MACH("mach_port_deallocate()", machret);
         }
         // Deallocate the thread list now we're done with it.
         machret = vm_deallocate(mach_task_self(), (vm_address_t)pThreads, cThreads * sizeof(thread_act_t));
index 783e68c..ec0c408 100644 (file)
@@ -852,6 +852,11 @@ thread_act_t MachMessage::GetThreadFromState(thread_state_flavor_t eFlavor, thre
                 CHECK_MACH("mach_port_mod_refs()", machret);
 
                 // Deallocate the thread list now we're done with it.
+                for (mach_msg_type_number_t j = 0; j < cThreads; j++)
+                {
+                    machret = mach_port_deallocate(mach_task_self(), pThreads[j]);
+                    CHECK_MACH("mach_port_deallocate()", machret);
+                }
                 machret = vm_deallocate(mach_task_self(), (vm_address_t)pThreads, cThreads * sizeof(thread_act_t));
                 CHECK_MACH("vm_deallocate()", machret);
 
index 9f0a179..7fd95e4 100644 (file)
@@ -2797,6 +2797,9 @@ FlushProcessWriteBuffers()
             {
                 CHECK_MACH("thread_get_register_pointer_values()", machret);
             }
+
+            machret = mach_port_deallocate(mach_task_self(), pThreads[i]);
+            CHECK_MACH("mach_port_deallocate()", machret);
         }
         // Deallocate the thread list now we're done with it.
         machret = vm_deallocate(mach_task_self(), (vm_address_t)pThreads, cThreads * sizeof(thread_act_t));