Skip to content

Fix double-free on FreeBSD #58085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

#pragma warning disable CA1823 // analyzer incorrectly flags fixed buffer length const (https://github.com/dotnet/roslyn/issues/37593)
Expand Down Expand Up @@ -186,11 +187,6 @@ public unsafe struct kinfo_proc
public static unsafe kinfo_proc* GetProcInfo(int pid, bool threads, out int count)
{
Span<int> sysctlName = stackalloc int[4];
int bytesLength = 0;
byte* pBuffer = null;
kinfo_proc* kinfo = null;

count = -1;

if (pid == 0)
{
Expand All @@ -207,23 +203,17 @@ public unsafe struct kinfo_proc
sysctlName[1] = KERN_PROC;
sysctlName[0] = CTL_KERN;

try
{
Interop.Sys.Sysctl(sysctlName, ref pBuffer, ref bytesLength);
kinfo = (kinfo_proc*)pBuffer;
if (kinfo->ki_structsize != sizeof(kinfo_proc))
{
// failed consistency check
throw new ArgumentOutOfRangeException(nameof(pid));
}

count = (int)bytesLength / sizeof(kinfo_proc);
}
finally
{
Marshal.FreeHGlobal((IntPtr)pBuffer);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fixing the double-free.

}
byte* pBuffer = null;
int bytesLength = 0;
Interop.Sys.Sysctl(sysctlName, ref pBuffer, ref bytesLength);

kinfo_proc* kinfo = (kinfo_proc*)pBuffer;

Debug.Assert(kinfo->ki_structsize == sizeof(kinfo_proc));

count = (int)bytesLength / sizeof(kinfo_proc);

// Buffer ownership transferred to the caller
return kinfo;
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal static unsafe int[] ListAllPids()
}
finally
{
Marshal.FreeHGlobal((IntPtr)entries);
NativeMemory.Free(entries);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took this as an opportunity to switch to NativeMemory API and avoid the casts in these files.

}
}

Expand All @@ -87,7 +87,7 @@ internal static unsafe int[] ListAllPids()
}
finally
{
Marshal.FreeHGlobal((IntPtr)pBuffer);
NativeMemory.Free(pBuffer);
}
}

Expand All @@ -107,9 +107,9 @@ public static unsafe ProcessInfo GetProcessInfoById(int pid)
throw new ArgumentOutOfRangeException(nameof(pid));
}

kinfo_proc* kinfo = GetProcInfo(pid, true, out int count);
ProcessInfo info;

kinfo_proc* kinfo = GetProcInfo(pid, true, out int count);
try
{
if (count < 1)
Expand Down Expand Up @@ -142,7 +142,7 @@ public static unsafe ProcessInfo GetProcessInfoById(int pid)
}
finally
{
Marshal.FreeHGlobal((IntPtr)kinfo);
NativeMemory.Free(kinfo);
}

return info;
Expand All @@ -160,12 +160,11 @@ public static unsafe ProcessInfo GetProcessInfoById(int pid)
public static unsafe proc_stats GetThreadInfo(int pid, int tid)
{
proc_stats ret = default;
kinfo_proc* info = null;
int count;

kinfo_proc* info = GetProcInfo(pid, (tid != 0), out count);
try
{
info = GetProcInfo(pid, (tid != 0), out count);
if (info != null && count >= 1)
{
if (tid == 0)
Expand Down Expand Up @@ -194,7 +193,7 @@ public static unsafe proc_stats GetThreadInfo(int pid, int tid)
}
finally
{
Marshal.FreeHGlobal((IntPtr)info);
NativeMemory.Free(info);
}

return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ namespace System
{
public static partial class Environment
{
public static unsafe long WorkingSet => Interop.Process.GetProcInfo(ProcessId, true, out _)->ki_rssize;
public static unsafe long WorkingSet
{
get
{
Interop.Process.kinfo_proc* processInfo = Interop.Process.GetProcInfo(ProcessId, true, out _);
try
{
return processInfo->ki_rssize;
}
finally
{
NativeMemory.Free(processInfo);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fixing a related memory leak.

}
}
}
}
}